tripal_views.views.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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. $data[$base_table][$column_name] = array(
  214. 'title' => t($base_field['name']),
  215. 'help' => t($base_field['help']),
  216. 'field' => array(
  217. 'click sortable' => TRUE,
  218. ),
  219. );
  220. // now add the handlers
  221. $sql = "SELECT * FROM {tripal_views_handlers} WHERE setup_id = :setup AND column_name = :column";
  222. $handlers = db_query($sql, array(':setup' => $setup_id, ':column' => $column_name));
  223. foreach ($handlers as $handler) {
  224. $data[$base_table][$column_name][$handler->handler_type]['handler'] = $handler->handler_name;
  225. // Add in any additional arguments
  226. // This should be a serialized array including (at a minimum) name => <handler name>
  227. if ($handler->arguments) {
  228. $data[$base_table][$column_name][$handler->handler_type] = array_merge($data[$base_table][$column_name][$handler->handler_type], unserialize($handler->arguments));
  229. }
  230. };
  231. }
  232. // ADD JOINS & RELATIONSHIPS TO DATA ARRAY
  233. // only add joins if this is a base table
  234. // this keeps the list of available fields manageable
  235. // all other tables should be added via relationships
  236. $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = :setup";
  237. $joins = db_query($sql, array(':setup' => $setup_id));
  238. if (!isset($joins)) {
  239. $joins = array();
  240. }
  241. foreach ($joins as $join) {
  242. $left_table = $join->left_table;
  243. $left_field = $join->left_field;
  244. $base_table = $join->base_table;
  245. $base_field = $join->base_field;
  246. $handler = $join->handler;
  247. $base_title = ucwords(str_replace('_', ' ', $base_table));
  248. $left_title = ucwords(str_replace('_', ' ', $left_table));
  249. // if the 'node' table is in our integrated list then
  250. // we want to skip it. It shouldn't be there.
  251. if ($left_table == 'node') {
  252. continue;
  253. }
  254. // add join entry
  255. if (!$join->relationship_only) {
  256. $data[$left_table]['table']['join'][$base_table] = array(
  257. 'left_field' => $base_field,
  258. 'field' => $left_table . '_id',
  259. );
  260. if ($handler) {
  261. $data[$left_table]['table']['join'][$base_table]['handler'] = $handler;
  262. }
  263. if (!empty($join->arguments)) {
  264. array_merge($data[$left_table]['table']['join'][$base_table], unserialize($join->arguments));
  265. }
  266. }
  267. // warn if deprecated method of relationship addition was used (ie: through handlers)
  268. if (isset($data[$base_table][$base_field]['relationship'])) {
  269. tripal_report_error('tripal_views', TRIPAL_NOTICE,
  270. 'DEPRECATED: Currently using tripal_views_handlers to store relationship for %base => %left when you should be using tripal_views_joins.',
  271. array('%base' => $base_table, '%left' => $left_table));
  272. }
  273. // add relationship entry
  274. $fake_field = $base_field . '_to_' . $left_table;
  275. $data[$base_table][$fake_field] = array(
  276. 'title' => "$base_title.$base_field => $left_title",
  277. 'help' => "Joins $base_title.$base_field to $left_title",
  278. 'relationship' => array(
  279. 'handler' => $join->relationship_handler,
  280. 'title' => t("$base_field => $left_title"),
  281. 'label' => t("$base_field => $left_title"),
  282. 'real field' => $base_field,
  283. 'base' => $left_table,
  284. 'base field' => $left_field
  285. )
  286. );
  287. if (!empty($join->arguments)) {
  288. array_merge($data[$base_table][$fake_field]['relationship'], unserialize($join->arguments));
  289. }
  290. }
  291. }
  292. return $data;
  293. }
  294. /**
  295. * Describes the tripal views integration tables to views for the administration views
  296. *
  297. * @ingroup tripal_views
  298. */
  299. function tripal_views_views_data_tripal_views_tables($data) {
  300. $data['tripal_views']['table']['group'] = t('Tripal Views Integration');
  301. $data['tripal_views']['table']['base'] = array(
  302. 'field' => 'setup_id', // This is the identifier field for the view.
  303. 'title' => t('Tripal Views Integration'),
  304. 'help' => t('Specifications on how to integrate various tables with Drupal Views'),
  305. 'weight' => -10,
  306. );
  307. // Implicit Join to Materialized Views
  308. $data['tripal_views']['table']['join'] = array(
  309. 'tripal_mviews' => array(
  310. 'left_field' => 'mview_id',
  311. 'field' => 'mview_id',
  312. ),
  313. );
  314. // setup_id
  315. $data['tripal_views']['setup_id'] = array(
  316. 'title' => t('Setup ID'),
  317. 'help' => t('Primary key of the integration'),
  318. 'field' => array(
  319. 'handler' => 'views_handler_field_numeric',
  320. 'click sortable' => TRUE,
  321. ),
  322. 'filter' => array(
  323. 'handler' => 'views_handler_filter_numeric',
  324. ),
  325. 'sort' => array(
  326. 'handler' => 'views_handler_sort',
  327. ),
  328. );
  329. // mview_id
  330. $data['tripal_views']['mview_id'] = array(
  331. 'title' => t('Materialized View ID'),
  332. 'help' => t('The primary key of the Mview integrated.'),
  333. 'field' => array(
  334. 'handler' => 'views_handler_field_numeric',
  335. 'click sortable' => TRUE,
  336. ),
  337. 'filter' => array(
  338. 'handler' => 'views_handler_filter_numeric',
  339. ),
  340. 'sort' => array(
  341. 'handler' => 'views_handler_sort',
  342. ),
  343. );
  344. // base_table
  345. $data['tripal_views']['base_table'] = array(
  346. 'title' => t('Base Table?'),
  347. 'help' => t('Whether the table being integrated should be considered a base table.'),
  348. 'field' => array(
  349. 'handler' => 'views_handler_field_boolean',
  350. 'click sortable' => TRUE,
  351. ),
  352. 'filter' => array(
  353. 'handler' => 'views_handler_filter_boolean_operator',
  354. 'label' => t('Base Table?'),
  355. 'type' => 'yes-no',
  356. 'use equal' => TRUE,
  357. ),
  358. 'sort' => array(
  359. 'handler' => 'views_handler_sort',
  360. ),
  361. );
  362. // table_name
  363. $data['tripal_views']['table_name'] = array(
  364. 'title' => t('Chado Table Name'),
  365. 'help' => t('The name of the table being integrated'),
  366. 'field' => array(
  367. 'handler' => 'views_handler_field',
  368. 'click sortable' => TRUE, // This is use by the table display plugin.
  369. ),
  370. 'sort' => array(
  371. 'handler' => 'views_handler_sort',
  372. ),
  373. 'filter' => array(
  374. 'handler' => 'views_handler_filter_string',
  375. ),
  376. 'argument' => array(
  377. 'handler' => 'views_handler_argument_string',
  378. ),
  379. );
  380. // priority
  381. $data['tripal_views']['priority'] = array(
  382. 'title' => t('Priority'),
  383. 'help' => t('The priority of the integration.'),
  384. 'field' => array(
  385. 'handler' => 'views_handler_field_numeric',
  386. 'click sortable' => TRUE,
  387. ),
  388. 'filter' => array(
  389. 'handler' => 'views_handler_filter_numeric',
  390. ),
  391. 'sort' => array(
  392. 'handler' => 'views_handler_sort',
  393. ),
  394. );
  395. // name
  396. $data['tripal_views']['name'] = array(
  397. 'title' => t('Name'),
  398. 'help' => t('The name of the integration'),
  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. // comment
  414. $data['tripal_views']['comment'] = array(
  415. 'title' => t('Description'),
  416. 'help' => t('Short description or comment about this integration.'),
  417. 'field' => array(
  418. 'handler' => 'views_handler_field',
  419. 'click sortable' => TRUE, // This is use by the table display plugin.
  420. ),
  421. 'sort' => array(
  422. 'handler' => 'views_handler_sort',
  423. ),
  424. 'filter' => array(
  425. 'handler' => 'views_handler_filter_string',
  426. ),
  427. 'argument' => array(
  428. 'handler' => 'views_handler_argument_string',
  429. ),
  430. );
  431. return $data;
  432. }
  433. /**
  434. * Implements hook_views_data_alter().
  435. * Used to add Chado <-> Node Joins & Relationships
  436. * since you need to add to the $data['node'] to do this
  437. *
  438. * @ingroup tripal_views
  439. */
  440. function tripal_views_views_data_alter(&$data) {
  441. // ADD IN NODE JOINS & RELATIONSHIPS
  442. // D7 @todo: Create custom handler to allow join from Node => Base (ie: organism)
  443. // with the addition of a single relationship
  444. // D7 @todo: Create custom handler to allow join from Base (ie: organism)
  445. // with the addition of a single relationship
  446. // D7 @todo: Add support for Mview <-> Node joins and relationships
  447. $tvi_query = db_query('SELECT * FROM {tripal_views} WHERE base_table=1');
  448. foreach ($tvi_query as $tvi_row) {
  449. //ids we'll use for queries
  450. $setup_id = $tvi_row->setup_id;
  451. $base_table = $tvi_row->table_name;
  452. $linker_table = 'chado_' . $base_table;
  453. $base_title = ucwords(str_replace('_', ' ', $base_table));
  454. // add in joins to the node tables if the Chado schema is local
  455. if (tripal_core_chado_schema_exists()) {
  456. // if a node linking table exists then add in the joins
  457. if (db_table_exists($linker_table)) {
  458. // Adds content (node) fields to chado base table field lists automatically
  459. $data['node']['table']['join'][$linker_table] = array(
  460. 'left_field' => 'nid',
  461. 'field' => 'nid',
  462. );
  463. $data[$linker_table]['table']['join'][$base_table] = array(
  464. 'left_field' => $base_table . '_id',
  465. 'field' => $base_table . '_id',
  466. );
  467. $data['node']['table']['join'][$base_table] = array(
  468. 'left_table' => $linker_table,
  469. 'left_field' => 'nid',
  470. 'field' => 'nid',
  471. );
  472. // Adds in a chado base table => node relationship
  473. // This allows controlled joining to multiple nodes per line
  474. // Use Case: link to feature and organism nodes on a feature listing
  475. // D7 todo: a custom relationship handler to get from feature.organism_id => organism node
  476. // without 1st needing to add relationship to organism table
  477. $base_field = $base_table . '_id';
  478. $data[$linker_table][$base_field] = array(
  479. 'group' => $base_title,
  480. 'title' => $base_title . 'Node',
  481. 'help' => "Links $base_title to it's node.",
  482. 'relationship' => array(
  483. 'handler' => 'views_handler_relationship',
  484. 'title' => t("$base_title => Node"),
  485. 'label' => t("$base_title => Node"),
  486. 'real field' => 'nid',
  487. 'base' => 'node',
  488. 'base field' => 'nid'
  489. ),
  490. );
  491. // Add Chado fields to a node-based view
  492. // This will only be done with relationships
  493. $base_field = $base_table . '_id';
  494. $data['node'][$base_field] = array(
  495. 'group' => $base_title,
  496. 'title' => $base_title,
  497. 'help' => "Links node to chado $base_title.",
  498. 'relationship' => array(
  499. 'handler' => 'views_handler_relationship',
  500. 'title' => t("Node => $base_title"),
  501. 'label' => t("Node => $base_title"),
  502. 'real field' => 'nid',
  503. 'base' => $linker_table,
  504. 'base field' => 'nid'
  505. ),
  506. );
  507. }
  508. }
  509. }
  510. return $data;
  511. }
  512. /**
  513. * Implementation of hook_views_pre_view().
  514. *
  515. * Merge the $_GET and $_POST into the $_GET. This is because
  516. * Views and Views Data Export modules only uses the $_GET variable but
  517. * file uploads require $_POST. We need to make sure these two modules
  518. * have access to everything needed for this view to work properly.
  519. *
  520. * @ingroup tripal_views
  521. */
  522. function tripal_views_views_pre_view(&$view, &$display_id, &$args) {
  523. $_GET = array_merge($_GET, $_POST);
  524. }