tripal_views.views.inc 16 KB

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