tripal_stock.views.inc 18 KB

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