tripal_organism.views.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php
  2. /**
  3. * @file
  4. * This file contains the basic functions for views integration of
  5. * chado/tripal organism 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_organism_views Organism Views Integration
  13. * @ingroup views
  14. * @ingroup tripal_organism
  15. */
  16. /**
  17. * Implements hook_views_data()
  18. * Purpose: Describe chado/tripal tables & fields to views
  19. * @return: a data array which follows the structure outlined in the
  20. * views2 documentation for this hook. Essentially, it's an array of table
  21. * definitions keyed by chado/tripal table name. Each table definition
  22. * includes basic details about the table, fields in that table and
  23. * relationships between that table and others (joins)
  24. *
  25. * @ingroup tripal_organism_views
  26. */
  27. function tripal_organism_views_data() {
  28. $data = array();
  29. if (module_exists('tripal_views')) {
  30. // Base Table
  31. $tables = array(
  32. 'organism'
  33. );
  34. foreach ($tables as $tablename) {
  35. // get the setup with the lightest priority. That's the table
  36. // that currently integrated with views.
  37. $priority = tripal_views_get_lightest_priority_setup($tablename);
  38. // check to see if the table is integrated. If it is then integrate it's
  39. // corresponding 'chado_[table]' table.
  40. if (!tripal_views_is_integrated($tablename, $priority)) {
  41. $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, TRUE);
  42. // Add specialty handlers
  43. $table_integration_array['fields']['common_name']['handlers']['filter']['name'] = 'tripal_views_handler_filter_select_string';
  44. $table_integration_array['fields']['genus']['handlers']['filter']['name'] = 'tripal_views_handler_filter_select_string';
  45. $table_integration_array['fields']['species']['handlers']['filter']['name'] = 'tripal_views_handler_filter_select_string';
  46. $table_integration_array['fields']['abbreviation']['handlers']['filter']['name'] = 'tripal_views_handler_filter_select_string';
  47. // Add in node relationships if chado is in the same db as drupal
  48. if (tripal_core_chado_schema_exists()) {
  49. $integrations = tripal_views_add_node_relationship_to_chado_table_integration($table_integration_array);
  50. foreach ($integrations as $integration) {
  51. tripal_views_integration_add_entry($integration);
  52. }
  53. }
  54. else {
  55. tripal_views_integration_add_entry($table_integration_array);
  56. }
  57. }
  58. }
  59. // Additional Tables
  60. $tables = array(
  61. 'organismprop',
  62. 'organism_dbxref'
  63. );
  64. foreach ($tables as $tablename) {
  65. $priority = tripal_views_get_lightest_priority_setup($tablename);
  66. if (!tripal_views_is_integrated($tablename, $priority)) {
  67. $table_integration_array = tripal_views_get_integration_array_for_chado_table($tablename, FALSE);
  68. tripal_views_integration_add_entry($table_integration_array);
  69. }
  70. }
  71. }
  72. return $data;
  73. }
  74. /**
  75. * Implements hook_views_handlers()
  76. * Purpose: Register all custom handlers with views
  77. * where a handler describes either "the type of field",
  78. * "how a field should be filtered", "how a field should be sorted"
  79. * @return: An array of handler definitions
  80. *
  81. * @ingroup tripal_organism_views
  82. */
  83. function tripal_organism_views_handlers() {
  84. return array(
  85. 'info' => array(
  86. 'path' => drupal_get_path('module', 'tripal_organism') . '/views/handlers',
  87. ),
  88. 'handlers' => array(
  89. 'views_handler_field_computed_organism_nid' => array(
  90. 'parent' => 'views_handler_field_numeric',
  91. ),
  92. 'views_handler_filter_organism_common_name' => array(
  93. 'parent' => 'views_handler_filter_string',
  94. ),
  95. ),
  96. );
  97. }
  98. /**
  99. * Implementation of hook_views_data_alter().
  100. */
  101. function tripal_organism_views_data_alter(&$data) {
  102. if ( !(is_array($db_url) and array_key_exists('chado', $db_url)) ) {
  103. // Add featuer relationship to node
  104. $data['node']['organism_chado_nid'] = array(
  105. 'group' => 'Organism',
  106. 'title' => 'Organism Node',
  107. 'help' => 'Links Chado Organism Fields/Data to the Nodes in the current View.',
  108. 'real field' => 'nid',
  109. 'relationship' => array(
  110. 'handler' => 'views_handler_relationship',
  111. 'title' => t('Node => Chado'),
  112. 'label' => t('Node => Chado'),
  113. 'real field' => 'nid',
  114. 'base' => 'chado_organism',
  115. 'base field' => 'nid'
  116. ),
  117. );
  118. }
  119. }
  120. /**
  121. *
  122. * @ingroup tripal_organism_views
  123. */
  124. function tripal_organism_views_default_views() {
  125. $views = array();
  126. if (!module_exists('tripal_views')) {
  127. return $views;
  128. }
  129. // Main default view
  130. $view = new view;
  131. $view->name = 'organism_listing';
  132. $view->description = 'A listing of all organism in chado';
  133. $view->tag = 'chado default';
  134. $view->base_table = 'organism';
  135. $view->core = 0;
  136. $view->api_version = '2';
  137. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  138. $handler = $view->new_display('default', 'Defaults', 'default');
  139. $handler->override_option('fields', array(
  140. 'common_name' => array(
  141. 'label' => 'Common Name',
  142. 'alter' => array(
  143. 'alter_text' => 0,
  144. 'text' => '',
  145. 'make_link' => 0,
  146. 'path' => '',
  147. 'link_class' => '',
  148. 'alt' => '',
  149. 'prefix' => '',
  150. 'suffix' => '',
  151. 'target' => '',
  152. 'help' => '',
  153. 'trim' => 0,
  154. 'max_length' => '',
  155. 'word_boundary' => 1,
  156. 'ellipsis' => 1,
  157. 'html' => 0,
  158. 'strip_tags' => 0,
  159. ),
  160. 'empty' => '',
  161. 'hide_empty' => 0,
  162. 'empty_zero' => 0,
  163. 'link_to_node' => 1,
  164. 'exclude' => 0,
  165. 'id' => 'common_name',
  166. 'table' => 'organism',
  167. 'field' => 'common_name',
  168. 'relationship' => 'none',
  169. ),
  170. 'genus' => array(
  171. 'label' => 'Genus',
  172. 'alter' => array(
  173. 'alter_text' => 0,
  174. 'text' => '',
  175. 'make_link' => 0,
  176. 'path' => '',
  177. 'link_class' => '',
  178. 'alt' => '',
  179. 'prefix' => '',
  180. 'suffix' => '',
  181. 'target' => '',
  182. 'help' => '',
  183. 'trim' => 0,
  184. 'max_length' => '',
  185. 'word_boundary' => 1,
  186. 'ellipsis' => 1,
  187. 'html' => 0,
  188. 'strip_tags' => 0,
  189. ),
  190. 'empty' => '',
  191. 'hide_empty' => 0,
  192. 'empty_zero' => 0,
  193. 'exclude' => 0,
  194. 'id' => 'genus',
  195. 'table' => 'organism',
  196. 'field' => 'genus',
  197. 'relationship' => 'none',
  198. ),
  199. 'species' => array(
  200. 'label' => 'Species',
  201. 'alter' => array(
  202. 'alter_text' => 0,
  203. 'text' => '',
  204. 'make_link' => 0,
  205. 'path' => '',
  206. 'link_class' => '',
  207. 'alt' => '',
  208. 'prefix' => '',
  209. 'suffix' => '',
  210. 'target' => '',
  211. 'help' => '',
  212. 'trim' => 0,
  213. 'max_length' => '',
  214. 'word_boundary' => 1,
  215. 'ellipsis' => 1,
  216. 'html' => 0,
  217. 'strip_tags' => 0,
  218. ),
  219. 'empty' => '',
  220. 'hide_empty' => 0,
  221. 'empty_zero' => 0,
  222. 'exclude' => 0,
  223. 'id' => 'species',
  224. 'table' => 'organism',
  225. 'field' => 'species',
  226. 'relationship' => 'none',
  227. ),
  228. 'abbreviation' => array(
  229. 'label' => 'Abbreviation',
  230. 'alter' => array(
  231. 'alter_text' => 0,
  232. 'text' => '',
  233. 'make_link' => 0,
  234. 'path' => '',
  235. 'link_class' => '',
  236. 'alt' => '',
  237. 'prefix' => '',
  238. 'suffix' => '',
  239. 'target' => '',
  240. 'help' => '',
  241. 'trim' => 0,
  242. 'max_length' => '',
  243. 'word_boundary' => 1,
  244. 'ellipsis' => 1,
  245. 'html' => 0,
  246. 'strip_tags' => 0,
  247. ),
  248. 'empty' => '',
  249. 'hide_empty' => 0,
  250. 'empty_zero' => 0,
  251. 'link_to_node' => 0,
  252. 'exclude' => 0,
  253. 'id' => 'abbreviation',
  254. 'table' => 'organism',
  255. 'field' => 'abbreviation',
  256. 'relationship' => 'none',
  257. ),
  258. ));
  259. $handler->override_option('sorts', array(
  260. 'genus' => array(
  261. 'order' => 'ASC',
  262. 'id' => 'genus',
  263. 'table' => 'organism',
  264. 'field' => 'genus',
  265. 'relationship' => 'none',
  266. ),
  267. 'species' => array(
  268. 'order' => 'ASC',
  269. 'id' => 'species',
  270. 'table' => 'organism',
  271. 'field' => 'species',
  272. 'relationship' => 'none',
  273. ),
  274. ));
  275. $handler->override_option('access', array(
  276. 'type' => 'perm',
  277. 'perm' => 'access chado_organism content',
  278. ));
  279. $handler->override_option('cache', array(
  280. 'type' => 'none',
  281. ));
  282. $handler->override_option('title', 'Organisms');
  283. $handler->override_option('empty', 'No organisms matched the supplied criteria.');
  284. $handler->override_option('empty_format', '1');
  285. $handler->override_option('items_per_page', 0);
  286. $handler->override_option('style_plugin', 'table');
  287. $handler->override_option('style_options', array(
  288. 'grouping' => '',
  289. 'override' => 1,
  290. 'sticky' => 0,
  291. 'order' => 'asc',
  292. 'summary' => '',
  293. 'columns' => array(
  294. 'common_name' => 'common_name',
  295. 'genus' => 'genus',
  296. 'species' => 'species',
  297. 'abbreviation' => 'abbreviation',
  298. ),
  299. 'info' => array(
  300. 'common_name' => array(
  301. 'sortable' => 1,
  302. 'separator' => '',
  303. ),
  304. 'genus' => array(
  305. 'sortable' => 1,
  306. 'separator' => '',
  307. ),
  308. 'species' => array(
  309. 'sortable' => 1,
  310. 'separator' => '',
  311. ),
  312. 'abbreviation' => array(
  313. 'sortable' => 1,
  314. 'separator' => '',
  315. ),
  316. ),
  317. 'default' => '-1',
  318. ));
  319. $default_handler = $handler;
  320. $handler = $view->new_display('page', 'Page', 'page_1');
  321. $handler->override_option('path', 'chado/organisms');
  322. $handler->override_option('menu', array(
  323. 'type' => 'normal',
  324. 'title' => 'Organisms',
  325. 'description' => 'A biological organism.',
  326. 'weight' => '10',
  327. 'name' => 'navigation',
  328. ));
  329. $handler->override_option('tab_options', array(
  330. 'type' => 'none',
  331. 'title' => '',
  332. 'description' => '',
  333. 'weight' => 0,
  334. 'name' => 'navigation',
  335. ));
  336. // Add code specific to a local chado installation
  337. // NOTE: Edit $handler above to $default_handler for the default display
  338. if (tripal_core_chado_schema_exists()) {
  339. // Add nid field
  340. $fields = $view->get_items('field', 'default');
  341. $new_fields = array(
  342. 'nid' => array(
  343. 'label' => 'Nid',
  344. 'alter' => array(
  345. 'alter_text' => 0,
  346. 'text' => '',
  347. 'make_link' => 0,
  348. 'path' => '',
  349. 'absolute' => 0,
  350. 'link_class' => '',
  351. 'alt' => '',
  352. 'rel' => '',
  353. 'prefix' => '',
  354. 'suffix' => '',
  355. 'target' => '',
  356. 'help' => '',
  357. 'trim' => 0,
  358. 'max_length' => '',
  359. 'word_boundary' => 1,
  360. 'ellipsis' => 1,
  361. 'html' => 0,
  362. 'strip_tags' => 0,
  363. ),
  364. 'empty' => '',
  365. 'hide_empty' => 0,
  366. 'empty_zero' => 0,
  367. 'hide_alter_empty' => 1,
  368. 'link_to_node' => 0,
  369. 'exclude' => 1,
  370. 'id' => 'nid',
  371. 'table' => 'node',
  372. 'field' => 'nid',
  373. 'relationship' => 'none',
  374. )
  375. );
  376. $fields = $new_fields + $fields;
  377. // Adds organism => Node relationship
  378. $default_handler->override_option('relationships', array(
  379. 'nid' => array(
  380. 'label' => 'Organism to Node',
  381. 'required' => 0,
  382. 'id' => 'nid',
  383. 'table' => 'chado_organism',
  384. 'field' => 'nid',
  385. 'relationship' => 'none',
  386. ),
  387. ));
  388. // Change analysis.name to have a link to the node
  389. $fields['common_name']['alter']['make_link'] = 1;
  390. $fields['common_name']['alter']['path'] = 'node/[nid]';
  391. $default_handler->override_option('fields', $fields);
  392. // Only show records with published nodes
  393. $filters = $view->get_items('filter', 'default');
  394. $filters['status'] = array(
  395. 'operator' => '=',
  396. 'value' => '1',
  397. 'group' => '0',
  398. 'exposed' => FALSE,
  399. 'expose' => array(
  400. 'operator' => FALSE,
  401. 'label' => '',
  402. ),
  403. 'id' => 'status',
  404. 'table' => 'node',
  405. 'field' => 'status',
  406. 'relationship' => 'none',
  407. );
  408. $default_handler->override_option('filters', $filters);
  409. }
  410. $views[$view->name] = $view;
  411. return $views;
  412. }