You are here

function nodeaccess_admin_form_submit in Nodeaccess 6

Same name and namespace in other branches
  1. 5 nodeaccess.module \nodeaccess_admin_form_submit()
  2. 6.2 nodeaccess.module \nodeaccess_admin_form_submit()
  3. 7 nodeaccess.admin.inc \nodeaccess_admin_form_submit()

Submit function for nodeaccess_admin_form.

File

./nodeaccess.module, line 228

Code

function nodeaccess_admin_form_submit($form, &$form_state) {
  $form_values = $form_state['values'];

  // Save priority.
  variable_set('nodeaccess-priority', $form_values['priority']);

  // Save preserve.
  variable_set('nodeaccess-preserve', $form_values['preserve']);

  // Save allowed grants.
  foreach ($form_values['grant'] as $id => $val) {
    $allowed_grants[$id] = $val;
  }
  variable_set('nodeaccess-grants', $allowed_grants);

  // Save allowed roles, role aliases and weights.
  foreach ($form_values['role'] as $id => $val) {
    $allowed_roles[$id] = $val['allow'];

    // Save alias and weight only for allowed roles.
    if ($val['allow']) {

      // If alias is empty, default to role name.
      if ($val['alias']) {
        $alias_prefs[$id]['name'] = $val['alias'];
      }
      else {
        $alias_prefs[$id]['name'] = $val['name'];
      }
      $alias_prefs[$id]['weight'] = $val['weight'];
    }
    else {

      // Otherwise, we only save alias if one was specified.
      if ($val['alias']) {
        $alias_prefs[$id]['name'] = $val['alias'];
        $alias_prefs[$id]['weight'] = $val['weight'];
      }
    }
  }
  variable_set('nodeaccess-roles', $allowed_roles);
  nodeaccess_save_role_aliases($alias_prefs);

  // Save author and role permissions for each node type.
  $author_prefs = array();
  foreach (node_get_types() as $type => $name) {
    $grants = array();
    foreach ($form_values[$type]['roles'] as $role => $val) {
      $grants[] = array(
        'gid' => $role,
        'realm' => 'nodeaccess_rid',
        'grant_view' => $val['grant_view'],
        'grant_update' => $val['grant_update'],
        'grant_delete' => $val['grant_delete'],
      );
    }
    variable_set('nodeaccess_' . $type, $grants);
    if ($form_values[$type]['show']) {
      $allowed_types[$type] = 1;
    }
    else {
      $allowed_types[$type] = 0;
    }
    $author_prefs[$type] = $form_values[$type]['author'];

    // Also save userreference default permissions if enabled.
    if (module_exists('userreference') && isset($form_values[$type]['userreference'])) {
      $userreference_grants = array();
      foreach ($form_values[$type]['userreference'] as $userreference_field => $val) {
        $userreference_grants[$userreference_field] = array(
          'gid' => 'nodeaccess_uid',
          'enabled' => $val['enabled'],
          'grant_view' => $val['grant_view'],
          'grant_update' => $val['grant_update'],
          'grant_delete' => $val['grant_delete'],
        );
      }
      variable_set('nodeaccess_' . $type . '_userreference', $userreference_grants);
    }
  }
  variable_set('nodeaccess_authors', $author_prefs);

  // Save allowed node types.
  variable_set('nodeaccess-types', $allowed_types);
  node_access_needs_rebuild(TRUE);
  drupal_set_message(t('Grants saved.'));
}