tripal_views.views.inc 19 KB

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