You are here

function nodeaccess_grants_form in Nodeaccess 7

Same name and namespace in other branches
  1. 5 nodeaccess.module \nodeaccess_grants_form()
  2. 6.2 nodeaccess.module \nodeaccess_grants_form()
  3. 6 nodeaccess.module \nodeaccess_grants_form()

Menu callback. Draws the grant tab.

_state

Parameters

$form:

$node:

Return value

mixed

1 string reference to 'nodeaccess_grants_form'
nodeaccess_grant_tab in ./nodeaccess.module
Menu callback. Draws the grant tab.

File

./nodeaccess.module, line 222
Provide per node access control

Code

function nodeaccess_grants_form($form, &$form_state, $node) {
  if (isset($form_state['values'])) {
    $form_values = $form_state['values'];
  }
  if (!isset($form_values)) {
    $form_values = array();
    $grants = nodeaccess_get_grants($node);
    $form_values['rid'] = isset($grants['rid']) ? $grants['rid'] : array();
    $form_values['uid'] = isset($grants['uid']) ? $grants['uid'] : array();
  }
  elseif ($form_values['keys']) {
    $name = preg_replace('!\\*+!', '%', $form_values['keys']);
    $query = db_select('users', 'u')
      ->fields('u', array(
      'uid',
      'name',
    ))
      ->condition('name', $name, "LIKE");
    if (isset($form_values['uid']) && count($form_values['uid'])) {
      $uid = array_keys($form_values['uid']);
      $query
        ->condition('uid', $uid, 'NOT IN');
    }
    $result = $query
      ->execute();
    while ($account = $result
      ->fetch()) {
      $form_values['uid'][$account->uid] = array(
        'name' => $account->name,
        'keep' => 1,
        'grant_view' => isset($form_values['rid'][DRUPAL_AUTHENTICATED_RID]['grant_view']) ? $form_values['rid'][DRUPAL_AUTHENTICATED_RID]['grant_view'] : 0,
        'grant_update' => isset($form_values['rid'][DRUPAL_AUTHENTICATED_RID]['grant_update']) ? $form_values['rid'][DRUPAL_AUTHENTICATED_RID]['grant_update'] : 0,
        'grant_delete' => isset($form_values['rid'][DRUPAL_AUTHENTICATED_RID]['grant_delete']) ? $form_values['rid'][DRUPAL_AUTHENTICATED_RID]['grant_delete'] : 0,
      );
    }
  }
  if (!isset($form_values['rid'])) {
    $form_values['rid'] = array();
  }
  if (!isset($form_values['uid'])) {
    $form_values['uid'] = array();
  }
  $roles = $form_values['rid'];
  $users = $form_values['uid'];
  $form['nid'] = array(
    '#type' => 'hidden',
    '#value' => $node->nid,
  );
  $allowed_roles = variable_get('nodeaccess-roles', array());
  $allowed_grants = variable_get('nodeaccess-grants', array());

  // If $preserve is TRUE, the fields the user is not allowed to view or
  // edit are included in the form as hidden fields to preserve them.
  $preserve = variable_get('nodeaccess-preserve', 1);

  // Roles table.
  if (is_array($roles)) {
    $form['rid'] = array(
      '#tree' => TRUE,
    );
    foreach ($roles as $key => $field) {
      if (isset($allowed_roles[$key]) && $allowed_roles[$key]) {
        $form['rid'][$key]['name'] = array(
          '#type' => 'hidden',
          '#value' => $field['name'],
        );
        if ($allowed_grants['view']) {
          $form['rid'][$key]['grant_view'] = array(
            '#type' => 'checkbox',
            '#default_value' => $field['grant_view'],
          );
        }
        elseif ($preserve) {
          $form['rid'][$key]['grant_view'] = array(
            '#type' => 'hidden',
            '#value' => $field['grant_view'],
          );
        }
        if ($allowed_grants['edit']) {
          $form['rid'][$key]['grant_update'] = array(
            '#type' => 'checkbox',
            '#default_value' => $field['grant_update'],
          );
        }
        elseif ($preserve) {
          $form['rid'][$key]['grant_update'] = array(
            '#type' => 'hidden',
            '#value' => $field['grant_update'],
          );
        }
        if ($allowed_grants['delete']) {
          $form['rid'][$key]['grant_delete'] = array(
            '#type' => 'checkbox',
            '#default_value' => $field['grant_delete'],
          );
        }
        elseif ($preserve) {
          $form['rid'][$key]['grant_delete'] = array(
            '#type' => 'hidden',
            '#value' => $field['grant_delete'],
          );
        }
      }
      elseif ($preserve) {
        $form['rid'][$key]['name'] = array(
          '#type' => 'hidden',
          '#value' => $field['name'],
        );
        $form['rid'][$key]['grant_view'] = array(
          '#type' => 'hidden',
          '#value' => $field['grant_view'],
        );
        $form['rid'][$key]['grant_update'] = array(
          '#type' => 'hidden',
          '#value' => $field['grant_update'],
        );
        $form['rid'][$key]['grant_delete'] = array(
          '#type' => 'hidden',
          '#value' => $field['grant_delete'],
        );
      }
    }
  }

  // Users table.
  if (is_array($users)) {
    $form['uid'] = array(
      '#tree' => TRUE,
    );
    foreach ($users as $key => $field) {
      $form['uid'][$key]['name'] = array(
        '#type' => 'hidden',
        '#value' => $field['name'],
      );
      $form['uid'][$key]['keep'] = array(
        '#type' => 'checkbox',
        '#default_value' => $field['keep'],
      );
      if ($allowed_grants['view']) {
        $form['uid'][$key]['grant_view'] = array(
          '#type' => 'checkbox',
          '#default_value' => $field['grant_view'],
        );
      }
      elseif ($preserve) {
        $form['uid'][$key]['grant_view'] = array(
          '#type' => 'hidden',
          '#value' => $field['grant_view'],
        );
      }
      if ($allowed_grants['edit']) {
        $form['uid'][$key]['grant_update'] = array(
          '#type' => 'checkbox',
          '#default_value' => $field['grant_update'],
        );
      }
      elseif ($preserve) {
        $form['uid'][$key]['grant_update'] = array(
          '#type' => 'hidden',
          '#value' => $field['grant_update'],
        );
      }
      if ($allowed_grants['delete']) {
        $form['uid'][$key]['grant_delete'] = array(
          '#type' => 'checkbox',
          '#default_value' => $field['grant_delete'],
        );
      }
      elseif ($preserve) {
        $form['uid'][$key]['grant_delete'] = array(
          '#type' => 'hidden',
          '#value' => $field['grant_delete'],
        );
      }
    }
  }

  // Autocomplete returns errors if users don't have access to profiles.
  if (user_access('access user profiles')) {
    $form['keys'] = array(
      '#type' => 'textfield',
      '#default_value' => isset($form_values['keys']) ? $form_values['keys'] : '',
      '#size' => 40,
      '#autocomplete_path' => 'user/autocomplete',
    );
  }
  else {
    $form['keys'] = array(
      '#type' => 'textfield',
      '#default_value' => isset($form_values['keys']) ? $form_values['keys'] : '',
      '#size' => 40,
    );
  }
  $form['search'] = array(
    '#type' => 'submit',
    '#value' => t('Search'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Grants'),
  );
  return $form;
}