tripal_stock.views.inc 16 KB

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