You are here

function taxonomy_tools_overview in Taxonomy Tools 7

Same name and namespace in other branches
  1. 8 taxonomy_tools.admin.inc \taxonomy_tools_overview()

Build taxonomy overview form.

Parameters

array $form: An associative array representing form element.

array $form_state: An associative array representing form_state element.

object $vocabulary: A vocabulary object.

string $root: Parent taxonomy term ID of the branch that is to be shown. NULL by default.

See also

theme_taxonomy_tools_overview()

1 string reference to 'taxonomy_tools_overview'
taxonomy_tools_menu in ./taxonomy_tools.module
Implements hook_menu().

File

./taxonomy_tools.admin.inc, line 24
Administrative page callbacks for the Taxonomy Tools module.

Code

function taxonomy_tools_overview($form, &$form_state, $vocabulary, $root = NULL) {
  $in_page = FALSE;
  if (!$root) {
    drupal_set_title($vocabulary->name);
  }
  elseif (is_object($root)) {
    drupal_set_title($root->name);
  }
  elseif (is_numeric($root)) {
    $query = db_select('taxonomy_term_data', 'foo');
    $query
      ->addField('foo', 'name');
    $query
      ->condition('foo.tid', $root);
    $name = $query
      ->execute()
      ->fetchField();
    drupal_set_title($name);
  }

  // @todo Duplicate if statement
  if (is_object($root)) {
    $root = $root->tid;
  }
  if (!isset($vocabulary)) {

    // It means we are not in administrative pages.
    $in_page = TRUE;
    $query = db_select('taxonomy_term_data', 'foo');
    $query
      ->addField('foo', 'vid');
    $query
      ->condition('foo.tid', $root);
    $vid = $query
      ->execute()
      ->fetchField();
    $vocabulary = taxonomy_vocabulary_load($vid);
  }

  // Make sure Drupal Ajax framework javascript is around.
  drupal_add_library('system', 'drupal.ajax');
  if (isset($form_state['confirm_term_delete'])) {

    // Show term confirmation deletion form.
    return taxonomy_tools_confirm_term_delete($form, $form_state, $vocabulary, $root);
  }
  $form = array();
  $form['#attached']['js'][] = drupal_get_path('module', 'taxonomy_tools') . '/js/taxonomy_tools.js';
  $form['#attached']['css'][] = drupal_get_path('module', 'taxonomy_tools') . '/style/taxonomy_tools.css';
  $form['#vocabulary'] = $vocabulary;

  // Link for adding new term.
  $href = '';
  $destination = drupal_get_destination();
  $href = $destination['destination'] . '/add';
  if (is_numeric(strpos($destination['destination'], 'taxonomy/term'))) {
    $href .= '/' . $vocabulary->machine_name;
  }
  $form['add_term'] = array(
    '#type' => 'link',
    '#title' => t('Add term'),
    '#href' => $href,
    '#attributes' => array(
      'title' => t('Add term'),
    ),
    '#options' => array(
      'query' => array(
        'destination' => $destination['destination'],
      ),
    ),
    '#access' => taxonomy_tools_overview_access($vocabulary, TRUE),
  );
  if ($root) {

    // Link to move up the term hierarchy.
    $href = '';
    $query = db_select('taxonomy_term_hierarchy', 'foo');
    $query
      ->addField('foo', 'parent');
    $query
      ->condition('foo.tid', $root);
    $parent = $query
      ->execute()
      ->fetchField();
    if ($in_page && $parent > 0 || !$in_page) {

      // @todo Implement use of destination variables to
      // achieve more universal approach.
      // Correct href depending on wether we are in administrative
      // pages or not.
      if ($in_page) {
        $href = 'taxonomy/term/' . $parent . '/overview';
      }
      else {
        $href = 'admin/structure/taxonomy/' . $vocabulary->machine_name . '/overview';
        if ($parent > 0) {
          $href .= '/' . $parent;
        }
      }
      $form['level_up'] = array(
        '#type' => 'link',
        '#title' => t('Up one level'),
        '#href' => $href,
        '#attributes' => array(
          'title' => t('Up one level'),
        ),
      );
    }
  }

  // Fetch all the necessary information from the database.
  $query = db_select('taxonomy_term_data', 'db1');
  $query
    ->addField('db1', 'tid');
  $query
    ->addField('db1', 'name');
  $query
    ->addField('db1', 'weight');
  $query
    ->condition('db1.vid', $vocabulary->vid);
  $query
    ->join('taxonomy_term_hierarchy', 'db2', 'db1.tid = db2.tid');
  if (!$root) {
    $root = 0;
  }
  $query
    ->condition('db2.parent', $root);
  if (module_exists('taxonomy_tools_publisher')) {
    $config = array_filter(variable_get('taxonomy_tools_publisher_config', array()));
    if (!empty($config) && in_array($vocabulary->machine_name, $config)) {
      $query
        ->leftJoin('field_data_field_taxonomy_term_status', 'db3', 'db1.tid = db3.entity_id');
      $query
        ->addField('db3', 'field_taxonomy_term_status_value', 'status');
      $query
        ->leftJoin('field_data_field_taxonomy_term_publish_on', 'db4', 'db1.tid = db4.entity_id');
      $query
        ->addField('db4', 'field_taxonomy_term_publish_on_value', 'publish_on');
      $query
        ->leftJoin('field_data_field_taxonomy_term_unpublish_on', 'db5', 'db1.tid = db5.entity_id');
      $query
        ->addField('db5', 'field_taxonomy_term_unpublish_on_value', 'unpublish_on');
    }
  }
  $query
    ->orderBy('db1.weight');
  $query
    ->orderBy('db1.name');
  $result = $query
    ->execute()
    ->fetchAll();
  $length = count($result);
  $i = 1;
  $form['tree_root'] = array(
    '#type' => 'hidden',
    '#value' => $root,
  );
  foreach ($result as $term) {
    $form[$term->tid] = array();

    // Term name.
    $href = '';
    if (arg(3) == 'overview') {
      $href = 'taxonomy/term/' . $term->tid . '/overview';
    }
    else {
      $href = 'admin/structure/taxonomy/' . $vocabulary->machine_name . '/overview/' . $term->tid;
    }
    $form[$term->tid]['name'] = array(
      '#type' => 'link',
      '#title' => check_plain($term->name),
      '#href' => $href,
    );

    // Term children count.
    $query = db_select('taxonomy_term_hierarchy', 'foo');
    $query
      ->addField('foo', 'tid');
    $query
      ->condition('foo.parent', $term->tid);
    $child_count = $query
      ->countQuery()
      ->execute()
      ->fetchField();
    $form[$term->tid]['child_count'] = array(
      '#markup' => '<span class="term-child-count">' . $child_count . '</span>',
    );

    // Informative icons.
    $icons = module_invoke_all('taxonomy_tools_overview_info_icons', $term->tid);
    foreach ($icons as $key => $icon) {
      $form[$term->tid]['info_icons'][$key] = array(
        '#markup' => theme_image($icon),
      );
    }

    // Term weight.
    $form[$term->tid]['weight_' . $term->tid] = array(
      '#type' => 'weight',
      '#delta' => $length,
      '#title_display' => 'invisible',
      '#title' => t('Weight for added term'),
      '#default_value' => $term->weight,
      '#access' => user_access('administer taxonomy') || user_access('edit terms in ' . $vocabulary->vid),
    );

    // @todo Hook to alter operations
    // View link.
    $form[$term->tid]['operations'] = array();
    $view_link = 'taxonomy/term/' . $term->tid;
    $form[$term->tid]['operations']['view'] = array(
      '#type' => 'link',
      '#title' => '',
      '#href' => $view_link,
      '#attributes' => array(
        'class' => array(
          'view-term',
        ),
        'title' => t('view'),
      ),
    );

    // Edit link.
    $edit_link = 'taxonomy/term/' . $term->tid . '/edit';
    $destination = drupal_get_destination();
    $form[$term->tid]['operations']['edit'] = array(
      '#type' => 'link',
      '#title' => '',
      '#href' => $edit_link,
      '#attributes' => array(
        'class' => array(
          'edit-term',
        ),
        'title' => t('edit'),
      ),
      '#options' => array(
        'query' => array(
          'destination' => $destination['destination'],
        ),
      ),
      '#access' => user_access('administer taxonomy') || user_access('edit terms in ' . $vocabulary->vid),
    );

    // Any other operation links added by other modules.
    $additional_links = module_invoke_all('taxonomy_tools_overview_links', $term->tid);
    foreach ($additional_links as $key => $link) {
      $form[$term->tid]['operations'][$key] = $link;
    }

    // Deletion checkbox.
    $form[$term->tid]['delete_' . $term->tid] = array(
      '#type' => 'checkbox',
      '#title' => t('delete'),
      '#title_display' => 'invisible',
      '#access' => user_access('administer taxonomy') || user_access('delete terms in ' . $vocabulary->vid),
    );
    if (module_exists('taxonomy_tools_publisher')) {

      // Publishing status.
      if (!isset($term->status) || $term->status == 1) {
        $status = t('Published.');
        if (isset($term->unpublish_on)) {
          $status .= ' ';
          $status .= t('Will be unpublished on :date', array(
            ':date' => format_date($term->unpublish_on, 'custom', TAXONOMY_PUBLISHER_MESSAGE_DATE_FORMAT),
          ));
        }
      }
      else {
        $status = t('Unpublished.');
        if (isset($term->publish_on)) {
          $status .= ' ';
          $status .= t('Will be published on :date.', array(
            ':date' => format_date($term->publish_on, 'custom', TAXONOMY_PUBLISHER_MESSAGE_DATE_FORMAT),
          ));
        }
        if (isset($term->unpublish_on)) {
          $status .= ' ';
          $status .= t('Will be unpublished on :date.', array(
            ':date' => format_date($term->unpublish_on, 'custom', TAXONOMY_PUBLISHER_MESSAGE_DATE_FORMAT),
          ));
        }
      }
      $form[$term->tid]['status'] = array(
        '#markup' => $status,
      );
    }
    if (module_exists('taxonomy_tools_role_access')) {

      // Buttons for changing term access for user roles.
      $config = array_filter(variable_get('taxonomy_tools_role_access_vocab_config', array()));
      if (!empty($config) && in_array($vocabulary->machine_name, $config)) {
        $config = variable_get('taxonomy_tools_role_access_role_config', array());
        if (!empty($config)) {
          $roles = user_roles();
          foreach ($roles as $rid => $role) {
            if (in_array($rid, $config)) {
              $access = taxonomy_tools_role_access_get_access($term->tid, $rid);
              $attributes = array(
                'class' => array(
                  'use-ajax',
                  'access_' . $term->tid . '_' . $rid,
                ),
                'title' => t('change'),
              );
              if ($access) {
                $attributes['class'][] = 'true';
              }
              else {
                $attributes['class'][] = 'false';
              }
              $opposite = $access ? 'false' : 'true';
              $link = 'taxonomy-role-access/nojs/' . $vocabulary->machine_name . '/' . $term->tid . '/' . $rid . '/' . $opposite;
              if ($root > 0) {
                $link .= '/' . $root;
              }
              $form[$term->tid]['access'][$role] = array(
                '#type' => 'link',
                '#title' => '',
                '#href' => $link,
                '#attributes' => $attributes,
                '#access' => user_access('administer taxonomy role access'),
              );
            }
          }
        }
      }
    }
    $i++;
  }
  $description = t('The number of child taxonomy terms is displayed next to the taxonomy term name. If the current overview page location is other than root level, the first row of the table will display a link with the functionality to go up one level.');
  $form['description'] = array(
    '#markup' => '<div class="description">' . $description . '</div>',
  );
  $form['actions']['save_order'] = array(
    '#type' => 'submit',
    '#value' => t('Save order'),
    '#access' => user_access('administer taxonomy') || user_access('edit terms in ' . $vocabulary->vid),
  );
  $form['actions']['delete_terms'] = array(
    '#type' => 'submit',
    '#value' => t('Delete terms'),
    '#access' => user_access('administer taxonomy') || user_access('delete terms in ' . $vocabulary->vid),
  );
  return $form;
}