tripal_views.views.inc 21 KB

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