tripal_views.views.inc 20 KB

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