tripal_core.views.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. <?php
  2. include('views/handlers/views_handler_join_chado_through_linking.inc');
  3. include('views/handlers/views_handler_join_chado_aggregator.inc');
  4. /**
  5. * @defgroup views Views Integration
  6. * @{
  7. * Provide rules for formatting and composition of fields
  8. * @}
  9. */
  10. /**
  11. * @defgroup views_field_handlers Views Field Handlers
  12. * @{
  13. * Provide rules for formatting and composition of fields
  14. * @}
  15. * @ingroup views
  16. */
  17. /**
  18. * @defgroup views_filter_handlers Views Filter Handlers
  19. * @{
  20. * Provide the ability to filter based on specified data
  21. * @}
  22. * @ingroup views
  23. */
  24. /**
  25. * @defgroup views_sort_handlers Views Sort Handlers
  26. * @{
  27. * Provide methods describing how specific data should be sorted
  28. * @}
  29. * @ingroup views
  30. */
  31. /**
  32. * @defgroup views_argument_handlers Views Arguement Handlers
  33. * @{
  34. * Provide the ability to filter pased on arguments in the path of the view
  35. * @}
  36. * @ingroup views
  37. */
  38. /**
  39. * Implements hook_views_handlers()
  40. *
  41. * Purpose: Register all custom handlers with views
  42. * where a handler describes either "the type of field",
  43. * "how a field should be filtered", "how a field should be sorted"
  44. *
  45. * @return
  46. * An array of handler definitions
  47. *
  48. * @ingroup tripal_core
  49. */
  50. function tripal_core_views_handlers() {
  51. return array(
  52. 'info' => array(
  53. 'path' => drupal_get_path('module', 'tripal_core') . '/views/handlers',
  54. ),
  55. 'handlers' => array(
  56. // Custom Chado Handlers
  57. 'chado_views_handler_field_aggregate' => array(
  58. 'parent' => 'chado_views_handler_field',
  59. ),
  60. 'views_handler_filter_chado_select_string' => array(
  61. 'parent' => 'views_handler_filter_string',
  62. ),
  63. 'views_handler_filter_chado_select_cvterm_name' => array(
  64. 'parent' => 'views_handler_filter_string',
  65. ),
  66. // Other Custom Handlers
  67. 'views_handler_filter_no_results' => array(
  68. 'parent' => 'views_handler_filter'
  69. ),
  70. // Old Handlers
  71. 'views_handler_field_node_optional' => array(
  72. 'parent' => 'views_handler_field_node',
  73. ),
  74. 'views_handler_field_cvterm_name' => array(
  75. 'parent' => 'views_handler_field',
  76. ),
  77. 'views_handler_field_chado_tf_boolean' => array(
  78. 'parent' => 'views_handler_field_boolean',
  79. ),
  80. 'views_handler_field_chado_count' => array(
  81. 'parent' => 'views_handler_field',
  82. ),
  83. 'views_handler_filter_chado_boolean' => array(
  84. 'parent' => 'views_handler_filter_boolean_operator',
  85. ),
  86. 'views_handler_field_chado_rel_by_type' => array(
  87. 'parent' => 'views_handler_field_prerender_list',
  88. ),
  89. // Wrappers for Default Views Handlers-----
  90. // Field Handlers
  91. 'chado_views_handler_field' => array(
  92. 'parent' => 'views_handler_field'
  93. ),
  94. 'chado_views_handler_field_boolean' => array(
  95. 'parent' => 'views_handler_field_boolean'
  96. ),
  97. 'chado_views_handler_field_counter' => array(
  98. 'parent' => 'views_handler_field_counter'
  99. ),
  100. 'chado_views_handler_field_custom' => array(
  101. 'parent' => 'views_handler_field_custom'
  102. ),
  103. 'chado_views_handler_field_date' => array(
  104. 'parent' => 'views_handler_field_date'
  105. ),
  106. 'chado_views_handler_field_markup' => array(
  107. 'parent' => 'views_handler_field_markup'
  108. ),
  109. 'chado_views_handler_field_math' => array(
  110. 'parent' => 'views_handler_field_math'
  111. ),
  112. 'chado_views_handler_field_numeric' => array(
  113. 'parent' => 'views_handler_field_numeric'
  114. ),
  115. // Filter Handlers
  116. 'chado_views_handler_filter_string' => array(
  117. 'parent' => 'views_handler_filter_string',
  118. ),
  119. 'chado_views_handler_filter_boolean_operator_string' => array(
  120. 'parent' => 'views_handler_filter_boolean_operator_string',
  121. ),
  122. 'chado_views_handler_filter_boolean_operator' => array(
  123. 'parent' => 'views_handler_filter_boolean_operator',
  124. ),
  125. 'chado_views_handler_filter_date' => array(
  126. 'parent' => 'views_handler_filter_date',
  127. ),
  128. 'chado_views_handler_filter_equality' => array(
  129. 'parent' => 'views_handler_filter_equality',
  130. ),
  131. 'chado_views_handler_filter_float' => array(
  132. 'parent' => 'views_handler_filter_float',
  133. ),
  134. 'chado_views_handler_filter_numeric' => array(
  135. 'parent' => 'views_handler_filter_numeric',
  136. ),
  137. // Sort Handlers
  138. 'chado_views_handler_sort' => array(
  139. 'parent' => 'views_handler_sort'
  140. ),
  141. 'chado_views_handler_sort_date' => array(
  142. 'parent' => 'views_handler_sort_date'
  143. ),
  144. 'chado_views_handler_sort_formula' => array(
  145. 'parent' => 'views_handler_sort_formula'
  146. ),
  147. 'chado_views_handler_sort_menu_hierarchy' => array(
  148. 'parent' => 'views_handler_sort_menu_hierarchy'
  149. ),
  150. 'chado_views_handler_sort_random' => array(
  151. 'parent' => 'views_handler_sort_random'
  152. ),
  153. ),
  154. );
  155. }
  156. /**
  157. * Implements hook_views_pre_render
  158. *
  159. * Purpose: Intercepts the view after the query has been executed
  160. * All the results are stored in $view->result
  161. * Looking up the NID here ensures the query is only executed once
  162. * for all stocks in the table.
  163. *
  164. * @todo add if !<chado/drupal same db> around NID portion
  165. *
  166. * @ingroup tripal_core
  167. */
  168. function tripal_core_views_pre_render (&$view) {
  169. //Add Node IDs in to every table that needs them
  170. // @see file: tripal_core.views.inc
  171. tripal_core_add_node_ids_to_view ($view);
  172. }
  173. /**
  174. * Adds nid fields to all pertinent view results
  175. *
  176. * Purpose: To add basetable_nid fields to all result arrays of a view
  177. * only if the basetable_nid field is added. This function will only be
  178. * called if chado/drupal are not in the same database (ie: only if
  179. * a join between the base and node table isn't possible.
  180. *
  181. * Note: Supports adding Node IDs to analysis, feature, library, organism, stock
  182. *
  183. * @param $view
  184. * the view object passed to hook_views_pre_render
  185. *
  186. * @return the views object with nids added to the result array
  187. *
  188. * @ingroup tripal_core
  189. */
  190. function tripal_core_add_node_ids_to_view (&$view) {
  191. //-----Analysis----------------------------------------------
  192. if (!empty($view->field['analysis_nid'])) {
  193. // retrieve the analysis_id for each record in the views current page
  194. $analysis_ids = array();
  195. foreach ($view->result as $row_num => $row) {
  196. if (!empty($row->analysis_id)) {
  197. //we're looking at analysis id field already in table
  198. $analysis_ids[$row_num] = $row->analysis_id;
  199. } else {
  200. //we're looking at analysis id added by field handler
  201. $analysis_ids[$row_num] = $row->analysis_analysis_id;
  202. }
  203. }
  204. $unique_analysis_ids = array_filter($analysis_ids);
  205. $unique_analysis_ids = array_unique($unique_analysis_ids);
  206. if (!empty($unique_analysis_ids)) {
  207. // Using the list of analysis_ids from the view
  208. // lookup the NIDs from drupal
  209. // and add that to the results of the view
  210. $sql = "SELECT nid, analysis_id FROM {chado_analysis} WHERE analysis_id IN (".implode(',',$unique_analysis_ids).")";
  211. $resource = db_query($sql);
  212. while ($r = db_fetch_object($resource)) {
  213. $keys = array_keys($analysis_ids, $r->analysis_id);
  214. foreach ($keys as $k) {
  215. $view->result[$k]->analysis_nid = $r->nid;
  216. }
  217. }
  218. } // if there are any analysis'
  219. } //end of case for analysis NID
  220. //-----Feature-----------------------------------------------
  221. if (!empty($view->field['feature_nid'])) {
  222. // retrieve the feature_id for each record in the views current page
  223. $feature_ids = array();
  224. foreach ($view->result as $row_num => $row) {
  225. if (!empty($row->feature_id)) {
  226. //we're looking at feature id field already in table
  227. $feature_ids[$row_num] = $row->feature_id;
  228. } else {
  229. //we're looking at feature id added by field handler
  230. $feature_ids[$row_num] = $row->feature_feature_id;
  231. }
  232. }
  233. $unique_feature_ids = array_filter($feature_ids);
  234. $unique_feature_ids = array_unique($unique_feature_ids);
  235. if (!empty($unique_feature_ids)) {
  236. // Using the list of feature_ids from the view
  237. // lookup the NIDs from drupal
  238. // and add that to the results of the view
  239. $sql = "SELECT nid, feature_id FROM {chado_feature} WHERE feature_id IN (".implode(',',$unique_feature_ids).")";
  240. $resource = db_query($sql);
  241. while ($r = db_fetch_object($resource)) {
  242. $keys = array_keys($feature_ids, $r->feature_id);
  243. foreach ($keys as $k) {
  244. $view->result[$k]->feature_nid = $r->nid;
  245. }
  246. }
  247. } // if there are any features
  248. } //end of case for feature NID
  249. //-----Library-----------------------------------------------
  250. if (!empty($view->field['library_nid'])) {
  251. // retrieve the library_id for each record in the views current page
  252. $library_ids = array();
  253. foreach ($view->result as $row_num => $row) {
  254. if (!empty($row->library_id)) {
  255. //we're looking at library id field already in table
  256. $library_ids[$row_num] = $row->library_id;
  257. } else {
  258. //we're looking at library id added by field handler
  259. $library_ids[$row_num] = $row->library_library_id;
  260. }
  261. }
  262. $unique_library_ids = array_filter($library_ids);
  263. $unique_library_ids = array_unique($unique_library_ids);
  264. if (!empty($unique_library_ids)) {
  265. // Using the list of library_ids from the view
  266. // lookup the NIDs from drupal
  267. // and add that to the results of the view
  268. $sql = "SELECT nid, library_id FROM {chado_library} WHERE library_id IN (".implode(',',$unique_library_ids).")";
  269. $resource = db_query($sql);
  270. while ($r = db_fetch_object($resource)) {
  271. $keys = array_keys($library_ids, $r->library_id);
  272. foreach ($keys as $k) {
  273. $view->result[$k]->library_nid = $r->nid;
  274. }
  275. }
  276. } // if there are libraries
  277. } //end of case for library NID
  278. //-----Organism----------------------------------------------
  279. if (!empty($view->field['organism_nid'])) {
  280. // retrieve the organism_id for each record in the views current page
  281. $organism_ids = array();
  282. foreach ($view->result as $row_num => $row) {
  283. if (!empty($row->organism_id)) {
  284. //we're looking at organism id field already in table
  285. $organism_ids[$row_num] = $row->organism_id;
  286. } else {
  287. //we're looking at organism id added by field handler
  288. $organism_ids[$row_num] = $row->organism_organism_id;
  289. }
  290. }
  291. $unique_organism_ids = array_filter($organism_ids);
  292. $unique_organism_ids = array_unique($unique_organism_ids);
  293. if (!empty($unique_organism_ids)) {
  294. // Using the list of organism_ids from the view
  295. // lookup the NIDs from drupal
  296. // and add that to the results of the view
  297. $sql = "SELECT nid, organism_id FROM {chado_organism} WHERE organism_id IN (".implode(',',$unique_organism_ids).")";
  298. $resource = db_query($sql);
  299. while ($r = db_fetch_object($resource)) {
  300. $keys = array_keys($organism_ids, $r->organism_id);
  301. foreach ($keys as $k) {
  302. $view->result[$k]->organism_nid = $r->nid;
  303. }
  304. }
  305. } // if there are organisms
  306. } //end of case for organism NID
  307. //-----Stock-------------------------------------------------
  308. if (!empty($view->field['stock_nid'])) {
  309. // retrieve the stock_id for each record in the views current page
  310. $stock_ids = array();
  311. foreach ($view->result as $row_num => $row) {
  312. if (!empty($row->stock_id)) {
  313. //we're looking at stock id field already in table
  314. $stock_ids[$row_num] = $row->stock_id;
  315. } else {
  316. //we're looking at stock id added by field handler
  317. $stock_ids[$row_num] = $row->stock_stock_id;
  318. }
  319. }
  320. $unique_stock_ids = array_filter($stock_ids);
  321. $unique_stock_ids = array_unique($unique_stock_ids);
  322. if (!empty($unique_stock_ids)) {
  323. // Using the list of stock_ids from the view
  324. // lookup the NIDs from drupal
  325. // and add that to the results of the view
  326. $sql = "SELECT nid, stock_id FROM {chado_stock} WHERE stock_id IN (".implode(',',$unique_stock_ids).")";
  327. $resource = db_query($sql);
  328. while ($r = db_fetch_object($resource)) {
  329. $keys = array_keys($stock_ids, $r->stock_id);
  330. foreach ($keys as $k) {
  331. $view->result[$k]->stock_nid = $r->nid;
  332. }
  333. }
  334. } //if there are stocks
  335. } //end of case for stock NID
  336. return $view;
  337. }
  338. /**
  339. *
  340. * @ingroup tripal_core
  341. */
  342. function tripal_core_views_data(){
  343. // Define Global Fields -------------
  344. // Filter handler that lets the admin say:
  345. // "Show no results until they enter search parameters"
  346. $data['views']['search_results'] = array(
  347. 'title' => t('Search Results'),
  348. 'help' => t('Delay results until Apply/Search is clicked by the user.'),
  349. 'filter' => array(
  350. 'handler' => 'views_handler_filter_no_results',
  351. ),
  352. );
  353. $tvi_query = db_query('SELECT * FROM {tripal_views}');
  354. while($tvi_row = db_fetch_object($tvi_query)){
  355. // ids we'll use for queries
  356. $setup_id = $tvi_row->setup_id;
  357. $mview_id = $tvi_row->mview_id;
  358. // holds the base table name and fields
  359. $base_table = '';
  360. $base_fields = array();
  361. $type_prefix = '';
  362. // populate the base table name and fields. If an $mview_id is given
  363. // the get the materialized view info, otherwise get the Chado table info
  364. if($mview_id){
  365. $type_prefix = 'MView';
  366. // get the base table name from the materialized view
  367. $sql = "SELECT name, mv_specs FROM {tripal_mviews} WHERE mview_id = %d";
  368. $mview_table = db_fetch_object(db_query($sql,$mview_id));
  369. $base_table = $mview_table->name;
  370. // get the columns in this materialized view. They are separated by commas
  371. // where the first word is the column name and the rest is the type
  372. $columns = explode(",",$mview_table->mv_specs);
  373. foreach ($columns as $column){
  374. $column = trim($column); // trim trailing and leading spaces
  375. preg_match("/^(.*?)\ (.*?)$/",$column,$matches);
  376. $column_name = $matches[1];
  377. $column_type = $matches[2];
  378. $base_fields[] = $column_name;
  379. }
  380. }
  381. else {
  382. $type_prefix = 'Chado Table';
  383. // TODO: get the chado table info and populate these variables
  384. // 1) $base_table
  385. // 2) $base_fields (an array of just the table field names)
  386. }
  387. // Setup the base table info in the data array
  388. $data[$base_table]['table']['group'] = t($tvi_row->name);
  389. $data[$base_table]['table']['base'] = array(
  390. 'group' => "$type_prefix: $tvi_row->name",
  391. 'title' => "$type_prefix: $tvi_row->name",
  392. 'help' => $tvi_row->comment,
  393. );
  394. // first add the fields
  395. foreach ($base_fields as $base_field){
  396. $data[$base_table][$base_field] = array(
  397. 'title' => $base_field,
  398. 'help' => t("The $base_field from the $base_table table (added by Tripal Views Integration)"),
  399. 'field' => array(
  400. 'click sortable' => TRUE,
  401. ),
  402. );
  403. // now add the handlers
  404. $sql = "SELECT * FROM {tripal_views_handlers} WHERE setup_id = %d AND column_name = '%s'";
  405. $handlers = db_query($sql,$setup_id,$base_field);
  406. while($handler = db_fetch_object($handlers)){
  407. $data[$base_table][$base_field][$handler->handler_type]['handler'] = $handler->handler_name;
  408. };
  409. }
  410. // now add the joins
  411. $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = %d";
  412. $joins = db_query($sql,$setup_id);
  413. while($join = db_fetch_object($joins)){
  414. $left_table = $join->left_table;
  415. $left_field = $join->left_field;
  416. $base_field = $join->base_field;
  417. // add join entry
  418. $data[$base_table]['table']['join'][$left_table] = array(
  419. 'left_field' => $left_field,
  420. 'field' => $base_field,
  421. );
  422. // check to see if the join table is one that correlates with Drupal nodes
  423. // if so, there will be a chado_<table_name> table in the Drupal database
  424. // if there is, then we need to add the linking join information
  425. $sql = "SELECT tablename FROM pg_tables WHERE tablename = 'chado_$left_table'";
  426. if(db_fetch_object(db_query($sql))){
  427. // join the mview to the linker table
  428. $data[$base_table]['table']['join']["chado_$left_table"] = array(
  429. 'left_field' => $left_field,
  430. 'field' => $base_field,
  431. );
  432. }
  433. }
  434. }
  435. return $data;
  436. }
  437. /**
  438. *
  439. * @ingroup tripal_core
  440. */
  441. function tripal_core_views_data_alter(&$data) {
  442. $tvi_query = db_query('SELECT * FROM {tripal_views}');
  443. // iterate through the views that we manage
  444. while($tvi_row = db_fetch_object($tvi_query)){
  445. //ids we'll use for queries
  446. $mview_id = $tvi_row->mview_id;
  447. $setup_id = $tvi_row->setup_id;
  448. // iterate through the columns and alter the existing data array for
  449. // joins to other tables
  450. $sql = "SELECT * FROM {tripal_views_join} WHERE setup_id = %d";
  451. $joins = db_query($sql,$setup_id);
  452. while($join = db_fetch_object($joins)){
  453. $left_table = $join->left_table;
  454. $left_field = $join->left_field;
  455. $base_field = $join->base_field;
  456. $base_table = $join->base_table;
  457. // add the recipricol join entries for each column
  458. if(array_key_exists($left_table,$data)){
  459. $data[$left_table]['table']['join'][$base_table] = array(
  460. 'left_field' => $base_field,
  461. 'field' => $left_field,
  462. );
  463. }
  464. // check to see if this table is one that correlates with Drupal nodes
  465. // if so, there will be a chado_<table_name> table in the Drupal database
  466. // if there is, then we need to add the linking join information. We did
  467. // this step in the hook_views_data function above, but now we need
  468. // to add the reciprical joins
  469. $sql = "SELECT tablename FROM pg_tables WHERE tablename = '%s'";
  470. if(db_fetch_object(db_query($sql,"chado_$left_table"))){
  471. // join the linker table to the mview
  472. if(array_key_exists("chado_$left_table",$data)){
  473. $data["chado_$left_table"]['table']['join'][$base_table] = array(
  474. 'left_field' => $base_field,
  475. 'field' => $left_field,
  476. );
  477. // Join the node table to the view by way of the chado linker table
  478. $data['node']['table']['join'][$base_table] = array(
  479. 'left_table' => "chado_$left_table",
  480. 'left_field' => 'nid',
  481. 'field' => 'nid',
  482. );
  483. }
  484. }
  485. }
  486. }
  487. return $data;
  488. }