Bladeren bron

Updated Tripal Collections views handler to provide a modified form on a drupal search api view.

Lacey Sanderson 7 jaren geleden
bovenliggende
commit
934acb319b
1 gewijzigde bestanden met toevoegingen van 161 en 9 verwijderingen
  1. 161 9
      tripal/views_handlers/tripal_views_handler_area_collections.inc

+ 161 - 9
tripal/views_handlers/tripal_views_handler_area_collections.inc

@@ -11,24 +11,39 @@ class tripal_views_handler_area_collections extends views_handler_area_result {
    */
   function render($empty = FALSE) {
 
-    // If collections are dispabled then don't show anything.
+    // If collections are disbabled then don't show anything.
     $collections_enabled = variable_get('tripal_data_collections_enabled', 1);
     if (!$collections_enabled) {
       return '';
     }
 
-    // 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 '';
+    // We need a specific form to work with Tripal content types and the tripal_views_query plugin.
+    if ($this->query->plugin_name == 'tripal_views_query') {
+      $form = drupal_get_form('tripal_views_handler_area_collections_form', $this->view, $this->query);
+      return drupal_render($form);
+    }
+    // We also want to support views created via the Drupal Search API.
+    elseif ($this->query->plugin_name == 'search_api_views_query') {
+      $form = drupal_get_form('tripal_views_handler_area_collections_search_api_form', $this->view, $this->query);
+      return drupal_render($form);
+    }
+    // If we don't support this type of view, then we should tell the admin
+    // so they're not left scratching their head like I was ;-).
+    else {
+      tripal_set_message('Unfortunatly Tripal Collections are not supported for your current view type.');
     }
-    $form = drupal_get_form('tripal_views_handler_area_collections_form', $this->view, $this->query);
-    return drupal_render($form);
   }
 }
 
 /**
+ * Views Area Handler Form: Tripal Collections on Tripal Entity-based Views
  *
+ * @param $form
+ * @param $form_state
+ * @param $view
+ *   The views object that this form will be rendered on.
+ * @param $query
+ *   The views query object generating the views results.
  */
 function tripal_views_handler_area_collections_form($form, $form_state, $view, $query) {
 
@@ -186,6 +201,7 @@ function tripal_views_handler_area_collections_form($form, $form_state, $view, $
   $form['#suffix'] = '</div>';
   return $form;
 }
+
 /**
  * Theme the fields section of the tripal_views_handler_area_collections form.
  *
@@ -230,13 +246,14 @@ function theme_tripal_views_handler_area_collections_fields_fset($variables) {
 }
 
 /**
- *
+ * AJAX: Tripal Collections on Tripal Entity-based Views
  */
 function tripal_views_handler_area_collections_form_ajax($form, $form_state) {
   return $form;
 }
+
 /**
- *
+ * Views Area Handler Form SUBMIT: Tripal Collections on Tripal Entity-based Views
  */
 function tripal_views_handler_area_collections_form_submit($form, $form_state) {
   global $user;
@@ -277,3 +294,138 @@ function tripal_views_handler_area_collections_form_submit($form, $form_state) {
     'fields' => $selected_fids,
   ));
 }
+
+/**
+ * Views Area Handler Form: Tripal Collections on Drupal Search API-based
+ *
+ * @param $form
+ * @param $form_state
+ * @param $view
+ *   The views object that this form will be rendered on.
+ * @param $query
+ *   The views query object generating the views results.
+ */
+function tripal_views_handler_area_collections_search_api_form($form, $form_state, $view, $query) {
+
+  // Set form defaults.
+  $collection_name = '';
+  $collection_desc = '';
+
+  $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']['summary'] = array(
+    '#type' => 'item',
+    '#title' => 'Results Summary',
+    '#markup' => t('There are @total_rows record(s) that can be added to a data collection.', 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']['description_fset'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Add a Description'),
+    '#collapsible' => TRUE,
+    '#collapsed' => TRUE,
+  );
+
+  $form['save_collection']['description_fset']['collection_desc'] = array(
+    '#type' => 'textarea',
+    '#title' => t('Description'),
+    '#description' => t('Please provide a description about this data collection. This is meant to help you remember what is in the collection.'),
+    '#default_value' => $collection_name,
+  );
+
+  // @todo add ability to choose fields for the collection.
+
+  $form['save_collection']['button'] = array(
+    '#type' => 'submit',
+    '#value' => 'Save Data Collection',
+    '#name' => 'save_collection',
+    '#ajax' => array(
+      'callback' => "tripal_views_handler_area_collections_form_ajax",
+      'wrapper' => 'tripal-views-handler-area-collections',
+      'effect'   => 'fade',
+      'method'   => 'replace',
+      'prevent'  => 'click'
+    ),
+  );
+
+  $form['#prefix'] = '<div id="tripal-views-handler-area-collections">';
+  $form['#suffix'] = '</div>';
+
+  return $form;
+}
+
+/**
+ * Views Area Handler Form SUBMIT: Tripal Collections on Drupal Search API-based Views
+ */
+function tripal_views_handler_area_collections_search_api_form_submit($form, $form_state) {
+  global $user;
+
+  $view = $form_state['values']['view'];
+  $query = $form_state['values']['query'];
+  $collection_name = trim($form_state['values']['collection_name']);
+  $description = $form_state['values']['collection_desc'];
+  $uid = $user->uid;
+
+  // @todo implment creating the collection
+  // This can't be done via tripal_create_collection() since we support more
+  // then a single bundle. Instead we are going to use the controller class directly.
+  /* @code
+    $collection = new TripalEntityCollection();
+    $collection->create($details);
+    $collection_id = $collection->getCollectionID();
+    $collection->addBundle($details);
+
+    drupal_set_message(t("Collection '%name' created with %num_recs record(s). Check the !view to generate file links.",
+      array(
+        '%name' => $details['collection_name'],
+        '%num_recs' => count($details['ids']),
+        '!view' => l('data collections page', 'user/' . $user->uid . '/data-collections'),
+      ))
+    );
+    */
+
+  /* How it was implemented for tripal entities.
+  // Get the fields that have been selected
+  $selected_fids = array();
+  foreach ($form_state['values'] as $key => $value) {
+    $matches = array();
+    if (preg_match('/select-(\d+)/', $key, $matches)) {
+      if ($value == 1) {
+        $selected_fids[] = $matches[1];
+      }
+    }
+  }
+
+  // Get the entity Ids that match results
+  $query->range['length'] = $view->total_rows;
+  $results = $query->execute();
+  $entities = array();
+  foreach ($results['TripalEntity'] as $entity) {
+    $entities[] = $entity->id;
+  }
+  $collection = tripal_create_collection(array(
+    'uid'  => $uid,
+    'collection_name' => $collection_name,
+    'description'  => $description,
+    'bundle_name' => $bundle_name,
+    'ids' => $entities,
+    'fields' => $selected_fids,
+  ));
+  */
+}