tripal_views.views.inc 21 KB

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