tripal_views_handler_area_collections.inc

File

tripal/views_handlers/tripal_views_handler_area_collections.inc
View source
  1. <?php
  2. class tripal_views_handler_area_collections extends views_handler_area_result {
  3. function options_form(&$form, &$form_state) {
  4. // We have no options so we have to implement this function with
  5. // nothing in it.
  6. }
  7. /**
  8. * Implements views_handler_area_result::render().
  9. */
  10. function render($empty = FALSE) {
  11. // If collections are dispabled then don't show anything.
  12. $collections_enabled = variable_get('tripal_data_collections_enabled', 1);
  13. if (!$collections_enabled) {
  14. return '';
  15. }
  16. // This will only work with Tripal content types and the tripal_views_query
  17. // plugin. So don't show anything for others.
  18. if ($this->query->plugin_name != 'tripal_views_query') {
  19. return '';
  20. }
  21. $form = drupal_get_form('tripal_views_handler_area_collections_form', $this->view, $this->query);
  22. return drupal_render($form);
  23. }
  24. }
  25. /**
  26. *
  27. */
  28. function tripal_views_handler_area_collections_form($form, $form_state, $view, $query) {
  29. // Set form defaults.
  30. $collection_name = '';
  31. $collection_desc = '';
  32. // Get the bundle for this query.
  33. $matches = array();
  34. preg_match('/^(.+?)__(.+?)$/', $view->base_table, $matches);
  35. $vocabulary = $matches[1];
  36. $accession = $matches[2];
  37. $term = tripal_load_term_entity(array('vocabulary' => $vocabulary, 'accession' => $accession));
  38. $bundle = tripal_load_bundle_entity(array('term_id' => $term->id));
  39. $form = array();
  40. $form['save_collection'] = array(
  41. '#type' => 'fieldset',
  42. '#title' => t('Save Results'),
  43. '#collapsible' => TRUE,
  44. '#collapsed' => TRUE,
  45. '#description' => t('A data collection is a virtual container into which you can
  46. save data. You can place your search results into a data collection for
  47. download or use with other tools on this site that support data collections.'),
  48. );
  49. $form['save_collection']['bundle'] = array(
  50. '#type' => 'value',
  51. '#value' => $bundle,
  52. );
  53. $form['save_collection']['view'] = array(
  54. '#type' => 'value',
  55. '#value' => unserialize(serialize($view))
  56. );
  57. $form['save_collection']['query'] = array(
  58. '#type' => 'value',
  59. '#value' => unserialize(serialize($query->query))
  60. );
  61. $form['save_collection']['summary'] = array(
  62. '#type' => 'item',
  63. '#title' => 'Results Summary',
  64. '#markup' => t('There are @total_rows record(s) that can be added to a data collection.', array('@total_rows' => $view->total_rows)),
  65. );
  66. $form['save_collection']['collection_name'] = array(
  67. '#type' => 'textfield',
  68. '#title' => t('Collection Name'),
  69. '#description' => t('Please name this collection for future reference.'),
  70. '#default_value' => $collection_name,
  71. '#required' => TRUE,
  72. );
  73. $form['save_collection']['description_fset'] = array(
  74. '#type' => 'fieldset',
  75. '#title' => t('Add a Description'),
  76. '#collapsible' => TRUE,
  77. '#collapsed' => TRUE,
  78. );
  79. $form['save_collection']['description_fset']['collection_desc'] = array(
  80. '#type' => 'textarea',
  81. '#title' => t('Description'),
  82. '#description' => t('Please provide a description about this data collection. This is meant to help you remember what is in the collection.'),
  83. '#default_value' => $collection_name,
  84. );
  85. // Get the list of fields used in the view.
  86. $current_display = $view->current_display;
  87. if (array_key_exists('fields', $view->display[$current_display]->display_options)) {
  88. $view_fields = $view->display[$current_display]->display_options['fields'];
  89. }
  90. else {
  91. $view_fields = $view->display['default']->display_options['fields'];
  92. }
  93. $form['save_collection']['fields'] = array(
  94. '#type' => 'fieldset',
  95. '#title' => t('Add or Update Fields'),
  96. '#description' => t('You may select any of the additional fields below to
  97. add to this data collection. Please note that different fields may be able
  98. to create different output file types.'),
  99. '#collapsible' => TRUE,
  100. '#collapsed' => TRUE,
  101. );
  102. // We want to theme all of the fields, so we add this next level in the
  103. // form array to theme.
  104. $form['save_collection']['fields']['items'] = array(
  105. '#theme' => 'tripal_views_handler_area_collections_fields_fset'
  106. );
  107. // Get the list of fields in this view.
  108. $field_ids = array();
  109. $defaults = array();
  110. $fields = field_info_instances('TripalEntity', $bundle->name);
  111. foreach ($fields as $field_name => $instance) {
  112. $field = field_info_field($field_name);
  113. $field_type = $field['type'];
  114. if ($instance['field_name'] == 'entity_id') {
  115. continue;
  116. }
  117. // Skip hidden fields.
  118. if ($instance['display']['default']['type'] == 'hidden') {
  119. continue;
  120. }
  121. $field_label = $instance['label'];
  122. // Add in in any non CSV or Tab formatters to the label.
  123. $formatters = array();
  124. $field_formatters = tripal_get_field_field_formatters($field, $instance);
  125. foreach ($field_formatters as $class_name => $label) {
  126. tripal_load_include_downloader_class($class_name);
  127. $formatters[] = $class_name::$label;
  128. }
  129. // Add the field to those supported.
  130. $field_ids[$instance['field_id']]= $field_label;
  131. // Automatically check fields that are in the view and not excluded.
  132. $checked = FALSE;
  133. if (array_key_exists($field_name, $view_fields)) {
  134. if (array_key_exists('exclude', $view_fields[$field_name]) and
  135. $view_fields[$field_name]['exclude'] == TRUE) {
  136. continue;
  137. }
  138. $checked = TRUE;
  139. }
  140. $form['save_collection']['fields']['items'] ['select-' . $instance['field_id']] = array(
  141. '#type' => 'checkbox',
  142. '#title' => $field_label,
  143. '#default_value' => $checked,
  144. );
  145. $form['save_collection']['fields']['items'] ['description-' . $instance['field_id']] = array(
  146. '#type' => 'markup',
  147. '#markup' => $instance['description']
  148. );
  149. $form['save_collection']['fields']['items'] ['formatters-' . $instance['field_id']] = array(
  150. '#type' => 'markup',
  151. '#markup' => join(', ', $formatters)
  152. );
  153. }
  154. $form['save_collection']['button'] = array(
  155. '#type' => 'submit',
  156. '#value' => 'Save Data Collection',
  157. '#name' => 'save_collection',
  158. '#ajax' => array(
  159. 'callback' => "tripal_views_handler_area_collections_form_ajax",
  160. 'wrapper' => 'tripal-views-handler-area-collections',
  161. 'effect' => 'fade',
  162. 'method' => 'replace',
  163. 'prevent' => 'click'
  164. ),
  165. );
  166. $form['#prefix'] = '<div id="tripal-views-handler-area-collections">';
  167. $form['#suffix'] = '</div>';
  168. return $form;
  169. }
  170. /**
  171. * Theme the fields section of the tripal_views_handler_area_collections form.
  172. *
  173. * @ingroup tripal_pub
  174. */
  175. function theme_tripal_views_handler_area_collections_fields_fset($variables) {
  176. $form = $variables['form'];
  177. // Organize the elements by the same field id
  178. $fields = array();
  179. $order = array();
  180. $children = element_children($form);
  181. foreach ($children as $key) {
  182. list($item, $field_id) = preg_split('/-/', $key);
  183. $fields[$field_id][$item] = $form[$key];
  184. if (!in_array($field_id, $order)) {
  185. $order[] = $field_id;
  186. }
  187. }
  188. // Next create a table with each field in a different row.
  189. $headers = array('Field', 'Description', 'Supported Files Types');
  190. $rows = array();
  191. foreach ($order as $field_id) {
  192. $rows[] = array(
  193. drupal_render($fields[$field_id]['select']),
  194. drupal_render($fields[$field_id]['description']),
  195. drupal_render($fields[$field_id]['formatters'])
  196. );
  197. }
  198. $table = array(
  199. 'header' => $headers,
  200. 'rows' => $rows,
  201. 'attributes' => array(),
  202. 'sticky' => TRUE,
  203. 'caption' => '',
  204. 'colgroups' => array(),
  205. 'empty' => '',
  206. );
  207. return theme_table($table);
  208. }
  209. /**
  210. *
  211. */
  212. function tripal_views_handler_area_collections_form_ajax($form, $form_state) {
  213. return $form;
  214. }
  215. /**
  216. *
  217. */
  218. function tripal_views_handler_area_collections_form_submit($form, $form_state) {
  219. global $user;
  220. $bundle = $form_state['values']['bundle'];
  221. $view = $form_state['values']['view'];
  222. $query = $form_state['values']['query'];
  223. $collection_name = trim($form_state['values']['collection_name']);
  224. $description = $form_state['values']['collection_desc'];
  225. $field_ids = array_key_exists('field_ids', $form_state['values']) ? $form_state['values']['field_ids'] : array();
  226. $uid = $user->uid;
  227. $bundle_name = $bundle->name;
  228. // Get the fields that have been selected
  229. $selected_fids = array();
  230. foreach ($form_state['values'] as $key => $value) {
  231. $matches = array();
  232. if (preg_match('/select-(\d+)/', $key, $matches)) {
  233. if ($value == 1) {
  234. $selected_fids[] = $matches[1];
  235. }
  236. }
  237. }
  238. // Get the entity Ids that match results
  239. $query->range['length'] = $view->total_rows;
  240. $results = $query->execute();
  241. $entities = array();
  242. foreach ($results['TripalEntity'] as $entity) {
  243. $entities[] = $entity->id;
  244. }
  245. $collection = tripal_create_collection(array(
  246. 'uid' => $uid,
  247. 'collection_name' => $collection_name,
  248. 'description' => $description,
  249. 'bundle_name' => $bundle_name,
  250. 'ids' => $entities,
  251. 'fields' => $selected_fids,
  252. ));
  253. }