tripal_chado_views.views.inc 24 KB

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