Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.13 KB | None | 0 0
  1. <?php
  2.  
  3. // $Id: caubo_registration.module,v 1.9.2.11 2008/11/02 13:04:01 Exp $
  4.  
  5. /**
  6. * @file
  7. *
  8. *
  9. * Custom user registration process for CAUBO
  10. */
  11.  
  12.  
  13. /**
  14. * Implementation of hook_menu().
  15. *
  16. * @return array
  17. */
  18. function caubo_registration_menu() {
  19. $items['admin/settings/caubo_registration'] = array(
  20. 'title' => 'CAUBO Registration',
  21. 'description' => 'Configure Registration for CAUBO.',
  22. 'page callback' => 'drupal_get_form',
  23. 'page arguments' => array('_caubo_registration_admin_settings_form'),
  24. 'access arguments' => array('administer site configuration'),
  25. 'type' => MENU_NORMAL_ITEM,
  26. );
  27.  
  28. return $items;
  29. }
  30.  
  31.  
  32. function caubo_registration_form_alter(&$form, &$form_state, $form_id) {
  33. if ($form_id != "user_register") return;
  34.  
  35. if ($form_state['redirect'] == "user") drupal_goto("user");
  36.  
  37. if (isset($form_state['storage']['page']) && $form_state['storage']['page'] == 2) {
  38. caubo_register_page_two($form, $form_state);
  39. return;
  40. }
  41.  
  42. if (isset($form_state['storage']['page']) && $form_state['storage']['page'] == 3) {
  43. caubo_register_page_three($form, $form_state);
  44. return;
  45. }
  46.  
  47. //page 1 of registration is displayed
  48. $form['group_contact_details']['#type'] = 'value';
  49.  
  50. $form['submit']['#value'] = "next";
  51.  
  52. // lets store whatever submit functions we have and replace with our own
  53. // we want to store so we can add these back on later so we don't interfere with other modules
  54. $form_state['storage']['submits'] = $form['#submit'];
  55. $form['#submit'] = array('caubo_register_form_submit');
  56.  
  57. return;
  58. }
  59.  
  60. // Second page of registration
  61. function caubo_register_page_two(&$form, &$form_state) {
  62. global $pass_email_address;
  63.  
  64. // set Page 1 fields as entered on Page 1 and disable
  65. $form['name']['#value'] = $form_state['values']['name'];
  66. $form['mail']['#value'] = $form_state['values']['mail'];
  67. $form['title']['#value'] = $form_state['content_profile_registration']['profile']['node']->title;
  68.  
  69. $form['name']['#disabled'] = true;
  70. $form['mail']['#disabled'] = true;
  71. $form['title']['#disabled'] = true;
  72.  
  73. // To HIDE the fields rather than show DISABLED
  74. /* $form['name']['#type'] = 'value';
  75. $form['mail']['#type'] = 'value';
  76. $form['title']['#type'] = 'value';*/
  77.  
  78. // set Organization but display as disabled
  79. $pass_email_address = $form['mail']['#value'];
  80. $form['group_contact_details']['field_organization']['#pre_render'] = array('_caubo_limit_organizations');
  81. $form['group_contact_details']['field_organization']['#value'][0]['nid'] = $form_state['storage']['orgid'];
  82. $form['group_contact_details']['#attributes'] = array('style' => 'display:none');
  83. $settings['caubo_registration']['disorg'] = 1; // disable Org selector as FormAPI seems to have a bug
  84.  
  85. // add Role selection
  86. _caubo_registration_roles($form, $form_state);
  87.  
  88. // replace user_reg submit function with our own
  89. foreach ($form_state['storage']['submits'] as $key => $submit) {
  90. if ($submit == "user_register_submit") $form_state['storage']['submits'][$key] = 'caubo_user_register_submit';
  91. }
  92. $form['#submit'] = $form_state['storage']['submits'];
  93.  
  94. // add our JS twekas
  95. $settings['caubo_registration']['clean'] = true;
  96. drupal_add_js($settings, 'setting');
  97. drupal_add_js(drupal_get_path('module', 'caubo_registration') . "/caubo_registration.js");
  98.  
  99. // set custom page title
  100. drupal_set_title(t("User Account - select your preferred Title"));
  101.  
  102. return $form;
  103. }
  104.  
  105. // Third page of registration - same as Page 2 except Org selector is enabled and we will return to Page 2
  106. function caubo_register_page_three(&$form, &$form_state) {
  107. global $pass_email_address;
  108.  
  109. // set Page 1 fields as entered on Page 1 and disable
  110. $form['name']['#value'] = $form_state['values']['name'];
  111. $form['mail']['#value'] = $form_state['values']['mail'];
  112. $form['title']['#value'] = $form_state['content_profile_registration']['profile']['node']->title;
  113.  
  114. $pass_email_address = $form['mail']['#value'];
  115.  
  116. $form['name']['#disabled'] = true;
  117. $form['mail']['#disabled'] = true;
  118. $form['title']['#disabled'] = true;
  119.  
  120. $form['group_contact_details']['field_organization']['#pre_render'] = array('_caubo_limit_organizations');
  121.  
  122. $form_state['storage']['page_three'] = TRUE;
  123.  
  124. // return to Page 2
  125. $form['submit']['#value'] = "next";
  126. $form['#submit'] = array('caubo_register_form_submit');
  127.  
  128. // add our JS tweaks
  129. $settings['caubo_registration']['clean'] = true;
  130. drupal_add_js($settings, 'setting');
  131. drupal_add_js(drupal_get_path('module', 'caubo_registration') . "/caubo_registration.js");
  132.  
  133. // set custom page title
  134. drupal_set_title(t("User Account - select your Organization"));
  135.  
  136. return $form;
  137. }
  138.  
  139. function _caubo_limit_organizations($element) {
  140. global $pass_email_address;
  141.  
  142. $nids = _caubo_get_organizations_from_email($pass_email_address);
  143. foreach ($element['nid']['nid']['#options'] as $nid => $option) {
  144. if (!in_array($nid, $nids)) unset($element['nid']['nid']['#options'][$nid]);
  145. }
  146. return $element;
  147. }
  148.  
  149. function test_form_validate($form, &$form_state){
  150. // Validating page 2
  151. if (isset($form_state['storage']['page_two'])) {
  152. form_set_error('last-name', 'Please enter a last name.'); // Example only
  153. }
  154. return;
  155. }
  156.  
  157. /**
  158. * handle sorting out which page to go to based on if distinct or non-distinct org case
  159. *
  160. * @param mixed $form
  161. * @param mixed $form_state
  162. */
  163. function caubo_register_form_submit($form, &$form_state) {
  164. if ($orgid = $form_state['content_profile_registration']['profile']['node']->field_organization[0]['nid']) $orgs[] = $orgid;
  165. else $orgs = _caubo_get_organizations_from_email($form['mail']['#value']);
  166. $n = count($orgs);
  167.  
  168. switch ($n) {
  169. case 0:
  170. drupal_access_denied(); // likely do something else here
  171. break;
  172.  
  173. case 1: // distinct case - goto Page 2
  174. $form_state['rebuild'] = TRUE;
  175. $form_state['storage']['page'] = 2;
  176. $form_state['storage']['orgid'] = current($orgs);
  177. $form_state['storage']['page_one_values'] = $form_state['values']; // Carry forward Page 1 values - WHY??
  178. break;
  179.  
  180. default: // non-distinct
  181. $form_state['rebuild'] = TRUE;
  182. $form_state['storage']['page'] = 3;
  183. $form_state['storage']['page_one_values'] = $form_state['values'];
  184. break;
  185.  
  186. }
  187. }
  188.  
  189.  
  190. /**
  191. * return an array of NIDs matching Org nodes with email_domains set that match $mail
  192. *
  193. * - more than one Org can use this mail domain
  194. * - an Org may have more than mail domain set
  195. *
  196. * @param mixed $mail
  197. */
  198. function _caubo_get_organizations_from_email($mail) {
  199. $matches = explode("@", $mail);
  200. $domain = $matches[1];
  201.  
  202. $sql = "SELECT n.nid FROM {content_field_email_domains} AS e
  203. Inner Join {node} AS n ON n.vid = e.vid
  204. WHERE e.field_email_domains_value = '%s' AND n.status = 1 AND n.type = 'organization'";
  205.  
  206. $results = db_query($sql, $domain);
  207. $nids = array();
  208. while ($result = db_fetch_object($results)) $nids[] = $result->nid;
  209.  
  210. return $nids;
  211. }
  212.  
  213. function _caubo_get_organization_type($orgid) {
  214. $org = node_load($orgid);
  215. return $org->field_orgtype[0]['value'];
  216. }
  217.  
  218. function _caubo_registration_roles(&$form, &$form_state){
  219. $roles = array();
  220. $display_roles = variable_get('caubo_registration_profile_roles', $roles);
  221. $approval_roles = variable_get('caubo_registration_profile_roles_approval', $roles);
  222.  
  223. if (empty($display_roles)){
  224. $link = l("here.", 'admin/settings/caubo_registration');
  225. drupal_set_message("You must configure the CAUBO Registration module " . $link, "error");
  226. return;
  227. }
  228.  
  229. $orgid = $form_state['storage']['orgid'];
  230. $orgtype = _caubo_get_organization_type($orgid);
  231. if ($orgtype === "1") $filter = "C.";
  232. if ($orgtype === "2") $filter = "U.";
  233.  
  234. foreach($display_roles as $role){
  235. if ($role) {
  236. $role_name = db_result(db_query("SELECT name FROM {role} WHERE rid = %d", $role));
  237. if (substr($role_name, 0, 2) != $filter) {
  238. if(in_array($role, $approval_roles)) {
  239. $role_name .= " <i>*needs administration approval</i>";
  240. }
  241. $roles[$role] = $role_name;
  242. }
  243. }
  244. }
  245.  
  246. asort($roles);
  247.  
  248. $form['roles'] = array(
  249. '#type' => 'radios',
  250. '#title' =>'Choose a role',
  251. '#options' => $roles,
  252. '#weight' => 20,
  253. );
  254. }
  255.  
  256. /**
  257. * admin settings form.
  258. *
  259. * @return
  260. */
  261. function _caubo_registration_admin_settings_form() {
  262. //Obtain all roles
  263.  
  264. $result = db_query("SELECT * FROM {role} WHERE rid > 2");
  265. if ($result == TRUE){
  266. while ($role = db_fetch_array($result)){
  267. $roles[$role['rid']] = $role['name'];
  268. $default_value[$role['rid']]=0;
  269. }
  270.  
  271. $form['caubo_registration_profile_roles'] = array(
  272. '#type' => 'checkboxes',
  273. '#title' =>'Roles',
  274. '#options' => $roles,
  275. '#description' => "Choose roles that will be displayed on registration form",
  276. '#default_value' => variable_get('caubo_registration_profile_roles', $default_value),
  277. );
  278.  
  279. $form['caubo_registration_profile_roles_approval'] = array(
  280. '#type' => 'checkboxes',
  281. '#title' =>'Approval Roles',
  282. '#options' => $roles,
  283. '#description' => "Choose roles that need administration approval. Users that select this role during registration will
  284. be disabled until administrator approves them.",
  285. '#default_value' => variable_get('caubo_registration_profile_roles_approval', $default_value),
  286. );
  287.  
  288. return system_settings_form($form);
  289. }
  290. }
  291.  
  292. /**
  293. * Submit handler for the user registration form. - OUR VERSION
  294. *
  295. * This function is shared by the installation form and the normal registration form,
  296. * which is why it can't be in the user.pages.inc file.
  297. */
  298. function caubo_user_register_submit($form, &$form_state) {
  299. global $base_url;
  300. $admin = user_access('administer users');
  301.  
  302. $mail = $form_state['values']['mail'];
  303. $name = $form_state['values']['name'];
  304. if (!variable_get('user_email_verification', TRUE) || $admin) {
  305. $pass = $form_state['values']['pass'];
  306. }
  307. else {
  308. $pass = user_password();
  309. };
  310. $notify = isset($form_state['values']['notify']) ? $form_state['values']['notify'] : NULL;
  311. $from = variable_get('site_mail', ini_get('sendmail_from'));
  312. if (isset($form_state['values']['roles'])) {
  313. $roles = array($form_state['values']['roles']);
  314. $rid = $form_state['values']['roles'];
  315. }
  316. else {
  317. $roles = array();
  318. }
  319.  
  320. if (!$admin && array_intersect(array_keys($form_state['values']), array('uid', 'init', 'session', 'status'))) {
  321. watchdog('security', 'Detected malicious attempt to alter protected user fields.', array(), WATCHDOG_WARNING);
  322. $form_state['redirect'] = 'user/register';
  323. return;
  324. }
  325. // The unset below is needed to prevent these form values from being saved as
  326. // user data.
  327. unset($form_state['values']['form_token'], $form_state['values']['submit'], $form_state['values']['op'], $form_state['values']['notify'],
  328. $form_state['values']['form_id'], $form_state['values']['affiliates'], $form_state['values']['destination']);
  329.  
  330. $merge_data = array('pass' => $pass, 'init' => $mail, 'roles' => $roles);
  331.  
  332. // NOTE - this is where we deviate from std user_register and instead of simply checking the site wide (admin//user/settings) setting for the type of
  333. // approval required (none or admin) - we now check against the selected role and set accordingly
  334.  
  335. if (!$admin) {
  336. $approval_roles = variable_get('caubo_registration_profile_roles_approval', array());
  337. if(in_array($rid, $approval_roles)) $merge_data['status'] = 0;
  338. else $merge_data['status'] = 1;
  339. }
  340.  
  341. $account = user_save('', array_merge($form_state['values'], $merge_data));
  342.  
  343. // this won't save our Role so let's do it now
  344. db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $account->uid, $rid);
  345.  
  346. // Terminate if an error occured during user_save().
  347. if (!$account) {
  348. drupal_set_message(t("Error saving user account."), 'error');
  349. $form_state['redirect'] = '';
  350. return;
  351. }
  352. $form_state['user'] = $account;
  353.  
  354. watchdog('user', 'New user: %name (%email).', array('%name' => $name, '%email' => $mail), WATCHDOG_NOTICE, l(t('edit'), 'user/'. $account->uid .'/edit'));
  355.  
  356. // The first user may login immediately, and receives a customized welcome e-mail.
  357. if ($account->uid == 1) {
  358. drupal_set_message(t('Welcome to Drupal. You are now logged in as user #1, which gives you full control over your website.'));
  359. if (variable_get('user_email_verification', TRUE)) {
  360. drupal_set_message(t('</p><p> Your password is <strong>%pass</strong>. You may change your password below.</p>', array('%pass' => $pass)));
  361. }
  362.  
  363. user_authenticate(array_merge($form_state['values'], $merge_data));
  364.  
  365. $form_state['redirect'] = 'user/1/edit';
  366. return;
  367. }
  368. else {
  369. // Add plain text password into user account to generate mail tokens.
  370. $account->password = $pass;
  371. if ($admin && !$notify) {
  372. drupal_set_message(t('Created a new user account for <a href="@url">%name</a>. No e-mail has been sent.', array('@url' => url("user/$account->uid"), '%name' => $account->name)));
  373. }
  374. else if (!variable_get('user_email_verification', TRUE) && $account->status && !$admin) {
  375. // No e-mail verification is required, create new user account, and login
  376. // user immediately.
  377. _user_mail_notify('register_no_approval_required', $account);
  378. if (user_authenticate(array_merge($form_state['values'], $merge_data))) {
  379. drupal_set_message(t('Registration successful. You are now logged in.'));
  380. }
  381. $form_state['redirect'] = '';
  382. return;
  383. }
  384. else if ($account->status || $notify) {
  385. // Create new user account, no administrator approval required.
  386. $op = $notify ? 'register_admin_created' : 'register_no_approval_required';
  387. _user_mail_notify($op, $account);
  388. if ($notify) {
  389. drupal_set_message(t('Password and further instructions have been e-mailed to the new user <a href="@url">%name</a>.', array('@url' => url("user/$account->uid"), '%name' => $account->name)));
  390. }
  391. else {
  392. drupal_set_message(t('Your password and further instructions have been sent to your e-mail address.'));
  393. $form_state['redirect'] = 'user';
  394. _caubo_clean_form_state($form_state);
  395. return;
  396. }
  397. }
  398. else {
  399. // Create new user account, administrator approval required.
  400. _user_mail_notify('register_pending_approval', $account);
  401. drupal_set_message(t('Thank you for applying for an account. Your account is currently pending approval by the site administrator.<br />In the meantime, a welcome message with further instructions has been sent to your e-mail address.'));
  402. $form_state['redirect'] = 'user';
  403. _caubo_clean_form_state($form_state);
  404. return;
  405. }
  406. }
  407. }
  408.  
  409. function _caubo_clean_form_state(&$form_state) {
  410. $form_state['storage'] = array();
  411. unset($form_state['content_profile_registration'], $form_state['values']);
  412.  
  413. return;
  414. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement