[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/modules/user/ -> user.admin.inc (source)

   1  <?php
   2  
   3  /**
   4   * @file
   5   * Admin page callback file for the user module.
   6   */
   7  
   8  function user_admin($callback_arg = '') {
   9    $op = isset($_POST['op']) ? $_POST['op'] : $callback_arg;
  10  
  11    switch ($op) {
  12      case t('Create new account'):
  13      case 'create':
  14        $output = drupal_get_form('user_register');
  15        break;
  16      default:
  17        if (!empty($_POST['accounts']) && isset($_POST['operation']) && ($_POST['operation'] == 'delete')) {
  18          $output = drupal_get_form('user_multiple_delete_confirm');
  19        }
  20        else {
  21          $output = drupal_get_form('user_filter_form');
  22          $output .= drupal_get_form('user_admin_account');
  23        }
  24    }
  25    return $output;
  26  }
  27  
  28  /**
  29   * Form builder; Return form for user administration filters.
  30   *
  31   * @ingroup forms
  32   * @see user_filter_form_submit()
  33   */
  34  function user_filter_form() {
  35    $session = &$_SESSION['user_overview_filter'];
  36    $session = is_array($session) ? $session : array();
  37    $filters = user_filters();
  38  
  39    $i = 0;
  40    $form['filters'] = array(
  41      '#type' => 'fieldset',
  42      '#title' => t('Show only users where'),
  43      '#theme' => 'user_filters',
  44    );
  45    foreach ($session as $filter) {
  46      list($type, $value) = $filter;
  47      // Merge an array of arrays into one if necessary.
  48      $options = $type == 'permission' ? call_user_func_array('array_merge', $filters[$type]['options']) : $filters[$type]['options'];
  49      $params = array('%property' => $filters[$type]['title'] , '%value' => $options[$value]);
  50      if ($i++ > 0) {
  51        $form['filters']['current'][] = array('#value' => t('<em>and</em> where <strong>%property</strong> is <strong>%value</strong>', $params));
  52      }
  53      else {
  54        $form['filters']['current'][] = array('#value' => t('<strong>%property</strong> is <strong>%value</strong>', $params));
  55      }
  56    }
  57  
  58    foreach ($filters as $key => $filter) {
  59      $names[$key] = $filter['title'];
  60      $form['filters']['status'][$key] = array(
  61        '#type' => 'select',
  62        '#options' => $filter['options'],
  63      );
  64    }
  65  
  66    $form['filters']['filter'] = array(
  67      '#type' => 'radios',
  68      '#options' => $names,
  69    );
  70    $form['filters']['buttons']['submit'] = array(
  71      '#type' => 'submit',
  72      '#value' => (count($session) ? t('Refine') : t('Filter')),
  73    );
  74    if (count($session)) {
  75      $form['filters']['buttons']['undo'] = array(
  76        '#type' => 'submit',
  77        '#value' => t('Undo'),
  78      );
  79      $form['filters']['buttons']['reset'] = array(
  80        '#type' => 'submit',
  81        '#value' => t('Reset'),
  82      );
  83    }
  84  
  85    drupal_add_js('misc/form.js', 'core');
  86  
  87    return $form;
  88  }
  89  
  90  /**
  91   * Process result from user administration filter form.
  92   */
  93  function user_filter_form_submit($form, &$form_state) {
  94    $op = $form_state['values']['op'];
  95    $filters = user_filters();
  96    switch ($op) {
  97      case t('Filter'): case t('Refine'):
  98        if (isset($form_state['values']['filter'])) {
  99          $filter = $form_state['values']['filter'];
 100          // Merge an array of arrays into one if necessary.
 101          $options = $filter == 'permission' ? call_user_func_array('array_merge', $filters[$filter]['options']) : $filters[$filter]['options'];
 102          if (isset($options[$form_state['values'][$filter]])) {
 103            $_SESSION['user_overview_filter'][] = array($filter, $form_state['values'][$filter]);
 104          }
 105        }
 106        break;
 107      case t('Undo'):
 108        array_pop($_SESSION['user_overview_filter']);
 109        break;
 110      case t('Reset'):
 111        $_SESSION['user_overview_filter'] = array();
 112        break;
 113      case t('Update'):
 114        return;
 115    }
 116  
 117    $form_state['redirect'] = 'admin/user/user';
 118    return;
 119  }
 120  
 121  /**
 122   * Form builder; User administration page.
 123   *
 124   * @ingroup forms
 125   * @see user_admin_account_validate()
 126   * @see user_admin_account_submit()
 127   */
 128  function user_admin_account() {
 129    $filter = user_build_filter_query();
 130  
 131    $header = array(
 132      array(),
 133      array('data' => t('Username'), 'field' => 'u.name'),
 134      array('data' => t('Status'), 'field' => 'u.status'),
 135      t('Roles'),
 136      array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc'),
 137      array('data' => t('Last access'), 'field' => 'u.access'),
 138      t('Operations')
 139    );
 140  
 141    if ($filter['join'] != "") {
 142      $sql = 'SELECT DISTINCT u.uid, u.name, u.status, u.created, u.access FROM {users} u LEFT JOIN {users_roles} ur ON u.uid = ur.uid '. $filter['join'] .' WHERE u.uid != 0 '. $filter['where'];
 143      $query_count = 'SELECT COUNT(DISTINCT u.uid) FROM {users} u LEFT JOIN {users_roles} ur ON u.uid = ur.uid '. $filter['join'] .' WHERE u.uid != 0 '. $filter['where'];
 144    }
 145    else {
 146      $sql = 'SELECT u.uid, u.name, u.status, u.created, u.access FROM {users} u WHERE u.uid != 0 '. $filter['where'];
 147      $query_count = 'SELECT COUNT(u.uid) FROM {users} u WHERE u.uid != 0 '. $filter['where'];
 148    }
 149  
 150    
 151    $sql .= tablesort_sql($header);
 152  
 153    $result = pager_query($sql, 50, 0, $query_count, $filter['args']);
 154  
 155    $form['options'] = array(
 156      '#type' => 'fieldset',
 157      '#title' => t('Update options'),
 158      '#prefix' => '<div class="container-inline">',
 159      '#suffix' => '</div>',
 160    );
 161    $options = array();
 162    foreach (module_invoke_all('user_operations') as $operation => $array) {
 163      $options[$operation] = $array['label'];
 164    }
 165    $form['options']['operation'] = array(
 166      '#type' => 'select',
 167      '#options' => $options,
 168      '#default_value' => 'unblock',
 169    );
 170    $form['options']['submit'] = array(
 171      '#type' => 'submit',
 172      '#value' => t('Update'),
 173    );
 174  
 175    $destination = drupal_get_destination();
 176  
 177    $status = array(t('blocked'), t('active'));
 178    $roles = user_roles(TRUE);
 179    $accounts = array();
 180    while ($account = db_fetch_object($result)) {
 181      $accounts[$account->uid] = '';
 182      $form['name'][$account->uid] = array('#value' => theme('username', $account));
 183      $form['status'][$account->uid] =  array('#value' => $status[$account->status]);
 184      $users_roles = array();
 185      $roles_result = db_query('SELECT rid FROM {users_roles} WHERE uid = %d', $account->uid);
 186      while ($user_role = db_fetch_object($roles_result)) {
 187        $users_roles[] = $roles[$user_role->rid];
 188      }
 189      asort($users_roles);
 190      $form['roles'][$account->uid][0] = array('#value' => theme('item_list', $users_roles));
 191      $form['member_for'][$account->uid] = array('#value' => format_interval(time() - $account->created));
 192      $form['last_access'][$account->uid] =  array('#value' => $account->access ? t('@time ago', array('@time' => format_interval(time() - $account->access))) : t('never'));
 193      $form['operations'][$account->uid] = array('#value' => l(t('edit'), "user/$account->uid/edit", array('query' => $destination)));
 194    }
 195    $form['accounts'] = array(
 196      '#type' => 'checkboxes',
 197      '#options' => $accounts
 198    );
 199    $form['pager'] = array('#value' => theme('pager', NULL, 50, 0));
 200  
 201    return $form;
 202  }
 203  
 204  /**
 205   * Submit the user administration update form.
 206   */
 207  function user_admin_account_submit($form, &$form_state) {
 208    $operations = module_invoke_all('user_operations', $form_state);
 209    $operation = $operations[$form_state['values']['operation']];
 210    // Filter out unchecked accounts.
 211    $accounts = array_filter($form_state['values']['accounts']);
 212    if ($function = $operation['callback']) {
 213      // Add in callback arguments if present.
 214      if (isset($operation['callback arguments'])) {
 215        $args = array_merge(array($accounts), $operation['callback arguments']);
 216      }
 217      else {
 218        $args = array($accounts);
 219      }
 220      call_user_func_array($function, $args);
 221  
 222      drupal_set_message(t('The update has been performed.'));
 223    }
 224  }
 225  
 226  function user_admin_account_validate($form, &$form_state) {
 227    $form_state['values']['accounts'] = array_filter($form_state['values']['accounts']);
 228    if (count($form_state['values']['accounts']) == 0) {
 229      form_set_error('', t('No users selected.'));
 230    }
 231  }
 232  
 233  /**
 234   * Form builder; Configure user settings for this site.
 235   *
 236   * @ingroup forms
 237   * @see system_settings_form()
 238   */
 239  function user_admin_settings() {
 240    // User registration settings.
 241    $form['registration'] = array('#type' => 'fieldset', '#title' => t('User registration settings'));
 242    $form['registration']['user_register'] = array('#type' => 'radios', '#title' => t('Public registrations'), '#default_value' => variable_get('user_register', 1), '#options' => array(t('Only site administrators can create new user accounts.'), t('Visitors can create accounts and no administrator approval is required.'), t('Visitors can create accounts but administrator approval is required.')));
 243    $form['registration']['user_email_verification'] = array('#type' => 'checkbox', '#title' => t('Require e-mail verification when a visitor creates an account'), '#default_value' => variable_get('user_email_verification', TRUE), '#description' => t('If this box is checked, new users will be required to validate their e-mail address prior to logging into the site, and will be assigned a system-generated password. With it unchecked, users will be logged in immediately upon registering, and may select their own passwords during registration.'));
 244    $form['registration']['user_registration_help'] = array('#type' => 'textarea', '#title' => t('User registration guidelines'), '#default_value' => variable_get('user_registration_help', ''), '#description' => t('This text is displayed at the top of the user registration form and is useful for helping or instructing your users.'));
 245  
 246    // User e-mail settings.
 247    $form['email'] = array(
 248      '#type' => 'fieldset',
 249      '#title' => t('User e-mail settings'),
 250      '#description' => t('Drupal sends emails whenever new users register on your site, and optionally, may also notify users after other account actions. Using a simple set of content templates, notification e-mails can be customized to fit the specific needs of your site.'),
 251    );
 252    // These email tokens are shared for all settings, so just define
 253    // the list once to help ensure they stay in sync.
 254    $email_token_help = t('Available variables are:') .' !username, !site, !password, !uri, !uri_brief, !mailto, !date, !login_uri, !edit_uri, !login_url.';
 255  
 256    $form['email']['admin_created'] = array(
 257      '#type' => 'fieldset',
 258      '#title' => t('Welcome, new user created by administrator'),
 259      '#collapsible' => TRUE,
 260      '#collapsed' => (variable_get('user_register', 1) != 0),
 261      '#description' => t('Customize welcome e-mail messages sent to new member accounts created by an administrator.') .' '. $email_token_help,
 262    );
 263    $form['email']['admin_created']['user_mail_register_admin_created_subject'] = array(
 264      '#type' => 'textfield',
 265      '#title' => t('Subject'),
 266      '#default_value' => _user_mail_text('register_admin_created_subject'),
 267      '#maxlength' => 180,
 268    );
 269    $form['email']['admin_created']['user_mail_register_admin_created_body'] = array(
 270      '#type' => 'textarea',
 271      '#title' => t('Body'),
 272      '#default_value' => _user_mail_text('register_admin_created_body'),
 273      '#rows' => 15,
 274    );
 275  
 276    $form['email']['no_approval_required'] = array(
 277      '#type' => 'fieldset',
 278      '#title' => t('Welcome, no approval required'),
 279      '#collapsible' => TRUE,
 280      '#collapsed' => (variable_get('user_register', 1) != 1),
 281      '#description' => t('Customize welcome e-mail messages sent to new members upon registering, when no administrator approval is required.') .' '. $email_token_help
 282    );
 283    $form['email']['no_approval_required']['user_mail_register_no_approval_required_subject'] = array(
 284      '#type' => 'textfield',
 285      '#title' => t('Subject'),
 286      '#default_value' => _user_mail_text('register_no_approval_required_subject'),
 287      '#maxlength' => 180,
 288    );
 289    $form['email']['no_approval_required']['user_mail_register_no_approval_required_body'] = array(
 290      '#type' => 'textarea',
 291      '#title' => t('Body'),
 292      '#default_value' => _user_mail_text('register_no_approval_required_body'),
 293      '#rows' => 15,
 294    );
 295  
 296    $form['email']['pending_approval'] = array(
 297      '#type' => 'fieldset',
 298      '#title' => t('Welcome, awaiting administrator approval'),
 299      '#collapsible' => TRUE,
 300      '#collapsed' => (variable_get('user_register', 1) != 2),
 301      '#description' => t('Customize welcome e-mail messages sent to new members upon registering, when administrative approval is required.') .' '. $email_token_help,
 302    );
 303    $form['email']['pending_approval']['user_mail_register_pending_approval_subject'] = array(
 304      '#type' => 'textfield',
 305      '#title' => t('Subject'),
 306      '#default_value' => _user_mail_text('register_pending_approval_subject'),
 307      '#maxlength' => 180,
 308    );
 309    $form['email']['pending_approval']['user_mail_register_pending_approval_body'] = array(
 310      '#type' => 'textarea',
 311      '#title' => t('Body'),
 312      '#default_value' => _user_mail_text('register_pending_approval_body'),
 313      '#rows' => 8,
 314    );
 315  
 316    $form['email']['password_reset'] = array(
 317      '#type' => 'fieldset',
 318      '#title' => t('Password recovery email'),
 319      '#collapsible' => TRUE,
 320      '#collapsed' => TRUE,
 321      '#description' => t('Customize e-mail messages sent to users who request a new password.') .' '. $email_token_help,
 322    );
 323    $form['email']['password_reset']['user_mail_password_reset_subject'] = array(
 324      '#type' => 'textfield',
 325      '#title' => t('Subject'),
 326      '#default_value' => _user_mail_text('password_reset_subject'),
 327      '#maxlength' => 180,
 328    );
 329    $form['email']['password_reset']['user_mail_password_reset_body'] = array(
 330      '#type' => 'textarea',
 331      '#title' => t('Body'),
 332      '#default_value' => _user_mail_text('password_reset_body'),
 333      '#rows' => 12,
 334    );
 335  
 336    $form['email']['activated'] = array(
 337      '#type' => 'fieldset',
 338      '#title' => t('Account activation email'),
 339      '#collapsible' => TRUE,
 340      '#collapsed' => TRUE,
 341      '#description' => t('Enable and customize e-mail messages sent to users upon account activation (when an administrator activates an account of a user who has already registered, on a site where administrative approval is required).') .' '. $email_token_help,
 342    );
 343    $form['email']['activated']['user_mail_status_activated_notify'] = array(
 344      '#type' => 'checkbox',
 345      '#title' => t('Notify user when account is activated.'),
 346      '#default_value' => variable_get('user_mail_status_activated_notify', TRUE),
 347    );
 348    $form['email']['activated']['user_mail_status_activated_subject'] = array(
 349      '#type' => 'textfield',
 350      '#title' => t('Subject'),
 351      '#default_value' => _user_mail_text('status_activated_subject'),
 352      '#maxlength' => 180,
 353    );
 354    $form['email']['activated']['user_mail_status_activated_body'] = array(
 355      '#type' => 'textarea',
 356      '#title' => t('Body'),
 357      '#default_value' => _user_mail_text('status_activated_body'),
 358      '#rows' => 15,
 359    );
 360  
 361    $form['email']['blocked'] = array(
 362      '#type' => 'fieldset',
 363      '#title' => t('Account blocked email'),
 364      '#collapsible' => TRUE,
 365      '#collapsed' => TRUE,
 366      '#description' => t('Enable and customize e-mail messages sent to users when their accounts are blocked.') .' '. $email_token_help,
 367    );
 368    $form['email']['blocked']['user_mail_status_blocked_notify'] = array(
 369      '#type' => 'checkbox',
 370      '#title' => t('Notify user when account is blocked.'),
 371      '#default_value' => variable_get('user_mail_status_blocked_notify', FALSE),
 372    );
 373    $form['email']['blocked']['user_mail_status_blocked_subject'] = array(
 374      '#type' => 'textfield',
 375      '#title' => t('Subject'),
 376      '#default_value' => _user_mail_text('status_blocked_subject'),
 377      '#maxlength' => 180,
 378    );
 379    $form['email']['blocked']['user_mail_status_blocked_body'] = array(
 380      '#type' => 'textarea',
 381      '#title' => t('Body'),
 382      '#default_value' => _user_mail_text('status_blocked_body'),
 383      '#rows' => 3,
 384    );
 385  
 386    $form['email']['deleted'] = array(
 387      '#type' => 'fieldset',
 388      '#title' => t('Account deleted email'),
 389      '#collapsible' => TRUE,
 390      '#collapsed' => TRUE,
 391      '#description' => t('Enable and customize e-mail messages sent to users when their accounts are deleted.') .' '. $email_token_help,
 392    );
 393    $form['email']['deleted']['user_mail_status_deleted_notify'] = array(
 394      '#type' => 'checkbox',
 395      '#title' => t('Notify user when account is deleted.'),
 396      '#default_value' => variable_get('user_mail_status_deleted_notify', FALSE),
 397    );
 398    $form['email']['deleted']['user_mail_status_deleted_subject'] = array(
 399      '#type' => 'textfield',
 400      '#title' => t('Subject'),
 401      '#default_value' => _user_mail_text('status_deleted_subject'),
 402      '#maxlength' => 180,
 403    );
 404    $form['email']['deleted']['user_mail_status_deleted_body'] = array(
 405      '#type' => 'textarea',
 406      '#title' => t('Body'),
 407      '#default_value' => _user_mail_text('status_deleted_body'),
 408      '#rows' => 3,
 409    );
 410  
 411    // User signatures.
 412    $form['signatures'] = array(
 413      '#type' => 'fieldset',
 414      '#title' => t('Signatures'),
 415    );
 416    $form['signatures']['user_signatures'] = array(
 417      '#type' => 'radios',
 418      '#title' => t('Signature support'),
 419      '#default_value' => variable_get('user_signatures', 0),
 420      '#options' => array(t('Disabled'), t('Enabled')),
 421    );
 422  
 423    // If picture support is enabled, check whether the picture directory exists:
 424    if (variable_get('user_pictures', 0)) {
 425      $picture_path = file_create_path(variable_get('user_picture_path', 'pictures'));
 426      file_check_directory($picture_path, 1, 'user_picture_path');
 427    }
 428  
 429    $form['pictures'] = array(
 430      '#type' => 'fieldset',
 431      '#title' => t('Pictures'),
 432    );
 433    $picture_support = variable_get('user_pictures', 0);
 434    $form['pictures']['user_pictures'] = array(
 435      '#type' => 'radios',
 436      '#title' => t('Picture support'),
 437      '#default_value' => $picture_support,
 438      '#options' => array(t('Disabled'), t('Enabled')),
 439      '#prefix' => '<div class="user-admin-picture-radios">',
 440      '#suffix' => '</div>',
 441    );
 442    drupal_add_js(drupal_get_path('module', 'user') .'/user.js');
 443    // If JS is enabled, and the radio is defaulting to off, hide all
 444    // the settings on page load via .css using the js-hide class so
 445    // that there's no flicker.
 446    $css_class = 'user-admin-picture-settings';
 447    if (!$picture_support) {
 448      $css_class .= ' js-hide';
 449    }
 450    $form['pictures']['settings'] = array(
 451      '#prefix' => '<div class="'. $css_class .'">',
 452      '#suffix' => '</div>',
 453    );
 454    $form['pictures']['settings']['user_picture_path'] = array(
 455      '#type' => 'textfield',
 456      '#title' => t('Picture image path'),
 457      '#default_value' => variable_get('user_picture_path', 'pictures'),
 458      '#size' => 30,
 459      '#maxlength' => 255,
 460      '#description' => t('Subdirectory in the directory %dir where pictures will be stored.', array('%dir' => file_directory_path() .'/')),
 461    );
 462    $form['pictures']['settings']['user_picture_default'] = array(
 463      '#type' => 'textfield',
 464      '#title' => t('Default picture'),
 465      '#default_value' => variable_get('user_picture_default', ''),
 466      '#size' => 30,
 467      '#maxlength' => 255,
 468      '#description' => t('URL of picture to display for users with no custom picture selected. Leave blank for none.'),
 469    );
 470    $form['pictures']['settings']['user_picture_dimensions'] = array(
 471      '#type' => 'textfield',
 472      '#title' => t('Picture maximum dimensions'),
 473      '#default_value' => variable_get('user_picture_dimensions', '85x85'),
 474      '#size' => 15,
 475      '#maxlength' => 10,
 476      '#description' => t('Maximum dimensions for pictures, in pixels.'),
 477    );
 478    $form['pictures']['settings']['user_picture_file_size'] = array(
 479      '#type' => 'textfield',
 480      '#title' => t('Picture maximum file size'),
 481      '#default_value' => variable_get('user_picture_file_size', '30'),
 482      '#size' => 15,
 483      '#maxlength' => 10,
 484      '#description' => t('Maximum file size for pictures, in kB.'),
 485    );
 486    $form['pictures']['settings']['user_picture_guidelines'] = array(
 487      '#type' => 'textarea',
 488      '#title' => t('Picture guidelines'),
 489      '#default_value' => variable_get('user_picture_guidelines', ''),
 490      '#description' => t("This text is displayed at the picture upload form in addition to the default guidelines. It's useful for helping or instructing your users."),
 491    );
 492  
 493    return system_settings_form($form);
 494  }
 495  
 496  /**
 497   * Menu callback: administer permissions.
 498   *
 499   * @ingroup forms
 500   * @see user_admin_perm_submit()
 501   * @see theme_user_admin_perm()
 502   */
 503  function user_admin_perm($form_state, $rid = NULL) {
 504    if (is_numeric($rid)) {
 505      $result = db_query('SELECT r.rid, p.perm FROM {role} r LEFT JOIN {permission} p ON r.rid = p.rid WHERE r.rid = %d', $rid);
 506    }
 507    else {
 508      $result = db_query('SELECT r.rid, p.perm FROM {role} r LEFT JOIN {permission} p ON r.rid = p.rid ORDER BY name');
 509    }
 510  
 511    // Compile role array:
 512    // Add a comma at the end so when searching for a permission, we can
 513    // always search for "$perm," to make sure we do not confuse
 514    // permissions that are substrings of each other.
 515    while ($role = db_fetch_object($result)) {
 516      $role_permissions[$role->rid] = $role->perm .',';
 517    }
 518  
 519    // Retrieve role names for columns.
 520    $role_names = user_roles();
 521    if (is_numeric($rid)) {
 522      $role_names = array($rid => $role_names[$rid]);
 523    }
 524  
 525    // Render role/permission overview:
 526    $options = array();
 527    foreach (module_list(FALSE, FALSE, TRUE) as $module) {
 528      if ($permissions = module_invoke($module, 'perm')) {
 529        $form['permission'][] = array(
 530          '#value' => $module,
 531        );
 532        asort($permissions);
 533        foreach ($permissions as $perm) {
 534          $options[$perm] = '';
 535          $form['permission'][$perm] = array('#value' => t($perm));
 536          foreach ($role_names as $rid => $name) {
 537            // Builds arrays for checked boxes for each role
 538            if (strpos($role_permissions[$rid], $perm .',') !== FALSE) {
 539              $status[$rid][] = $perm;
 540            }
 541          }
 542        }
 543      }
 544    }
 545  
 546    // Have to build checkboxes here after checkbox arrays are built
 547    foreach ($role_names as $rid => $name) {
 548      $form['checkboxes'][$rid] = array('#type' => 'checkboxes', '#options' => $options, '#default_value' => isset($status[$rid]) ? $status[$rid] : array());
 549      $form['role_names'][$rid] = array('#value' => $name, '#tree' => TRUE);
 550    }
 551    $form['submit'] = array('#type' => 'submit', '#value' => t('Save permissions'));
 552  
 553    return $form;
 554  }
 555  
 556  function user_admin_perm_submit($form, &$form_state) {
 557    // Save permissions:
 558    $result = db_query('SELECT * FROM {role}');
 559    while ($role = db_fetch_object($result)) {
 560      if (isset($form_state['values'][$role->rid])) {
 561        // Delete, so if we clear every checkbox we reset that role;
 562        // otherwise permissions are active and denied everywhere.
 563        db_query('DELETE FROM {permission} WHERE rid = %d', $role->rid);
 564        $form_state['values'][$role->rid] = array_filter($form_state['values'][$role->rid]);
 565        if (count($form_state['values'][$role->rid])) {
 566          db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $role->rid, implode(', ', array_keys($form_state['values'][$role->rid])));
 567        }
 568      }
 569    }
 570  
 571    drupal_set_message(t('The changes have been saved.'));
 572  
 573    // Clear the cached pages
 574    cache_clear_all();
 575  }
 576  
 577  /**
 578   * Theme the administer permissions page.
 579   *
 580   * @ingroup themeable
 581   */
 582  function theme_user_admin_perm($form) {
 583    $roles = user_roles();
 584    foreach (element_children($form['permission']) as $key) {
 585      // Don't take form control structures
 586      if (is_array($form['permission'][$key])) {
 587        $row = array();
 588        // Module name
 589        if (is_numeric($key)) {
 590          $row[] = array('data' => t('@module module', array('@module' => drupal_render($form['permission'][$key]))), 'class' => 'module', 'id' => 'module-'. $form['permission'][$key]['#value'], 'colspan' => count($form['role_names']) + 1);
 591        }
 592        else {
 593          $row[] = array('data' => drupal_render($form['permission'][$key]), 'class' => 'permission');
 594          foreach (element_children($form['checkboxes']) as $rid) {
 595            if (is_array($form['checkboxes'][$rid])) {
 596              $row[] = array('data' => drupal_render($form['checkboxes'][$rid][$key]), 'class' => 'checkbox', 'title' => $roles[$rid] .' : '. t($key));
 597            }
 598          }
 599        }
 600        $rows[] = $row;
 601      }
 602    }
 603    $header[] = (t('Permission'));
 604    foreach (element_children($form['role_names']) as $rid) {
 605      if (is_array($form['role_names'][$rid])) {
 606        $header[] = array('data' => drupal_render($form['role_names'][$rid]), 'class' => 'checkbox');
 607      }
 608    }
 609    $output = theme('table', $header, $rows, array('id' => 'permissions'));
 610    $output .= drupal_render($form);
 611    return $output;
 612  }
 613  
 614  /**
 615   * Menu callback: administer roles.
 616   *
 617   * @ingroup forms
 618   * @see user_admin_role_validate()
 619   * @see user_admin_role_submit()
 620   * @see theme_user_admin_new_role()
 621   */
 622  function user_admin_role() {
 623    $rid = arg(4);
 624    if ($rid) {
 625      if ($rid == DRUPAL_ANONYMOUS_RID || $rid == DRUPAL_AUTHENTICATED_RID) {
 626        drupal_goto('admin/user/roles');
 627      }
 628      // Display the edit role form.
 629      $role = db_fetch_object(db_query('SELECT * FROM {role} WHERE rid = %d', $rid));
 630      $form['name'] = array(
 631        '#type' => 'textfield',
 632        '#title' => t('Role name'),
 633        '#default_value' => $role->name,
 634        '#size' => 30,
 635        '#required' => TRUE,
 636        '#maxlength' => 64,
 637        '#description' => t('The name for this role. Example: "moderator", "editorial board", "site architect".'),
 638      );
 639      $form['rid'] = array(
 640        '#type' => 'value',
 641        '#value' => $rid,
 642      );
 643      $form['submit'] = array(
 644        '#type' => 'submit',
 645        '#value' => t('Save role'),
 646      );
 647      $form['delete'] = array(
 648        '#type' => 'submit',
 649        '#value' => t('Delete role'),
 650      );
 651    }
 652    else {
 653      $form['name'] = array(
 654        '#type' => 'textfield',
 655        '#size' => 32,
 656        '#maxlength' => 64,
 657      );
 658      $form['submit'] = array(
 659        '#type' => 'submit',
 660        '#value' => t('Add role'),
 661      );
 662      $form['#submit'][] = 'user_admin_role_submit';
 663      $form['#validate'][] = 'user_admin_role_validate';
 664    }
 665    return $form;
 666  }
 667  
 668  function user_admin_role_validate($form, &$form_state) {
 669    if ($form_state['values']['name']) {
 670      if ($form_state['values']['op'] == t('Save role')) {
 671        if (db_result(db_query("SELECT COUNT(*) FROM {role} WHERE name = '%s' AND rid != %d", $form_state['values']['name'], $form_state['values']['rid']))) {
 672          form_set_error('name', t('The role name %name already exists. Please choose another role name.', array('%name' => $form_state['values']['name'])));
 673        }
 674      }
 675      else if ($form_state['values']['op'] == t('Add role')) {
 676        if (db_result(db_query("SELECT COUNT(*) FROM {role} WHERE name = '%s'", $form_state['values']['name']))) {
 677          form_set_error('name', t('The role name %name already exists. Please choose another role name.', array('%name' => $form_state['values']['name'])));
 678        }
 679      }
 680    }
 681    else {
 682      form_set_error('name', t('You must specify a valid role name.'));
 683    }
 684  }
 685  
 686  function user_admin_role_submit($form, &$form_state) {
 687    if ($form_state['values']['op'] == t('Save role')) {
 688      db_query("UPDATE {role} SET name = '%s' WHERE rid = %d", $form_state['values']['name'], $form_state['values']['rid']);
 689      drupal_set_message(t('The role has been renamed.'));
 690    }
 691    else if ($form_state['values']['op'] == t('Delete role')) {
 692      db_query('DELETE FROM {role} WHERE rid = %d', $form_state['values']['rid']);
 693      db_query('DELETE FROM {permission} WHERE rid = %d', $form_state['values']['rid']);
 694      // Update the users who have this role set:
 695      db_query('DELETE FROM {users_roles} WHERE rid = %d', $form_state['values']['rid']);
 696  
 697      drupal_set_message(t('The role has been deleted.'));
 698    }
 699    else if ($form_state['values']['op'] == t('Add role')) {
 700      db_query("INSERT INTO {role} (name) VALUES ('%s')", $form_state['values']['name']);
 701      drupal_set_message(t('The role has been added.'));
 702    }
 703    $form_state['redirect'] = 'admin/user/roles';
 704    return;
 705  }
 706  
 707  /**
 708   * Menu callback: list all access rules
 709   */
 710  function user_admin_access_check() {
 711    $output = drupal_get_form('user_admin_check_user');
 712    $output .= drupal_get_form('user_admin_check_mail');
 713    $output .= drupal_get_form('user_admin_check_host');
 714    return $output;
 715  }
 716  
 717  /**
 718   * Menu callback: add an access rule.
 719   */
 720  function user_admin_access_add($mask = NULL, $type = NULL) {
 721    $edit = array();
 722    $edit['aid'] = 0;
 723    $edit['mask'] = $mask;
 724    $edit['type'] = $type;
 725    return drupal_get_form('user_admin_access_add_form', $edit, t('Add rule'));
 726  }
 727  
 728  /**
 729   * Menu callback: edit an access rule.
 730   */
 731  function user_admin_access_edit($aid = 0) {
 732    $edit = db_fetch_array(db_query('SELECT aid, type, status, mask FROM {access} WHERE aid = %d', $aid));
 733    return drupal_get_form('user_admin_access_edit_form', $edit, t('Save rule'));
 734  }
 735  
 736  /**
 737   * Form builder; Configure access rules.
 738   *
 739   * @ingroup forms
 740   */
 741  function user_admin_access_form(&$form_state, $edit, $submit) {
 742    $form = array();
 743    $form['aid'] = array(
 744      '#type' => 'value',
 745      '#value' => $edit['aid'],
 746    );
 747    $form['status'] = array(
 748      '#type' => 'radios',
 749      '#title' => t('Access type'),
 750      '#default_value' => isset($edit['status']) ? $edit['status'] : 0,
 751      '#options' => array('1' => t('Allow'), '0' => t('Deny')),
 752    );
 753    $type_options = array('user' => t('Username'), 'mail' => t('E-mail'), 'host' => t('Host'));
 754    $form['type'] = array(
 755      '#type' => 'radios',
 756      '#title' => t('Rule type'),
 757      '#default_value' => (isset($type_options[$edit['type']]) ? $edit['type'] : 'user'),
 758      '#options' => $type_options,
 759    );
 760    $form['mask'] = array(
 761      '#type' => 'textfield',
 762      '#title' => t('Mask'),
 763      '#size' => 30,
 764      '#maxlength' => 64,
 765      '#default_value' => $edit['mask'],
 766      '#description' => '%: '. t('Matches any number of characters, even zero characters') .'.<br />_: '. t('Matches exactly one character.'),
 767      '#required' => TRUE,
 768    );
 769    $form['submit'] = array('#type' => 'submit', '#value' => $submit);
 770    $form['#submit'] = array('user_admin_access_form_submit');
 771  
 772    return $form;
 773  }
 774  
 775  /**
 776   * Submit callback for user_admin_access_form().
 777   */
 778  function user_admin_access_form_submit($form, &$form_state) {
 779    $edit = $form_state['values'];
 780    if ($edit['aid']) {
 781      db_query("UPDATE {access} SET mask = '%s', type = '%s', status = '%s' WHERE aid = %d", $edit['mask'], $edit['type'], $edit['status'], $edit['aid']);
 782      drupal_set_message(t('The access rule has been saved.'));
 783    }
 784    else {
 785      db_query("INSERT INTO {access} (mask, type, status) VALUES ('%s', '%s', %d)", $edit['mask'], $edit['type'], $edit['status']);
 786      drupal_set_message(t('The access rule has been added.'));
 787    }
 788    $form_state['redirect'] = 'admin/user/rules';
 789  }
 790  
 791  function user_admin_access_check_validate($form, &$form_state) {
 792    if (empty($form_state['values']['test'])) {
 793      form_set_error($form_state['values']['type'], t('No value entered. Please enter a test string and try again.'));
 794    }
 795  }
 796  
 797  function user_admin_check_user() {
 798    $form['user'] = array('#type' => 'fieldset', '#title' => t('Username'));
 799    $form['user']['test'] = array('#type' => 'textfield', '#title' => '', '#description' => t('Enter a username to check if it will be denied or allowed.'), '#size' => 30, '#maxlength' => USERNAME_MAX_LENGTH);
 800    $form['user']['type'] = array('#type' => 'hidden', '#value' => 'user');
 801    $form['user']['submit'] = array('#type' => 'submit', '#value' => t('Check username'));
 802    $form['#submit'][] = 'user_admin_access_check_submit';
 803    $form['#validate'][] = 'user_admin_access_check_validate';
 804    $form['#theme'] = 'user_admin_access_check';
 805    return $form;
 806  }
 807  
 808  function user_admin_check_mail() {
 809    $form['mail'] = array('#type' => 'fieldset', '#title' => t('E-mail'));
 810    $form['mail']['test'] = array('#type' => 'textfield', '#title' => '', '#description' => t('Enter an e-mail address to check if it will be denied or allowed.'), '#size' => 30, '#maxlength' => EMAIL_MAX_LENGTH);
 811    $form['mail']['type'] = array('#type' => 'hidden', '#value' => 'mail');
 812    $form['mail']['submit'] = array('#type' => 'submit', '#value' => t('Check e-mail'));
 813    $form['#submit'][] = 'user_admin_access_check_submit';
 814    $form['#validate'][] = 'user_admin_access_check_validate';
 815    $form['#theme'] = 'user_admin_access_check';
 816    return $form;
 817  }
 818  
 819  function user_admin_check_host() {
 820    $form['host'] = array('#type' => 'fieldset', '#title' => t('Hostname'));
 821    $form['host']['test'] = array('#type' => 'textfield', '#title' => '', '#description' => t('Enter a hostname or IP address to check if it will be denied or allowed.'), '#size' => 30, '#maxlength' => 64);
 822    $form['host']['type'] = array('#type' => 'hidden', '#value' => 'host');
 823    $form['host']['submit'] = array('#type' => 'submit', '#value' => t('Check hostname'));
 824    $form['#submit'][] = 'user_admin_access_check_submit';
 825    $form['#validate'][] = 'user_admin_access_check_validate';
 826    $form['#theme'] = 'user_admin_access_check';
 827    return $form;
 828  }
 829  
 830  function user_admin_access_check_submit($form, &$form_state) {
 831    switch ($form_state['values']['type']) {
 832      case 'user':
 833        if (drupal_is_denied('user', $form_state['values']['test'])) {
 834          drupal_set_message(t('The username %name is not allowed.', array('%name' => $form_state['values']['test'])));
 835        }
 836        else {
 837          drupal_set_message(t('The username %name is allowed.', array('%name' => $form_state['values']['test'])));
 838        }
 839        break;
 840      case 'mail':
 841        if (drupal_is_denied('mail', $form_state['values']['test'])) {
 842          drupal_set_message(t('The e-mail address %mail is not allowed.', array('%mail' => $form_state['values']['test'])));
 843        }
 844        else {
 845          drupal_set_message(t('The e-mail address %mail is allowed.', array('%mail' => $form_state['values']['test'])));
 846        }
 847        break;
 848      case 'host':
 849        if (drupal_is_denied('host', $form_state['values']['test'])) {
 850          drupal_set_message(t('The hostname %host is not allowed.', array('%host' => $form_state['values']['test'])));
 851        }
 852        else {
 853          drupal_set_message(t('The hostname %host is allowed.', array('%host' => $form_state['values']['test'])));
 854        }
 855        break;
 856      default:
 857        break;
 858    }
 859  }
 860  
 861  /**
 862   * Menu callback: delete an access rule
 863   *
 864   * @ingroup forms
 865   * @see user_admin_access_delete_confirm_submit()
 866   */
 867  function user_admin_access_delete_confirm($form_state, $aid = 0) {
 868    $access_types = array('user' => t('username'), 'mail' => t('e-mail'), 'host' => t('host'));
 869    $edit = db_fetch_object(db_query('SELECT aid, type, status, mask FROM {access} WHERE aid = %d', $aid));
 870  
 871    $form = array();
 872    $form['aid'] = array('#type' => 'hidden', '#value' => $aid);
 873    $output = confirm_form($form,
 874                    t('Are you sure you want to delete the @type rule for %rule?', array('@type' => $access_types[$edit->type], '%rule' => $edit->mask)),
 875                    'admin/user/rules',
 876                    t('This action cannot be undone.'),
 877                    t('Delete'),
 878                    t('Cancel'));
 879    return $output;
 880  }
 881  
 882  function user_admin_access_delete_confirm_submit($form, &$form_state) {
 883    db_query('DELETE FROM {access} WHERE aid = %d', $form_state['values']['aid']);
 884    drupal_set_message(t('The access rule has been deleted.'));
 885    $form_state['redirect'] = 'admin/user/rules';
 886    return;
 887  }
 888  
 889  /**
 890   * Menu callback: list all access rules
 891   */
 892  function user_admin_access() {
 893    $header = array(array('data' => t('Access type'), 'field' => 'status'), array('data' => t('Rule type'), 'field' => 'type'), array('data' => t('Mask'), 'field' => 'mask'), array('data' => t('Operations'), 'colspan' => 2));
 894    $result = db_query("SELECT aid, type, status, mask FROM {access}". tablesort_sql($header));
 895    $access_types = array('user' => t('username'), 'mail' => t('e-mail'), 'host' => t('host'));
 896    $rows = array();
 897    while ($rule = db_fetch_object($result)) {
 898      $rows[] = array($rule->status ? t('allow') : t('deny'), $access_types[$rule->type], $rule->mask, l(t('edit'), 'admin/user/rules/edit/'. $rule->aid), l(t('delete'), 'admin/user/rules/delete/'. $rule->aid));
 899    }
 900    if (empty($rows)) {
 901      $rows[] = array(array('data' => '<em>'. t('There are currently no access rules.') .'</em>', 'colspan' => 5));
 902    }
 903    return theme('table', $header, $rows);
 904  }
 905  
 906  /**
 907   * Theme user administration overview.
 908   *
 909   * @ingroup themeable
 910   */
 911  function theme_user_admin_account($form) {
 912    // Overview table:
 913    $header = array(
 914      theme('table_select_header_cell'),
 915      array('data' => t('Username'), 'field' => 'u.name'),
 916      array('data' => t('Status'), 'field' => 'u.status'),
 917      t('Roles'),
 918      array('data' => t('Member for'), 'field' => 'u.created', 'sort' => 'desc'),
 919      array('data' => t('Last access'), 'field' => 'u.access'),
 920      t('Operations')
 921    );
 922  
 923    $output = drupal_render($form['options']);
 924    if (isset($form['name']) && is_array($form['name'])) {
 925      foreach (element_children($form['name']) as $key) {
 926        $rows[] = array(
 927          drupal_render($form['accounts'][$key]),
 928          drupal_render($form['name'][$key]),
 929          drupal_render($form['status'][$key]),
 930          drupal_render($form['roles'][$key]),
 931          drupal_render($form['member_for'][$key]),
 932          drupal_render($form['last_access'][$key]),
 933          drupal_render($form['operations'][$key]),
 934        );
 935      }
 936    }
 937    else {
 938      $rows[] = array(array('data' => t('No users available.'), 'colspan' => '7'));
 939    }
 940  
 941    $output .= theme('table', $header, $rows);
 942    if ($form['pager']['#value']) {
 943      $output .= drupal_render($form['pager']);
 944    }
 945  
 946    $output .= drupal_render($form);
 947  
 948    return $output;
 949  }
 950  
 951  /**
 952   * Theme the new-role form.
 953   *
 954   * @ingroup themeable
 955   */
 956  function theme_user_admin_new_role($form) {
 957    $header = array(t('Name'), array('data' => t('Operations'), 'colspan' => 2));
 958    foreach (user_roles() as $rid => $name) {
 959      $edit_permissions = l(t('edit permissions'), 'admin/user/permissions/'. $rid);
 960      if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
 961        $rows[] = array($name, l(t('edit role'), 'admin/user/roles/edit/'. $rid), $edit_permissions);
 962      }
 963      else {
 964        $rows[] = array($name, t('locked'), $edit_permissions);
 965      }
 966    }
 967    $rows[] = array(drupal_render($form['name']), array('data' => drupal_render($form['submit']), 'colspan' => 2));
 968  
 969    $output = drupal_render($form);
 970    $output .= theme('table', $header, $rows);
 971  
 972    return $output;
 973  }
 974  
 975  /**
 976   * Theme user administration filter form.
 977   *
 978   * @ingroup themeable
 979   */
 980  function theme_user_filter_form($form) {
 981    $output = '<div id="user-admin-filter">';
 982    $output .= drupal_render($form['filters']);
 983    $output .= '</div>';
 984    $output .= drupal_render($form);
 985    return $output;
 986  }
 987  
 988  /**
 989   * Theme user administration filter selector.
 990   *
 991   * @ingroup themeable
 992   */
 993  function theme_user_filters($form) {
 994    $output = '<ul class="clear-block">';
 995    if (!empty($form['current'])) {
 996      foreach (element_children($form['current']) as $key) {
 997        $output .= '<li>'. drupal_render($form['current'][$key]) .'</li>';
 998      }
 999    }
1000  
1001    $output .= '<li><dl class="multiselect">'. (!empty($form['current']) ? '<dt><em>'. t('and') .'</em> '. t('where') .'</dt>' : '') .'<dd class="a">';
1002    foreach (element_children($form['filter']) as $key) {
1003      $output .= drupal_render($form['filter'][$key]);
1004    }
1005    $output .= '</dd>';
1006  
1007    $output .= '<dt>'. t('is') .'</dt><dd class="b">';
1008  
1009    foreach (element_children($form['status']) as $key) {
1010      $output .= drupal_render($form['status'][$key]);
1011    }
1012    $output .= '</dd>';
1013  
1014    $output .= '</dl>';
1015    $output .= '<div class="container-inline" id="user-admin-buttons">'. drupal_render($form['buttons']) .'</div>';
1016    $output .= '</li></ul>';
1017  
1018    return $output;
1019  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7