tripal_stock.views.inc 18 KB

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