You are here

function nodeaccess_admin_form_submit in Nodeaccess 7

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. 6 nodeaccess.module \nodeaccess_admin_form_submit()

Submit function for nodeaccess_admin_form.

_state

Parameters

$form:

File

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

Code

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

  // 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.
  $alias_prefs = array();
  $allowed_roles = array();
  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_type_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);
    $author_prefs[$type] = $form_values[$type]['author'];

    // Also save userreference default permissions if enabled.
    if (module_exists('user_reference') && isset($form_values[$type]['user_reference'])) {
      $user_reference_grants = array();
      foreach ($form_values[$type]['user_reference'] as $user_reference_field => $val) {
        $user_reference_grants[$user_reference_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 . '_user_reference', $user_reference_grants);
    }
  }
  variable_set('nodeaccess_authors', $author_prefs);

  // Save allowed node type grant tab.
  $allowed_types = array();
  foreach ($form_values['tabs']['show'] as $type => $value) {
    $allowed_types[$type] = (bool) $value;
  }
  nodeaccess_set_type_grants($allowed_types);
  drupal_set_message(t('Grants saved.'));
}