You are here

function taxonomy_access_save_all_submit in Taxonomy Access Control 7

Form submission handler for taxonomy_access_admin_form().

Processes submissions for the 'Save all' button.

1 string reference to 'taxonomy_access_save_all_submit'
taxonomy_access_admin_role in ./taxonomy_access.admin.inc
Form constructor for a form to manage grants by role.

File

./taxonomy_access.admin.inc, line 770
Administrative interface for taxonomy access control.

Code

function taxonomy_access_save_all_submit($form, &$form_state) {
  $values = $form_state['values'];
  $rid = $values['rid'];
  $vocabs = taxonomy_get_vocabularies();

  // Create four lists of records to update.
  $update_terms = array();
  $skip_terms = array();
  $update_defaults = array();
  $skip_defaults = array();
  foreach ($values['grants'] as $vid => $rows) {
    if ($vid == TAXONOMY_ACCESS_GLOBAL_DEFAULT) {
      $element = $form['global_default'];
    }
    else {
      $vocab = $vocabs[$vid];
      $element = $form[$vocab->machine_name];
    }
    foreach ($rows as $tid => $row) {

      // Check the default values for this row.
      $defaults = array();
      $grants = array();
      foreach (array(
        'view',
        'update',
        'delete',
        'create',
        'list',
      ) as $grant_name) {
        $grants[$grant_name] = $row[$grant_name];
        $defaults[$grant_name] = $element['grants'][$vid][$tid][$grant_name]['#default_value'];
      }

      // Proceed if the user changed the row (values differ from defaults).
      if ($defaults != $grants) {

        // If the grants for node access match the defaults, then we
        // can skip updating node access records for this row.
        $update_nodes = FALSE;
        foreach (array(
          'view',
          'update',
          'delete',
        ) as $op) {
          if ($defaults[$op] != $grants[$op]) {
            $update_nodes = TRUE;
          }
        }

        // Add the row to one of the four arrays.
        switch (TRUE) {

          // Term record with node grant changes.
          case $tid && $update_nodes:
            $update_terms[$tid] = _taxonomy_access_format_grant_record($tid, $rid, $grants);
            break;

          // Term record and no node grant changes.
          case $tid && !$update_nodes:
            $skip_terms[$tid] = _taxonomy_access_format_grant_record($tid, $rid, $grants);
            break;

          // Vocab record with node grant changes.
          case !$tid && $update_nodes:
            $update_defaults[$vid] = _taxonomy_access_format_grant_record($vid, $rid, $grants, TRUE);
            break;

          // Vocab record and no node grant changes.
          case !$tid && !$update_nodes:
            $skip_defaults[$vid] = _taxonomy_access_format_grant_record($vid, $rid, $grants, TRUE);
            break;
        }
      }
    }
  }

  // Process each set.
  if (!empty($update_terms)) {
    taxonomy_access_set_term_grants($update_terms);
  }
  if (!empty($skip_terms)) {
    taxonomy_access_set_term_grants($skip_terms, FALSE);
  }
  if (!empty($update_defaults)) {
    taxonomy_access_set_default_grants($update_defaults);
  }
  if (!empty($skip_defaults)) {
    taxonomy_access_set_default_grants($skip_defaults, FALSE);
  }
}