tripal_chado.views.inc 16 KB

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