tripal_views.views.inc 21 KB

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