tripal_views.views.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. <?php
  2. include('api/tripal_views.api.inc');
  3. /**
  4. * @file
  5. * Tripal Views Integration
  6. *
  7. * @defgroup views Views Integration
  8. * @{
  9. * Provide rules for formatting and composition of fields
  10. * @}
  11. *
  12. * @defgroup views_handlers Views Integration Handlers
  13. * @ingroup views
  14. * @{
  15. * Provide rules for formatting and composition of fields
  16. * @}
  17. *
  18. * @defgroup views_field_handlers Views Field Handlers
  19. * @ingroup views_handlers
  20. * @{
  21. * Provide rules for formatting and composition of fields
  22. * @}
  23. *
  24. *
  25. * @defgroup views_filter_handlers Views Filter Handlers
  26. * @ingroup views_handlers
  27. * @{
  28. * Provide the ability to filter based on specified data
  29. * @}
  30. *
  31. * @defgroup views_sort_handlers Views Sort Handlers
  32. * @ingroup views_handlers
  33. * @{
  34. * Provide methods describing how specific data should be sorted
  35. * @}
  36. *
  37. * @defgroup views_argument_handlers Views Arguement Handlers
  38. * @ingroup views_handlers
  39. * @{
  40. * Provide the ability to filter pased on arguments in the path of the view
  41. * @}
  42. */
  43. /**
  44. * Implements hook_views_handlers()
  45. *
  46. * Purpose: Register all custom handlers with views
  47. * where a handler describes either "the type of field",
  48. * "how a field should be filtered", "how a field should be sorted"
  49. *
  50. * @return
  51. * An array of handler definitions
  52. *
  53. * @ingroup tripal_views
  54. */
  55. function tripal_views_views_handlers() {
  56. return array(
  57. 'info' => array(
  58. 'path' => drupal_get_path('module', 'tripal_views') . '/views/handlers',
  59. ),
  60. 'handlers' => array(
  61. // Custom Tripal Field Handlers
  62. /** CANT FIX UNTIL Tripal Feature is working
  63. 'tripal_views_handler_field_sequence' => array(
  64. 'parent' => 'views_handler_field',
  65. ),
  66. */
  67. // Custom Tripal Filter Handlers
  68. 'tripal_views_handler_filter_no_results' => array(
  69. 'parent' => 'views_handler_filter'
  70. ),
  71. 'tripal_views_handler_filter_select_cvterm' => array(
  72. 'parent' => 'tripal_views_handler_filter_select_string',
  73. ),
  74. 'tripal_views_handler_filter_select_string' => array(
  75. 'parent' => 'views_handler_filter_string',
  76. ),
  77. 'tripal_views_handler_filter_textarea' => array(
  78. 'parent' => 'views_handler_filter',
  79. ),
  80. 'tripal_views_handler_filter_file_upload' => array(
  81. 'parent' => 'views_handler_filter',
  82. ),
  83. /** D7 @todo: get handlers working
  84. */
  85. /** CANT FIX UNTIL Tripal Feature is working
  86. 'tripal_views_handler_filter_sequence' => array(
  87. 'parent' => 'chado_views_handler_filter_string',
  88. ),
  89. */
  90. ),
  91. );
  92. }
  93. /**
  94. * Implements hook_views_pre_render
  95. *
  96. * Purpose: Intercepts the view after the query has been executed
  97. * All the results are stored in $view->result
  98. * Looking up the NID here ensures the query is only executed once
  99. * for all stocks in the table.
  100. *
  101. * @ingroup tripal_views
  102. */
  103. function tripal_views_views_pre_render(&$view) {
  104. // We need to unset the exposed_input for the view so we can repopulate that
  105. // variable. This is necessary if we're using the file_upload_combo
  106. // custom form element which adds the file_path variable to the $_GET after the
  107. // view has populated the $view->exposed_input variable
  108. unset($view->exposed_input);
  109. }
  110. /**
  111. * Generates a dynamic data array for Views
  112. *
  113. * Purpose: This function is a hook used by the Views module. It populates and
  114. * returns a data array that specifies for the Views module the base table,
  115. * the tables it joins with and handlers. The data array is populated
  116. * using the data stored in the tripal_views tables.
  117. *
  118. * @return a data array formatted for the Views module
  119. *
  120. * D7 @todo: Add support for materialized views relationships using the new method
  121. *
  122. * @ingroup tripal_views
  123. */
  124. function tripal_views_views_data() {
  125. $data = array();
  126. // MAKE SURE ALL CHADO TABLES ARE INTEGRATED
  127. tripal_views_integrate_all_chado_tables();
  128. // DEFINE GLOBAL FIELDS
  129. // Filter handler that lets the admin say:
  130. // "Show no results until they enter search parameters"
  131. $data['views']['search_results'] = array(
  132. 'title' => t('Delay Results'),
  133. 'help' => t('Delay results until Apply/Search is clicked by the user.'),
  134. 'filter' => array(
  135. 'handler' => 'tripal_views_handler_filter_no_results',
  136. ),
  137. );
  138. $tvi_query = db_query('SELECT * FROM {tripal_views}');
  139. // INTEGRATE THE LIGHTEST SETUP FOR EACH TABLE
  140. foreach ($tvi_query as $tvi_row) {
  141. // check to see if this is the lightest (drupal-style) priority setup for this table
  142. // if not then don't use this definition
  143. $lightest_priority_setup = tripal_views_is_lightest_priority_setup($tvi_row->setup_id, $tvi_row->table_name);
  144. if (!$lightest_priority_setup) {
  145. continue;
  146. }
  147. // ids we'll use for queries
  148. $setup_id = $tvi_row->setup_id;
  149. $mview_id = $tvi_row->mview_id;
  150. // holds the base table name and fields
  151. $base_table = '';
  152. $base_fields = array();
  153. // indicated whether the current table is a base table or not
  154. $is_base_table = $tvi_row->base_table;
  155. // POPULATE THE BASE TABLE NAME AND FIELDS
  156. // If an $mview_id is given then get the materialized view info,
  157. // otherwise get the Chado table info
  158. if ($mview_id) {
  159. // get the base table name from the materialized view
  160. // D7 TODO: Check DBTNG changes work
  161. $sql = "SELECT name, mv_specs FROM {tripal_mviews} WHERE mview_id = :id";
  162. $mview_table = db_query($sql, array(':id' => $mview_id));
  163. $mview_table = $mview_table->fetchObject();
  164. $base_table = $mview_table->name;
  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 = tripal_core_get_chado_table_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. 'type' => $attrs['type'],
  200. );
  201. }
  202. // get the field name and descriptions
  203. $sql = "SELECT * FROM {tripal_views_field} WHERE setup_id=:setup";
  204. $query = db_query($sql, array(':setup' => $setup_id));
  205. foreach ($query as $field) {
  206. $base_fields[$field->column_name]['name'] = $field->name;
  207. $base_fields[$field->column_name]['help'] = $field->description;
  208. }
  209. }
  210. // SETUP THE BASE TABLE INFO IN THE DATA ARRAY
  211. $data[$base_table]['table']['group'] = t("$tvi_row->name");
  212. if ($is_base_table) {
  213. $data[$base_table]['table']['base'] = array(
  214. 'group' => "$tvi_row->name",
  215. 'title' => "$tvi_row->name",
  216. 'help' => $tvi_row->comment,
  217. 'search_path' => 'chado'
  218. );
  219. }
  220. else {
  221. $data[$base_table]['table'] = array(
  222. 'group' => "$tvi_row->name",
  223. 'title' => "$tvi_row->name",
  224. 'help' => $tvi_row->comment,
  225. 'search_path' => 'chado'
  226. );
  227. }
  228. // ADD THE FIELDS TO THE DATA ARRAY
  229. foreach ($base_fields as $column_name => $base_field) {
  230. $data[$base_table][$column_name] = array(
  231. 'title' => t($base_field['name']),
  232. 'help' => t($base_field['help']),
  233. 'field' => array(
  234. 'click sortable' => TRUE,
  235. ),
  236. );
  237. // now add the handlers
  238. $sql = "SELECT * FROM {tripal_views_handlers} WHERE setup_id = :setup AND column_name = :column";
  239. $handlers = db_query($sql, array(':setup' => $setup_id, ':column' => $column_name));
  240. foreach ($handlers as $handler) {
  241. $data[$base_table][$column_name][$handler->handler_type]['handler'] = $handler->handler_name;
  242. // Add in any additional arguments
  243. // This should be a serialized array including (at a minimum) name => <handler name>
  244. if ($handler->arguments) {
  245. $data[$base_table][$column_name][$handler->handler_type] = array_merge($data[$base_table][$column_name][$handler->handler_type], unserialize($handler->arguments));
  246. }
  247. };
  248. }
  249. // ADD JOINS & RELATIONSHIPS TO DATA ARRAY
  250. // only add joins if this is a base table
  251. // this keeps the list of available fields manageable
  252. // all other tables should be added via relationships
  253. // D7 @todo: add tripal_views_join field that determines whether a join is added
  254. if ($is_base_table) {
  255. $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = :setup";
  256. $joins = db_query($sql, array(':setup' => $setup_id));
  257. if (!isset($joins)) {
  258. $joins = array();
  259. }
  260. foreach ($joins as $join) {
  261. $left_table = $join->left_table;
  262. $left_field = $join->left_field;
  263. $base_table = $join->base_table;
  264. $base_field = $join->base_field;
  265. $handler = $join->handler;
  266. $base_title = ucwords(str_replace('_', ' ', $base_table));
  267. $left_title = ucwords(str_replace('_', ' ', $left_table));
  268. // if the 'node' table is in our integrated list then
  269. // we want to skip it. It shouldn't be there.
  270. if ($left_table == 'node') {
  271. continue;
  272. }
  273. // add join entry
  274. if (!$join->relationship_only) {
  275. $data[$left_table]['table']['join'][$base_table] = array(
  276. 'left_field' => $base_field,
  277. 'field' => $left_table . '_id',
  278. );
  279. if ($handler) {
  280. $data[$left_table]['table']['join'][$base_table]['handler'] = $handler;
  281. }
  282. if (!empty($join->arguments)) {
  283. array_merge($data[$left_table]['table']['join'][$base_table], unserialize($join->arguments));
  284. }
  285. }
  286. // warn if deprecated method of relationship addition was used (ie: through handlers)
  287. if (isset($data[$base_table][$base_field]['relationship'])) {
  288. watchdog('tripal_views',
  289. 'DEPRECATED: Currently using tripal_views_handlers to store relationship for %base => %left when you should be using tripal_views_joins.',
  290. array('%base' => $base_table, '%left' => $left_table),
  291. WATCHDOG_NOTICE);
  292. }
  293. // add relationship entry
  294. $fake_field = $base_table . '_to_' . $left_table;
  295. $data[$base_table][$fake_field] = array(
  296. 'title' => "$base_title => $left_title",
  297. 'help' => "Joins $base_title to $left_title",
  298. 'relationship' => array(
  299. 'handler' => $join->relationship_handler,
  300. 'title' => t("$base_title => $left_title"),
  301. 'label' => t("$base_title => $left_title"),
  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. }
  313. return $data;
  314. }
  315. /**
  316. * Implements hook_views_data_alter().
  317. * Used to add Chado <-> Node Joins & Relationships
  318. * since you need to add to the $data['node'] to do this
  319. *
  320. * @ingroup tripal_views
  321. */
  322. function tripal_views_views_data_alter(&$data) {
  323. // ADD IN NODE JOINS & RELATIONSHIPS
  324. // D7 @todo: Create custom handler to allow join from Node => Base (ie: organism)
  325. // with the addition of a single relationship
  326. // D7 @todo: Create custom handler to allow join from Base (ie: organism)
  327. // with the addition of a single relationship
  328. // D7 @todo: Add support for Mview <-> Node joins and relationships
  329. $tvi_query = db_query('SELECT * FROM {tripal_views} WHERE base_table=1');
  330. foreach ($tvi_query as $tvi_row) {
  331. //ids we'll use for queries
  332. $setup_id = $tvi_row->setup_id;
  333. $base_table = $tvi_row->table_name;
  334. $linker_table = 'chado_' . $base_table;
  335. $base_title = ucwords(str_replace('_', ' ', $base_table));
  336. // add in joins to the node tables if the Chado schema is local
  337. if (tripal_core_chado_schema_exists()) {
  338. // if a node linking table exists then add in the joins
  339. if (db_table_exists($linker_table)) {
  340. // Adds content (node) fields to chado base table field lists automatically
  341. $data['node']['table']['join'][$linker_table] = array(
  342. 'left_field' => 'nid',
  343. 'field' => 'nid',
  344. );
  345. $data[$linker_table]['table']['join'][$base_table] = array(
  346. 'left_field' => $base_table . '_id',
  347. 'field' => $base_table . '_id',
  348. );
  349. $data['node']['table']['join'][$base_table] = array(
  350. 'left_table' => $linker_table,
  351. 'left_field' => 'nid',
  352. 'field' => 'nid',
  353. );
  354. // Adds in a chado base table => node relationship
  355. // This allows controlled joining to multiple nodes per line
  356. // Use Case: link to feature and organism nodes on a feature listing
  357. // D7 todo: a custom relationship handler to get from feature.organism_id => organism node
  358. // without 1st needing to add relationship to organism table
  359. $base_field = $base_table . '_id';
  360. $data[$linker_table][$base_field] = array(
  361. 'group' => $base_title,
  362. 'title' => $base_title . 'Node',
  363. 'help' => "Links $base_title to it's node.",
  364. 'relationship' => array(
  365. 'handler' => 'views_handler_relationship',
  366. 'title' => t("$base_title => Node"),
  367. 'label' => t("$base_title => Node"),
  368. 'real field' => 'nid',
  369. 'base' => 'node',
  370. 'base field' => 'nid'
  371. ),
  372. );
  373. // Add Chado fields to a node-based view
  374. // This will only be done with relationships
  375. $base_field = $base_table . '_id';
  376. $data['node'][$base_field] = array(
  377. 'group' => $base_title,
  378. 'title' => $base_title,
  379. 'help' => "Links node to chado $base_title.",
  380. 'relationship' => array(
  381. 'handler' => 'views_handler_relationship',
  382. 'title' => t("Node => $base_title"),
  383. 'label' => t("Node => $base_title"),
  384. 'real field' => 'nid',
  385. 'base' => $linker_table,
  386. 'base field' => 'nid'
  387. ),
  388. );
  389. }
  390. }
  391. }
  392. return $data;
  393. }
  394. /**
  395. * Implementation of hook_views_pre_view().
  396. */
  397. function tripal_views_views_pre_view(&$view, &$display_id, &$args) {
  398. // merge the $_GET and $_POST into the $_GET. This is because
  399. // Views and Views Data Export modules only uses the $_GET variable but
  400. // file uploads require $_POST. We need to make sure these two modules
  401. // have access to everything needed for this view to work properly
  402. $_GET = array_merge($_GET, $_POST);
  403. }