tripal_stock.views.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. <?php
  2. /**
  3. * @file
  4. * This file contains the basic functions for views integration of
  5. * chado/tripal stock tables. Supplementary functions can be found in
  6. * ./views/
  7. *
  8. * Documentation on views integration can be found at
  9. * http://views2.logrus.com/doc/html/index.html.
  10. */
  11. /**
  12. * @defgroup tripal_stock_views Stock Views Integration
  13. * @ingroup views
  14. * @ingroup tripal_stock
  15. */
  16. require_once('views/stock.views.inc');
  17. require_once('views/chado_stock.views.inc');
  18. require_once('views/misc_tables.views.inc');
  19. /**
  20. * Implements hook_views_data(): Describe chado/tripal tables & fields to views
  21. *
  22. * @return array
  23. * A data array which follows the structure outlined in the
  24. * views2 documentation for this hook. Essentially, it's an array of table
  25. * definitions keyed by chado/tripal table name. Each table definition
  26. * includes basic details about the table, fields in that table and
  27. * relationships between that table and others (joins)
  28. *
  29. * @ingroup tripal_stock_views
  30. */
  31. function tripal_stock_views_data() {
  32. $data = array();
  33. $data = array_merge($data, retrieve_stock_views_data());
  34. $data = array_merge($data, retrieve_chado_stock_views_data());
  35. $data = array_merge($data, retrieve_stock_misc_tables_views_data());
  36. return $data;
  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 array
  46. * An array of handler definitions
  47. *
  48. * @ingroup tripal_stock_views
  49. */
  50. function tripal_stock_views_handlers() {
  51. return array(
  52. 'info' => array(
  53. 'path' => drupal_get_path('module', 'tripal_stock') . '/views/handlers',
  54. ),
  55. 'handlers' => array(
  56. 'views_handler_field_computed_stock_nid' => array(
  57. 'parent' => 'views_handler_field_numeric',
  58. ),
  59. 'views_handler_field_stockprop_by_type' => array(
  60. 'parent' => 'views_handler_field_prerender_list',
  61. ),
  62. 'views_handler_field_stockprop_all' => array(
  63. 'parent' => 'views_handler_field_prerender_list',
  64. ),
  65. 'views_handler_field_stockrel_by_type' => array(
  66. 'parent' => 'views_handler_field_prerender_list',
  67. ),
  68. 'views_handler_field_stockrel_all' => array(
  69. 'parent' => 'views_handler_field_prerender_list',
  70. ),
  71. 'views_handler_field_stock_dbxref_by_type' => array(
  72. 'parent' => 'views_handler_field_prerender_list',
  73. ),
  74. 'views_handler_field_stock_dbxref_all' => array(
  75. 'parent' => 'views_handler_field_prerender_list',
  76. ),
  77. 'views_handler_filter_stockprop_id' => array(
  78. 'parent' => 'views_handler_filter',
  79. ),
  80. 'views_handler_filter_stock_dbxref_id' => array(
  81. 'parent' => 'views_handler_filter',
  82. ),
  83. 'views_handler_argument_stockprop_id' => array(
  84. 'parent' => 'views_handler_argument_string',
  85. ),
  86. ),
  87. );
  88. }
  89. /**
  90. * Implements hook_views_pre_render
  91. *
  92. * Purpose: Intercepts the view after the query has been executed
  93. * All the results are stored in $view->result
  94. * Looking up the NID here ensures the query is only executed once
  95. * for all stocks in the table.
  96. *
  97. * @todo add if !<chado/drupal same db> around NID portion
  98. *
  99. * @ingroup tripal_stock_views
  100. */
  101. function tripal_stock_views_pre_render (&$view) {
  102. if (preg_match('/stock/', $view->base_table)) {
  103. //-----Node IDs---------------------------------------------
  104. // @see file: tripal_core.views.inc function: tripal_core_add_node_ids_to_view (&$view);
  105. // retrieve the stock_id for each record in the views current page
  106. $stock_ids = array();
  107. foreach ($view->result as $row_num => $row) {
  108. $stock_ids[$row_num] = $row->stock_id;
  109. }
  110. if (sizeof($stock_ids)) {
  111. //-----Properties------------------------------------------
  112. $field_names = array_keys($view->field);
  113. //if any property fields are in the current view
  114. $property_field_names = preg_grep('/properties/',$field_names);
  115. if (!empty($property_field_names)) {
  116. $sql = "SELECT stockprop.*, cvterm.name as type_name FROM stockprop "
  117. ."INNER JOIN cvterm cvterm ON stockprop.type_id=cvterm.cvterm_id "
  118. ."WHERE stockprop.stock_id IN (".implode(',',$stock_ids).")";
  119. $previous_db = tripal_db_set_active('chado');
  120. $resource = db_query($sql);
  121. tripal_db_set_active($previous_db);
  122. $view->result[$key]->properties = array();
  123. while ($r = db_fetch_object($resource)) {
  124. $key = array_search($r->stock_id, $stock_ids);
  125. $view->result[$key]->properties[] = $r;
  126. }
  127. }
  128. //-----Relationships----------------------------------------
  129. //if any relationship fields are in the current view
  130. $relationship_field_names = preg_grep('/relationships/', $field_names);
  131. if (!empty($relationship_field_names)) {
  132. $sql = "SELECT stock_relationship.*, cvterm.name as type_name, "
  133. ."subject_stock.name as subject_name, object_stock.name as object_name "
  134. ."FROM stock_relationship "
  135. ."LEFT JOIN stock subject_stock ON stock_relationship.subject_id=subject_stock.stock_id "
  136. ."LEFT JOIN stock object_stock ON stock_relationship.object_id=object_stock.stock_id "
  137. ."LEFT JOIN cvterm cvterm ON stock_relationship.type_id = cvterm.cvterm_id "
  138. ."WHERE stock_relationship.subject_id IN (".implode(',',$stock_ids).") "
  139. ."OR stock_relationship.object_id IN (".implode(',',$stock_ids).") ";
  140. $previous_db = tripal_db_set_active('chado');
  141. $resource = db_query($sql);
  142. tripal_db_set_active($previous_db);
  143. while ($r = db_fetch_object($resource)) {
  144. if (in_array($r->subject_id, $stock_ids)) {
  145. $key = array_search($r->subject_id, $stock_ids);
  146. $r->stock_id = $r->subject_id;
  147. $view->result[$key]->relationships[] = clone $r;
  148. }
  149. if (in_array($r->object_id, $stock_ids)) {
  150. $key = array_search($r->object_id, $stock_ids);
  151. $r->stock_id = $r->object_id;
  152. $view->result[$key]->relationships[] = clone $r;
  153. }
  154. }
  155. }
  156. //-----DB References--------------------------------------------
  157. //if any dbxref fields are in the current view
  158. $dbxref_field_names = preg_grep('/dbxref/',$field_names);
  159. if (!empty($dbxref_field_names)) {
  160. $sql = "SELECT stock_dbxref.*, dbxref.db_id, db.name as db_name, db.urlprefix, "
  161. ."dbxref.accession, dbxref.version, dbxref.description "
  162. ."FROM stock_dbxref "
  163. ."LEFT JOIN dbxref dbxref ON stock_dbxref.dbxref_id=dbxref.dbxref_id "
  164. ."LEFT JOIN db db ON dbxref.db_id=db.db_id "
  165. ."WHERE stock_dbxref.stock_id IN (".implode(',',$stock_ids).")";
  166. $previous_db = tripal_db_set_active('chado');
  167. $resource = db_query($sql);
  168. tripal_db_set_active($previous_db);
  169. $view->result[$key]->dbxref = array();
  170. while ($r = db_fetch_object($resource)) {
  171. $key = array_search($r->stock_id, $stock_ids);
  172. $view->result[$key]->dbxref[] = $r;
  173. }
  174. }
  175. } //if there are stocks
  176. } //if we're dealing with a stock view
  177. }
  178. /**
  179. *
  180. * @ingroup tripal_stock_views
  181. */
  182. function tripal_stock_views_default_views() {
  183. $views = array();
  184. $view = new view;
  185. $view->name = 'all_stocks';
  186. $view->description = 'This view lists all stocks by default. There are exposed filters available but no arguments are used.';
  187. $view->tag = 'chado';
  188. $view->view_php = '';
  189. $view->base_table = 'stock';
  190. $view->is_cacheable = FALSE;
  191. $view->api_version = 2;
  192. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  193. $handler = $view->new_display('default', 'Defaults', 'default');
  194. $handler->override_option('fields', array(
  195. 'uniquename' => array(
  196. 'label' => 'Unique Name',
  197. 'alter' => array(
  198. 'alter_text' => 0,
  199. 'text' => '',
  200. 'make_link' => 0,
  201. 'path' => '',
  202. 'link_class' => '',
  203. 'alt' => '',
  204. 'prefix' => '',
  205. 'suffix' => '',
  206. 'target' => '',
  207. 'help' => '',
  208. 'trim' => 0,
  209. 'max_length' => '',
  210. 'word_boundary' => 1,
  211. 'ellipsis' => 1,
  212. 'html' => 0,
  213. 'strip_tags' => 0,
  214. ),
  215. 'empty' => '',
  216. 'hide_empty' => 0,
  217. 'empty_zero' => 0,
  218. 'link_to_node' => 1,
  219. 'exclude' => 0,
  220. 'id' => 'uniquename',
  221. 'table' => 'stock',
  222. 'field' => 'uniquename',
  223. 'relationship' => 'none',
  224. ),
  225. 'name_2' => array(
  226. 'label' => 'Name',
  227. 'alter' => array(
  228. 'alter_text' => 0,
  229. 'text' => '',
  230. 'make_link' => 0,
  231. 'path' => '',
  232. 'link_class' => '',
  233. 'alt' => '',
  234. 'prefix' => '',
  235. 'suffix' => '',
  236. 'target' => '',
  237. 'help' => '',
  238. 'trim' => 0,
  239. 'max_length' => '',
  240. 'word_boundary' => 1,
  241. 'ellipsis' => 1,
  242. 'html' => 0,
  243. 'strip_tags' => 0,
  244. ),
  245. 'empty' => '',
  246. 'hide_empty' => 0,
  247. 'empty_zero' => 0,
  248. 'link_to_node' => 1,
  249. 'exclude' => 0,
  250. 'id' => 'name_2',
  251. 'table' => 'stock',
  252. 'field' => 'name',
  253. 'relationship' => 'none',
  254. ),
  255. 'name' => array(
  256. 'label' => 'Type',
  257. 'alter' => array(
  258. 'alter_text' => 0,
  259. 'text' => '',
  260. 'make_link' => 0,
  261. 'path' => '',
  262. 'link_class' => '',
  263. 'alt' => '',
  264. 'prefix' => '',
  265. 'suffix' => '',
  266. 'target' => '',
  267. 'help' => '',
  268. 'trim' => 0,
  269. 'max_length' => '',
  270. 'word_boundary' => 1,
  271. 'ellipsis' => 1,
  272. 'html' => 0,
  273. 'strip_tags' => 0,
  274. ),
  275. 'empty' => '',
  276. 'hide_empty' => 0,
  277. 'empty_zero' => 0,
  278. 'exclude' => 0,
  279. 'id' => 'name',
  280. 'table' => 'cvterm',
  281. 'field' => 'name',
  282. 'relationship' => 'none',
  283. ),
  284. 'common_name' => array(
  285. 'label' => 'Organism',
  286. 'alter' => array(
  287. 'alter_text' => 0,
  288. 'text' => '',
  289. 'make_link' => 0,
  290. 'path' => '',
  291. 'link_class' => '',
  292. 'alt' => '',
  293. 'prefix' => '',
  294. 'suffix' => '',
  295. 'target' => '',
  296. 'help' => '',
  297. 'trim' => 0,
  298. 'max_length' => '',
  299. 'word_boundary' => 1,
  300. 'ellipsis' => 1,
  301. 'html' => 0,
  302. 'strip_tags' => 0,
  303. ),
  304. 'empty' => '',
  305. 'hide_empty' => 0,
  306. 'empty_zero' => 0,
  307. 'link_to_node' => 1,
  308. 'exclude' => 0,
  309. 'id' => 'common_name',
  310. 'table' => 'organism',
  311. 'field' => 'common_name',
  312. 'relationship' => 'none',
  313. ),
  314. 'all_dbxref' => array(
  315. 'label' => 'All Database References',
  316. 'alter' => array(
  317. 'alter_text' => 0,
  318. 'text' => '',
  319. 'make_link' => 0,
  320. 'path' => '',
  321. 'link_class' => '',
  322. 'alt' => '',
  323. 'prefix' => '',
  324. 'suffix' => '',
  325. 'target' => '',
  326. 'help' => '',
  327. 'trim' => 0,
  328. 'max_length' => '',
  329. 'word_boundary' => 1,
  330. 'ellipsis' => 1,
  331. 'html' => 0,
  332. 'strip_tags' => 0,
  333. ),
  334. 'empty' => '',
  335. 'hide_empty' => 0,
  336. 'empty_zero' => 0,
  337. 'type' => 'ul',
  338. 'separator' => ', ',
  339. 'exclude' => 0,
  340. 'id' => 'all_dbxref',
  341. 'table' => 'stock',
  342. 'field' => 'all_dbxref',
  343. 'relationship' => 'none',
  344. ),
  345. 'all_properties' => array(
  346. 'label' => 'All Properties',
  347. 'alter' => array(
  348. 'alter_text' => 0,
  349. 'text' => '',
  350. 'make_link' => 0,
  351. 'path' => '',
  352. 'link_class' => '',
  353. 'alt' => '',
  354. 'prefix' => '',
  355. 'suffix' => '',
  356. 'target' => '',
  357. 'help' => '',
  358. 'trim' => 0,
  359. 'max_length' => '',
  360. 'word_boundary' => 1,
  361. 'ellipsis' => 1,
  362. 'html' => 0,
  363. 'strip_tags' => 0,
  364. ),
  365. 'empty' => '',
  366. 'hide_empty' => 0,
  367. 'empty_zero' => 0,
  368. 'type' => 'ul',
  369. 'separator' => ', ',
  370. 'exclude' => 0,
  371. 'id' => 'all_properties',
  372. 'table' => 'stock',
  373. 'field' => 'all_properties',
  374. 'relationship' => 'none',
  375. ),
  376. 'all_relationships' => array(
  377. 'label' => 'All Relationships',
  378. 'alter' => array(
  379. 'alter_text' => 0,
  380. 'text' => '',
  381. 'make_link' => 0,
  382. 'path' => '',
  383. 'link_class' => '',
  384. 'alt' => '',
  385. 'prefix' => '',
  386. 'suffix' => '',
  387. 'target' => '',
  388. 'help' => '',
  389. 'trim' => 0,
  390. 'max_length' => '',
  391. 'word_boundary' => 1,
  392. 'ellipsis' => 1,
  393. 'html' => 0,
  394. 'strip_tags' => 0,
  395. ),
  396. 'empty' => '',
  397. 'hide_empty' => 0,
  398. 'empty_zero' => 0,
  399. 'type' => 'ul',
  400. 'separator' => ', ',
  401. 'exclude' => 0,
  402. 'id' => 'all_relationships',
  403. 'table' => 'stock',
  404. 'field' => 'all_relationships',
  405. 'relationship' => 'none',
  406. ),
  407. ));
  408. $handler->override_option('sorts', array(
  409. 'common_name' => array(
  410. 'order' => 'ASC',
  411. 'id' => 'common_name',
  412. 'table' => 'organism',
  413. 'field' => 'common_name',
  414. 'relationship' => 'none',
  415. ),
  416. 'uniquename' => array(
  417. 'order' => 'ASC',
  418. 'id' => 'uniquename',
  419. 'table' => 'stock',
  420. 'field' => 'uniquename',
  421. 'relationship' => 'none',
  422. ),
  423. ));
  424. $handler->override_option('filters', array(
  425. 'common_name' => array(
  426. 'operator' => '=',
  427. 'value' => 'All',
  428. 'group' => '0',
  429. 'exposed' => TRUE,
  430. 'expose' => array(
  431. 'use_operator' => 0,
  432. 'operator' => 'common_name_op',
  433. 'identifier' => 'organism_common_name',
  434. 'label' => 'Organism',
  435. 'optional' => 1,
  436. 'remember' => 0,
  437. ),
  438. 'case' => 1,
  439. 'id' => 'common_name',
  440. 'table' => 'organism',
  441. 'field' => 'common_name',
  442. 'relationship' => 'none',
  443. ),
  444. 'name' => array(
  445. 'operator' => '=',
  446. 'value' => 'All',
  447. 'group' => '0',
  448. 'exposed' => TRUE,
  449. 'expose' => array(
  450. 'use_operator' => 0,
  451. 'operator' => 'name_op',
  452. 'identifier' => 'name',
  453. 'label' => 'Chado CV Terms: Name',
  454. 'optional' => 1,
  455. 'remember' => 0,
  456. ),
  457. 'case' => 1,
  458. 'id' => 'name',
  459. 'table' => 'cvterm',
  460. 'field' => 'name',
  461. 'relationship' => 'none',
  462. ),
  463. ));
  464. $handler->override_option('access', array(
  465. 'type' => 'perm',
  466. 'perm' => 'access chado_stock content',
  467. ));
  468. $handler->override_option('cache', array(
  469. 'type' => 'none',
  470. ));
  471. $handler->override_option('title', 'Stocks');
  472. $handler->override_option('empty', 'There are no stocks that match this criteria. If you think there should be, ensure that all stocks in chado are sync\'d with your website.');
  473. $handler->override_option('empty_format', '1');
  474. $handler->override_option('items_per_page', 25);
  475. $handler->override_option('use_pager', '1');
  476. $handler->override_option('style_plugin', 'table');
  477. $handler->override_option('style_options', array(
  478. 'grouping' => '',
  479. 'override' => 1,
  480. 'sticky' => 0,
  481. 'order' => 'asc',
  482. 'columns' => array(
  483. 'uniquename' => 'uniquename',
  484. 'name_2' => 'name_2',
  485. 'name' => 'name',
  486. 'common_name' => 'common_name',
  487. 'all_dbxref' => 'all_dbxref',
  488. 'all_properties' => 'all_properties',
  489. 'all_relationships' => 'all_relationships',
  490. ),
  491. 'info' => array(
  492. 'uniquename' => array(
  493. 'sortable' => 1,
  494. 'separator' => '',
  495. ),
  496. 'name_2' => array(
  497. 'sortable' => 1,
  498. 'separator' => '',
  499. ),
  500. 'name' => array(
  501. 'sortable' => 1,
  502. 'separator' => '',
  503. ),
  504. 'common_name' => array(
  505. 'sortable' => 1,
  506. 'separator' => '',
  507. ),
  508. 'all_dbxref' => array(
  509. 'separator' => '',
  510. ),
  511. 'all_properties' => array(
  512. 'separator' => '',
  513. ),
  514. 'all_relationships' => array(
  515. 'separator' => '',
  516. ),
  517. ),
  518. 'default' => '-1',
  519. ));
  520. $handler = $view->new_display('page', 'Page', 'page_1');
  521. $handler->override_option('path', 'stocks');
  522. $handler->override_option('menu', array(
  523. 'type' => 'normal',
  524. 'title' => 'Stocks',
  525. 'description' => 'A full listing of all chado stocks',
  526. 'weight' => '0',
  527. 'name' => 'primary-links',
  528. ));
  529. $handler->override_option('tab_options', array(
  530. 'type' => 'none',
  531. 'title' => '',
  532. 'description' => '',
  533. 'weight' => 0,
  534. 'name' => 'navigation',
  535. ));
  536. $views[$view->name] = $view;
  537. return $views;
  538. }