function tripal_content_overview_form

3.x TripalEntityUIController.inc tripal_content_overview_form($form, &$form_state)

Display a listing of Tripal entities.

@TODO Filters and bulk operations needed to be added to this form.

_state

Parameters

array $form:

Return value

A form array describing this listing to the Form API.

1 string reference to 'tripal_content_overview_form'
tripal_content_view in tripal/includes/TripalEntityUIController.inc
Provide a data listing for tripal entites (ie: biological data).

File

tripal/includes/TripalEntityUIController.inc, line 160

Code

function tripal_content_overview_form($form, &$form_state) {
  global $user;

  // Set form defaults.  The $_SESSION contains the last known selection
  // by this user. That should be overridden if the $_GET variable contains
  // values.
  $etype = '';
  $status = '';
  if (array_key_exists('tripal_content_overview', $_SESSION)) {
    $etype = $_SESSION['tripal_content_overview']['type'];
    $status = $_SESSION['tripal_content_overview']['status'];
  }
  $etype = array_key_exists('type', $_GET) ? $_GET['type'] : $etype;
  $status = array_key_exists('status', $_GET) ? $_GET['status'] : $status;

  $headers = array(
    'title' => array(
      'data' => t('Title'),
      'type' => 'property',
      'specifier' => 'title'
    ),
    'Type',
    'Term',
    'Author',
    'status' => array(
      'data' => t('Status'),
      'type' => 'property',
      'specifier' => 'status'
    ),
    'changed' => array(
      'data' => t('Updated'),
      'type' => 'property',
      'specifier' => 'changed',
      'sort' => 'desc',
    ),
    'Operations',
  );
  $rows = array();

  // Get the Options arrays used by the fields below. We need to build them
  // here because we will use them both for creating the item list below
  // and for the fields.
  $etypes = db_select('tripal_bundle', 'tb')
    ->fields('tb', array('name', 'label'))
    ->execute()
    ->fetchAllKeyed();
  $etypes = array('0' => 'any') + $etypes;
  $statuses = array(
    '0' => 'any',
    'status-1' => 'published',
    'status-0' => 'not published'
  );

  // Build the list of existing filters.
  $items = array();
  if ($etype) {
    $items[] = 'where <strong>type</strong> is <strong>' . $etypes[$etype] . '</strong>';
  }
  if ($status) {
    $items[] = 'where <strong>status</strong> is <strong>' . $statuses[$status] . '</strong>';
  }
  // Add 'and' to the beginning of every filter after the first
  if (count($items) > 1) {
    for ($i = 1; $i < count($items); $i++) {
      $items[$i] = 'and ' . $items[$i];
    }
  }
  $list = theme_item_list(array(
    'items' => $items,
    'title' => '',
    'type' => 'ul',
    'attributes' => array(),
  ));

  // Set the title to be informative (defaults to content for some reason).
  drupal_set_title('Tripal Content');

  $form['filter'] = array(
    '#type' => 'fieldset',
    '#title' => 'Show only items where',
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
    '#prefix' => '<div class="exposed-filters">',
    '#suffix' => '</div>',
  );
  if (count($items) > 0) {
    $form['filter']['filters'] = array(
      '#type' => 'item',
      '#markup' => $list,
    );
  }
  if (!$status) {
    $form['filter']['status'] = array(
      '#type' => 'select',
      '#title' => 'Status',
      '#options' => $statuses,
    );
  }
  else {
    $form['filter']['status'] = array(
      '#type' => 'value',
      '#value' => $status,
    );
  }

  if (!$etype) {
    $form['filter']['type'] = array(
      '#type' => 'select',
      '#title' => 'Content Type',
      '#options' => $etypes,
    );
  }
  else {
    $form['filter']['type'] = array(
      '#type' => 'value',
      '#value' => $etype,
    );
  }

  $form['filter']['buttons'] = array(
    '#type' => 'value',
    '#prefix' => '<div id="edit-actions" class="container-inline form-actions form-wrapper">',
    '#suffix' => '</div>',
  );
  if (count($items) > 0) {
    if (count($items) < 2) {
      $form['filter']['buttons']['refinebtn'] = array(
        '#type' => 'submit',
        '#value' => 'Refine',
        '#name' => 'refine',
      );
    }
    $form['filter']['buttons']['filterbtn'] = array(
      '#type' => 'submit',
      '#value' => 'Reset',
      '#name' => 'reset',
    );
  }
  else {
    $form['filter']['buttons']['filterbtn'] = array(
      '#type' => 'submit',
      '#value' => 'Filter',
      '#name' => 'filter',
    );
  }

  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'TripalEntity');
  if ($etype) {
    $query->entityCondition('bundle', $etype);
  }
  if ($status) {
    if ($status == 'status-1') {
      $query->propertyCondition('status', 1);
    }
    if ($status == 'status-0') {
      $query->propertyCondition('status', 0);
    }
  }
  //$query->propertyOrderBy('created', 'DESC');

  // Find out the total number of records and determine what page we're on, and
  // initialize the pager.
  $cquery = clone $query;
  $cquery->count();
  $num_records = $cquery->execute();

  // Calculate the range and create a pager.
  $query->pager(25);
  $query->tableSort($headers);
  $results = $query->execute();
  $pager = theme('pager');


  // For each entity retrieved add a row to the data listing.
  if (!isset($results['TripalEntity'])) {
    $results['TripalEntity'] = array();
  }
  foreach ($results['TripalEntity'] as $entity_id => $stub) {
    $vocabulary = '';
    $term_name = '';

    // We don't need all of the attached fields for an entity so, we'll
    // not use the entity_load() function.  Instead just pull it from the
    // database table.
    $equery = db_select('tripal_entity', 'TE');
    $equery->join('tripal_bundle', 'TB', 'TE.bundle = TB.name');
    $equery->fields('TB', array('label'));
    $equery->fields('TE');
    $equery->condition('TE.id', $entity_id);
    $entity = $equery->execute()->fetchObject();
    if (!$entity) {
      continue;
    }

    // Get the term
    $term = entity_load('TripalTerm', array('id' => $entity->term_id));
    $term = reset($term);
    if ($term) {
      $term_name = $term->name;
      $vocab = entity_load('TripalVocab', array('id' => $term->vocab_id));
      $vocab = reset($vocab);
      $vocabulary = $vocab->vocabulary;
    }

    // Retrieve details about the user who created this data.
    $author = user_load($entity->uid);

    // Build the action links
    $links = '';
    if (entity_access('edit', 'TripalEntity', $entity, $user)) {
      $links .= '&nbsp;&nbsp;' . l('edit', 'bio_data/' . $entity->id . '/edit');
    }
    if (entity_access('delete', 'TripalEntity', $entity, $user)) {
      $links .= '&nbsp;&nbsp;' . l('delete', 'bio_data/' . $entity->id . '/delete');
    }

    // Add information to the table.
    $rows[] = array(
      l($entity->title, 'bio_data/' . $entity->id),
      $entity->label,
      $vocabulary . ':' . $term_name,
      l($author->name, 'user/' . $entity->uid),
      $entity->status == 1 ? 'published' : 'unpublished',
      format_date($entity->changed, 'short'),
      $links,
    );
  }

  // If there are no entites created yet then add a message to the table to
  // provide guidance to administrators.
  if (empty($rows)) {
    $rows[] = array(
      array(
        'data' => t('No content can be found.'),
        'colspan' => 7
      )
    );
  }

  // Render the data listing.
  $table_vars = array(
    'header' => $headers,
    'rows' => $rows,
    'attributes' => array(),
    'sticky' => TRUE,
    'caption' => '',
    'colgroups' => array(),
    'empty' => '',
  );

  $output = "<div><strong>Found $num_records records</strong></div>" . $pager . theme('table', $table_vars) . $pager;
  $form['results'] = array(
    '#type' => 'markup',
    '#markup' => $output,
  );

  return $form;
}