tripal_views.views.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. <?php
  2. include('views/handlers/views_handler_join_chado_through_linking.inc');
  3. include('views/handlers/views_handler_join_chado_aggregator.inc');
  4. include('api/tripal_views.api.inc');
  5. // TEMPORARY
  6. include('views/chado_linking.TMP.inc');
  7. /**
  8. * @file
  9. * Tripal Views Integration
  10. *
  11. * @defgroup views Views Integration
  12. * @{
  13. * Provide rules for formatting and composition of fields
  14. * @}
  15. *
  16. * @defgroup views_field_handlers Views Field Handlers
  17. * @{
  18. * Provide rules for formatting and composition of fields
  19. * @}
  20. * @ingroup views
  21. *
  22. * @defgroup views_filter_handlers Views Filter Handlers
  23. * @{
  24. * Provide the ability to filter based on specified data
  25. * @}
  26. * @ingroup views
  27. *
  28. * @defgroup views_sort_handlers Views Sort Handlers
  29. * @{
  30. * Provide methods describing how specific data should be sorted
  31. * @}
  32. * @ingroup views
  33. *
  34. * @defgroup views_argument_handlers Views Arguement Handlers
  35. * @{
  36. * Provide the ability to filter pased on arguments in the path of the view
  37. * @}
  38. * @ingroup views
  39. */
  40. /**
  41. * Implements hook_views_handlers()
  42. *
  43. * Purpose: Register all custom handlers with views
  44. * where a handler describes either "the type of field",
  45. * "how a field should be filtered", "how a field should be sorted"
  46. *
  47. * @return
  48. * An array of handler definitions
  49. *
  50. * @ingroup tripal_views
  51. */
  52. function tripal_views_views_handlers() {
  53. return array(
  54. 'info' => array(
  55. 'path' => drupal_get_path('module', 'tripal_views') . '/views/handlers',
  56. ),
  57. 'handlers' => array(
  58. // Custom Tripal Filter Handlers
  59. 'tripal_views_handler_filter_file_upload' => array(
  60. 'parent' => 'views_handler_filter',
  61. ),
  62. 'tripal_views_handler_filter_textarea' => array(
  63. 'parent' => 'views_handler_filter',
  64. ),
  65. 'tripal_views_handler_filter_no_results' => array(
  66. 'parent' => 'views_handler_filter'
  67. ),
  68. 'tripal_views_handler_filter_select_cvterm' => array(
  69. 'parent' => 'views_handler_filter_string',
  70. ),
  71. 'tripal_views_handler_filter_select_string' => array(
  72. 'parent' => 'chado_views_handler_filter_string',
  73. ),
  74. 'tripal_views_handler_filter_sequence' => array(
  75. 'parent' => 'chado_views_handler_filter_string',
  76. ),
  77. // Custom Tripal Field Handlers
  78. 'tripal_views_handler_field_aggregate' => array(
  79. 'parent' => 'chado_views_handler_field',
  80. ),
  81. 'tripal_views_handler_field_sequence' => array(
  82. 'parent' => 'chado_views_handler_field',
  83. ),
  84. // Join Handlers
  85. 'views_handler_join_chado_aggregator' => array(
  86. 'parent' => 'views_join',
  87. ),
  88. 'views_handler_join_chado_through_linking' => array(
  89. 'parent' => 'views_join',
  90. ),
  91. // Relationship Handlers
  92. 'chado_views_handler_relationship' => array(
  93. 'parent' => 'views_handler_relationship'
  94. ),
  95. 'chado_views_handler_relationship_to_node' => array(
  96. 'parent' => 'views_handler_relationship'
  97. ),
  98. // Wrappers for Default Views Handlers-----
  99. // Field Handlers
  100. 'chado_views_handler_field' => array(
  101. 'parent' => 'views_handler_field'
  102. ),
  103. 'chado_views_handler_field_boolean' => array(
  104. 'parent' => 'views_handler_field_boolean'
  105. ),
  106. 'chado_views_handler_field_counter' => array(
  107. 'parent' => 'views_handler_field_counter'
  108. ),
  109. 'chado_views_handler_field_custom' => array(
  110. 'parent' => 'views_handler_field_custom'
  111. ),
  112. 'chado_views_handler_field_date' => array(
  113. 'parent' => 'views_handler_field_date'
  114. ),
  115. 'chado_views_handler_field_markup' => array(
  116. 'parent' => 'views_handler_field_markup'
  117. ),
  118. 'chado_views_handler_field_math' => array(
  119. 'parent' => 'views_handler_field_math'
  120. ),
  121. 'chado_views_handler_field_numeric' => array(
  122. 'parent' => 'views_handler_field_numeric'
  123. ),
  124. // Filter Handlers
  125. 'chado_views_handler_filter_string' => array(
  126. 'parent' => 'views_handler_filter_string',
  127. ),
  128. 'chado_views_handler_filter_boolean_operator_string' => array(
  129. 'parent' => 'views_handler_filter_boolean_operator_string',
  130. ),
  131. 'chado_views_handler_filter_boolean_operator' => array(
  132. 'parent' => 'views_handler_filter_boolean_operator',
  133. ),
  134. 'chado_views_handler_filter_date' => array(
  135. 'parent' => 'views_handler_filter_date',
  136. ),
  137. 'chado_views_handler_filter_equality' => array(
  138. 'parent' => 'views_handler_filter_equality',
  139. ),
  140. 'chado_views_handler_filter_float' => array(
  141. 'parent' => 'views_handler_filter_float',
  142. ),
  143. 'chado_views_handler_filter_numeric' => array(
  144. 'parent' => 'views_handler_filter_numeric',
  145. ),
  146. // Sort Handlers
  147. 'chado_views_handler_sort' => array(
  148. 'parent' => 'views_handler_sort'
  149. ),
  150. 'chado_views_handler_sort_date' => array(
  151. 'parent' => 'views_handler_sort_date'
  152. ),
  153. 'chado_views_handler_sort_formula' => array(
  154. 'parent' => 'views_handler_sort_formula'
  155. ),
  156. 'chado_views_handler_sort_menu_hierarchy' => array(
  157. 'parent' => 'views_handler_sort_menu_hierarchy'
  158. ),
  159. 'chado_views_handler_sort_random' => array(
  160. 'parent' => 'views_handler_sort_random'
  161. ),
  162. ),
  163. );
  164. }
  165. /**
  166. * Implements hook_views_pre_render
  167. *
  168. * Purpose: Intercepts the view after the query has been executed
  169. * All the results are stored in $view->result
  170. * Looking up the NID here ensures the query is only executed once
  171. * for all stocks in the table.
  172. *
  173. * @todo add if !<chado/drupal same db> around NID portion
  174. *
  175. * @ingroup tripal_views
  176. */
  177. function tripal_views_views_pre_render(&$view) {
  178. // We need to unset the exposed_input for the view so we can repopulate that
  179. // variable. This is necessary if we're using the file_upload_combo
  180. // custom form element which adds the file_path variable to the $_GET after the
  181. // view has populated the $view->exposed_input variable
  182. unset($view->exposed_input);
  183. // we want to add to the bottom of the views the form for downloading
  184. // results in other formats (e.g. Excel, FASTA, CSV, etc.). The Views Data
  185. // Export module provides small images at the bottom, but we want to provide
  186. // a more intutitive interface for getting different file formats
  187. $form = drupal_get_form('tripal_views_data_export_download_form', $view, $display_id, $args);
  188. $view->attachment_after = $form;
  189. }
  190. /**
  191. * Generates a dynamic data array for Views
  192. *
  193. * Purpose: This function is a hook used by the Views module. It populates and
  194. * returns a data array that specifies for the Views module the base table,
  195. * the tables it joins with and handlers. The data array is populated
  196. * using the data stored in the tripal_views tables.
  197. *
  198. * @return a data array formatted for the Views module
  199. *
  200. * @ingroup tripal_views
  201. */
  202. function tripal_views_views_data() {
  203. // Make sure all chado tables are integrated
  204. tripal_views_integrate_all_chado_tables();
  205. // Define Global Fields
  206. // Filter handler that lets the admin say:
  207. // "Show no results until they enter search parameters"
  208. $data['views']['search_results'] = array(
  209. 'title' => t('Search Results'),
  210. 'help' => t('Delay results until Apply/Search is clicked by the user.'),
  211. 'filter' => array(
  212. 'handler' => 'tripal_views_handler_filter_no_results',
  213. ),
  214. );
  215. $tvi_query = db_query('SELECT * FROM {tripal_views}');
  216. while ($tvi_row = db_fetch_object($tvi_query)) {
  217. // check to see if this is the lightest (drupal-style) priority setup for this table
  218. // if not then don't use this definition
  219. $lightest_priority_setup = tripal_views_is_lightest_priority_setup($tvi_row->setup_id, $tvi_row->table_name);
  220. if (!$lightest_priority_setup) {
  221. continue;
  222. }
  223. // ids we'll use for queries
  224. $setup_id = $tvi_row->setup_id;
  225. $mview_id = $tvi_row->mview_id;
  226. // holds the base table name and fields
  227. $base_table = '';
  228. $base_fields = array();
  229. // indicated whether the current table is a base table or not
  230. $is_base_table = $tvi_row->base_table;
  231. // populate the base table name and fields. If an $mview_id is given
  232. // then get the materialized view info, otherwise get the Chado table info
  233. if ($mview_id) {
  234. // get the base table name from the materialized view
  235. $sql = "SELECT name, mv_specs FROM {tripal_mviews} WHERE mview_id = %d";
  236. $mview_table = db_fetch_object(db_query($sql, $mview_id));
  237. $base_table = $mview_table->name;
  238. // get the columns in this materialized view. They are separated by commas
  239. // where the first word is the column name and the rest is the type
  240. $columns = explode(",", $mview_table->mv_specs);
  241. foreach ($columns as $column) {
  242. $column = trim($column); // trim trailing and leading spaces
  243. preg_match("/^(.*?)\ (.*?)$/", $column, $matches);
  244. $column_name = $matches[1];
  245. $column_type = $matches[2];
  246. $base_fields[$column_name] = array(
  247. 'column_name' => $column_name,
  248. 'type' => $column_type,
  249. );
  250. }
  251. // get the field name and descriptions
  252. $sql = "SELECT * FROM {tripal_views_field} WHERE setup_id=%d";
  253. $query = db_query($sql, $setup_id);
  254. while ($field = db_fetch_object($query)) {
  255. $base_fields[$field->column_name]['name'] = $field->name;
  256. $base_fields[$field->column_name]['help'] = $field->description;
  257. }
  258. }
  259. // if this is not a legacy materialized view
  260. else {
  261. $base_table = $tvi_row->table_name;
  262. // Check if we are trying to integrate the node table
  263. // if we are we want to add to a current integration
  264. // as compared to create a whole new one
  265. if ($base_table == 'node') {
  266. // Add any joins between the node table and other tables
  267. $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = %d";
  268. $joins = db_query($sql, $setup_id);
  269. while ($join = db_fetch_object($joins)) {
  270. $left_table = $join->left_table;
  271. $left_field = $join->left_field;
  272. $base_field = $join->base_field;
  273. $handler = $join->handler;
  274. // add join entry
  275. $data[$base_table]['table']['join'][$left_table] = array(
  276. 'left_field' => $left_field,
  277. 'field' => $base_field,
  278. );
  279. if ($handler) {
  280. $data[$base_table]['table']['join'][$left_table]['handler'] = $handler;
  281. }
  282. }
  283. // Add in any relationships for node fields
  284. $sql = "SELECT * FROM {tripal_views_handlers} WHERE setup_id=%d";
  285. $query = db_query($sql, $setup_id);
  286. while ($handler = db_fetch_object($query)) {
  287. $data[$base_table][$handler->column_name][$handler->handler_type]['handler'] = $handler->handler_name;
  288. // Add in any additional arguments
  289. // This should be a serialized array including (at a minimum) name => <handler name>
  290. if ($handler->arguments) {
  291. $data[$base_table][$handler->column_name][$handler->handler_type] = array_merge($data[$base_table][$handler->column_name][$handler->handler_type], unserialize($handler->arguments));
  292. }
  293. }
  294. continue;
  295. }
  296. // get the table description
  297. $table_desc = tripal_core_get_chado_table_schema($base_table);
  298. $fields = $table_desc['fields'];
  299. if (!is_array($fields)) {
  300. $fields = array();
  301. watchdog('tripal_views', 'No fields were described for this table (%table) through the Tripal Schema API.', array('%table' => $base_table), WATCHDOG_ERROR);
  302. }
  303. foreach ($fields as $column => $attrs) {
  304. $base_fields[$column] = array(
  305. 'column_name' => $column,
  306. 'type' => $attrs['type'],
  307. );
  308. }
  309. // get the field name and descriptions
  310. $sql = "SELECT * FROM {tripal_views_field} WHERE setup_id=%d";
  311. $query = db_query($sql, $setup_id);
  312. while ($field = db_fetch_object($query)) {
  313. $base_fields[$field->column_name]['name'] = $field->name;
  314. $base_fields[$field->column_name]['help'] = $field->description;
  315. }
  316. }
  317. // Setup the base table info in the data array
  318. $data[$base_table]['table']['group'] = t("$tvi_row->name");
  319. if ($is_base_table) {
  320. $data[$base_table]['table']['base'] = array(
  321. 'group' => "$tvi_row->name",
  322. 'title' => "$tvi_row->name",
  323. 'help' => $tvi_row->comment,
  324. );
  325. }
  326. else {
  327. $data[$base_table]['table'] = array(
  328. 'group' => "$tvi_row->name",
  329. 'title' => "$tvi_row->name",
  330. 'help' => $tvi_row->comment,
  331. );
  332. }
  333. // first add the fields
  334. foreach ($base_fields as $column_name => $base_field) {
  335. $data[$base_table][$column_name] = array(
  336. 'title' => t($base_field['name']),
  337. 'help' => t($base_field['help']),
  338. 'field' => array(
  339. 'click sortable' => TRUE,
  340. ),
  341. );
  342. // now add the handlers
  343. $sql = "SELECT * FROM {tripal_views_handlers} WHERE setup_id = %d AND column_name = '%s'";
  344. $handlers = db_query($sql, $setup_id, $column_name);
  345. while ($handler = db_fetch_object($handlers)) {
  346. $data[$base_table][$column_name][$handler->handler_type]['handler'] = $handler->handler_name;
  347. // Add in any additional arguments
  348. // This should be a serialized array including (at a minimum) name => <handler name>
  349. if ($handler->arguments) {
  350. $data[$base_table][$column_name][$handler->handler_type] = array_merge($data[$base_table][$column_name][$handler->handler_type], unserialize($handler->arguments));
  351. }
  352. };
  353. }
  354. // now add the joins
  355. $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = %d";
  356. $joins = db_query($sql, $setup_id);
  357. if (!isset($joins)) {
  358. $joins = array();
  359. }
  360. while ($join = db_fetch_object($joins)) {
  361. $left_table = $join->left_table;
  362. $left_field = $join->left_field;
  363. $base_field = $join->base_field;
  364. $handler = $join->handler;
  365. // add join entry
  366. $data[$base_table]['table']['join'][$left_table] = array(
  367. 'left_field' => $left_field,
  368. 'field' => $base_field,
  369. );
  370. if ($handler) {
  371. $data[$base_table]['table']['join'][$left_table]['handler'] = $handler;
  372. }
  373. }
  374. }
  375. // TEMPORARY: needed to join chado base tables to node linking tables
  376. // currently done using old-style data arrays
  377. //$data = tripal_views_TEMPORARY_chado_linking_data($data);
  378. return $data;
  379. }
  380. /**
  381. *
  382. * @ingroup tripal_views
  383. */
  384. function tripal_views_views_data_alter(&$data) {
  385. $tvi_query = db_query('SELECT * FROM {tripal_views}');
  386. // iterate through the views that we manage
  387. while ($tvi_row = db_fetch_object($tvi_query)) {
  388. //ids we'll use for queries
  389. $mview_id = $tvi_row->mview_id;
  390. $setup_id = $tvi_row->setup_id;
  391. // iterate through the columns and alter the existing data array for
  392. // joins to other tables
  393. $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = %d";
  394. $joins = db_query($sql, $setup_id);
  395. while ($join = db_fetch_object($joins)) {
  396. $left_table = $join->left_table;
  397. $left_field = $join->left_field;
  398. $base_field = $join->base_field;
  399. $base_table = $join->base_table;
  400. // add the recipricol join entries for each column
  401. if (array_key_exists($left_table, $data)) {
  402. $data[$left_table]['table']['join'][$base_table] = array(
  403. 'left_field' => $base_field,
  404. 'field' => $left_field,
  405. );
  406. }
  407. }
  408. }
  409. return $data;
  410. }
  411. /**
  412. * Implementation of hook_views_plugins().
  413. */
  414. function tripal_views_views_plugins() {
  415. $tc_path = drupal_get_path('module', 'tripal_views');
  416. $style_defaults = array(
  417. 'path' => $tc_path . '/views_data_export/plugins',
  418. 'parent' => 'views_data_export',
  419. 'theme' => 'views_data_export',
  420. 'theme path' => $tc_path . '/views_data_export/theme',
  421. 'theme file' => 'tripal_views_data_export.theme.inc',
  422. 'uses row plugin' => FALSE,
  423. 'uses fields' => TRUE,
  424. 'uses options' => TRUE,
  425. 'type' => 'data_export',
  426. );
  427. // add FASTA format as a new style for the existing views_export_data Display
  428. return array(
  429. 'style' => array(
  430. 'views_data_export_fasta' => array(
  431. 'title' => t('FASTA file'),
  432. 'help' => t('Display results in FASTA format. All fields in results are on the definition line while the feature.residues field must be present .'),
  433. 'handler' => 'tripal_views_plugin_style_export_fasta',
  434. // Views Data Export element that will be used to set additional headers when serving the feed.
  435. 'export headers' => array('Content-type: text/plain; charset=utf-8'),
  436. // Views Data Export element mostly used for creating some additional classes and template names.
  437. 'export feed type' => 'fasta',
  438. 'export feed text' => 'FASTA',
  439. 'export feed file' => '%view.fna',
  440. 'export feed icon' => $tc_path . '/views_data_export/images/fasta.png',
  441. 'additional themes' => array(
  442. 'views_data_export_fasta_header' => 'style',
  443. 'views_data_export_fasta_body' => 'style',
  444. 'views_data_export_fasta_footer' => 'style',
  445. ),
  446. 'additional themes base' => 'views_data_export_fasta',
  447. ) + $style_defaults,
  448. ),
  449. );
  450. }
  451. /**
  452. * Implementation of hook_views_pre_view().
  453. */
  454. function tripal_views_views_pre_view(&$view, &$display_id, &$args) {
  455. // merge the $_GET and $_POST into the $_GET. This is because
  456. // Views and Views Data Export modules only uses the $_GET variable but
  457. // file uploads require $_POST. We need to make sure these two modules
  458. // have access to everything needed for this view to work properlys
  459. $_GET = array_merge($_GET, $_POST);
  460. }
  461. /**
  462. * Implementation of hook_views_pre_build().
  463. */
  464. /* function tripal_views_views_pre_render(&$view, &$display_id, &$args){
  465. // we want to add to the bottom of the views the form for downloading
  466. // results in other formats (e.g. Excel, FASTA, CSV, etc.). The Views Data
  467. // Export module provides small images at the bottom, but we want to provide
  468. // a more intutitive interface for getting different file formats
  469. $form = drupal_get_form('tripal_views_data_export_download_form',$view,$display_id,$args);
  470. $view->attachment_after = $form;
  471. }*/
  472. /**
  473. * the Download Views data export form
  474. */
  475. function tripal_views_data_export_download_form(&$form_state, $view, $display_id, $args) {
  476. $form = array();
  477. $urls = array();
  478. // get any export_data_export displays
  479. $displays = $view->display;
  480. $options = array();
  481. $default = '';
  482. $current_display = $view->current_display;
  483. foreach ($displays as $name => $display) {
  484. if (preg_match("/^views_data_export/", $name)) {
  485. // only add this display to the form if it is attached
  486. $display_options = $display->display_options;
  487. if (strcmp($display_options['displays'][$current_display], $current_display) != 0) {
  488. continue;
  489. }
  490. // set the first item as default
  491. if (!$default) {
  492. $default = $display->id;
  493. }
  494. $path = $display->display_options['path'];
  495. $query = $view->get_exposed_input(); // retrieves elements in $_GET array
  496. $urls[$display->id]['path'] = $path;
  497. $urls[$display->id]['query'] = $query;
  498. // add the new item to the options array
  499. $options[$display->id] = $display->display_title;
  500. }
  501. }
  502. // only generate the form if we have views_data_export displays attached
  503. // to this view
  504. if (count($options) > 0) {
  505. $form_state['storage']['urls'] = $urls;
  506. $form['#cache'] = TRUE;
  507. // we want the form to redirect to a new window
  508. $form['#attributes']['target'] = "_blank";
  509. // now build the form elements
  510. $form['downloads'] = array(
  511. '#type' => 'fieldset',
  512. '#title' => 'Download Results',
  513. '#collapsible' => TRUE,
  514. '#collapsed' => FALSE
  515. );
  516. $form['downloads']['file_type'] = array(
  517. '#title' => t('File format'),
  518. '#type' => 'radios',
  519. '#options' => $options,
  520. '#required' => TRUE,
  521. '#default_value' => $default,
  522. '#description' => t('Please select a file format to download'),
  523. );
  524. $form['downloads']['submit'] = array(
  525. '#value' => t('Download Results'),
  526. '#type' => 'submit',
  527. );
  528. }
  529. return $form;
  530. }
  531. /**
  532. * Submit for the Download Views data export form
  533. */
  534. function tripal_views_data_export_download_form_submit($form, &$form_state) {
  535. $urls = $form_state['storage']['urls'];
  536. $display_id = $form_state['values']['file_type'];
  537. drupal_goto($urls[$display_id]['path'], $urls[$display_id]['query']);
  538. }