tripal_views.views.inc 20 KB

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