You are here

function nodeaccess_admin_form in Nodeaccess 7

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

Menu callback. Draws the admin page.

_state

Parameters

$form:

Return value

array $form

1 string reference to 'nodeaccess_admin_form'
nodeaccess_admin in ./nodeaccess.admin.inc
Menu callback. Draws the admin page.

File

./nodeaccess.admin.inc, line 22
Nodeaccess admin forms.

Code

function nodeaccess_admin_form($form, $form_state) {

  // Set defaults from variable_get.
  $show = variable_get('nodeaccess-types', array());
  $roles = nodeaccess_get_role_aliases();
  $allowed_roles = variable_get('nodeaccess-roles', array());
  $allowed_grants = variable_get('nodeaccess-grants', array());
  $form['priority'] = array(
    '#type' => 'checkbox',
    '#title' => t('Give node grants priority'),
    '#default_value' => variable_get('nodeaccess-priority', 0),
    '#description' => t('If you are only using this access control module,
      you can safely ignore this. If you are using multiple access control
      modules, and you want the grants given on individual nodes to override
      any grants given by other modules, you should check this box.'),
  );

  // Select whether to preserve hidden grants.
  $form['preserve'] = array(
    '#type' => 'checkbox',
    '#title' => t('Preserve hidden grants'),
    '#default_value' => variable_get('nodeaccess-preserve', 1),
    '#description' => '<small>' . t('If you check this box, any hidden grants
      are preserved when you save grants. Otherwise all grants users are not
      allowed to view or edit are revoked on save.') . '</small>',
  );

  // Select permissions you want to allow users to view and edit.
  $form['grant'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Allowed Grants'),
    '#tree' => TRUE,
    '#description' => '<small>' . t('The selected grants will be listed on
      individual node grants. If you wish for certain grants to be hidden from
      users on the node grants tab, make sure they are not selected here.') . '</small>',
  );
  $form['grant']['view'] = array(
    '#type' => 'checkbox',
    '#title' => t('View'),
    '#default_value' => $allowed_grants['view'],
  );
  $form['grant']['edit'] = array(
    '#type' => 'checkbox',
    '#title' => t('Edit'),
    '#default_value' => $allowed_grants['edit'],
  );
  $form['grant']['delete'] = array(
    '#type' => 'checkbox',
    '#title' => t('Delete'),
    '#default_value' => $allowed_grants['delete'],
  );

  // Select roles the permissions of which you want to allow users to
  // view and edit, and the aliases and weights of those roles.
  $form['role'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Allowed Roles'),
    '#tree' => TRUE,
    '#theme' => 'nodeaccess_admin_form_roles',
    '#description' => t('The selected roles will be listed on individual node
      grants. If you wish for certain roles to be hidden from users on the node
      grants tab, make sure they are not selected here. You may also provide an
      alias for each role to be displayed to the user and a weight to order them
      by. This is useful if your roles have machine-readable names not intended
      for human users.'),
  );
  foreach ($roles as $id => $role) {

    // Catch NULL values.
    if (!$role['alias']) {
      $role['alias'] = '';
    }
    if (!$role['weight']) {
      $role['weight'] = 0;
    }
    $form['role'][$id]['name'] = array(
      '#type' => 'hidden',
      '#value' => $role['name'],
    );
    $form['role'][$id]['allow'] = array(
      '#type' => 'checkbox',
      '#title' => check_plain($role['name']),
      '#default_value' => isset($allowed_roles[$id]) ? $allowed_roles[$id] : 0,
    );
    $form['role'][$id]['alias'] = array(
      '#type' => 'textfield',
      '#default_value' => $role['alias'],
      '#size' => 50,
      '#maxlength' => 50,
    );
    $form['role'][$id]['weight'] = array(
      '#type' => 'weight',
      '#default_value' => $role['weight'],
      '#delta' => 10,
    );
  }

  // Grant tab to node types.
  $form['nodeaccess']['tabs'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#title' => t('Content Type Grant Tab Access'),
    '#tree' => TRUE,
    '#description' => t('Show grant tab for the following node types.'),
  );
  $options = array();
  foreach (node_type_get_types() as $type => $bundle) {
    $options[$type] = array(
      'content_type' => check_plain($bundle->name),
    );
  }
  $form['nodeaccess']['tabs']['show'] = array(
    '#type' => 'tableselect',
    '#header' => array(
      'content_type' => t('Content type'),
    ),
    '#options' => $options,
    '#default_value' => $show,
    '#empty' => t('No content types to add a grant tab.'),
  );

  // Generate fieldsets for each node type.
  foreach (node_type_get_types() as $type => $bundle) {
    $form['nodeaccess'][$type] = array(
      '#type' => 'fieldset',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#title' => check_plain($bundle->name),
      '#tree' => TRUE,
      '#theme' => 'nodeaccess_admin_form_types',
    );

    // Set default author permissions for node type.
    $author_prefs = variable_get('nodeaccess_authors', array());
    $form['nodeaccess'][$type]['author']['grant_view'] = array(
      '#type' => 'checkbox',
      '#default_value' => $author_prefs[$type]['grant_view'],
    );
    $form['nodeaccess'][$type]['author']['grant_update'] = array(
      '#type' => 'checkbox',
      '#default_value' => $author_prefs[$type]['grant_update'],
    );
    $form['nodeaccess'][$type]['author']['grant_delete'] = array(
      '#type' => 'checkbox',
      '#default_value' => $author_prefs[$type]['grant_delete'],
    );
    $perms = variable_get('nodeaccess_' . $type, array());
    foreach ($perms as $perm) {
      $opts[$perm['gid']] = $perm;
    }

    // Set default role permissions for node type.
    foreach (user_roles() as $id => $role) {
      $form['nodeaccess'][$type]['roles'][$id]['name'] = array(
        '#markup' => $role,
      );
      $form['nodeaccess'][$type]['roles'][$id]['grant_view'] = array(
        '#type' => 'checkbox',
        '#default_value' => isset($opts[$id]['grant_view']) ? $opts[$id]['grant_view'] : 0,
      );
      $form['nodeaccess'][$type]['roles'][$id]['grant_update'] = array(
        '#type' => 'checkbox',
        '#default_value' => isset($opts[$id]['grant_update']) ? $opts[$id]['grant_update'] : 0,
      );
      $form['nodeaccess'][$type]['roles'][$id]['grant_delete'] = array(
        '#type' => 'checkbox',
        '#default_value' => isset($opts[$id]['grant_delete']) ? $opts[$id]['grant_delete'] : 0,
      );
    }

    // Set the default permissions if userreference exists and is enabled on
    // the content type.
    if (module_exists('user_reference')) {
      $bundle = field_extract_bundle('node', $bundle);
      $fields = field_info_instances('node', $bundle);
      $user_reference_perms = variable_get('nodeaccess_' . $type . '_user_reference', array());
      $field_types = field_info_field_types();
      foreach ($fields as $field) {
        $field = field_info_field($field['field_name']);
        if ($field['type'] == 'user_reference') {
          $enabled = isset($user_reference_perms[$field['field_name']]['enabled']) ? $user_reference_perms[$field['field_name']]['enabled'] : 0;
          $view = isset($user_reference_perms[$field['field_name']]['grant_view']) ? $user_reference_perms[$field['field_name']]['grant_view'] : 0;
          $update = isset($user_reference_perms[$field['field_name']]['grant_update']) ? $user_reference_perms[$field['field_name']]['grant_update'] : 0;
          $delete = isset($user_reference_perms[$field['field_name']]['grant_delete']) ? $user_reference_perms[$field['field_name']]['grant_delete'] : 0;
          $form['nodeaccess'][$type]['user_reference'][$field['field_name']]['name'] = array(
            '#value' => t($field_types[$field['type']]['label']),
          );
          $form['nodeaccess'][$type]['user_reference'][$field['field_name']]['enabled'] = array(
            '#type' => 'checkbox',
            '#default_value' => $enabled,
          );
          $form['nodeaccess'][$type]['user_reference'][$field['field_name']]['grant_view'] = array(
            '#type' => 'checkbox',
            '#default_value' => $view,
          );
          $form['nodeaccess'][$type]['user_reference'][$field['field_name']]['grant_update'] = array(
            '#type' => 'checkbox',
            '#default_value' => $update,
          );
          $form['nodeaccess'][$type]['user_reference'][$field['field_name']]['grant_delete'] = array(
            '#type' => 'checkbox',
            '#default_value' => $delete,
          );
        }
      }
    }
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Grants'),
  );
  return $form;
}