tripal_views.views.inc 20 KB

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