tripal_stock.views.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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, 9)) {
  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, 9)) {
  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. // Main default view
  234. $view = new view;
  235. $view->name = 'stock_listing';
  236. $view->description = 'This view lists all stocks by default.';
  237. $view->tag = 'chado default';
  238. $view->base_table = 'stock';
  239. $view->core = 0;
  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. ));
  364. $handler->override_option('sorts', array(
  365. 'common_name' => array(
  366. 'order' => 'ASC',
  367. 'id' => 'common_name',
  368. 'table' => 'organism',
  369. 'field' => 'common_name',
  370. 'relationship' => 'none',
  371. ),
  372. 'uniquename' => array(
  373. 'order' => 'ASC',
  374. 'id' => 'uniquename',
  375. 'table' => 'stock',
  376. 'field' => 'uniquename',
  377. 'relationship' => 'none',
  378. ),
  379. ));
  380. $handler->override_option('filters', array(
  381. 'common_name' => array(
  382. 'operator' => '=',
  383. 'value' => array(),
  384. 'group' => '0',
  385. 'exposed' => TRUE,
  386. 'expose' => array(
  387. 'use_operator' => 0,
  388. 'operator' => 'common_name_op',
  389. 'identifier' => 'organism_common_name',
  390. 'label' => 'Organism',
  391. 'remember' => 0,
  392. ),
  393. 'case' => 1,
  394. 'id' => 'common_name',
  395. 'table' => 'organism',
  396. 'field' => 'common_name',
  397. 'relationship' => 'none',
  398. 'values_form_type' => 'select',
  399. 'multiple' => 1,
  400. 'optional' => 0,
  401. 'agg' => array(
  402. 'records_with' => 1,
  403. 'aggregates_with' => 1,
  404. ),
  405. ),
  406. 'search_results' => array(
  407. 'operator' => '=',
  408. 'value' => '',
  409. 'group' => '0',
  410. 'exposed' => FALSE,
  411. 'expose' => array(
  412. 'operator' => FALSE,
  413. 'label' => '',
  414. ),
  415. 'id' => 'search_results',
  416. 'table' => 'views',
  417. 'field' => 'search_results',
  418. 'relationship' => 'none',
  419. 'apply_button' => 'Show',
  420. 'no_results_text' => 'Click "Show" to see a list of all stocks matching the entered criteria. If you leave a any of the criteria blank then the stocks will be not be filtered based on that field. Furthermore, if you leave all criteria blank then all stocks will be listed.',
  421. ),
  422. 'type_id' => array(
  423. 'operator' => '=',
  424. 'value' => '',
  425. 'group' => '0',
  426. 'exposed' => TRUE,
  427. 'expose' => array(
  428. 'use_operator' => 0,
  429. 'operator' => 'type_id_op',
  430. 'identifier' => 'type_id',
  431. 'label' => 'Type',
  432. 'remember' => 0,
  433. ),
  434. 'case' => 1,
  435. 'id' => 'type_id',
  436. 'table' => 'stock',
  437. 'field' => 'type_id',
  438. 'relationship' => 'none',
  439. 'values_form_type' => 'select',
  440. 'multiple' => 1,
  441. 'optional' => 0,
  442. 'show_all' => 0,
  443. 'agg' => array(
  444. 'records_with' => 1,
  445. 'aggregates_with' => 1,
  446. ),
  447. ),
  448. 'name' => array(
  449. 'operator' => 'contains',
  450. 'value' => '',
  451. 'group' => '0',
  452. 'exposed' => TRUE,
  453. 'expose' => array(
  454. 'use_operator' => 0,
  455. 'operator' => 'name_op',
  456. 'identifier' => 'name',
  457. 'label' => 'Name Contains',
  458. 'remember' => 0,
  459. ),
  460. 'case' => 0,
  461. 'id' => 'name',
  462. 'table' => 'stock',
  463. 'field' => 'name',
  464. 'relationship' => 'none',
  465. 'agg' => array(
  466. 'records_with' => 1,
  467. 'aggregates_with' => 1,
  468. ),
  469. ),
  470. ));
  471. $handler->override_option('access', array(
  472. 'type' => 'perm',
  473. 'perm' => 'access chado_stock content',
  474. ));
  475. $handler->override_option('cache', array(
  476. 'type' => 'none',
  477. ));
  478. $handler->override_option('title', 'Stocks');
  479. $handler->override_option('header', 'Click "Show" to see a list of all stocks matching the entered criteria. If you leave a any of the criteria blank then the stocks will be not be filtered based on that field. Furthermore, if you leave all criteria blank then all stocks will be listed.');
  480. $handler->override_option('header_format', '2');
  481. $handler->override_option('header_empty', 0);
  482. $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.');
  483. $handler->override_option('empty_format', '1');
  484. $handler->override_option('items_per_page', 50);
  485. $handler->override_option('use_pager', '1');
  486. $handler->override_option('style_plugin', 'table');
  487. $handler->override_option('style_options', array(
  488. 'grouping' => '',
  489. 'override' => 1,
  490. 'sticky' => 0,
  491. 'order' => 'asc',
  492. 'columns' => array(
  493. 'uniquename' => 'uniquename',
  494. 'name_2' => 'name_2',
  495. 'name' => 'name',
  496. 'common_name' => 'common_name',
  497. 'all_dbxref' => 'all_dbxref',
  498. 'all_properties' => 'all_properties',
  499. 'all_relationships' => 'all_relationships',
  500. ),
  501. 'info' => array(
  502. 'uniquename' => array(
  503. 'sortable' => 1,
  504. 'separator' => '',
  505. ),
  506. 'name_2' => array(
  507. 'sortable' => 1,
  508. 'separator' => '',
  509. ),
  510. 'name' => array(
  511. 'sortable' => 1,
  512. 'separator' => '',
  513. ),
  514. 'common_name' => array(
  515. 'sortable' => 1,
  516. 'separator' => '',
  517. ),
  518. 'all_dbxref' => array(
  519. 'separator' => '',
  520. ),
  521. 'all_properties' => array(
  522. 'separator' => '',
  523. ),
  524. 'all_relationships' => array(
  525. 'separator' => '',
  526. ),
  527. ),
  528. 'default' => '-1',
  529. ));
  530. $handler = $view->new_display('page', 'Page', 'page_1');
  531. $handler->override_option('path', 'stocks');
  532. $handler->override_option('menu', array(
  533. 'type' => 'normal',
  534. 'title' => 'Stocks',
  535. 'description' => 'A full listing of all chado stocks',
  536. 'weight' => '0',
  537. 'name' => 'primary-links',
  538. ));
  539. $handler->override_option('tab_options', array(
  540. 'type' => 'none',
  541. 'title' => '',
  542. 'description' => '',
  543. 'weight' => 0,
  544. 'name' => 'navigation',
  545. ));
  546. $views[$view->name] = $view;
  547. return $views;
  548. }