tripal_core.views.inc 15 KB

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