tripal_core.views.inc 18 KB

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