tripal_chado.views.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. // Define sorting.
  103. $data['tripal_entity'][ $field['field_name'] ]['sort']['chado_field'] = $field['settings']['chado_column'];
  104. $data['tripal_entity'][ $field['field_name'] ]['sort']['chado_table'] = $field['settings']['chado_table'];
  105. $data['tripal_entity'][ $field['field_name'] ]['sort']['field_name'] = $field['field_name'];
  106. $data['tripal_entity'][ $field['field_name'] ]['sort']['entity_table'] = 'tripal_entity';
  107. $data['tripal_entity'][ $field['field_name'] ]['sort']['entity_type'] = 'TripalEntity';
  108. $data['tripal_entity'][ $field['field_name'] ]['sort']['bundles'] = $field['bundles']['TripalEntity'];
  109. $data['tripal_entity'][ $field['field_name'] ]['sort']['handler'] = 'chado_views_handler_sort';
  110. // Specify special handlers.
  111. if ($fk_defn) {
  112. $data['tripal_entity'][ $field['field_name'] ]['filter']['handler'] = 'chado_views_handler_filter_fk';
  113. $data['tripal_entity'][ $field['field_name'] ]['filter']['foreign_key'] = $fk_defn;
  114. }
  115. if ($field_defn['type'] == 'boolean') {
  116. $data['tripal_entity'][ $field['field_name'] ]['filter']['handler'] = 'chado_views_handler_filter_boolean';
  117. $data['tripal_entity'][ $field['field_name'] ]['filter']['label'] = $field['settings']['chado_column'];
  118. $data['tripal_entity'][ $field['field_name'] ]['filter']['type'] = 'yes-no';
  119. }
  120. elseif ($field_defn['type'] == 'datetime') {
  121. $data['tripal_entity'][ $field['field_name'] ]['filter']['handler'] = 'chado_views_handler_filter_date';
  122. }
  123. }
  124. }
  125. }
  126. /**
  127. * Provides the data array for the tripal custom tables management
  128. *
  129. * @param $data
  130. * Previously generated tripal views data array
  131. * return
  132. * $data array with custom tables management described
  133. *
  134. * @ingroup tripal
  135. */
  136. function tripal_chado_views_data_custom_tables($data) {
  137. $data['tripal_custom_tables']['table']['group'] = t('Tripal Custom Tables');
  138. $data['tripal_custom_tables']['table']['base'] = array(
  139. 'field' => 'table_id', // This is the identifier field for the view.
  140. 'title' => t('Tripal Custom Tables'),
  141. 'help' => t('Custom Tables in Chado created by this Tripal Installation.'),
  142. 'weight' => 10,
  143. );
  144. // Table ID
  145. $data['tripal_custom_tables']['table_id'] = array(
  146. 'title' => t('Custom Table ID'),
  147. 'help' => t('Custom table primary key.'),
  148. 'field' => array(
  149. 'handler' => 'views_handler_field_numeric',
  150. 'click sortable' => TRUE,
  151. ),
  152. 'filter' => array(
  153. 'handler' => 'views_handler_filter_numeric',
  154. ),
  155. 'sort' => array(
  156. 'handler' => 'views_handler_sort',
  157. ),
  158. );
  159. // Table Name
  160. $data['tripal_custom_tables']['table_name'] = array(
  161. 'title' => t('Table Name'),
  162. 'help' => t('The name of the table in the database.'),
  163. 'field' => array(
  164. 'handler' => 'views_handler_field',
  165. 'click sortable' => TRUE, // This is use by the table display plugin.
  166. ),
  167. 'sort' => array(
  168. 'handler' => 'views_handler_sort',
  169. ),
  170. 'filter' => array(
  171. 'handler' => 'views_handler_filter_string',
  172. ),
  173. 'argument' => array(
  174. 'handler' => 'views_handler_argument_string',
  175. ),
  176. );
  177. // Schema
  178. $data['tripal_custom_tables']['schema'] = array(
  179. 'title' => t('Table Schema'),
  180. 'help' => t('The schema definition of the table.'),
  181. 'field' => array(
  182. 'handler' => 'views_handler_field',
  183. 'click sortable' => TRUE, // This is use by the table display plugin.
  184. ),
  185. 'sort' => array(
  186. 'handler' => 'views_handler_sort',
  187. ),
  188. 'filter' => array(
  189. 'handler' => 'views_handler_filter_string',
  190. ),
  191. 'argument' => array(
  192. 'handler' => 'views_handler_argument_string',
  193. ),
  194. );
  195. // Table ID
  196. $data['tripal_custom_tables']['mview_id'] = array(
  197. 'title' => t('Materialized View ID'),
  198. 'help' => t('Foreign key to tripal_mviews table for the materialized view.'),
  199. 'field' => array(
  200. 'handler' => 'views_handler_field_numeric',
  201. 'click sortable' => TRUE,
  202. ),
  203. 'filter' => array(
  204. 'handler' => 'views_handler_filter_numeric',
  205. ),
  206. 'sort' => array(
  207. 'handler' => 'views_handler_sort',
  208. ),
  209. );
  210. return $data;
  211. }
  212. /**
  213. * Provides the data array for the tripal custom tables management
  214. *
  215. * @param $data
  216. * Previously generated tripal views data array
  217. * return
  218. * $data array with custom tables management described
  219. *
  220. * @ingroup tripal
  221. */
  222. function tripal_chado_views_data_mviews($data) {
  223. $data['tripal_mviews']['table']['group'] = t('Tripal Materialized Views');
  224. $data['tripal_mviews']['table']['base'] = array(
  225. 'field' => 'mview_id', // This is the identifier field for the view.
  226. 'title' => t('Tripal Materialized Views'),
  227. 'help' => t('Materialized Views in Chado created by this Tripal Installation.'),
  228. 'weight' => 10,
  229. );
  230. // Implicit Join to Tripal Views
  231. $data['tripal_mviews']['table']['join'] = array(
  232. 'tripal_views' => array(
  233. 'left_field' => 'mview_id',
  234. 'field' => 'mview_id',
  235. ),
  236. );
  237. // Mview ID
  238. $data['tripal_mviews']['mview_id'] = array(
  239. 'title' => t('Materialized View ID'),
  240. 'help' => t('The primary key.'),
  241. 'field' => array(
  242. 'handler' => 'views_handler_field_numeric',
  243. 'click sortable' => TRUE,
  244. ),
  245. 'filter' => array(
  246. 'handler' => 'views_handler_filter_numeric',
  247. ),
  248. 'sort' => array(
  249. 'handler' => 'views_handler_sort',
  250. ),
  251. );
  252. // name
  253. $data['tripal_mviews']['name'] = array(
  254. 'title' => t('Name'),
  255. 'help' => t('Human-readable name of the materialized view.'),
  256. 'field' => array(
  257. 'handler' => 'views_handler_field',
  258. 'click sortable' => TRUE, // This is use by the table display plugin.
  259. ),
  260. 'sort' => array(
  261. 'handler' => 'views_handler_sort',
  262. ),
  263. 'filter' => array(
  264. 'handler' => 'views_handler_filter_string',
  265. ),
  266. 'argument' => array(
  267. 'handler' => 'views_handler_argument_string',
  268. ),
  269. );
  270. // modulename
  271. $data['tripal_mviews']['modulename'] = array(
  272. 'title' => t('Module Name'),
  273. 'help' => t('The module that created the materialized view.'),
  274. 'field' => array(
  275. 'handler' => 'views_handler_field',
  276. 'click sortable' => TRUE, // This is use by the table display plugin.
  277. ),
  278. 'sort' => array(
  279. 'handler' => 'views_handler_sort',
  280. ),
  281. 'filter' => array(
  282. 'handler' => 'views_handler_filter_string',
  283. ),
  284. 'argument' => array(
  285. 'handler' => 'views_handler_argument_string',
  286. ),
  287. );
  288. // mv_table
  289. $data['tripal_mviews']['mv_table'] = array(
  290. 'title' => t('Table'),
  291. 'help' => t('The database table the materialized view is stored in.'),
  292. 'field' => array(
  293. 'handler' => 'views_handler_field',
  294. 'click sortable' => TRUE, // This is use by the table display plugin.
  295. ),
  296. 'sort' => array(
  297. 'handler' => 'views_handler_sort',
  298. ),
  299. 'filter' => array(
  300. 'handler' => 'views_handler_filter_string',
  301. ),
  302. 'argument' => array(
  303. 'handler' => 'views_handler_argument_string',
  304. ),
  305. );
  306. // mv_specs
  307. $data['tripal_mviews']['mv_specs'] = array(
  308. 'title' => t('Specification'),
  309. 'help' => t('Materialized View Specification.'),
  310. 'field' => array(
  311. 'handler' => 'views_handler_field',
  312. 'click sortable' => TRUE, // This is use by the table display plugin.
  313. ),
  314. 'sort' => array(
  315. 'handler' => 'views_handler_sort',
  316. ),
  317. 'filter' => array(
  318. 'handler' => 'views_handler_filter_string',
  319. ),
  320. 'argument' => array(
  321. 'handler' => 'views_handler_argument_string',
  322. ),
  323. );
  324. // mv_schema
  325. $data['tripal_mviews']['mv_schema'] = array(
  326. 'title' => t('Schema'),
  327. 'help' => t('Schema definition for the materialized view table.'),
  328. 'field' => array(
  329. 'handler' => 'views_handler_field',
  330. 'click sortable' => TRUE, // This is use by the table display plugin.
  331. ),
  332. 'sort' => array(
  333. 'handler' => 'views_handler_sort',
  334. ),
  335. 'filter' => array(
  336. 'handler' => 'views_handler_filter_string',
  337. ),
  338. 'argument' => array(
  339. 'handler' => 'views_handler_argument_string',
  340. ),
  341. );
  342. // indexed
  343. $data['tripal_mviews']['indexed'] = array(
  344. 'title' => t('Indices'),
  345. 'help' => t('Any indices for this materialized view.'),
  346. 'field' => array(
  347. 'handler' => 'views_handler_field',
  348. 'click sortable' => TRUE, // This is use by the table display plugin.
  349. ),
  350. 'sort' => array(
  351. 'handler' => 'views_handler_sort',
  352. ),
  353. 'filter' => array(
  354. 'handler' => 'views_handler_filter_string',
  355. ),
  356. 'argument' => array(
  357. 'handler' => 'views_handler_argument_string',
  358. ),
  359. );
  360. // query
  361. $data['tripal_mviews']['query'] = array(
  362. 'title' => t('Query'),
  363. 'help' => t('The query used to populate the materialized view.'),
  364. 'field' => array(
  365. 'handler' => 'views_handler_field',
  366. 'click sortable' => TRUE, // This is use by the table display plugin.
  367. ),
  368. 'sort' => array(
  369. 'handler' => 'views_handler_sort',
  370. ),
  371. 'filter' => array(
  372. 'handler' => 'views_handler_filter_string',
  373. ),
  374. 'argument' => array(
  375. 'handler' => 'views_handler_argument_string',
  376. ),
  377. );
  378. // special_index
  379. $data['tripal_mviews']['special_index'] = array(
  380. 'title' => t('Special Index'),
  381. 'help' => t('Any special indices for the materialized view.'),
  382. 'field' => array(
  383. 'handler' => 'views_handler_field',
  384. 'click sortable' => TRUE, // This is use by the table display plugin.
  385. ),
  386. 'sort' => array(
  387. 'handler' => 'views_handler_sort',
  388. ),
  389. 'filter' => array(
  390. 'handler' => 'views_handler_filter_string',
  391. ),
  392. 'argument' => array(
  393. 'handler' => 'views_handler_argument_string',
  394. ),
  395. );
  396. // last_update
  397. $data['tripal_mviews']['last_update'] = array(
  398. 'title' => t('Updated'),
  399. 'help' => t('Date Last Updated.'),
  400. 'field' => array(
  401. 'handler' => 'views_handler_field_date',
  402. 'click sortable' => TRUE,
  403. ),
  404. 'sort' => array(
  405. 'handler' => 'views_handler_sort_date',
  406. ),
  407. 'filter' => array(
  408. 'handler' => 'views_handler_filter_date',
  409. ),
  410. );
  411. // status
  412. $data['tripal_mviews']['status'] = array(
  413. 'title' => t('Status'),
  414. 'help' => t('The status of the materialized view.'),
  415. 'field' => array(
  416. 'handler' => 'views_handler_field',
  417. 'click sortable' => TRUE, // This is use by the table display plugin.
  418. ),
  419. 'sort' => array(
  420. 'handler' => 'views_handler_sort',
  421. ),
  422. 'filter' => array(
  423. 'handler' => 'views_handler_filter_string',
  424. ),
  425. 'argument' => array(
  426. 'handler' => 'views_handler_argument_string',
  427. ),
  428. );
  429. // comment
  430. $data['tripal_mviews']['comment'] = array(
  431. 'title' => t('Description'),
  432. 'help' => t('Human-Readable Admin Description.'),
  433. 'field' => array(
  434. 'handler' => 'views_handler_field',
  435. 'click sortable' => TRUE, // This is use by the table display plugin.
  436. ),
  437. 'sort' => array(
  438. 'handler' => 'views_handler_sort',
  439. ),
  440. 'filter' => array(
  441. 'handler' => 'views_handler_filter_string',
  442. ),
  443. 'argument' => array(
  444. 'handler' => 'views_handler_argument_string',
  445. ),
  446. );
  447. return $data;
  448. }