tripal_views.views.inc 20 KB

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