Advertisement
harshancx

no country

Nov 6th, 2017
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.64 KB | None | 0 0
  1. <?php
  2. /**
  3. * @file
  4. * Code for the EDB User feature.
  5. */
  6.  
  7. include_once 'edb_user.features.inc';
  8. include_once 'includes/edb_user.views.inc';
  9.  
  10. /**
  11. * Implements hook_form_FORM_ID_alter().
  12. */
  13. function edb_user_form_user_profile_form_alter(&$form, &$form_state) {
  14. if (!user_access('administer users')) {
  15. // Hide legacy fields and other fields that should not be accessible to lower-level users
  16. $legacy_fields = array(
  17. 'field_legacy_project_timeframe',
  18. 'field_legacy_project_wherestatus',
  19. 'field_legacy_requires_database',
  20. 'field_init_value',
  21. 'field_forum_id_',
  22. 'field_user_last_campaign',
  23. 'field_user_last_asset',
  24. 'field_blog_author_avatar',
  25. # hiding these fields as well even though it's not a "legacy" field
  26. 'field_formid',
  27. 'flag_notify_field_defaults',
  28. );
  29.  
  30. foreach ($legacy_fields as $legacy_field) {
  31. $form[$legacy_field]['#access'] = FALSE;
  32. }
  33. // Hide the blog author bio.
  34. $blog_author_fields = $form['#fieldgroups']['group_user_blog_author_info']->children;
  35. if (!empty($blog_author_fields)) {
  36. foreach ($blog_author_fields as $blog_author_field) {
  37. $form[$blog_author_field]['#access'] = FALSE;
  38. }
  39. }
  40. }
  41. }
  42.  
  43. /**
  44. * Implements hook_form_FORM_ID_alter().
  45. */
  46. function edb_user_form_user_pass_alter(&$form, &$form_state){
  47. $form['actions']['submit']['#submit'][] = 'edb_user_pass_form_submit';
  48. }
  49.  
  50. /**
  51. * custom submit handler for password reset page
  52. * @param $form
  53. * @param $form_state
  54. */
  55. function edb_user_pass_form_submit($form, &$form_state){
  56. global $language;
  57.  
  58. $account = $form_state['values']['account'];
  59. // Mail one time login URL and instructions using current language.
  60. $mail = _user_mail_notify('password_reset', $account, $language);
  61. if (!empty($mail)) {
  62. watchdog('user', 'Password reset instructions mailed to %name at %email.', array('%name' => $account->name, '%email' => $account->mail));
  63. drupal_set_message(t('If there is an account associated with (email address), you will receive an email with a link to reset your password.'));
  64. }
  65.  
  66. $form_state['redirect'] = 'user';
  67. return;
  68. }
  69.  
  70. /**
  71. * Implements hook_form_FORM_ID_alter().
  72. */
  73. function edb_user_form_user_register_alter(&$form, &$form_state){
  74.  
  75. $excluded_options = array(
  76. 'Information Technology',
  77. 'Telecommunications',
  78. 'Healthcare Facilities/Services',
  79. 'Media/Marketing/Advertising',
  80. 'Insurance',
  81. 'Technology',
  82. 'Aerospace & Defense',
  83. 'Communications - Equipment/Services',
  84. 'Computer Software',
  85. 'Manufacturing- Hardware/Software',
  86. 'Public Sector',
  87. 'Communications - Equipment & Services',
  88. 'Financial Services',
  89. 'Education',
  90. 'Research/Development',
  91. 'Financial',
  92. );
  93. foreach ($excluded_options as $key) {
  94. $lang = $form['field_type_of_business']['#language'];
  95. unset($form['field_type_of_business'][$lang]['#options'][$key]);
  96. }
  97.  
  98. //Hide Embargoed countries from Country options without removing already existing users of these countries.
  99. $country_excluded = array(
  100. 'BY','CU','ER','IR','KP','SY','VE','SD','SS',
  101. );
  102. foreach ($country_excluded as $country) {
  103. $lang = $form['field_postal_address']['#language'];
  104. unset($form['field_postal_address'][$lang][0]['country']['#options'][$country]);
  105. }
  106.  
  107. $form['field_postal_address'][$lang][0]['country']['#empty_option'] = t('- Select -');
  108. $form['field_postal_address'][$lang][0]['country']['#required'] = TRUE;
  109. }
  110.  
  111. /**
  112. * Implements hook_form_FORM_ID_alter().
  113. */
  114. function edb_user_form_user_register_form_alter(&$form, &$form_state) {
  115. // EDB-1001: Hide specific options from field, without removing them from existing records
  116. $excluded_options = array(
  117. 'Information Technology',
  118. 'Telecommunications',
  119. 'Healthcare Facilities/Services',
  120. 'Media/Marketing/Advertising',
  121. 'Insurance',
  122. 'Technology',
  123. 'Aerospace & Defense',
  124. 'Communications - Equipment/Services',
  125. 'Computer Software',
  126. 'Manufacturing- Hardware/Software',
  127. 'Public Sector',
  128. 'Communications - Equipment & Services',
  129. 'Financial Services',
  130. 'Education',
  131. 'Research/Development',
  132. 'Financial',
  133. );
  134. foreach ($excluded_options as $key) {
  135. $lang = $form['field_type_of_business']['#language'];
  136. unset($form['field_type_of_business'][$lang]['#options'][$key]);
  137. }
  138.  
  139. //Hide Embargoed countries from Country options without removing already existing users of these countries.
  140. $country_excluded = array(
  141. 'BY','CU','ER','IR','KP','SY','VE','SD','SS',
  142. );
  143. foreach ($country_excluded as $country) {
  144. $lang = $form['field_postal_address']['#language'];
  145. unset($form['field_postal_address'][$lang][0]['country']['#options'][$country]);
  146. }
  147.  
  148. $form['field_postal_address'][$lang][0]['country']['#empty_option'] = t('- Select -');
  149. $form['field_postal_address'][$lang][0]['country']['#required'] = TRUE;
  150.  
  151. $form['actions']['submit']['#submit'][] = 'edb_user_register_form_submit';
  152. }
  153.  
  154. /**
  155. * custom submit handler for user registration form
  156. * @param $form
  157. * @param $form_state
  158. */
  159. function edb_user_register_form_submit($form, &$form_state){
  160. $reg_pass_set = !variable_get('user_email_verification', TRUE);
  161.  
  162. // Test here for a valid pre-auth -- if the pre-auth is set to the auth user, we
  163. // handle things a bit differently.
  164. $pre_auth = logintoboggan_validating_id() != DRUPAL_AUTHENTICATED_RID;
  165.  
  166. // If we are allowing user selected passwords then skip the auto-generate function
  167. // The new user's status will be 1 (visitors can create own accounts) if reg_pass_set == 1
  168. // Immediate login, we are going to assign a pre-auth role, until email validation completed
  169. if ($reg_pass_set) {
  170. $pass = $form_state['values']['pass'];
  171. $status = 1;
  172. }
  173. else {
  174. $pass = user_password();
  175. $status = variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS;
  176. }
  177.  
  178. // The unset below is needed to prevent these form values from being saved as
  179. // user data.
  180. form_state_values_clean($form_state);
  181.  
  182. // Set the roles for the new user -- add the pre-auth role if they can pick their own password,
  183. // and the pre-auth role isn't anon or auth user.
  184. $validating_id = logintoboggan_validating_id();
  185. $roles = isset($form_state['values']['roles']) ? array_filter($form_state['values']['roles']) : array();
  186. if ($reg_pass_set && ($validating_id > DRUPAL_AUTHENTICATED_RID)) {
  187. $roles[$validating_id] = 1;
  188. }
  189.  
  190. $form_state['values']['pass'] = $pass;
  191. $form_state['values']['init'] = $form_state['values']['mail'];
  192. $form_state['values']['roles'] = $roles;
  193. $form_state['values']['status'] = $status;
  194.  
  195. $account = $form['#user'];
  196.  
  197. entity_form_submit_build_entity('user', $account, $form, $form_state);
  198.  
  199. // Populate $edit with the properties of $account, which have been edited on
  200. // this form by taking over all values, which appear in the form values too.
  201. $edit = array_intersect_key((array) $account, $form_state['values']);
  202. $account = user_save($account, $edit);
  203.  
  204. // Terminate if an error occurred during user_save().
  205. if (!$account) {
  206. drupal_set_message(t("Error saving user account."), 'error');
  207. $form_state['redirect'] = '';
  208. return;
  209. }
  210. $form_state['user'] = $account;
  211. $form_state['values']['uid'] = $account->uid;
  212.  
  213. watchdog('user', 'New user: %name (%email).', array('%name' => $form_state['values']['name'], '%email' => $form_state['values']['mail']), WATCHDOG_NOTICE, l(t('edit'), 'user/' . $account->uid . '/edit'));
  214.  
  215. // Add plain text password into user account to generate mail tokens.
  216. $account->password = $pass;
  217.  
  218. // Compose the appropriate user message. Validation emails are only sent if:
  219. // 1. Users can set their own password.
  220. // 2. The pre-auth role isn't the auth user.
  221. // 3. Visitors can create their own accounts.
  222. $message = t('Thank you for registering with us!');
  223. if($reg_pass_set && $pre_auth && variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS) {
  224. $message = t('A validation e-mail has been sent to your e-mail address. You will need to follow the instructions in that message in order to gain full access to the site.');
  225. }
  226.  
  227. if (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS) {
  228.  
  229. // Create new user account, no administrator approval required.
  230. $mailkey = 'register_no_approval_required';
  231.  
  232. } elseif (variable_get('user_register', USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) == USER_REGISTER_VISITORS_ADMINISTRATIVE_APPROVAL) {
  233.  
  234. // Create new user account, administrator approval required.
  235. $mailkey = 'register_pending_approval';
  236.  
  237. $message = t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.<br />Once it has been approved, you will receive an e-mail containing further instructions.');
  238. }
  239.  
  240. // Mail the user.
  241. _user_mail_notify($mailkey, $account);
  242.  
  243. drupal_set_message($message);
  244.  
  245. // where do we need to redirect after registration?
  246. $redirect = _logintoboggan_process_redirect(variable_get('logintoboggan_redirect_on_register', ''), $account);
  247.  
  248. // Log the user in if they created the account and immediate login is enabled.
  249. if($reg_pass_set && variable_get('logintoboggan_immediate_login_on_register', TRUE)) {
  250. $form_state['redirect'] = logintoboggan_process_login($account, $form_state['values'], $redirect);
  251. }
  252. else {
  253. // Redirect to the appropriate page.
  254. $form_state['redirect'] = $redirect;
  255. }
  256. }
  257.  
  258.  
  259. /**
  260. * Implements hook_menu_alter().
  261. */
  262. function edb_user_menu_alter(&$items) {
  263.  
  264. $items['admin/config/people/accounts/remove-field-country'] = array(
  265. 'page callback' => 'drupal_get_form',
  266. 'page arguments' => array('edb_user_remove_field_country'),
  267. 'type' => MENU_CALLBACK,
  268. 'access arguments' => array('administer nodes'),
  269. 'title' => 'Remove the deprecated field_country from the site',
  270. );
  271.  
  272. }
  273.  
  274. /**
  275. * Simple form to trigger removing "number of employees" field altogether
  276. * @param $form
  277. * @param $form_state
  278. */
  279. function edb_user_remove_field_country($form, &$form_state) {
  280.  
  281. if(!isset($form_state['storage']['confirm'])) {
  282. $form['text'] = array(
  283. '#markup' => '<p>' . t('Use this form one time to completely remove the deprecated field "country".') . '</p>',
  284. );
  285. $form['submit'] = array(
  286. '#type' => 'submit',
  287. '#value' => t('Begin'),
  288. '#submit' => array('edb_user_remove_field_country_submit'),
  289. );
  290.  
  291. return $form;
  292. } else {
  293. drupal_set_message('Are you sure that you want to completely remove the deprecated field "country"?');
  294. return confirm_form($form);
  295. }
  296.  
  297. }
  298.  
  299. /**
  300. * Validation function for form edb_user_remove_field_country
  301. * @param $form
  302. * @param $form_state
  303. */
  304. function edb_user_remove_field_country_validate ($form, &$form_state) {
  305. // if field_country has any data in it, form_set_error
  306. $query = "SELECT field_country_value FROM field_data_field_country WHERE field_country_value IS NOT NULL;";
  307. $number_rows = db_query($query)->rowCount();
  308. if ($number_rows !== 0) {
  309. form_set_error('submit', t('Cannot remove the Country field, as there is still data in it.'));
  310. }
  311. }
  312.  
  313. /**
  314. * @param $form
  315. * @param $form_state
  316. */
  317. function edb_user_remove_field_country_submit($form, &$form_state) {
  318. if(!isset($form_state['storage']['confirm'])) {
  319. $form_state['storage']['confirm'] = TRUE;
  320. $form_state['rebuild'] = TRUE;
  321. } else {
  322. drupal_set_message('Purging "country" field from the database.');
  323. field_delete_field('field_country');
  324. // set a range large enough to delete all uses of this field
  325. $really_big_range = 9999999;
  326. field_purge_batch($really_big_range);
  327. drupal_set_message('Purge complete.');
  328. }
  329. }
  330.  
  331. /**
  332. * Implements hook_action_info().
  333. */
  334. function edb_user_action_info() {
  335. return array(
  336. 'edb_user_action_migrate_field_country_values' => array(
  337. 'type' => 'user',
  338. 'label' => t('Migrate values from user field_country, which we are deprecating'),
  339. 'behavior' => array('changes_property'),
  340. 'configurable' => FALSE,
  341. 'vbo_configurable' => FALSE,
  342. 'triggers' => array('any'),
  343. ),
  344. );
  345. }
  346.  
  347. /**
  348. * Action function to migrate some field_country values to field_postal_address
  349. * @param $entity
  350. * @param $context
  351. */
  352. function edb_user_action_migrate_field_country_values (&$entity, $context) {
  353. // field_country stored country name; field_postal_address stores
  354. // country data using ISO 2-character country codes - look up this row's
  355. // country code
  356. module_load_include('inc', 'locale', 'locale');
  357. $country_codes = country_get_list();
  358.  
  359. // The official list has 'Russia' and 'South Korea' - db has variants of these
  360. $supplemental_country_codes = array('RU' => 'Russian Federation', 'KR' => 'Korea, Republic of');
  361.  
  362. $lang = field_language('user', $entity, 'field_country');
  363. $country_code = array_search($entity->field_country[$lang][0]['value'], $country_codes);
  364. if (!$country_code) {
  365. $country_code = array_search($entity->field_country[$lang][0]['value'], $supplemental_country_codes);
  366. }
  367. $lang = field_language('user', $entity, 'field_postal_address');
  368. # only update field_postal_address if its country value is empty
  369. if (!empty($entity->field_postal_address) && $entity->field_postal_address[$lang][0]['country'] !== '' && $country_code) {
  370. $entity->field_postal_address[$lang][0]['country'] = $country_code;
  371. }
  372.  
  373. # done with this user - unset field_country
  374. unset($entity->field_country[$lang][0]);
  375. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement