tripal_stock.views.inc 17 KB

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