tripal_chado.views.inc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <?php
  2. /**
  3. * @file
  4. * Integrates many of the core database tables with drupal views
  5. */
  6. /**
  7. * Describe various Tripal Core systems to Views
  8. * for the creation of administrative views.
  9. *
  10. * @ingroup tripal
  11. */
  12. function tripal_chado_views_data() {
  13. $data = array();
  14. // Custom Tables Management
  15. $data = tripal_chado_views_data_custom_tables($data);
  16. // Materialized Views Management
  17. $data = tripal_chado_views_data_mviews($data);
  18. return $data;
  19. }
  20. /**
  21. * Implements hook_views_data_alter().
  22. */
  23. function tripal_chado_views_data_alter(&$data) {
  24. // Adds integration for chado-based fields.
  25. tripal_chado_add_field_views_data($data);
  26. return $data;
  27. }
  28. /**
  29. * Adds integration for chado-based fields.
  30. *
  31. * We can't use hook_field_view_data since this only works when the
  32. * storage engine is of type 'field_sql_storage' and of course,
  33. * ours is not. Thus we create our own implementation of field_views_data()
  34. * for our storage engine.
  35. */
  36. function tripal_chado_add_field_views_data(&$data) {
  37. foreach (field_info_fields() as $field) {
  38. if ($field['storage']['type'] != 'field_chado_storage') {
  39. continue;
  40. }
  41. // Currently, we only handle integration of chado fields with TripalEntity.
  42. // @todo: extend this to work with other entities in the future.
  43. if (isset($field['bundles']['TripalEntity']) AND isset($field['settings']['chado_column'])) {
  44. // We currently don't support prop tables for views integration due
  45. // in part to the multiple values but also b/c we can't indicate which
  46. // type of property to show. Thus, instead of warning the user,
  47. // we just won't integrate it at this time.
  48. // @todo: Handle property fields.
  49. if (preg_match('/prop$/', $field['settings']['chado_table'])) {
  50. continue;
  51. }
  52. // Get some information about the chado table in order to make good
  53. // choices for handlers.
  54. $table_desc = chado_get_schema($field['settings']['chado_table']);
  55. $field_defn = $table_desc['fields'][ $field['settings']['chado_column'] ];
  56. // We also need to know if this field is a foreign key.
  57. $fk_defn = FALSE;
  58. foreach ($table_desc['foreign keys'] as $details) {
  59. foreach ($details['columns'] as $left_field => $right_field) {
  60. if ($left_field == $field['settings']['chado_column']) {
  61. $fk_defn = array(
  62. 'left_table' => $field['settings']['chado_table'],
  63. 'left_field' => $left_field,
  64. 'right_table' => $details['table'],
  65. 'right_field' => $right_field,
  66. );
  67. }
  68. }
  69. }
  70. // Unfortunatly we can't use the field label since that is set at the
  71. // instance level and fields are integrated at the field level (independant of bundle).
  72. // Thus we will simply make the most readable and informative field name we can.
  73. $data['tripal_entity'][ $field['field_name'] ]['title'] = ucfirst(str_replace('_',' ',$field['settings']['chado_table']))
  74. . ' ' .ucfirst(str_replace('_',' ',$field['settings']['chado_column']));
  75. // The help should be 'Appears in: TripalEntity: gene, organism'
  76. // so that users know where they can use it. This requires a little extra work since all
  77. // we have access to at this point is bio-data_2, bio-data_4 but since that's not very
  78. // informative, extra work is worth it ;-).
  79. $entity_info = entity_get_info('TripalEntity');
  80. $bundle_labels = array();
  81. foreach ($field['bundles']['TripalEntity'] as $bundle_id) {
  82. $bundle_labels[] = $entity_info['bundles'][$bundle_id]['label'];
  83. }
  84. $data['tripal_entity'][ $field['field_name'] ]['help'] = 'Appears in: TripalEntity:' . implode(', ', $bundle_labels);
  85. // Define the field.
  86. $data['tripal_entity'][ $field['field_name'] ]['field']['chado_field'] = $field['settings']['chado_column'];
  87. $data['tripal_entity'][ $field['field_name'] ]['field']['chado_table'] = $field['settings']['chado_table'];
  88. $data['tripal_entity'][ $field['field_name'] ]['field']['field_name'] = $field['field_name'];
  89. $data['tripal_entity'][ $field['field_name'] ]['field']['entity_table'] = 'tripal_entity';
  90. $data['tripal_entity'][ $field['field_name'] ]['field']['entity_type'] = 'TripalEntity';
  91. $data['tripal_entity'][ $field['field_name'] ]['field']['bundles'] = $field['bundles']['TripalEntity'];
  92. $data['tripal_entity'][ $field['field_name'] ]['field']['handler'] = 'chado_views_handler_field';
  93. $data['tripal_entity'][ $field['field_name'] ]['field']['click sortable'] = FALSE;
  94. // Define the Filter.
  95. $data['tripal_entity'][ $field['field_name'] ]['filter']['chado_field'] = $field['settings']['chado_column'];
  96. $data['tripal_entity'][ $field['field_name'] ]['filter']['chado_table'] = $field['settings']['chado_table'];
  97. $data['tripal_entity'][ $field['field_name'] ]['filter']['field_name'] = $field['field_name'];
  98. $data['tripal_entity'][ $field['field_name'] ]['filter']['entity_table'] = 'tripal_entity';
  99. $data['tripal_entity'][ $field['field_name'] ]['filter']['entity_type'] = 'TripalEntity';
  100. $data['tripal_entity'][ $field['field_name'] ]['filter']['bundles'] = $field['bundles']['TripalEntity'];
  101. $data['tripal_entity'][ $field['field_name'] ]['filter']['handler'] = 'chado_views_handler_filter_string';
  102. // If this is a foreign key field like organism then we want a drop-down.
  103. // This requires a special handler.
  104. if ($fk_defn) {
  105. $data['tripal_entity'][ $field['field_name'] ]['filter']['handler'] = 'chado_views_handler_filter_fk';
  106. $data['tripal_entity'][ $field['field_name'] ]['filter']['foreign_key'] = $fk_defn;
  107. }
  108. if ($field_defn['type'] == 'boolean') {
  109. $data['tripal_entity'][ $field['field_name'] ]['filter']['handler'] = 'chado_views_handler_filter_boolean';
  110. $data['tripal_entity'][ $field['field_name'] ]['filter']['label'] = $field['settings']['chado_column'];
  111. $data['tripal_entity'][ $field['field_name'] ]['filter']['type'] = 'yes-no';
  112. }
  113. }
  114. }
  115. }
  116. /**
  117. * Provides the data array for the tripal custom tables management
  118. *
  119. * @param $data
  120. * Previously generated tripal views data array
  121. * return
  122. * $data array with custom tables management described
  123. *
  124. * @ingroup tripal
  125. */
  126. function tripal_chado_views_data_custom_tables($data) {
  127. $data['tripal_custom_tables']['table']['group'] = t('Tripal Custom Tables');
  128. $data['tripal_custom_tables']['table']['base'] = array(
  129. 'field' => 'table_id', // This is the identifier field for the view.
  130. 'title' => t('Tripal Custom Tables'),
  131. 'help' => t('Custom Tables in Chado created by this Tripal Installation.'),
  132. 'weight' => 10,
  133. );
  134. // Table ID
  135. $data['tripal_custom_tables']['table_id'] = array(
  136. 'title' => t('Custom Table ID'),
  137. 'help' => t('Custom table primary key.'),
  138. 'field' => array(
  139. 'handler' => 'views_handler_field_numeric',
  140. 'click sortable' => TRUE,
  141. ),
  142. 'filter' => array(
  143. 'handler' => 'views_handler_filter_numeric',
  144. ),
  145. 'sort' => array(
  146. 'handler' => 'views_handler_sort',
  147. ),
  148. );
  149. // Table Name
  150. $data['tripal_custom_tables']['table_name'] = array(
  151. 'title' => t('Table Name'),
  152. 'help' => t('The name of the table in the database.'),
  153. 'field' => array(
  154. 'handler' => 'views_handler_field',
  155. 'click sortable' => TRUE, // This is use by the table display plugin.
  156. ),
  157. 'sort' => array(
  158. 'handler' => 'views_handler_sort',
  159. ),
  160. 'filter' => array(
  161. 'handler' => 'views_handler_filter_string',
  162. ),
  163. 'argument' => array(
  164. 'handler' => 'views_handler_argument_string',
  165. ),
  166. );
  167. // Schema
  168. $data['tripal_custom_tables']['schema'] = array(
  169. 'title' => t('Table Schema'),
  170. 'help' => t('The schema definition of the table.'),
  171. 'field' => array(
  172. 'handler' => 'views_handler_field',
  173. 'click sortable' => TRUE, // This is use by the table display plugin.
  174. ),
  175. 'sort' => array(
  176. 'handler' => 'views_handler_sort',
  177. ),
  178. 'filter' => array(
  179. 'handler' => 'views_handler_filter_string',
  180. ),
  181. 'argument' => array(
  182. 'handler' => 'views_handler_argument_string',
  183. ),
  184. );
  185. // Table ID
  186. $data['tripal_custom_tables']['mview_id'] = array(
  187. 'title' => t('Materialized View ID'),
  188. 'help' => t('Foreign key to tripal_mviews table for the materialized view.'),
  189. 'field' => array(
  190. 'handler' => 'views_handler_field_numeric',
  191. 'click sortable' => TRUE,
  192. ),
  193. 'filter' => array(
  194. 'handler' => 'views_handler_filter_numeric',
  195. ),
  196. 'sort' => array(
  197. 'handler' => 'views_handler_sort',
  198. ),
  199. );
  200. return $data;
  201. }
  202. /**
  203. * Provides the data array for the tripal custom tables management
  204. *
  205. * @param $data
  206. * Previously generated tripal views data array
  207. * return
  208. * $data array with custom tables management described
  209. *
  210. * @ingroup tripal
  211. */
  212. function tripal_chado_views_data_mviews($data) {
  213. $data['tripal_mviews']['table']['group'] = t('Tripal Materialized Views');
  214. $data['tripal_mviews']['table']['base'] = array(
  215. 'field' => 'mview_id', // This is the identifier field for the view.
  216. 'title' => t('Tripal Materialized Views'),
  217. 'help' => t('Materialized Views in Chado created by this Tripal Installation.'),
  218. 'weight' => 10,
  219. );
  220. // Implicit Join to Tripal Views
  221. $data['tripal_mviews']['table']['join'] = array(
  222. 'tripal_views' => array(
  223. 'left_field' => 'mview_id',
  224. 'field' => 'mview_id',
  225. ),
  226. );
  227. // Mview ID
  228. $data['tripal_mviews']['mview_id'] = array(
  229. 'title' => t('Materialized View ID'),
  230. 'help' => t('The primary key.'),
  231. 'field' => array(
  232. 'handler' => 'views_handler_field_numeric',
  233. 'click sortable' => TRUE,
  234. ),
  235. 'filter' => array(
  236. 'handler' => 'views_handler_filter_numeric',
  237. ),
  238. 'sort' => array(
  239. 'handler' => 'views_handler_sort',
  240. ),
  241. );
  242. // name
  243. $data['tripal_mviews']['name'] = array(
  244. 'title' => t('Name'),
  245. 'help' => t('Human-readable name of the materialized view.'),
  246. 'field' => array(
  247. 'handler' => 'views_handler_field',
  248. 'click sortable' => TRUE, // This is use by the table display plugin.
  249. ),
  250. 'sort' => array(
  251. 'handler' => 'views_handler_sort',
  252. ),
  253. 'filter' => array(
  254. 'handler' => 'views_handler_filter_string',
  255. ),
  256. 'argument' => array(
  257. 'handler' => 'views_handler_argument_string',
  258. ),
  259. );
  260. // modulename
  261. $data['tripal_mviews']['modulename'] = array(
  262. 'title' => t('Module Name'),
  263. 'help' => t('The module that created the materialized view.'),
  264. 'field' => array(
  265. 'handler' => 'views_handler_field',
  266. 'click sortable' => TRUE, // This is use by the table display plugin.
  267. ),
  268. 'sort' => array(
  269. 'handler' => 'views_handler_sort',
  270. ),
  271. 'filter' => array(
  272. 'handler' => 'views_handler_filter_string',
  273. ),
  274. 'argument' => array(
  275. 'handler' => 'views_handler_argument_string',
  276. ),
  277. );
  278. // mv_table
  279. $data['tripal_mviews']['mv_table'] = array(
  280. 'title' => t('Table'),
  281. 'help' => t('The database table the materialized view is stored in.'),
  282. 'field' => array(
  283. 'handler' => 'views_handler_field',
  284. 'click sortable' => TRUE, // This is use by the table display plugin.
  285. ),
  286. 'sort' => array(
  287. 'handler' => 'views_handler_sort',
  288. ),
  289. 'filter' => array(
  290. 'handler' => 'views_handler_filter_string',
  291. ),
  292. 'argument' => array(
  293. 'handler' => 'views_handler_argument_string',
  294. ),
  295. );
  296. // mv_specs
  297. $data['tripal_mviews']['mv_specs'] = array(
  298. 'title' => t('Specification'),
  299. 'help' => t('Materialized View Specification.'),
  300. 'field' => array(
  301. 'handler' => 'views_handler_field',
  302. 'click sortable' => TRUE, // This is use by the table display plugin.
  303. ),
  304. 'sort' => array(
  305. 'handler' => 'views_handler_sort',
  306. ),
  307. 'filter' => array(
  308. 'handler' => 'views_handler_filter_string',
  309. ),
  310. 'argument' => array(
  311. 'handler' => 'views_handler_argument_string',
  312. ),
  313. );
  314. // mv_schema
  315. $data['tripal_mviews']['mv_schema'] = array(
  316. 'title' => t('Schema'),
  317. 'help' => t('Schema definition for the materialized view table.'),
  318. 'field' => array(
  319. 'handler' => 'views_handler_field',
  320. 'click sortable' => TRUE, // This is use by the table display plugin.
  321. ),
  322. 'sort' => array(
  323. 'handler' => 'views_handler_sort',
  324. ),
  325. 'filter' => array(
  326. 'handler' => 'views_handler_filter_string',
  327. ),
  328. 'argument' => array(
  329. 'handler' => 'views_handler_argument_string',
  330. ),
  331. );
  332. // indexed
  333. $data['tripal_mviews']['indexed'] = array(
  334. 'title' => t('Indices'),
  335. 'help' => t('Any indices for this materialized view.'),
  336. 'field' => array(
  337. 'handler' => 'views_handler_field',
  338. 'click sortable' => TRUE, // This is use by the table display plugin.
  339. ),
  340. 'sort' => array(
  341. 'handler' => 'views_handler_sort',
  342. ),
  343. 'filter' => array(
  344. 'handler' => 'views_handler_filter_string',
  345. ),
  346. 'argument' => array(
  347. 'handler' => 'views_handler_argument_string',
  348. ),
  349. );
  350. // query
  351. $data['tripal_mviews']['query'] = array(
  352. 'title' => t('Query'),
  353. 'help' => t('The query used to populate the materialized view.'),
  354. 'field' => array(
  355. 'handler' => 'views_handler_field',
  356. 'click sortable' => TRUE, // This is use by the table display plugin.
  357. ),
  358. 'sort' => array(
  359. 'handler' => 'views_handler_sort',
  360. ),
  361. 'filter' => array(
  362. 'handler' => 'views_handler_filter_string',
  363. ),
  364. 'argument' => array(
  365. 'handler' => 'views_handler_argument_string',
  366. ),
  367. );
  368. // special_index
  369. $data['tripal_mviews']['special_index'] = array(
  370. 'title' => t('Special Index'),
  371. 'help' => t('Any special indices for the materialized view.'),
  372. 'field' => array(
  373. 'handler' => 'views_handler_field',
  374. 'click sortable' => TRUE, // This is use by the table display plugin.
  375. ),
  376. 'sort' => array(
  377. 'handler' => 'views_handler_sort',
  378. ),
  379. 'filter' => array(
  380. 'handler' => 'views_handler_filter_string',
  381. ),
  382. 'argument' => array(
  383. 'handler' => 'views_handler_argument_string',
  384. ),
  385. );
  386. // last_update
  387. $data['tripal_mviews']['last_update'] = array(
  388. 'title' => t('Updated'),
  389. 'help' => t('Date Last Updated.'),
  390. 'field' => array(
  391. 'handler' => 'views_handler_field_date',
  392. 'click sortable' => TRUE,
  393. ),
  394. 'sort' => array(
  395. 'handler' => 'views_handler_sort_date',
  396. ),
  397. 'filter' => array(
  398. 'handler' => 'views_handler_filter_date',
  399. ),
  400. );
  401. // status
  402. $data['tripal_mviews']['status'] = array(
  403. 'title' => t('Status'),
  404. 'help' => t('The status of the materialized view.'),
  405. 'field' => array(
  406. 'handler' => 'views_handler_field',
  407. 'click sortable' => TRUE, // This is use by the table display plugin.
  408. ),
  409. 'sort' => array(
  410. 'handler' => 'views_handler_sort',
  411. ),
  412. 'filter' => array(
  413. 'handler' => 'views_handler_filter_string',
  414. ),
  415. 'argument' => array(
  416. 'handler' => 'views_handler_argument_string',
  417. ),
  418. );
  419. // comment
  420. $data['tripal_mviews']['comment'] = array(
  421. 'title' => t('Description'),
  422. 'help' => t('Human-Readable Admin Description.'),
  423. 'field' => array(
  424. 'handler' => 'views_handler_field',
  425. 'click sortable' => TRUE, // This is use by the table display plugin.
  426. ),
  427. 'sort' => array(
  428. 'handler' => 'views_handler_sort',
  429. ),
  430. 'filter' => array(
  431. 'handler' => 'views_handler_filter_string',
  432. ),
  433. 'argument' => array(
  434. 'handler' => 'views_handler_argument_string',
  435. ),
  436. );
  437. return $data;
  438. }