tripal_stock.views.inc 16 KB

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