Browse Source

Working on Views area handler

Stephen Ficklin 7 years ago
parent
commit
5c5c1c5e6a

+ 35 - 3
tripal/api/tripal.collections.api.inc

@@ -31,14 +31,46 @@ function tripal_create_collection($details) {
 /**
  * Retrieve a collection using the collection ID
  *
- * @param $collection_id
- *   The unique identifier for the collection.
+ * @param $values
+ *   An array of key/value pairs to uniquely identify a collection.  The
+ *   following keys can be used:
+ *   - collection_id:  The numeric value for the collection.
+ *   - uid: The ID of the user that owns the collection. This key must
+ *     always be used with the 'name' key.
+ *   - name:  The name of the collection.  This key must always be used
+ *     with the 'uid' key.
  *
  * @return
  *  An instance of a TripalEntityCollection class or FALSE on failure.
  *
  */
-function tripal_get_collection($collection_id) {
+function tripal_get_collection($values) {
+  $collection_id = array_key_exists('collection_id', $values) ? $values['collection_id'] : NULL;
+  $uid = array_key_exists('uid', $values) ? $values['uid'] : NULL;
+  $name = array_key_exists('name', $values) ? $values['name'] : NULL;
+
+  if ($uid and !$name) {
+    throw new Exception('tripal_get_collection: Missing the collection name when specifying the User ID.');
+  }
+  if (!$uid and $name) {
+    throw new Exception('tripal_get_collection: Missing the User ID when specifying the collection name.');
+  }
+  if (!$collection_id and !$uid and !$name) {
+    throw new Exception('tripal_get_collection: Missing a valid key.');
+  }
+
+  if ($name and $uid) {
+    $collection_id = db_select('tripal_collection', 'tc')
+      ->fields('tc', array('collection_id'))
+      ->condition('uid', $uid)
+      ->condition('collection_name', $name)
+      ->execute()
+      ->fetchField();
+    if (!$collection_id) {
+      throw new Exception('tripal_get_collection: The collection could not be found with the given uid and name.');
+    }
+  }
+
   try {
     $collection = new TripalEntityCollection();
     $collection->load($collection_id);

+ 1 - 1
tripal/includes/TripalFieldDownloaders/TripalCSVDownloader.inc

@@ -19,7 +19,7 @@ class TripalCSVDownloader extends TripalFieldDownloader {
     foreach ($this->fields as $field_id) {
       $field = field_info_field_by_id($field_id);
       $field_name = $field['field_name'];
-      $value = $entity->{$field_name}['und'][0]['value'];
+
       // If we only have one element then this is good.
       if (count($entity->{$field_name}['und']) == 1) {
         $value = $entity->{$field_name}['und'][0]['value'];

+ 1 - 1
tripal/includes/TripalFieldDownloaders/TripalTabDownloader.inc

@@ -19,7 +19,7 @@ class TripalTabDownloader extends TripalFieldDownloader {
     foreach ($this->fields as $field_id) {
       $field = field_info_field_by_id($field_id);
       $field_name = $field['field_name'];
-      $value = $entity->{$field_name}['und'][0]['value'];
+
       // If we only have one element then this is good.
       if (count($entity->{$field_name}['und']) == 1) {
         $value = $entity->{$field_name}['und'][0]['value'];

+ 1 - 0
tripal/tripal.info

@@ -9,6 +9,7 @@ configure = admin/tripal
 stylesheets[all][] = theme/css/tripal.css
 scripts[]          = theme/js/tripal.js
 
+files[] = views_handlers/tripal_views_handler_area_collections.inc
 files[] = views_handlers/tripal_views_handler_field.inc
 files[] = views_handlers/tripal_views_handler_field_element.inc
 files[] = views_handlers/tripal_views_handler_field_entity.inc

+ 7 - 0
tripal/tripal.views.inc

@@ -33,6 +33,13 @@ function tripal_views_data() {
   tripal_views_data_tripal_entity($data);
   tripal_views_data_fields($data);
 
+  $data['views']['tripal_area_collections'] = array(
+    'title' => t('Tripal Content Data Collections'),
+    'help' => t('Save Tripal content search results into a data collection for downloading or use with other tools.'),
+    'area' => array(
+      'handler' => 'tripal_views_handler_area_collections',
+    ),
+  );
   return $data;
 }
 

+ 124 - 0
tripal/views_handlers/tripal_views_handler_area_collections.inc

@@ -0,0 +1,124 @@
+<?php
+
+class tripal_views_handler_area_collections extends views_handler_area_result {
+
+  function options_form(&$form, &$form_state) {
+    // We have no options so we have to implement this function with
+    // nothing in it.
+  }
+  /**
+   * Implements views_handler_area_result::render().
+   */
+  function render($empty = FALSE) {
+
+    // This will only work with Tripal content types and the tripal_views_query
+    // plugin. So don't show anything for others.
+    if ($this->query->plugin_name != 'tripal_views_query') {
+      return '';
+    }
+    $form = drupal_get_form('tripal_views_handler_area_collections_form', $this->view, $this->query);
+    return drupal_render($form);
+  }
+}
+
+/**
+ *
+ */
+function tripal_views_handler_area_collections_form($form, $form_state, $view, $query) {
+
+  // Set form defaults.
+  $collection_name = '';
+  $collection_desc = '';
+
+  // Get the bundle for this query.
+  $matches = array();
+  preg_match('/^(.+?)__(.+?)$/', $view->base_table, $matches);
+  $vocabulary = $matches[1];
+  $accession = $matches[2];
+  $term = tripal_load_term_entity(array('vocabulary' => $vocabulary, 'accession' => $accession));
+  $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
+
+  $form = array();
+  $form['save_collection'] = array(
+   '#type' => 'fieldset',
+   '#title' => t('Save Results'),
+   '#collapsible' => TRUE,
+   '#collapsed' => TRUE,
+   '#description' => t('A data collection is a virtual container into which you can
+     save data.  You can place your search results into a data collection for
+     download or use with other tools on this site that support data collections.'),
+  );
+  $form['save_collection']['bundle_name'] = array(
+    '#type' => 'value',
+    '#value' => $bundle->name,
+  );
+  $form['save_collection']['summary'] = array(
+   '#type' => 'item',
+   '#title' => 'Results Summary',
+   '#markup' => t('There are @total_rows record(s) that can be added to a container.', array('@total_rows' => $view->total_rows)),
+  );
+  $form['save_collection']['collection_name'] = array(
+   '#type' => 'textfield',
+   '#title' => t('Collection Name'),
+   '#description' => t('Please name this collection for future reference.'),
+   '#default_value' => $collection_name,
+   '#required' => TRUE,
+  );
+  $form['save_collection']['collection_desc'] = array(
+   '#type' => 'textarea',
+   '#title' => t('Description'),
+   '#description' => t('Please provide a description about this data collection.'),
+   '#default_value' => $collection_name,
+  );
+
+  // Get the list of fields in this view.
+  $fields = $view->field;
+  $field_ids = array();
+  foreach ($fields as $field_name => $handler) {
+    if ($field_name == 'entity_id') {
+      continue;
+    }
+    $field = field_info_field($field_name);
+    $instance = field_info_instance('TripalEntity', $field_name, $bundle->name);
+    $field_ids[$field['id']] = $instance['label'];
+  }
+  $form['save_collection']['field_ids'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Field Selection'),
+    '#description' => t('Please select the fields to include in this data collection. If you do not select any fields then all fields will be included.'),
+    '#options' => $field_ids,
+  );
+
+  $form['save_collection']['button'] = array(
+    '#type' => 'submit',
+    '#value' => 'Save Data Collection',
+    '#name' => 'save_collection'
+  );
+
+  return $form;
+}
+
+
+/**
+ *
+ */
+function tripal_views_handler_area_collections_form_submit($form, $form_state) {
+  global $user;
+
+  $bundle_name = $form_state['values']['bundle_name'];
+  $collection_name = $form_state['values']['collection_name'];
+  $description = $form_state['values']['collection_desc'];
+  $field_ids = $form_state['values']['field_ids'];
+  $uid = $user->uid;
+
+  $entities = array();
+
+  tripal_create_collection(array(
+    'uid'  => $uid,
+    'collection_name' => $collection_name,
+    'bundle_name' => $bundle_name,
+    'ids' => $entities,
+    'fields' => $field_ids,
+    'description'  => $description,
+  ));
+}

+ 1 - 0
tripal_chado/api/tripal_chado.semweb.api.inc

@@ -225,6 +225,7 @@ function tripal_get_chado_semweb_column($chado_table, $term) {
     ->fields('CS')
     ->condition('chado_table', $chado_table)
     ->execute();
+
   while($column = $columns->fetchObject()) {
     $cvterm_id = $column->cvterm_id;