tripal_views.views.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. // Add css if tripal admin tagged view
  110. if ($view->tag == 'tripal admin') {
  111. $tripal_admin_view_css = drupal_get_path('module', 'tripal_views') . '/theme/tripal_views_admin_views.css';
  112. drupal_add_css($tripal_admin_view_css);
  113. }
  114. }
  115. /**
  116. * Generates a dynamic data array for Views
  117. *
  118. * Purpose: This function is a hook used by the Views module. It populates and
  119. * returns a data array that specifies for the Views module the base table,
  120. * the tables it joins with and handlers. The data array is populated
  121. * using the data stored in the tripal_views tables.
  122. *
  123. * @return a data array formatted for the Views module
  124. *
  125. * D7 @todo: Add support for materialized views relationships using the new method
  126. *
  127. * @ingroup tripal_views
  128. */
  129. function tripal_views_views_data() {
  130. $data = array();
  131. // MAKE SURE ALL CHADO TABLES ARE INTEGRATED
  132. tripal_views_integrate_all_chado_tables();
  133. // DEFINE GLOBAL FIELDS
  134. // Filter handler that lets the admin say:
  135. // "Show no results until they enter search parameters"
  136. $data['views']['search_results'] = array(
  137. 'title' => t('Delay Results'),
  138. 'help' => t('Delay results until Apply/Search is clicked by the user.'),
  139. 'filter' => array(
  140. 'handler' => 'tripal_views_handler_filter_no_results',
  141. ),
  142. );
  143. $tvi_query = db_query('SELECT * FROM {tripal_views}');
  144. // INTEGRATE THE LIGHTEST SETUP FOR EACH TABLE
  145. foreach ($tvi_query as $tvi_row) {
  146. // check to see if this is the lightest (drupal-style) priority setup for this table
  147. // if not then don't use this definition
  148. $lightest_priority_setup = tripal_views_is_lightest_priority_setup($tvi_row->setup_id, $tvi_row->table_name);
  149. if (!$lightest_priority_setup) {
  150. continue;
  151. }
  152. // ids we'll use for queries
  153. $setup_id = $tvi_row->setup_id;
  154. $mview_id = $tvi_row->mview_id;
  155. // holds the base table name and fields
  156. $base_table = '';
  157. $base_fields = array();
  158. // indicated whether the current table is a base table or not
  159. $is_base_table = $tvi_row->base_table;
  160. // POPULATE THE BASE TABLE NAME AND FIELDS
  161. // If an $mview_id is given then get the materialized view info,
  162. // otherwise get the Chado table info
  163. if ($mview_id) {
  164. // get the base table name from the materialized view
  165. // D7 TODO: Check DBTNG changes work
  166. $sql = "SELECT name, mv_specs FROM {tripal_mviews} WHERE mview_id = :id";
  167. $mview_table = db_query($sql, array(':id' => $mview_id));
  168. $mview_table = $mview_table->fetchObject();
  169. $base_table = $mview_table->name;
  170. // get the columns in this materialized view. They are separated by commas
  171. // where the first word is the column name and the rest is the type
  172. $columns = explode(",", $mview_table->mv_specs);
  173. foreach ($columns as $column) {
  174. $column = trim($column); // trim trailing and leading spaces
  175. preg_match("/^(.*?)\ (.*?)$/", $column, $matches);
  176. $column_name = $matches[1];
  177. $column_type = $matches[2];
  178. $base_fields[$column_name] = array(
  179. 'column_name' => $column_name,
  180. 'type' => $column_type,
  181. );
  182. }
  183. // get the field name and descriptions
  184. // D7 TODO: Check DBTNG changes work
  185. $sql = "SELECT * FROM {tripal_views_field} WHERE setup_id=:setup";
  186. $query = db_query($sql, array(':setup' => $setup_id));
  187. foreach($query as $field) {
  188. $base_fields[$field->column_name]['name'] = $field->name;
  189. $base_fields[$field->column_name]['help'] = $field->description;
  190. }
  191. }
  192. // if this is not a legacy materialized view
  193. else {
  194. $base_table = $tvi_row->table_name;
  195. // get the table description
  196. $table_desc = tripal_core_get_chado_table_schema($base_table);
  197. $fields = $table_desc['fields'];
  198. if (!is_array($fields)) {
  199. $fields = array();
  200. }
  201. foreach ($fields as $column => $attrs) {
  202. $base_fields[$column] = array(
  203. 'column_name' => $column,
  204. 'type' => $attrs['type'],
  205. );
  206. }
  207. // get the field name and descriptions
  208. $sql = "SELECT * FROM {tripal_views_field} WHERE setup_id=:setup";
  209. $query = db_query($sql, array(':setup' => $setup_id));
  210. foreach ($query as $field) {
  211. $base_fields[$field->column_name]['name'] = $field->name;
  212. $base_fields[$field->column_name]['help'] = $field->description;
  213. }
  214. }
  215. // SETUP THE BASE TABLE INFO IN THE DATA ARRAY
  216. $data[$base_table]['table']['group'] = t("$tvi_row->name");
  217. if ($is_base_table) {
  218. $data[$base_table]['table']['base'] = array(
  219. 'group' => "$tvi_row->name",
  220. 'title' => "$tvi_row->name",
  221. 'help' => $tvi_row->comment,
  222. 'search_path' => 'chado'
  223. );
  224. }
  225. else {
  226. $data[$base_table]['table'] = array(
  227. 'group' => "$tvi_row->name",
  228. 'title' => "$tvi_row->name",
  229. 'help' => $tvi_row->comment,
  230. 'search_path' => 'chado'
  231. );
  232. }
  233. // ADD THE FIELDS TO THE DATA ARRAY
  234. foreach ($base_fields as $column_name => $base_field) {
  235. $data[$base_table][$column_name] = array(
  236. 'title' => t($base_field['name']),
  237. 'help' => t($base_field['help']),
  238. 'field' => array(
  239. 'click sortable' => TRUE,
  240. ),
  241. );
  242. // now add the handlers
  243. $sql = "SELECT * FROM {tripal_views_handlers} WHERE setup_id = :setup AND column_name = :column";
  244. $handlers = db_query($sql, array(':setup' => $setup_id, ':column' => $column_name));
  245. foreach ($handlers as $handler) {
  246. $data[$base_table][$column_name][$handler->handler_type]['handler'] = $handler->handler_name;
  247. // Add in any additional arguments
  248. // This should be a serialized array including (at a minimum) name => <handler name>
  249. if ($handler->arguments) {
  250. $data[$base_table][$column_name][$handler->handler_type] = array_merge($data[$base_table][$column_name][$handler->handler_type], unserialize($handler->arguments));
  251. }
  252. };
  253. }
  254. // ADD JOINS & RELATIONSHIPS TO DATA ARRAY
  255. // only add joins if this is a base table
  256. // this keeps the list of available fields manageable
  257. // all other tables should be added via relationships
  258. $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = :setup";
  259. $joins = db_query($sql, array(':setup' => $setup_id));
  260. if (!isset($joins)) {
  261. $joins = array();
  262. }
  263. foreach ($joins as $join) {
  264. $left_table = $join->left_table;
  265. $left_field = $join->left_field;
  266. $base_table = $join->base_table;
  267. $base_field = $join->base_field;
  268. $handler = $join->handler;
  269. $base_title = ucwords(str_replace('_', ' ', $base_table));
  270. $left_title = ucwords(str_replace('_', ' ', $left_table));
  271. // if the 'node' table is in our integrated list then
  272. // we want to skip it. It shouldn't be there.
  273. if ($left_table == 'node') {
  274. continue;
  275. }
  276. // add join entry
  277. if (!$join->relationship_only) {
  278. $data[$left_table]['table']['join'][$base_table] = array(
  279. 'left_field' => $base_field,
  280. 'field' => $left_table . '_id',
  281. );
  282. if ($handler) {
  283. $data[$left_table]['table']['join'][$base_table]['handler'] = $handler;
  284. }
  285. if (!empty($join->arguments)) {
  286. array_merge($data[$left_table]['table']['join'][$base_table], unserialize($join->arguments));
  287. }
  288. }
  289. // warn if deprecated method of relationship addition was used (ie: through handlers)
  290. if (isset($data[$base_table][$base_field]['relationship'])) {
  291. watchdog('tripal_views',
  292. 'DEPRECATED: Currently using tripal_views_handlers to store relationship for %base => %left when you should be using tripal_views_joins.',
  293. array('%base' => $base_table, '%left' => $left_table),
  294. WATCHDOG_NOTICE);
  295. }
  296. // add relationship entry
  297. $fake_field = $base_table . '_to_' . $left_table;
  298. $data[$base_table][$fake_field] = array(
  299. 'title' => "$base_title => $left_title",
  300. 'help' => "Joins $base_title to $left_title",
  301. 'relationship' => array(
  302. 'handler' => $join->relationship_handler,
  303. 'title' => t("$base_title => $left_title"),
  304. 'label' => t("$base_title => $left_title"),
  305. 'real field' => $base_field,
  306. 'base' => $left_table,
  307. 'base field' => $left_field
  308. )
  309. );
  310. if (!empty($join->arguments)) {
  311. array_merge($data[$base_table][$fake_field]['relationship'], unserialize($join->arguments));
  312. }
  313. }
  314. }
  315. return $data;
  316. }
  317. /**
  318. * Implements hook_views_data_alter().
  319. * Used to add Chado <-> Node Joins & Relationships
  320. * since you need to add to the $data['node'] to do this
  321. *
  322. * @ingroup tripal_views
  323. */
  324. function tripal_views_views_data_alter(&$data) {
  325. // ADD IN NODE JOINS & RELATIONSHIPS
  326. // D7 @todo: Create custom handler to allow join from Node => Base (ie: organism)
  327. // with the addition of a single relationship
  328. // D7 @todo: Create custom handler to allow join from Base (ie: organism)
  329. // with the addition of a single relationship
  330. // D7 @todo: Add support for Mview <-> Node joins and relationships
  331. $tvi_query = db_query('SELECT * FROM {tripal_views} WHERE base_table=1');
  332. foreach ($tvi_query as $tvi_row) {
  333. //ids we'll use for queries
  334. $setup_id = $tvi_row->setup_id;
  335. $base_table = $tvi_row->table_name;
  336. $linker_table = 'chado_' . $base_table;
  337. $base_title = ucwords(str_replace('_', ' ', $base_table));
  338. // add in joins to the node tables if the Chado schema is local
  339. if (tripal_core_chado_schema_exists()) {
  340. // if a node linking table exists then add in the joins
  341. if (db_table_exists($linker_table)) {
  342. // Adds content (node) fields to chado base table field lists automatically
  343. $data['node']['table']['join'][$linker_table] = array(
  344. 'left_field' => 'nid',
  345. 'field' => 'nid',
  346. );
  347. $data[$linker_table]['table']['join'][$base_table] = array(
  348. 'left_field' => $base_table . '_id',
  349. 'field' => $base_table . '_id',
  350. );
  351. $data['node']['table']['join'][$base_table] = array(
  352. 'left_table' => $linker_table,
  353. 'left_field' => 'nid',
  354. 'field' => 'nid',
  355. );
  356. // Adds in a chado base table => node relationship
  357. // This allows controlled joining to multiple nodes per line
  358. // Use Case: link to feature and organism nodes on a feature listing
  359. // D7 todo: a custom relationship handler to get from feature.organism_id => organism node
  360. // without 1st needing to add relationship to organism table
  361. $base_field = $base_table . '_id';
  362. $data[$linker_table][$base_field] = array(
  363. 'group' => $base_title,
  364. 'title' => $base_title . 'Node',
  365. 'help' => "Links $base_title to it's node.",
  366. 'relationship' => array(
  367. 'handler' => 'views_handler_relationship',
  368. 'title' => t("$base_title => Node"),
  369. 'label' => t("$base_title => Node"),
  370. 'real field' => 'nid',
  371. 'base' => 'node',
  372. 'base field' => 'nid'
  373. ),
  374. );
  375. // Add Chado fields to a node-based view
  376. // This will only be done with relationships
  377. $base_field = $base_table . '_id';
  378. $data['node'][$base_field] = array(
  379. 'group' => $base_title,
  380. 'title' => $base_title,
  381. 'help' => "Links node to chado $base_title.",
  382. 'relationship' => array(
  383. 'handler' => 'views_handler_relationship',
  384. 'title' => t("Node => $base_title"),
  385. 'label' => t("Node => $base_title"),
  386. 'real field' => 'nid',
  387. 'base' => $linker_table,
  388. 'base field' => 'nid'
  389. ),
  390. );
  391. }
  392. }
  393. }
  394. return $data;
  395. }
  396. /**
  397. * Implementation of hook_views_pre_view().
  398. */
  399. function tripal_views_views_pre_view(&$view, &$display_id, &$args) {
  400. // merge the $_GET and $_POST into the $_GET. This is because
  401. // Views and Views Data Export modules only uses the $_GET variable but
  402. // file uploads require $_POST. We need to make sure these two modules
  403. // have access to everything needed for this view to work properly
  404. $_GET = array_merge($_GET, $_POST);
  405. }