You are here

function userprotect_protections_bypass in User protect 7

Same name and namespace in other branches
  1. 5 userprotect.module \userprotect_protections_bypass()
  2. 6 userprotect.module \userprotect_protections_bypass()

Helper function. Builds tables for protected users and admin bypass.

Return value

array A form array representing the table.

2 calls to userprotect_protections_bypass()
userprotect_administrator_bypass in ./userprotect.admin.inc
Builds a table of user admin bypass values.
userprotect_protected_users in ./userprotect.admin.inc
Builds a table of protected users, and their protections.

File

./userprotect.admin.inc, line 34
Administration functions for userprotect module.

Code

function userprotect_protections_bypass($type) {

  // Build the header.
  $header = array(
    array(
      'data' => t('User'),
      'field' => 'name',
      'sort' => 'asc',
    ),
  );
  $protect_columns = userprotect_get_protection_display();
  foreach ($protect_columns as $field => $data) {
    $header[] = array(
      'data' => $data,
      'field' => $field,
    );
  }
  $header[] = array(
    'data' => t('Operations'),
  );
  $query = db_select('userprotect', 'up');
  $query
    ->innerJoin('users', 'u', 'up.uid = u.uid');
  $query
    ->condition('up.up_type', $type);
  $count_query = clone $query;
  $count_query
    ->addExpression('COUNT(DISTINCT u.uid)');
  $query = $query
    ->extend('PagerDefault')
    ->extend('TableSort');

  // These are all protection fields in the database.
  $protection_fields = array_keys(userprotect_get_protection_display());

  // Grab the protected users.
  $query
    ->fields('up', $protection_fields)
    ->fields('u', array(
    'uid',
    'name',
  ))
    ->limit(25)
    ->orderByHeader($header)
    ->setCountQuery($count_query);
  $protected_users = $query
    ->execute();

  // Set some initial values.
  $delete = t('delete');
  $options = array();

  // These are all available protections.
  $protections = array_keys(userprotect_user_protection_defaults());

  // Pass in the header and list of protections to the form so they'll be
  // available to the theming function.
  $form = array();
  $form['protection']['#tree'] = TRUE;
  $form['#header'] = $header;
  $form['#protections'] = $protections;
  $form['#submit'][] = 'userprotect_protections_bypass_submit';
  $form['#theme'] = 'userprotect_protections_bypass';

  // Build the checkboxes options.
  foreach ($protections as $protection) {
    $options[$protection] = '';
  }

  // For each protected user, build their table row.
  foreach ($protected_users as $protected_user) {
    $defaults = array();
    $user = user_load($protected_user->uid);
    $form['user'][$user->uid]['uid'] = array(
      '#type' => 'value',
      '#value' => $user->uid,
    );
    $form[$user->uid]['name'] = array(
      '#theme' => 'username',
      '#account' => $user,
    );
    $form[$user->uid]['operations'] = array(
      '#markup' => $user->uid ? l($delete, "userprotect/delete/{$user->uid}/{$type}") : '',
    );

    // Build the protections for the user row.
    foreach ($protections as $protection) {
      if ($protected_user->{$protection}) {
        $defaults[] = $protection;
      }
    }

    // The checkboxes for this user.
    $form['protection'][$user->uid] = array(
      '#type' => 'checkboxes',
      '#options' => $options,
      '#default_value' => $defaults,
    );
  }

  // An autocomplete field to add new users for protection.
  // This needs a custom validation function to check the user
  // to be added.
  $form['up_add'] = array(
    '#type' => 'textfield',
    '#maxlength' => 60,
    '#autocomplete_path' => 'user/autocomplete',
    '#element_validate' => array(
      'userprotect_up_add_validate',
    ),
    '#userprotect_type' => $type,
  );
  $form['up_add_text'] = array(
    '#markup' => t('Add user'),
  );
  $form['userprotect_type'] = array(
    '#type' => 'value',
    '#value' => $type,
  );
  $form['pager'] = array(
    '#theme' => 'pager',
    '#tags' => NULL,
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  return $form;
}