tripal_views.views.inc 15 KB

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