Parent Directory
|
Revision Log
|
Revision Graph
fixed #218679 where required pages of pageroutes weren't sorted correctly fixed a problem where all stored variables tied to a membership type were not properly erased, leaving remnants behind and confusing admins
| 1 | <?php |
| 2 | /** |
| 3 | * This is the nf_registration_mod module for use with nodeprofiles and which will override some |
| 4 | * of the standard operations of registration and nodeprofiles. Additionally, this establishes |
| 5 | * a framework for sub-accounts and the managing of them by users. The actual management is done |
| 6 | * in another module. |
| 7 | |
| 8 | * @version $Id$; |
| 9 | * @package Registration_Mod |
| 10 | * @category NeighborForge |
| 11 | * @author Ryan Constantine |
| 12 | * @filesource |
| 13 | * @license http://www.gnu.org/licenses/gpl.txt GNU_GENERAL_PUBLIC_LICENSE |
| 14 | * @link none yet |
| 15 | * TODO add flag to users' data field to track whether they've completed the required pages or not; this should cut down |
| 16 | * on the time it takes to verify they've done it and will eliminate some DB queries. |
| 17 | */ |
| 18 | |
| 19 | //-----------------------------------------Various Drupal Hooks----------------------------------------- |
| 20 | //-----------------------------------------Various Drupal Hooks----------------------------------------- |
| 21 | //-----------------------------------------Various Drupal Hooks----------------------------------------- |
| 22 | |
| 23 | /** |
| 24 | * Implementation of hook_help |
| 25 | * |
| 26 | * Display help and module information |
| 27 | * @param string section which section of the site we're displaying help |
| 28 | * @return help text for section |
| 29 | */ |
| 30 | function nf_registration_mod_help($section='') { |
| 31 | $output = ''; |
| 32 | switch ($section) { |
| 33 | case "admin/help#nf_registration_mod": |
| 34 | $output = '<p>' . t("Changes the registration page to allow for multiple kinds of accounts and directs each to a unique pageroute. |
| 35 | See the pageroute module as well as the nodeprofiles module for more details."). '</p>'; |
| 36 | break; |
| 37 | } |
| 38 | |
| 39 | return $output; |
| 40 | } // function nf_registration_mod_help() |
| 41 | |
| 42 | /** |
| 43 | * Implementation of hook_perm |
| 44 | * |
| 45 | * Valid permissions for this module |
| 46 | * @return array An array of valid permissions for the onthisdate module |
| 47 | */ |
| 48 | function nf_registration_mod_perm() { |
| 49 | return array('administer registration pageroutes'); |
| 50 | } // function nf_registration_mod_perm() |
| 51 | |
| 52 | /** |
| 53 | * Implementation of hook_menu |
| 54 | * |
| 55 | * @return array An array of arrays, to add menu entries to the system menu. |
| 56 | */ |
| 57 | function nf_registration_mod_menu($may_cache) { |
| 58 | global $_menu, $user; |
| 59 | $items = array(); |
| 60 | if ($may_cache) { |
| 61 | $items[] = array( //adminster |
| 62 | 'path' => 'admin/user/nf_registration_mod', |
| 63 | 'title' => t('Registration Membership Types'), |
| 64 | 'callback' => 'drupal_get_form', |
| 65 | 'callback arguments' => array('nf_registration_mod_main'), |
| 66 | 'access' => user_access('administer registration pageroutes'), |
| 67 | 'description' => t('Create membership types, associate them with pageroutes, and update permissions.'), |
| 68 | 'type' => MENU_NORMAL_ITEM, |
| 69 | ); |
| 70 | |
| 71 | $items[] = array( //tab to take us to main administer page |
| 72 | 'path' => 'admin/user/nf_registration_mod/overview', |
| 73 | 'title' => t('Overview'), |
| 74 | 'access' => user_access('administer registration pageroutes'), |
| 75 | 'type' => MENU_DEFAULT_LOCAL_TASK, |
| 76 | 'weight' => -10, |
| 77 | ); |
| 78 | |
| 79 | $items[] = array( |
| 80 | 'path' => 'admin/user/nf_registration_mod/ct_adjustments', |
| 81 | 'title' => t('Content type adjustments'), |
| 82 | 'callback' => 'drupal_get_form', |
| 83 | 'callback arguments' => array('nf_registration_mod_content_type_adjustments'), |
| 84 | 'access' => user_access('administer registration pageroutes'), |
| 85 | 'type' => MENU_LOCAL_TASK, |
| 86 | ); |
| 87 | |
| 88 | $items[] = array( //delete a membership type and its pageroute |
| 89 | 'path' => 'admin/user/nf_registration_mod/delete', |
| 90 | 'title' => t('Delete'), |
| 91 | 'callback' => 'drupal_get_form', |
| 92 | 'callback arguments' => array('nf_registration_mod_confirm_delete'), |
| 93 | 'access' => user_access('administer registration pageroutes'), |
| 94 | 'type' => MENU_CALLBACK, |
| 95 | ); |
| 96 | |
| 97 | $items[] = array( //edit a membership type's name or pageroute tie |
| 98 | 'path' => 'admin/user/nf_registration_mod/edit', |
| 99 | 'title' => t('Edit'), |
| 100 | 'callback' => 'drupal_get_form', |
| 101 | 'callback arguments' => array('nf_registration_mod_edit'), |
| 102 | 'access' => user_access('administer registration pageroutes'), |
| 103 | 'type' => MENU_CALLBACK, |
| 104 | ); |
| 105 | $mem_select_access = ($user->uid == 0 ? TRUE : FALSE) || ($user->uid == 1? TRUE : FALSE) || user_access('administer users'); |
| 106 | $items[] = array( //select a membership type as first step for registration |
| 107 | 'path' => 'user/membership_select', |
| 108 | 'title' => t('Select desired Membership'), |
| 109 | 'callback' => 'drupal_get_form', |
| 110 | 'callback arguments' => array('nf_registration_mod_select_membership'), |
| 111 | 'access' => $mem_select_access, |
| 112 | 'type' => MENU_CALLBACK, |
| 113 | ); |
| 114 | |
| 115 | $items[] = array( //edit a membership type's name or pageroute tie |
| 116 | 'path' => 'admin/user/nf_registration_mod/set_access', |
| 117 | 'title' => t('Set access'), |
| 118 | 'callback' => 'drupal_get_form', |
| 119 | 'callback arguments' => array('nf_registration_mod_access'), |
| 120 | 'access' => user_access('administer registration pageroutes'), |
| 121 | 'type' => MENU_LOCAL_TASK, |
| 122 | ); |
| 123 | } |
| 124 | elseif (!$may_cache) { |
| 125 | $admin_access = user_access('administer users'); |
| 126 | $user_access = $user->uid == arg(1); |
| 127 | $user_access3 = $user->uid == arg(3); |
| 128 | if ($accounttypes = module_exists('accounttypes')) { |
| 129 | $items[] = array( |
| 130 | 'path' => 'admin/user/nf_registration_mod/account_type', |
| 131 | 'title' => t('Tie account type to membership'), |
| 132 | 'callback' => 'drupal_get_form', |
| 133 | 'callback arguments' => array('nf_registration_mod_tie_account_type'), |
| 134 | 'access' => user_access('administer registration pageroutes'), |
| 135 | 'type' => MENU_LOCAL_TASK, |
| 136 | ); |
| 137 | } |
| 138 | |
| 139 | if (arg(0) == 'user' && arg(2) == 'edit') { |
| 140 | $edit_user = user_load(array('uid' => arg(1))); |
| 141 | $membership_type = $edit_user->mem_choice; |
| 142 | $mem_pageroutes = _nf_registration_mod_get_added(); |
| 143 | $all_routes = _nf_registration_mod_get_pageroutes(); |
| 144 | $all_pages = _nf_registration_mod_get_pageroute_pages(); |
| 145 | $user_route = $all_routes[$mem_pageroutes[$membership_type]]; |
| 146 | $user_pages = $all_pages[$mem_pageroutes[$membership_type]]; |
| 147 | $sql = "SELECT nid FROM {node} WHERE uid = %d AND type = '%s'"; |
| 148 | $weight = -10; |
| 149 | $path = $user_route .'/0/'. arg(1); |
| 150 | //TODO for other modules that may add a link to 'Account settings' unset it from $_menu |
| 151 | $items[] = array( |
| 152 | 'path' => 'user/'. arg(1) .'/edit/pageroute', |
| 153 | 'title' => t('Personal data'), |
| 154 | 'callback' => 'drupal_goto', |
| 155 | 'callback arguments' => array($path, 'destination=user/'. arg(1) .'/edit'), |
| 156 | 'access' => $admin_access || $user_access, |
| 157 | 'type' => MENU_LOCAL_TASK, |
| 158 | 'weight' => $weight, |
| 159 | ); |
| 160 | $items[] = array( |
| 161 | 'path' => 'user/'. arg(1) .'/edit/settings', |
| 162 | 'title' => t('General settings'), |
| 163 | 'access' => $admin_access || $user_access, |
| 164 | 'type' => MENU_DEFAULT_LOCAL_TASK, |
| 165 | 'weight' => -20, |
| 166 | ); |
| 167 | } |
| 168 | if (is_numeric(arg(2)) && is_numeric(arg(3))) { |
| 169 | //this isn't working. pageroute is thwarting this - I had to patch pageroute and this still doesn't work - the callback never fires. |
| 170 | $edit_user = user_load(array('uid' => arg(3))); |
| 171 | $membership_type = $edit_user->mem_choice; |
| 172 | $mem_pageroutes = _nf_registration_mod_get_added(); |
| 173 | $all_routes = _nf_registration_mod_get_pageroutes(); |
| 174 | $user_route = $all_routes[$mem_pageroutes[$membership_type]]; |
| 175 | if (arg(0) == $user_route) { |
| 176 | $return_path = 'user/'. arg(3) .'/edit'; |
| 177 | global $_SESSION; |
| 178 | $_SESSION['myarg'] = arg(3); |
| 179 | $items[] = array( |
| 180 | 'path' => $user_route .'/return', |
| 181 | 'title' => t('Back to "My account"'), |
| 182 | 'callback' => 'drupal_goto', |
| 183 | 'callback arguments' => array($return_path), |
| 184 | 'access' => $admin_access || $user_access3, |
| 185 | 'type' => MENU_LOCAL_TASK, |
| 186 | 'weight' => 100, |
| 187 | ); |
| 188 | } |
| 189 | } |
| 190 | $_menu['callbacks']['node/add']['callback'] = 'nf_registration_mod_node_add'; |
| 191 | if (isset($_SESSION['no_browse']) && ($_SESSION['no_browse'] == 1)) { |
| 192 | if ($user->uid == 1) { |
| 193 | return $items; |
| 194 | } |
| 195 | $required_types = variable_get('nf_registration_mod_req_types', array()); |
| 196 | if (empty($required_types)) { |
| 197 | return $items; |
| 198 | } |
| 199 | else { |
| 200 | //get all of the data we need to determine which types correspond to the user |
| 201 | $membership_type = $user->mem_choice; |
| 202 | $mem_pageroutes = _nf_registration_mod_get_added(); |
| 203 | $all_routes = _nf_registration_mod_get_pageroutes(); |
| 204 | $all_pages = _nf_registration_mod_get_pageroute_pages(); |
| 205 | $user_route = $all_routes[$mem_pageroutes[$membership_type]]; |
| 206 | $user_pages = $all_pages[$mem_pageroutes[$membership_type]]; |
| 207 | //now check the route content types against those that are required |
| 208 | $completed_requirements = array(); |
| 209 | $incomplete_requirements = array(); |
| 210 | $sql = "SELECT nid FROM {node} WHERE uid = %d AND type = '%s'"; |
| 211 | foreach ($required_types as $type => $val) { |
| 212 | if ($page_name = array_search($type, $user_pages)) { |
| 213 | if ($result = db_fetch_object(db_query($sql, $user->uid, $type))) { |
| 214 | $completed_requirements[$type] = $result->nid; |
| 215 | } |
| 216 | else { |
| 217 | $incomplete_requirements[] = $page_name; |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | //Construct a string of the page names |
| 222 | $output = implode(', ', $incomplete_requirements); |
| 223 | //if the user hasn't met the requirements, send them to the right spot |
| 224 | if (!empty($incomplete_requirements) && (arg(0) != $user_route) && (arg(0) != 'logout') && (arg(0) != 'user') && (arg(1) != 'activeselect')) { |
| 225 | drupal_set_message(t('You still have some mandatory data to enter before you can use this website. |
| 226 | You need to complete: ') .$output); |
| 227 | unset($_REQUEST['destination']); |
| 228 | $_SESSION['no_browse'] = 1; |
| 229 | drupal_goto($user_route. '/' .$incomplete_requirements[0]); |
| 230 | return $items; |
| 231 | } |
| 232 | elseif (empty($incomplete_requirements)) { |
| 233 | unset($_SESSION['no_browse']); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | return $items; |
| 239 | } // function nf_registration_mod_menu() |
| 240 | |
| 241 | //-----------------------------------------Administrative Hooks and Functions----------------------------------------- |
| 242 | //-----------------------------------------Administrative Hooks and Functions----------------------------------------- |
| 243 | //-----------------------------------------Administrative Hooks and Functions----------------------------------------- |
| 244 | |
| 245 | /** |
| 246 | * Callback function: Add options for the . |
| 247 | * |
| 248 | * @return mixed |
| 249 | */ |
| 250 | function nf_registration_mod_main() { |
| 251 | if ($admin = user_access('administer registration pageroutes')) { |
| 252 | $form['memberships'] = array( |
| 253 | '#type' => 'textfield', |
| 254 | '#size' => 20, |
| 255 | '#maxlength' => 64, |
| 256 | '#description' => t('Should not contain spaces'), |
| 257 | ); |
| 258 | $form['msubmit'] = array( |
| 259 | '#type' => 'submit', |
| 260 | '#value' => t('Add membership type'), |
| 261 | ); |
| 262 | $pageroutes = _nf_registration_mod_get_pageroutes(); |
| 263 | $form['tie'] = array( |
| 264 | '#type' => 'select', |
| 265 | '#options' => $pageroutes, |
| 266 | ); |
| 267 | $form['tsubmit'] = array( |
| 268 | '#type' => 'submit', |
| 269 | '#value' => t('Add pageroute tie'), |
| 270 | ); |
| 271 | $form['secret_value'] = array( |
| 272 | '#type' => 'value', |
| 273 | '#value' => '', |
| 274 | ); |
| 275 | $added_names = _nf_registration_mod_get_added(); |
| 276 | if (!empty($added_names)) { |
| 277 | foreach ($added_names as $name => $tie) { |
| 278 | if (empty($tie) && ($form['secret_value']['#value'] == '')) { |
| 279 | $form['secret_value']['#value'] = $name; |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | $form['user_instructions'] = array( |
| 284 | '#type' => 'textarea', |
| 285 | '#title' => t('User instructions'), |
| 286 | '#description' => t('Write instructions to your users so they know how to choose their membership type.'), |
| 287 | '#default_value' => variable_get('nf_registration_mod_instructions', ''), |
| 288 | ); |
| 289 | $form['usubmit'] = array( |
| 290 | '#type' => 'submit', |
| 291 | '#value' => t('Save message'), |
| 292 | ); |
| 293 | } |
| 294 | return $form; |
| 295 | } // function nf_registration_mod_main() |
| 296 | |
| 297 | /** |
| 298 | * Validate the membership name before adding or saving it |
| 299 | * |
| 300 | * @param string $form_id |
| 301 | * @param mixed $form_values |
| 302 | */ |
| 303 | function nf_registration_mod_main_validate($form_id, $form_values) { |
| 304 | //make sure the user isn't injecting something already on the list. |
| 305 | if ($form_values['op'] == t('Add membership type')) { |
| 306 | if (!$form_values['memberships']) { |
| 307 | form_set_error('memberships', t('You must specify a valid membership name.')); |
| 308 | } |
| 309 | $check_options = _nf_registration_mod_get_added(); |
| 310 | if (empty($check_options)) { |
| 311 | $check_options = array(); |
| 312 | } |
| 313 | if (array_key_exists($form_values['memberships'], $check_options)) { |
| 314 | form_set_error('memberships', t('The membership name %name is already added. Please choose another name.', array('%name' => $form_values['memberships']))); |
| 315 | } |
| 316 | } |
| 317 | elseif ($form_values['op'] == t('Add pageroute tie')) { |
| 318 | if (!$form_values['tie']) { |
| 319 | form_set_error('tie', t('You must specify a valid pageroute.')); |
| 320 | } |
| 321 | $pageroutes = _nf_registration_mod_get_pageroutes(); |
| 322 | if (!array_key_exists($form_values['tie'], $pageroutes)) { |
| 323 | form_set_error('tie', t('The pageroute %name is not valid. You must choose from the dropdown list.', array('%name' => $form_values['tie']))); |
| 324 | } |
| 325 | } |
| 326 | } // function nf_registration_mod_main_validate() |
| 327 | |
| 328 | /** |
| 329 | * Submit the membership name to be added to the variable. |
| 330 | * |
| 331 | * @param string $form_id |
| 332 | * @param mixed $form_values |
| 333 | * @return string |
| 334 | */ |
| 335 | function nf_registration_mod_main_submit($form_id, $form_values) { |
| 336 | if ($form_values['op'] == t('Add membership type')) { |
| 337 | $mem_nums = variable_get('nf_registration_mod_mem_num', array()); |
| 338 | $mem_nums[] = $form_values['memberships']; |
| 339 | variable_set('nf_registration_mod_mem_num', $mem_nums); |
| 340 | $added_names = _nf_registration_mod_get_added(); |
| 341 | $added_names[$form_values['memberships']] = ''; |
| 342 | variable_set('nf_registration_mod_memberships', $added_names); |
| 343 | drupal_set_message(t('The membership name has been added.')); |
| 344 | } |
| 345 | elseif ($form_values['op'] == t('Add pageroute tie')) { |
| 346 | $added_names = _nf_registration_mod_get_added(); |
| 347 | $added_names[$form_values['secret_value']] = $form_values['tie']; |
| 348 | variable_set('nf_registration_mod_memberships', $added_names); |
| 349 | drupal_set_message(t('The pageroute tie has been added.')); |
| 350 | } |
| 351 | elseif ($form_values['op'] == t('Save message')) { |
| 352 | variable_set('nf_registration_mod_instructions', $form_values['user_instructions']); |
| 353 | } |
| 354 | return 'admin/user/nf_registration_mod'; |
| 355 | } // function nf_registration_mod_main_submit() |
| 356 | |
| 357 | /** |
| 358 | * Theme for the add page of the main function. |
| 359 | * |
| 360 | * <p>This lists all of the current membership option names with links to edit them or their assigned pageroute, followed with |
| 361 | * a field and button to add a new membership options name to the list.</p> |
| 362 | * @param mixed $form |
| 363 | * @return mixed |
| 364 | */ |
| 365 | function theme_nf_registration_mod_main($form) { |
| 366 | $header = array(t('Membership title'), array('data' => t('Pageroute'), 'colspan' => 2), array('data' => t('Operations'), 'colspan' => 2)); |
| 367 | $added_names = _nf_registration_mod_get_added(); |
| 368 | $pageroutes = _nf_registration_mod_get_pageroutes(); |
| 369 | if (!empty($added_names)) { |
| 370 | foreach ($added_names as $name => $tie) { |
| 371 | if (empty($tie)) { |
| 372 | $admin = l(t('edit'), 'admin/user/nf_registration_mod/edit/' .$name); |
| 373 | $delete = l(t('delete'), 'admin/user/nf_registration_mod/delete/' .$name); |
| 374 | $rows[] = array($name, drupal_render($form['tie']), drupal_render($form['tsubmit']), $admin, $delete); |
| 375 | } |
| 376 | else { |
| 377 | $route = $pageroutes[$tie]; |
| 378 | $admin = l(t('edit'), 'admin/user/nf_registration_mod/edit/' .$name); |
| 379 | $delete = l(t('delete'), 'admin/user/nf_registration_mod/delete/' .$name); |
| 380 | $rows[] = array($name, $route, '', $admin, $delete); |
| 381 | } |
| 382 | } |
| 383 | unset($form['tie']); |
| 384 | unset($form['tsubmit']); |
| 385 | } |
| 386 | else { |
| 387 | unset($form['tie']); |
| 388 | unset($form['tsubmit']); |
| 389 | } |
| 390 | $rows[] = array(drupal_render($form['memberships']), array('data' => drupal_render($form['msubmit']), colspan => 4)); |
| 391 | |
| 392 | $output = theme('table', $header, $rows); |
| 393 | $output .= drupal_render($form); |
| 394 | |
| 395 | return $output; |
| 396 | } //function theme_nf_registration_mod_main() |
| 397 | |
| 398 | /** |
| 399 | * Returns a confirmation page for deleting a membership. |
| 400 | * |
| 401 | * @param integer $name Name of membership to be deleted; passed in from path. |
| 402 | * @return array $form The name is passed to the submit. |
| 403 | */ |
| 404 | function nf_registration_mod_confirm_delete($name = NULL) { |
| 405 | if ($name == NULL) { |
| 406 | drupal_goto('admin/user/nf_registration_mod'); |
| 407 | return; |
| 408 | } |
| 409 | if ($admin = user_access('administer registration pageroutes')) { |
| 410 | $added_names = _nf_registration_mod_get_added(); |
| 411 | $form['name'] = array( |
| 412 | '#type' => 'value', |
| 413 | '#value' => $name, |
| 414 | ); |
| 415 | $return_address = 'admin/user/nf_registration_mod'; |
| 416 | $form = confirm_form($form, t('Are you sure you want to delete the membership type %name?', |
| 417 | array('%name' => $name)), $return_address, |
| 418 | t('Deleting a membership type here will not delete the pageroute it is tied to. You still do that through the pageroute administration. This action cannot be undone.'), t('Delete'), t('Cancel')); |
| 419 | return $form; |
| 420 | } |
| 421 | } // function nf_registration_mod_confirm_delete() |
| 422 | |
| 423 | /** |
| 424 | * Implementation of forms api _submit call. Deletes a membership from the variable list after confirmation. |
| 425 | * |
| 426 | * @param string $form_id |
| 427 | * @param mixed $form_values |
| 428 | * @return string $return_address The path to return to after we've deleted the record. |
| 429 | */ |
| 430 | function nf_registration_mod_confirm_delete_submit($form_id, $form_values) { |
| 431 | $added_names = _nf_registration_mod_get_added(); |
| 432 | unset($added_names[$form_values['name']]); |
| 433 | variable_set('nf_registration_mod_memberships', $added_names); |
| 434 | $mem_nums = variable_get('nf_registration_mod_mem_num', NULL); |
| 435 | if (is_array($mem_nums)) { |
| 436 | $key = array_keys($mem_nums, $form_values['name']); |
| 437 | drupal_set_message('<pre>Form values: ' .print_r($form_values['name'], TRUE). '</pre>'); |
| 438 | drupal_set_message('<pre>Mem nums: ' .print_r($mem_nums, TRUE). '</pre>'); |
| 439 | drupal_set_message('<pre>Key: ' .print_r($key, TRUE). '</pre>'); |
| 440 | unset($mem_nums[$key[0]]); |
| 441 | variable_set('nf_registration_mod_mem_num', $mem_nums); |
| 442 | } |
| 443 | if (!isset($added_names[$form_values['name']])) { |
| 444 | drupal_set_message(t('The membership type %name has been removed from administration.', array('%name' => $form_values['name']))); |
| 445 | watchdog('content', t('nf_registration_mod: deleted %name.', array('%name' => $form_values['name']))); |
| 446 | } |
| 447 | if ($added_accs = _nf_registration_mod_get_acc_types()) { |
| 448 | unset($added_accs[$form_values['name']]); |
| 449 | variable_set('nf_registration_mod_acc_types', $added_accs); |
| 450 | if (!isset($added_accs[$form_values['name']])) { |
| 451 | drupal_set_message(t('The account type %name has been untied from the membership type administration.', array('%name' => $form_values['name']))); |
| 452 | } |
| 453 | } |
| 454 | $return_address = 'admin/user/nf_registration_mod'; |
| 455 | return $return_address; |
| 456 | } // function nf_registration_mod_confirm_delete_submit() |
| 457 | |
| 458 | /** |
| 459 | * Callback function: Add, edit, or delete account types. |
| 460 | * |
| 461 | * <p>Here is where the admin interface is for creating new account types,editing their names, or deleting them.</p> |
| 462 | * @param integer $name The name of the membership type. |
| 463 | * @return mixed |
| 464 | */ |
| 465 | function nf_registration_mod_edit($name = NULL) { |
| 466 | if ($name == NULL) { |
| 467 | drupal_goto('admin/user/nf_registration_mod'); |
| 468 | return; |
| 469 | } |
| 470 | if ($admin = user_access('administer registration pageroutes')) { |
| 471 | $added_names = _nf_registration_mod_get_added(); |
| 472 | $form['name'] = array( |
| 473 | '#type' => 'textfield', |
| 474 | '#title' => t('Membership type name'), |
| 475 | '#default_value' => $name, |
| 476 | '#size' => 20, |
| 477 | '#required' => TRUE, |
| 478 | '#maxlength' => 64, |
| 479 | ); |
| 480 | $pageroutes = _nf_registration_mod_get_pageroutes(); |
| 481 | $form['tie'] = array( |
| 482 | '#type' => 'select', |
| 483 | '#title' => t('Pageroute'), |
| 484 | '#required' => TRUE, |
| 485 | '#default_value' => $added_names[$name], |
| 486 | '#options' => $pageroutes, |
| 487 | ); |
| 488 | $form['old_name'] = array( |
| 489 | '#type' => 'value', |
| 490 | '#value' => $name, |
| 491 | ); |
| 492 | if (!module_exists('accounttypes')) {//accounttypes implements auto-roll assign; don't step on toes |
| 493 | $roles = user_roles(TRUE); |
| 494 | unset($roles['2']); |
| 495 | $current_role = variable_get('nf_registration_mod_enhance_roles', array()); |
| 496 | $current_role = $current_role[$name]; |
| 497 | $form['nf_registration_mod_enhance_roles'] = array( |
| 498 | '#type' => 'radios', |
| 499 | '#title' => t('Select role to automatically assign to new users'), |
| 500 | '#options' => $roles, |
| 501 | '#default_value' => $current_role, |
| 502 | '#description' => t('The selected role will be assigned to new registrants from now on. Be sure this role does not have any privileges you fear giving out without reviewing who receives it.'), |
| 503 | ); |
| 504 | } |
| 505 | $form['submit'] = array( |
| 506 | '#type' => 'submit', |
| 507 | '#value' => t('Save membership type'), |
| 508 | ); |
| 509 | return $form; |
| 510 | } |
| 511 | } // function nf_registration_mod_edit() |
| 512 | |
| 513 | /** |
| 514 | * Validate the membership type name before saving it |
| 515 | * |
| 516 | * @param string $form_id |
| 517 | * @param mixed $form_values |
| 518 | */ |
| 519 | function nf_registration_mod_edit_validate($form_id, $form_values) { |
| 520 | if ($form_values['name']) { |
| 521 | if ($form_values['op'] == t('Save membership type')) { |
| 522 | if (!$form_values['name']) { |
| 523 | form_set_error('name', t('You must specify a valid membership name.')); |
| 524 | } |
| 525 | $check_options = _nf_registration_mod_get_added(); |
| 526 | if (empty($check_options)) { |
| 527 | $check_options = array(); |
| 528 | } |
| 529 | if (!$form_values['tie']) { |
| 530 | form_set_error('tie', t('You must specify a valid pageroute.')); |
| 531 | } |
| 532 | $pageroutes = _nf_registration_mod_get_pageroutes(); |
| 533 | if (!array_key_exists($form_values['tie'], $pageroutes)) { |
| 534 | form_set_error('tie', t('The pageroute %name is not valid. You must choose from the dropdown list.', array('%name' => $form_values['tie']))); |
| 535 | } |
| 536 | } |
| 537 | } |
| 538 | else { |
| 539 | form_set_error('name', t('You must specify a valid membership type name.')); |
| 540 | } |
| 541 | } // function nf_registration_mod_edit_validate() |
| 542 | |
| 543 | /** |
| 544 | * Submit the membership type name to be saved, to the database. |
| 545 | * |
| 546 | * @param string $form_id |
| 547 | * @param mixed $form_values |
| 548 | * @return string |
| 549 | */ |
| 550 | function nf_registration_mod_edit_submit($form_id, $form_values) { |
| 551 | if ($form_values['op'] == t('Save membership type')) { |
| 552 | //save membership-pageroute association |
| 553 | $added_names = _nf_registration_mod_get_added(); |
| 554 | unset($added_names[$form_values['old_name']]); |
| 555 | $added_names[$form_values['name']] = $form_values['tie']; |
| 556 | variable_set('nf_registration_mod_memberships', $added_names); |
| 557 | drupal_set_message(t('The membership type has been updated.')); |
| 558 | //save accounttype tie |
| 559 | if ($added_accs = variable_get('nf_registration_mod_acc_types', FALSE)) { |
| 560 | $added_accs[$form_values['name']] = $added_accs[$form_values['old_name']]; |
| 561 | unset($added_accs[$form_values['old_name']]); |
| 562 | variable_set('nf_registration_mod_acc_types', $added_accs); |
| 563 | } |
| 564 | if ($choices = variable_get('nf_registration_mod_mem_num', FALSE)) { |
| 565 | if ($key = array_search($form_values['old_name'], $choices)) { |
| 566 | $choices[$key] = $form_values['name']; |
| 567 | } |
| 568 | variable_set('nf_registration_mod_mem_num', $choices); |
| 569 | } |
| 570 | //for auto-role-assign |
| 571 | if (!module_exists('accounttypes')) {//accounttypes implements auto-roll assign; don't step on toes |
| 572 | $added_roles = variable_get('nf_registration_mod_enhance_roles', NULL); |
| 573 | unset($added_roles[$form_values['old_name']]); |
| 574 | $added_roles[$form_values['name']] = $form_values['nf_registration_mod_enhance_roles']; |
| 575 | variable_set('nf_registration_mod_enhance_roles', $added_roles); |
| 576 | } |
| 577 | } |
| 578 | return 'admin/user/nf_registration_mod'; |
| 579 | } // function nf_registration_mod_edit_submit() |
| 580 | |
| 581 | /** |
| 582 | * Present an interface to allow tying account types to membership types |
| 583 | */ |
| 584 | function nf_registration_mod_tie_account_type() { |
| 585 | if ($admin = user_access('administer registration pageroutes')) { |
| 586 | $added_names = _nf_registration_mod_get_added(); |
| 587 | $added_account_types = _nf_registration_mod_get_acc_types(); |
| 588 | $account_types = get_accounttypes(); |
| 589 | foreach ($added_names as $name => $prid) { |
| 590 | $form[$name] = array( |
| 591 | '#type' => 'select', |
| 592 | '#default_value' => $added_account_types[$name], |
| 593 | '#options' => $account_types, |
| 594 | ); |
| 595 | } |
| 596 | $form['submit'] = array( |
| 597 | '#type' => 'submit', |
| 598 | '#value' => t('Save assignments'), |
| 599 | ); |
| 600 | } |
| 601 | return $form; |
| 602 | } // function nf_registration_mod_tie_account_type() |
| 603 | |
| 604 | /** |
| 605 | * Validate the account type assignments before saving them |
| 606 | * |
| 607 | * @param string $form_id |
| 608 | * @param mixed $form_values |
| 609 | */ |
| 610 | function nf_registration_mod_tie_account_type_validate($form_id, $form_values) { |
| 611 | if ($form_values['op'] == t('Save assignments')) { |
| 612 | $added_names = _nf_registration_mod_get_added(); |
| 613 | $account_types = get_accounttypes(); |
| 614 | foreach ($added_names as $name => $prid) { |
| 615 | if (!array_key_exists($form_values[$name], $account_types)) { |
| 616 | form_set_error($name, t('The account type %name is not valid. You must choose from the dropdown list.', array('%name' => $form_values['atid'][$name]))); |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | } //function nf_registration_mod_tie_account_type_validate() |
| 621 | |
| 622 | /** |
| 623 | * Submit the membership/account type tie to be saved. |
| 624 | * |
| 625 | * @param string $form_id |
| 626 | * @param mixed $form_values |
| 627 | * @return string |
| 628 | */ |
| 629 | function nf_registration_mod_tie_account_type_submit($form_id, $form_values) { |
| 630 | if ($form_values['op'] == t('Save assignments')) { |
| 631 | $added_names = _nf_registration_mod_get_added(); |
| 632 | $added_account_types = _nf_registration_mod_get_acc_types(); |
| 633 | foreach ($added_names as $name => $prid) { |
| 634 | $added_account_types[$name] = $form_values[$name]; |
| 635 | } |
| 636 | variable_set('nf_registration_mod_acc_types', $added_account_types); |
| 637 | drupal_set_message(t('The membership/account type ties have been updated.')); |
| 638 | } |
| 639 | return 'admin/user/nf_registration_mod'; |
| 640 | } // function nf_registration_mod_tie_account_type_submit() |
| 641 | |
| 642 | /** |
| 643 | * Theme for the account type tie function. Display in tabular format. |
| 644 | * |
| 645 | * <p>This presents a table with membership type names in one column. Each has a drop down selector in the next column. If an account type |
| 646 | * has been selected for that membership type, it will be selected by default. Tying an account type to a membership type in this manner |
| 647 | * will assign that account type to any person that signs up using that membership type.</p> |
| 648 | * @param mixed $form |
| 649 | * @return mixed |
| 650 | */ |
| 651 | function theme_nf_registration_mod_tie_account_type($form) { |
| 652 | $header = array(t('Membership title'), t('Account types')); |
| 653 | $added_names = _nf_registration_mod_get_added(); |
| 654 | if (!empty($added_names)) { |
| 655 | foreach ($added_names as $name => $prid) { |
| 656 | $rows[] = array($name, drupal_render($form[$name])); |
| 657 | } |
| 658 | } |
| 659 | else { |
| 660 | foreach ($added_names as $name => $prid) { |
| 661 | unset($form[$name]); |
| 662 | } |
| 663 | unset($form['submit']); |
| 664 | } |
| 665 | $rows[] = array(array('data' => drupal_render($form['submit']), colspan => 2)); |
| 666 | |
| 667 | $output = drupal_render($form); |
| 668 | $output .= theme('table', $header, $rows); |
| 669 | |
| 670 | return $output; |
| 671 | } //function theme_nf_registration_mod_tie_account_type() |
| 672 | |
| 673 | /** |
| 674 | * Show menu items which were disabled from 'create content' menu. Also, show which pageroute menus were disabled. |
| 675 | */ |
| 676 | function nf_registration_mod_content_type_adjustments() { |
| 677 | if ($admin = user_access('administer registration pageroutes')) { |
| 678 | $types = _nf_registration_mod_group_pageroute_types(); |
| 679 | foreach ($types as $type => $prid) { |
| 680 | $form[$type] = array( |
| 681 | '#type' => 'checkbox', |
| 682 | ); |
| 683 | } |
| 684 | $form['info'] = array( |
| 685 | '#type' => 'item', |
| 686 | '#value' => t('This is to let you know which content types should be removed from the <em>Create content</em> menu and any OG menus if you\'ve got OG enabled. |
| 687 | It is advised that you check all items and then disable them as users should probably not be able to create these after the registration process. |
| 688 | That is of course, as long as you\'re using <em>Lonely Nodes</em> and the <em>Nodeprofile</em> module along with the <em>Pageroute</em> module. |
| 689 | You can set which of these content types are read-only and which can be edited and by which roles on the <em>Set access</em> tab.'), |
| 690 | ); |
| 691 | $form['submit'] = array( |
| 692 | '#type' => 'submit', |
| 693 | '#value' => t('Disable menu items'), |
| 694 | ); |
| 695 | $form['submit_required'] = array( |
| 696 | '#type' => 'submit', |
| 697 | '#value' => t('Require for login'), |
| 698 | ); |
| 699 | } |
| 700 | return $form; |
| 701 | } // function nf_registration_mod_content_type_adjustments() |
| 702 | |
| 703 | /** |
| 704 | * Submit the content type menu items to be disabled. Items are diabled from the 'create content' menu and also added to the Organic |
| 705 | * Groups list of omitted types if OG is present. We don't check for the presence of OG, because we're just setting a 'variable' from |
| 706 | * the variable table. |
| 707 | * |
| 708 | * This is a different approach than that used by the og_content_type_admin module because the changes here affect all users and |
| 709 | * aren't changed on a per-user basis. |
| 710 | * |
| 711 | * Also, on this page, we can set which content types are mandatory in order for the user to use the site. At login, the hook_user |
| 712 | * function will check to see if the user has created a node of each required type. If not, then they will be presented with the |
| 713 | * pageroute page they need to complete. So even if they started filling out their pageroute, if they abandoned it at any point, |
| 714 | * they cannot proceed. |
| 715 | * |
| 716 | * This may be a clunky interface. We'll have to see. I am using the same checkboxes for disabling as for requiring. Consequently, |
| 717 | * they cannot be set as default_values. However, a status column indicates their current settings. To keep a setting, it must be |
| 718 | * checked for the operation you execute. |
| 719 | * |
| 720 | * @param string $form_id |
| 721 | * @param mixed $form_values |
| 722 | * @return string |
| 723 | */ |
| 724 | function nf_registration_mod_content_type_adjustments_submit($form_id, $form_values) { |
| 725 | if ($form_values['op'] == t('Disable menu items')) { |
| 726 | //disable content type menu items |
| 727 | $status = _nf_registration_mod_menu_status(); |
| 728 | $sql = "UPDATE {menu} SET type = 48 WHERE path = '%s'"; |
| 729 | $og_omitted = variable_get('og_omitted', array()); |
| 730 | foreach ($status as $type => $nums) { |
| 731 | if (($form_values[$type] == 1) && ($nums[1] != 48)) { |
| 732 | if ($result = db_query($sql, $nums[0])) { |
| 733 | drupal_set_message(t('Successfully disabled content type menu item') .' <em>' .$nums[0]. '</em>'); |
| 734 | } |
| 735 | else { |
| 736 | drupal_set_message(t('Failed to disable content type menu item') .' <em>' .$nums[0]. '</em>', 'error'); |
| 737 | } |
| 738 | $og_omitted[$type] = $type; |
| 739 | } |
| 740 | } |
| 741 | variable_set('og_omitted', $og_omitted); |
| 742 | |
| 743 | //now disable pageroute menu items |
| 744 | $sqls = "UPDATE {menu} SET type = 48 WHERE path = '%s'"; |
| 745 | $results = db_query("SELECT * FROM {pageroute_routes} ORDER BY path"); |
| 746 | while ($route = db_fetch_object($results)) { |
| 747 | if ($resulta = db_query($sqls, $route->path)) { |
| 748 | drupal_set_message(t('Successfully disabled pageroute menu item') .'<em>' .$route->path. '</em>'); |
| 749 | } |
| 750 | else { |
| 751 | drupal_set_message(t('Failed to disable pageroute menu item') .' <em>' .$route->path. '</em>', 'error'); |
| 752 | } |
| 753 | } |
| 754 | cache_clear_all(NULL, 'cache_menu', '*'); |
| 755 | } |
| 756 | if ($form_values['op'] == t('Require for login')) { |
| 757 | $content_types = _nf_registration_mod_group_pageroute_types(); |
| 758 | $required_types = array(); |
| 759 | foreach ($content_types as $type => $nums) { |
| 760 | if ($form_values[$type] == 1) { |
| 761 | $required_types[$type] = $type; |
| 762 | } |
| 763 | } |
| 764 | variable_set('nf_registration_mod_req_types', $required_types); |
| 765 | drupal_set_message(t('Required content types saved.')); |
| 766 | } |
| 767 | |
| 768 | return 'admin/user/nf_registration_mod'; |
| 769 | } // function nf_registration_mod_content_type_adjustments_submit() |
| 770 | |
| 771 | /** |
| 772 | * Present the menu status for each content type |
| 773 | */ |
| 774 | function theme_nf_registration_mod_content_type_adjustments($form) { |
| 775 | $pageroutes = _nf_registration_mod_get_pageroutes(); |
| 776 | $pagepages = _nf_registration_mod_get_pageroute_pages(); |
| 777 | $types = _nf_registration_mod_group_pageroute_types(); |
| 778 | $status = _nf_registration_mod_menu_status(); |
| 779 | $status2 = _nf_registration_mod_required_content_types(); |
| 780 | |
| 781 | /*$header = array(t('Pageroute'), t('Page name'), t('Content type')); |
| 782 | if (!empty($pagepages)) { |
| 783 | foreach ($pagepages as $prid => $page) { |
| 784 | $rows[] = array($pageroutes[$prid], '', ''); |
| 785 | foreach ($page as $name => $type) { |
| 786 | $rows[] = array('', $name, $type); |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | $output = theme('table', $header, $rows);*/ |
| 791 | $output = drupal_render($form['info']); |
| 792 | $header2 = array(array('data' => t('Content type'), 'colspan' => 2), t('Status'), t('Menu path'), t('Pageroute'), t('Page name')); |
| 793 | if (!empty($types)) { |
| 794 | foreach ($types as $type => $prid) { |
| 795 | $sql = "SELECT name FROM {node_type} WHERE type = '%s'"; |
| 796 | $result = db_fetch_object(db_query($sql, $type)); |
| 797 | if ($status[$type][1] == 48) { |
| 798 | $current_status = '<strong>'. t('disabled') .'</strong>'; |
| 799 | if ($status2[$type] == $type) { |
| 800 | $current_status .= '<strong>/'. t('required') .'</strong>'; |
| 801 | } |
| 802 | } |
| 803 | else { |
| 804 | $current_status = '<strong>'. t('enabled') .'</strong>'; |
| 805 | if ($status2[$type] == $type) { |
| 806 | $current_status .= '<strong>/'. t('required') .'</strong>'; |
| 807 | } |
| 808 | } |
| 809 | $rows2[] = array(drupal_render($form[$type]), $result->name, $current_status, $status[$type][0], '', ''); |
| 810 | foreach ($prid as $id => $page) { |
| 811 | $rows2[] = array('', '', '', '', $pageroutes[$id], $page); |
| 812 | } |
| 813 | } |
| 814 | } |
| 815 | $rows2[] = array(array('data' => drupal_render($form['submit']), colspan => 6)); |
| 816 | $rows2[] = array(array('data' => drupal_render($form['submit_required']), colspan => 2), |
| 817 | array('data' => t('Prevent users from using the site until these content types are created for them. Login will route them to these pages.'), colspan => 4) |
| 818 | ); |
| 819 | $output .= theme('table', $header2, $rows2); |
| 820 | $output .= drupal_render($form); |
| 821 | |
| 822 | return $output; |
| 823 | } // function theme_nf_registration_mod_content_type_adjustments() |
| 824 | |
| 825 | /** |
| 826 | * Interception of the node module's node_add function. |
| 827 | * |
| 828 | * Present a node submission form or a set of links to such forms. |
| 829 | */ |
| 830 | function nf_registration_mod_node_add($type = NULL) { |
| 831 | global $user, $_GET; |
| 832 | |
| 833 | if (module_exists('og_content_type_admin')) { |
| 834 | exit();//let og_content_type_admin deal with this if it is enabled |
| 835 | } |
| 836 | $types = node_get_types(); |
| 837 | $pageroute_types = _nf_registration_mod_menu_status(); |
| 838 | foreach ($pageroute_types as $typea => $nums) { |
| 839 | if ($nums[1] == 48) { |
| 840 | if (isset($types[$typea])) { |
| 841 | unset($types[$typea]); |
| 842 | } |
| 843 | } |
| 844 | } |
| 845 | $type = isset($type) ? str_replace('-', '_', $type) : NULL; |
| 846 | |
| 847 | // If a node type has been specified, validate its existence and output the form if authorized. |
| 848 | if (isset($types[$type])) { |
| 849 | // Initialize settings: |
| 850 | $node = array('uid' => $user->uid, 'name' => $user->name, 'type' => $type); |
| 851 | drupal_set_title(t('Submit @name', array('@name' => $types[$type]->name))); |
| 852 | $output = drupal_get_form($type. '_node_form', $node); |
| 853 | } |
| 854 | else { |
| 855 | // If no (valid) node type has been provided, display a node type overview. |
| 856 | // Make sure to use the site-wide settings since we're on the node/add page in this case. |
| 857 | foreach ($types as $type) { |
| 858 | if (function_exists($type->module. '_form') && node_access('create', $type->type)) { |
| 859 | $type_url_str = str_replace('_', '-', $type->type); |
| 860 | $title = t('Add a new @s.', array('@s' => $type->name)); |
| 861 | $out = '<dt>' .l(drupal_ucfirst($type->name), "node/add/$type_url_str", array('title' => $title)). '</dt>'; |
| 862 | $out .= '<dd>' .filter_xss_admin($type->description). '</dd>'; |
| 863 | $item[$type->type] = $out; |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | if (isset($item)) { |
| 868 | uksort($item, 'strnatcasecmp'); |
| 869 | $output = t('Choose the appropriate item from the list:'). '<dl>' .implode('', $item). '</dl>'; |
| 870 | } |
| 871 | else { |
| 872 | drupal_set_title(t('Invalid Content Type')); |
| 873 | $output = t('Either the content type you are trying to create does not exist, or you are not authorized to access the content type you have tried to create in the current context. \'Create\' a content type by selecting from either the group menu or the \'Create content\' menu instead.'); |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | return $output; |
| 878 | } // function nf_registration_mod_node_add() |
| 879 | |
| 880 | /** |
| 881 | * Allow the admin to set two permissions for each content type here instead/in addition to the regular interface. |
| 882 | * |
| 883 | * The permissions that make sense to set here are the 'create' and 'edit own' permissions for each content type. |
| 884 | * In the author's application, there are several content types that the user should fill out. The first several |
| 885 | * contain required information and are also things that should never change. If the user wants to change these |
| 886 | * items, the author wants them to contact the admin for assistance. Consequently, the user will be given the |
| 887 | * 'create' permission for those content types, but not the 'edit own' permission. Other pages will contain info |
| 888 | * that should change regularly. For these content types, the user is given both the 'create' and 'edit own' |
| 889 | * permissions. This function deals not at all with the regular 'edit' permission allowing one to edit anybody's |
| 890 | * posts of that content type. |
| 891 | */ |
| 892 | function nf_registration_mod_access() { |
| 893 | if ($admin = user_access('administer registration pageroutes')) { |
| 894 | $roles = user_roles(); |
| 895 | //TODO add permissions modification section; not sure how to do that; storage implies that I need to list all perms... |
| 896 | $form['info'] = array( |
| 897 | '#type' => 'item', |
| 898 | '#value' => t('<p>In the future, you will be able to set the permissions that directly relate to the content types of your pageroutes here. |
| 899 | For now, proceed to the regular permissions page. I suggest you start by assigning the \'create [CONTENT TYPE] content\' to as many roles as will need it for each of the pageroute content types. |
| 900 | Then, if you were clever and designed a few content types that you\'d like users to NOT change after creation, make sure NOT to assign regular users\' roles the \'edit own [CONTENT TYPE] content\' permission. |
| 901 | For content types that are meant to be changed later or often, DO assign the \'edit own [CONTENT TYPE] content\' permissions. If you do things this way, make sure to also NOT use the \'Integrate into profile categories\' option. |
| 902 | For example, the author has a content type which holds a name and a birthdate. Clearly, these should (almost) never change and there is no reason the user should. |
| 903 | In this case, users can create the content but not edit it. Should a name need changing, the sys admin can do it.</p> |
| 904 | <p>Alternatively, use an access module like TAC to give your users view access without edit access.</p>'), |
| 905 | /*t('From here you can decide which content types will be read-only and which will be editable after creation. |
| 906 | Check the box in the appropriate column next to each content type where you would like to give the selected roles either the |
| 907 | <em>edit own CONTENT TYPE content</em> permission, or the <em>create CONTENT TYPE content</em> permission. Then click the corresponding button |
| 908 | to apply your changes. All other roles will be set to NOT have that permission. Remember, a user only needs one role with a |
| 909 | given permission in order to use it. Plan accordingly. Set any other permissions related to these content types in the usual way. |
| 910 | Of course, you may choose not to use this page at all. It is simply here for convenience.'),*/ |
| 911 | ); |
| 912 | } |
| 913 | return $form; |
| 914 | } // function nf_registration_mod_access() |
| 915 | |
| 916 | /** |
| 917 | * Present the menu status for each content type |
| 918 | */ |
| 919 | function theme_nf_registration_mod_access($form) { |
| 920 | $output = drupal_render($form['info']); |
| 921 | $output .= l(t('Set content type permissions in the node module section.'), 'admin/user/access'); |
| 922 | //$header2 = array(array('data' => t('Content type'), 'colspan' => 2), t('Status'), t('Menu path'), t('Pageroute'), t('Page name')); |
| 923 | return $output; |
| 924 | } // function nf_registration_mod_access() |
| 925 | |
| 926 | //-----------------------------------------Worker Hooks and Functions----------------------------------------- |
| 927 | //-----------------------------------------Worker Hooks and Functions----------------------------------------- |
| 928 | //-----------------------------------------Worker Hooks and Functions----------------------------------------- |
| 929 | |
| 930 | /** |
| 931 | * Implementation of hook_user |
| 932 | * |
| 933 | * We need to intercept the user processes at a couple of places. First, at registration, we need to add the |
| 934 | * membership selection to the user registration page so it will later be saved in the user's data field. Next, |
| 935 | * because we're using a session variable to keep track of the rerouting of the registration page see |
| 936 | * (hook_form_alter), we need to unset it when the user is inserted. Lastly, if we have required that certain |
| 937 | * pages of the user's pageroute be filled out before they can be active on the site, we intercept the login |
| 938 | * process and route the user where they need to go. |
| 939 | * |
| 940 | * @param string $op The operation to be performed. |
| 941 | * @param array $edit The data to be operated on. |
| 942 | * @param mixed &$account The user's account. |
| 943 | * @param string $category The category of the user account or registration page. |
| 944 | */ |
| 945 | function nf_registration_mod_user($op, &$edit, &$account, $category = NULL) { |
| 946 | if (!$edit['mem_choice']) { |
| 947 | if (isset($account->mem_choice)) { |
| 948 | $membership_type = $account->mem_choice; |
| 949 | } |
| 950 | } |
| 951 | else { |
| 952 | $membership_type = $edit['mem_choice']; |
| 953 | } |
| 954 | switch ($op) { |
| 955 | case 'register': |
| 956 | $form['mem_choice'] = array( |
| 957 | '#type' => 'hidden', |
| 958 | '#value' => $_SESSION['mem_choice'], |
| 959 | ); |
| 960 | if (!module_exists('accounttypes')) {//accounttypes implements auto-roll assign; don't step on toes |
| 961 | if (!empty($_SESSION['mem_choice']) && $rid = variable_get('nf_registration_mod_enhance_roles', '')) { |
| 962 | $rid = $rid[$_SESSION['mem_choice']]; |
| 963 | $form['mem_choice_role'] = array( |
| 964 | '#type' => 'hidden', |
| 965 | '#value' => $rid, |
| 966 | ); |
| 967 | } |
| 968 | } |
| 969 | return $form; |
| 970 | |
| 971 | case 'insert': |
| 972 | unset($_SESSION['mem_choice']); |
| 973 | if (!module_exists('accounttypes')) {//accounttypes implements auto-roll assign; don't step on toes |
| 974 | if(!empty($edit['mem_choice_role'])){ |
| 975 | db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $account->uid, $edit['mem_choice_role']); |
| 976 | } |
| 977 | } |
| 978 | break; |
| 979 | |
| 980 | case 'update': //copied and modified from nodeprofile.module |
| 981 | if (($membership_type = $user->mem_choice) && node_access('create', $category)) { |
| 982 | subform_element_submit(); |
| 983 | unset($edit[$category. '_node_form']); |
| 984 | } |
| 985 | break; |
| 986 | |
| 987 | case 'form': //copied and modified from nodeprofile.module |
| 988 | if ($membership_type = $account->mem_choice) { |
| 989 | $fields['membership_type'] = array( |
| 990 | '#type' => 'item', |
| 991 | '#title' => t('Membership Type'), |
| 992 | '#value' => $membership_type, |
| 993 | '#weight' => -50, |
| 994 | ); |
| 995 | } |
| 996 | if (($membership_type = $account->mem_choice) && node_access('create', $category) && ($category != 'account')) { |
| 997 | $type = $category; |
| 998 | $node = node_load(array('type' => $type, 'uid' => $account->uid)); |
| 999 | if (empty($node)) { |
| 1000 | $node = (object)array('uid' => $account->uid, 'name' => $account->name, 'type' => $type); |
| 1001 | } |
| 1002 | elseif (!node_access('update', $node)) { |
| 1003 | break; |
| 1004 | } |
| 1005 | $fields[$category] = array( |
| 1006 | '#type' => 'subform', |
| 1007 | '#id' => $node->type. '_node_form', |
| 1008 | '#arguments' => array($node), |
| 1009 | '#subform_after_build' => array('nf_registration_mod_adapt_node_form'),//this doesn't work if the diff module is enabled, see form_alter for the fix |
| 1010 | ); |
| 1011 | } |
| 1012 | return $fields; |
| 1013 | break; |
| 1014 | |
| 1015 | case 'login': |
| 1016 | global $user; |
| 1017 | if ($user->uid == 1) { |
| 1018 | break; |
| 1019 | } |
| 1020 | $required_types = variable_get('nf_registration_mod_req_types', array()); |
| 1021 | if (empty($required_types)) { |
| 1022 | break; |
| 1023 | } |
| 1024 | //get all of the data we need to determine which types correspond to the user |
| 1025 | $membership_type = $user->mem_choice; |
| 1026 | $mem_pageroutes = _nf_registration_mod_get_added(); |
| 1027 | $all_routes = _nf_registration_mod_get_pageroutes(); |
| 1028 | $all_pages = _nf_registration_mod_get_pageroute_pages(); |
| 1029 | $user_route = $all_routes[$mem_pageroutes[$membership_type]]; |
| 1030 | $user_pages = $all_pages[$mem_pageroutes[$membership_type]]; |
| 1031 | //now check the route content types against those that are required |
| 1032 | $completed_requirements = array(); |
| 1033 | $incomplete_requirements = array(); |
| 1034 | $sql = "SELECT nid FROM {node} WHERE uid = %d AND type = '%s'"; |
| 1035 | foreach ($required_types as $type => $val) { |
| 1036 | if ($page_name = array_search($type, $user_pages)) { |
| 1037 | if ($result = db_fetch_object(db_query($sql, $user->uid, $type))) { |
| 1038 | $completed_requirements[$type] = $result->nid; |
| 1039 | } |
| 1040 | else { |
| 1041 | $incomplete_requirements[] = $page_name; |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | //Construct a string of the page names |
| 1046 | $output = implode(', ', $incomplete_requirements); |
| 1047 | //if the user hasn't met the requirements, send them to the right spot |
| 1048 | if (!empty($incomplete_requirements)) { |
| 1049 | drupal_set_message(t('You still have some mandatory data to enter before you can use this website. |
| 1050 | You need to complete: ') .$output); |
| 1051 | unset($_REQUEST['destination']); |
| 1052 | $_SESSION['no_browse'] = 1; |
| 1053 | drupal_goto($user_route. '/' .$incomplete_requirements[0]); |
| 1054 | break; |
| 1055 | //} |
| 1056 | } |
| 1057 | else { |
| 1058 | unset($_SESSION['no_browse']); |
| 1059 | } |
| 1060 | break; |
| 1061 | } |
| 1062 | return; |
| 1063 | } // function nf_registration_mod_user() |
| 1064 | |
| 1065 | /** |
| 1066 | * Assign/change existing/old users' membership type |
| 1067 | * |
| 1068 | * This is the only interface to change a user's membership type. This is useful for existing sites that want to add this module |
| 1069 | * but want to ensure that existing users have a memebership type and not just new users. |
| 1070 | * |
| 1071 | * It is advised that admins take their site offline and update all existing users. I'm sure admins would do this anyway as they |
| 1072 | * will also have to construct the nodeprofiles and associated pageroutes. There aren't any checks for users that don't have a |
| 1073 | * membership type (except user 1), so if you leave off doing this, something may explode. :-) |
| 1074 | */ |
| 1075 | function nf_registration_mod_user_operations() { |
| 1076 | global $form_values; |
| 1077 | if (user_access('administer registration pageroutes')) { |
| 1078 | $allmemtypes = _nf_registration_mod_get_added(); |
| 1079 | |
| 1080 | $change_memtypes = array(); |
| 1081 | foreach ($allmemtypes as $key => $value) { |
| 1082 | $change_memtypes['change_membership_type@' .$key] = $key; |
| 1083 | } |
| 1084 | |
| 1085 | if (count($allmemtypes)) { |
| 1086 | $memtype_operations = array( |
| 1087 | t('Change the membership type of the selected users') => array( |
| 1088 | 'label' => $change_memtypes, |
| 1089 | ), |
| 1090 | ); |
| 1091 | |
| 1092 | $operations = $memtype_operations; |
| 1093 | } |
| 1094 | } |
| 1095 | // If the form has been posted, we need to insert the proper data for role editing if necessary. |
| 1096 | if ($form_values) { |
| 1097 | $operation_mid = explode('@', $form_values['operation']); |
| 1098 | $operation = $operation_mid[0]; |
| 1099 | $mid = $operation_mid[1]; |
| 1100 | if ($operation == 'change_membership_type') { |
| 1101 | if (user_access(' |