You are here

function tac_fields_admin_form in Taxonomy Access Control 6

Main administration form, with grid of permissions per term.

Parameters

$field: The field to configure.

$rid: The role to configure.

1 string reference to 'tac_fields_admin_form'
tac_fields_admin in tac_fields/tac_fields.admin.inc
Menu callback for TAC Fields admin page (admin/user/tac_fields).

File

tac_fields/tac_fields.admin.inc, line 287
Administrative interface for TAC Fields.

Code

function tac_fields_admin_form($form_state, $field, $rid) {

  // Fetch all default grants
  $r = db_query("SELECT * FROM {term_field_access_defaults} \n     WHERE field = '%s' AND rid = %d", $field, $rid);
  while ($row = db_fetch_array($r)) {
    $default_grants[$row['vid']] = $row;
  }

  // If we are adding a role, no global default is set yet, so insert it now.
  if (empty($default_grants[0]) && isset($rid) && isset($field)) {

    // Assemble a $row object for Schema API.
    $row = new stdClass();
    $row->vid = 0;
    $row->field = $field;
    $row->rid = $rid;
    $row->grant_view = 1;
    $row->grant_update = 0;

    // Insert the default.
    drupal_write_record('term_field_access_defaults', $row);
  }

  // Fetch all grants
  $result = db_query("SELECT * FROM {term_field_access} \n     WHERE field = '%s' AND rid = %d", $field, $rid);
  while ($row = db_fetch_array($result)) {
    $grants[$row['tid']] = $row;
  }
  $form['instructions'] = array(
    '#value' => _tac_fields_admin_instructions_html(),
    '#weight' => '20',
  );
  $form['rid'] = array(
    '#type' => 'value',
    '#value' => $rid,
  );
  $form['field'] = array(
    '#type' => 'value',
    '#value' => $field,
  );
  $form['grants'] = $form['selected_terms'] = $form['selected_defaults'] = array(
    '#tree' => TRUE,
  );

  // Global default
  $global_defaults = empty($default_grants[0]) ? NULL : $default_grants[0];
  $form['vocabs'][0]['#title'] = 'Global';
  $form['grants'][0][0] = tac_fields_build_row($global_defaults);
  $form['selected_defaults'][0] = array(
    '#type' => 'checkbox',
    '#disabled' => TRUE,
    '#title' => t('<em>default</em>'),
    '#description' => t("can't be disabled without disabling this role or field"),
  );
  foreach (taxonomy_get_vocabularies() as $vid => $vocabulary) {
    $form['vocabs'][$vid]['#title'] = check_plain($vocabulary->name);
    if (isset($default_grants[$vid])) {
      $form['grants'][$vid][0] = tac_fields_build_row($default_grants[$vid]);
      $form['selected_defaults'][$vid] = array(
        '#type' => 'checkbox',
        '#title' => t('<em>default</em>'),
      );
    }
    else {
      $add_items[$vocabulary->name]["default {$vid}"] = t('*default*');
    }
    if ($tree = taxonomy_get_tree($vid)) {
      foreach ($tree as $term) {
        if (isset($grants[$term->tid])) {
          $form['grants'][$vid][$term->tid] = tac_fields_build_row($grants[$term->tid]);
          $form['selected_terms'][$term->tid] = array(
            '#type' => 'checkbox',
            '#title' => str_repeat('&nbsp;&nbsp;', $term->depth) . check_plain($term->name),
          );
        }
        else {
          $add_items[$vocabulary->name]["term {$term->tid}"] = str_repeat('-', $term->depth) . check_plain($term->name);
        }
      }
    }
  }

  //New grant row
  if (isset($add_items)) {
    $form['new']['grants'] = tac_fields_build_row();
    $form['new']['#tree'] = TRUE;
    $form['new']['item'] = array(
      '#type' => 'select',
      '#options' => $add_items,
    );
    $form['new']['recursive'] = array(
      '#type' => 'checkbox',
      '#title' => t('with children'),
      '#description' => t('Add child terms recursively with these values.'),
    );
    $form['new']['add'] = array(
      '#type' => 'submit',
      '#value' => t('Add'),
    );
  }
  $form['delete'] = array(
    '#type' => 'submit',
    '#value' => t('Delete selected'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save all'),
  );
  return $form;
}