tripal.views.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. <?php
  2. /**
  3. * @file
  4. * Integrates many of the core database tables with drupal views
  5. */
  6. /**
  7. * Implements of hook_views_plugins().
  8. */
  9. function tripal_views_plugins() {
  10. return array(
  11. 'module' => 'tripal',
  12. 'query' => array(
  13. 'tripal_views_query' => array(
  14. 'title' => t('Tripal Entity Query'),
  15. 'help' => t('Query that allows you to search with Tripal entities.'),
  16. 'handler' => 'tripal_views_query',
  17. 'parent' => 'views_query',
  18. ),
  19. ),
  20. );
  21. }
  22. /**
  23. * Describe various Tripal Core systems to Views
  24. *
  25. * @ingroup tripal
  26. */
  27. function tripal_views_data() {
  28. $data = array();
  29. // Job Management System
  30. tripal_views_data_jobs($data);
  31. tripal_views_data_tripal_entity($data);
  32. tripal_views_data_fields($data);
  33. $data['views']['tripal_area_collections'] = array(
  34. 'title' => t('Tripal Content Data Collections'),
  35. 'help' => t('Save Tripal content search results into a data collection for downloading or use with other tools.'),
  36. 'area' => array(
  37. 'handler' => 'tripal_views_handler_area_collections',
  38. ),
  39. );
  40. return $data;
  41. }
  42. /**
  43. * Implements hook views_data_alter()
  44. *
  45. * Ensures that all fields attached to TripalEntities use the proper
  46. * handlers.
  47. */
  48. function tripal_views_data_alter(&$data) {
  49. // Iterate through all of the views data and find
  50. // those that are associated with fields attached to
  51. // Tripal entities. For known field types (e.g. Taxonomy) we
  52. // can support those.
  53. foreach ($data as $data_table => $definition) {
  54. foreach ($definition as $data_column => $element) {
  55. if (is_array($element) and array_key_exists('field', $element) and
  56. is_array($element['field']) and array_key_exists('field_name', $element['field'])) {
  57. $field_name = $element['field']['field_name'];
  58. $field = field_info_field($field_name);
  59. // Skip fields that aren't attached to a TripalEntity content type.
  60. if (!array_key_exists('TripalEntity', $field['bundles'])) {
  61. continue;
  62. }
  63. // Skip fields that use the Tripal Storage API.
  64. if (array_key_exists('tripal_storage_api', $field['storage']['settings'])) {
  65. continue;
  66. }
  67. //
  68. // Now update views for integrating other data with our Tripal Entities.
  69. //
  70. // Iterate through the bundles to which this field is attached and
  71. // if it is a TripalField field then we'll call the viewsData function.
  72. $bundles = $field['bundles']['TripalEntity'];
  73. $result = array();
  74. foreach ($bundles as $bundle_name) {
  75. // Skip fields that aren't setup for views with this bundle.
  76. // Fields should be associated with the bundle's term identifier
  77. // (i.e. [vocab]__[accession].
  78. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  79. $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  80. $bundle_term_id = $term->vocab->vocabulary . '__' . $term->accession;
  81. if (!array_key_exists($bundle_term_id, $data)) {
  82. continue;
  83. }
  84. // Support the taxonomy_term_reference field when it's added to a
  85. // Tripal content type
  86. if ($field['type'] == 'taxonomy_term_reference') {
  87. $data[$bundle_term_id][$field_name] = [
  88. 'title' => t('Tagged Categories'),
  89. 'help' => t('Relates this Tripal content type to categories that have been assigned to it using Drupal\'s Taxonomy system.'),
  90. 'relationship' => array(
  91. 'base' => $data_table,
  92. 'base field' => 'entity_id',
  93. 'relationship field' => 'entity_id',
  94. 'handler' => 'views_handler_relationship',
  95. 'label' => t('Tags'),
  96. ),
  97. ];
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. * Integreates the Tripal fields with Views.
  106. */
  107. function tripal_views_data_fields(&$data) {
  108. // Iterate through the fields.
  109. $fields = field_info_fields();
  110. foreach ($fields as $field) {
  111. // Skip fields that aren't attached to TripalEntity entities.
  112. if (!array_key_exists('TripalEntity', $field['bundles'])) {
  113. continue;
  114. }
  115. // Fields that don't connect to the Tripal Storage API should be added differently
  116. if (!array_key_exists('tripal_storage_api', $field['storage']['settings'])) {
  117. continue;
  118. }
  119. // TODO: do we really need this hook? Just substitute the code here
  120. // for what that hook does... it's only called in one plac.e
  121. // Call the hook_field_views_data() but only for the tripal module.
  122. // Otherwise the other modules will expect that this is an SQL-based
  123. // view.
  124. $result = (array) module_invoke('tripal', 'field_views_data', $field);
  125. // Set defaults for the field if no data array was returned.
  126. if (empty($result)) {
  127. // Iterate through the bundles to which this field is attached and
  128. // if it is a TripalField field then we'll call the viewsData function.
  129. $bundles = $field['bundles']['TripalEntity'];
  130. $result = array();
  131. foreach ($bundles as $bundle_name) {
  132. $field_name = $field['field_name'];
  133. // Fields should be associated with the bundle's term identifier
  134. // (i.e. [vocab]__[accession].
  135. $bundle = tripal_load_bundle_entity(array('name' => $bundle_name));
  136. $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  137. $view_base_id = $term->vocab->vocabulary . '__' . $term->accession;
  138. $instance = field_info_instance('TripalEntity', $field['field_name'], $bundle_name);
  139. $tfield = new TripalField($field, $instance);
  140. $result += $tfield->viewsData($view_base_id);
  141. }
  142. }
  143. drupal_alter('field_views_data', $result, $field, $module);
  144. if (is_array($result)) {
  145. $data = drupal_array_merge_deep($result, $data);
  146. }
  147. }
  148. }
  149. /**
  150. * Integrates the TripalEntity entities with Drupal Views.
  151. */
  152. function tripal_views_data_tripal_entity(&$data) {
  153. // Get the list of all of the bundles (entity types) and add them
  154. // as "base tables" for views.
  155. $bundles = db_select('tripal_bundle', 'tb')
  156. ->fields('tb')
  157. ->execute();
  158. // Iterate through the bundles.
  159. while ($bundle = $bundles->fetchObject()) {
  160. // This isn't really the table name, but because our bundle table
  161. // names are unique on every Tripal site we must ust a more generic
  162. // name. Because we're using our own query class this should be fine.
  163. $term = tripal_load_term_entity(array('term_id' => $bundle->term_id));
  164. $table = $term->vocab->vocabulary . '__' . $term->accession;
  165. // Each bundle gets it's own "table".
  166. $data[$table]['table']['group'] = t($bundle->label);
  167. $data[$table]['table']['base'] = array(
  168. 'query class' => 'tripal_views_query',
  169. 'title' => t($bundle->label),
  170. 'help' => t('Tripal ' . $bundle->label . ' pages'),
  171. );
  172. $data[$table]['entity_id'] = array(
  173. 'title' => t('Entity ID'),
  174. 'help' => t('The unique entity ID for this content type.'),
  175. 'field' => array(
  176. 'handler' => 'tripal_views_handler_field_entity',
  177. ),
  178. 'filter' => array(
  179. 'handler' => 'tripal_views_handler_filter',
  180. ),
  181. 'sort' => array(
  182. 'handler' => 'tripal_views_handler_sort',
  183. ),
  184. );
  185. $data[$table]['link'] = array(
  186. 'title' => t('Link'),
  187. 'help' => t('Provide a simple link to the content.'),
  188. 'field' => array(
  189. 'handler' => 'tripal_views_handler_field_entity_link',
  190. ),
  191. );
  192. $data[$table]['edit_link'] = array(
  193. 'title' => t('Edit Link'),
  194. 'help' => t('Provide a simple link to edit the content.'),
  195. 'field' => array(
  196. 'handler' => 'tripal_views_handler_field_entity_link_edit',
  197. ),
  198. );
  199. $data[$table]['delete_link'] = array(
  200. 'title' => t('Delete Link'),
  201. 'help' => t('Provide a simple link to delete the content.'),
  202. 'field' => array(
  203. 'handler' => 'tripal_views_handler_field_entity_link_delete',
  204. ),
  205. );
  206. $data[$table]['status'] = array(
  207. 'title' => t('Published'),
  208. 'help' => t('Whether or not the content is published.'),
  209. 'field' => array(
  210. 'handler' => 'tripal_views_handler_field_boolean',
  211. 'click sortable' => TRUE,
  212. 'output formats' => array(
  213. 'published-notpublished' => array(t('Published'), t('Not published')),
  214. ),
  215. ),
  216. 'filter' => array(
  217. 'handler' => 'tripal_views_handler_filter_boolean_operator',
  218. 'label' => t('Published'),
  219. 'type' => 'yes-no',
  220. 'use equal' => TRUE, // Use status = 1 instead of status <> 0 in WHERE statment
  221. ),
  222. 'sort' => array(
  223. 'handler' => 'tripal_views_handler_sort',
  224. ),
  225. );
  226. }
  227. }
  228. /**
  229. * Provides the data array for the tripal job management system
  230. *
  231. * @param $data
  232. * Previously generated tripal views data array
  233. * return
  234. * $data array with job management system described
  235. *
  236. * @ingroup tripal
  237. */
  238. function tripal_views_data_jobs(&$data) {
  239. $data['tripal_jobs']['table']['group'] = t('Tripal Jobs');
  240. $data['tripal_jobs']['table']['base'] = array(
  241. 'field' => 'job_id', // This is the identifier field for the view.
  242. 'title' => t('Tripal Jobs'),
  243. 'help' => t('The Job Management system for Tripal.'),
  244. 'weight' => 10,
  245. );
  246. // Job ID
  247. $data['tripal_jobs']['job_id'] = array(
  248. 'title' => t('Job ID'),
  249. 'help' => t('The job 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. // User ID: Submitter
  262. $data['tripal_jobs']['uid'] = array(
  263. 'title' => t('Job Submitter'),
  264. 'help' => t('The user who submitted the job.'),
  265. 'relationship' => array(
  266. 'base' => 'user', // The name of the table to join with.
  267. 'base field' => 'uid', // The name of the field on the joined table.
  268. 'handler' => 'views_handler_relationship',
  269. 'label' => t('Submitting User'),
  270. 'title' => t('Submitting User'),
  271. 'help' => t('The user who submitted the job'),
  272. ),
  273. );
  274. // Job Name
  275. $data['tripal_jobs']['job_name'] = array(
  276. 'title' => t('Job Name'),
  277. 'help' => t('The name of the job.'),
  278. 'field' => array(
  279. 'handler' => 'views_handler_field',
  280. 'click sortable' => TRUE, // This is use by the table display plugin.
  281. ),
  282. 'sort' => array(
  283. 'handler' => 'views_handler_sort',
  284. ),
  285. 'filter' => array(
  286. 'handler' => 'views_handler_filter_string',
  287. ),
  288. 'argument' => array(
  289. 'handler' => 'views_handler_argument_string',
  290. ),
  291. );
  292. // Module Name
  293. $data['tripal_jobs']['modulename'] = array(
  294. 'title' => t('Module Name'),
  295. 'help' => t('The name of the module that submitted the job.'),
  296. 'field' => array(
  297. 'handler' => 'views_handler_field',
  298. 'click sortable' => TRUE, // This is use by the table display plugin.
  299. ),
  300. 'sort' => array(
  301. 'handler' => 'views_handler_sort',
  302. ),
  303. 'filter' => array(
  304. 'handler' => 'views_handler_filter_string',
  305. ),
  306. 'argument' => array(
  307. 'handler' => 'views_handler_argument_string',
  308. ),
  309. );
  310. // Callback
  311. $data['tripal_jobs']['callback'] = array(
  312. 'title' => t('Callback'),
  313. 'help' => t('The callback executed when the job runs.'),
  314. 'field' => array(
  315. 'handler' => 'views_handler_field',
  316. 'click sortable' => TRUE, // This is use by the table display plugin.
  317. ),
  318. 'sort' => array(
  319. 'handler' => 'views_handler_sort',
  320. ),
  321. 'filter' => array(
  322. 'handler' => 'views_handler_filter_string',
  323. ),
  324. 'argument' => array(
  325. 'handler' => 'views_handler_argument_string',
  326. ),
  327. );
  328. // Arguments
  329. $data['tripal_jobs']['arguments'] = array(
  330. 'title' => t('Arguements'),
  331. 'help' => t('Any arguments passed to the callback.'),
  332. 'field' => array(
  333. 'handler' => 'views_handler_field',
  334. 'click sortable' => TRUE, // This is use by the table display plugin.
  335. ),
  336. 'sort' => array(
  337. 'handler' => 'views_handler_sort',
  338. ),
  339. 'filter' => array(
  340. 'handler' => 'views_handler_filter_string',
  341. ),
  342. 'argument' => array(
  343. 'handler' => 'views_handler_argument_string',
  344. ),
  345. );
  346. // Progress
  347. $data['tripal_jobs']['progress'] = array(
  348. 'title' => t('Progress'),
  349. 'help' => t('The current progress of the job.'),
  350. 'field' => array(
  351. 'handler' => 'views_handler_field_numeric',
  352. 'click sortable' => TRUE,
  353. ),
  354. 'filter' => array(
  355. 'handler' => 'views_handler_filter_numeric',
  356. ),
  357. 'sort' => array(
  358. 'handler' => 'views_handler_sort',
  359. ),
  360. );
  361. // Status
  362. $data['tripal_jobs']['status'] = array(
  363. 'title' => t('Status'),
  364. 'help' => t('The current status of the job.'),
  365. 'field' => array(
  366. 'handler' => 'views_handler_field',
  367. 'click sortable' => TRUE, // This is use by the table display plugin.
  368. ),
  369. 'sort' => array(
  370. 'handler' => 'views_handler_sort',
  371. ),
  372. 'filter' => array(
  373. 'handler' => 'views_handler_filter_string',
  374. ),
  375. 'argument' => array(
  376. 'handler' => 'views_handler_argument_string',
  377. ),
  378. );
  379. // Submit Data
  380. $data['tripal_jobs']['submit_date'] = array(
  381. 'title' => t('Submit Date'),
  382. 'help' => t('The date the job was submitted.'),
  383. 'field' => array(
  384. 'handler' => 'views_handler_field_date',
  385. 'click sortable' => TRUE,
  386. ),
  387. 'sort' => array(
  388. 'handler' => 'views_handler_sort_date',
  389. ),
  390. 'filter' => array(
  391. 'handler' => 'views_handler_filter_date',
  392. ),
  393. );
  394. // Start Time
  395. $data['tripal_jobs']['start_time'] = array(
  396. 'title' => t('Start Time'),
  397. 'help' => t('The time the job started.'),
  398. 'field' => array(
  399. 'handler' => 'views_handler_field_date',
  400. 'click sortable' => TRUE,
  401. ),
  402. 'sort' => array(
  403. 'handler' => 'views_handler_sort_date',
  404. ),
  405. 'filter' => array(
  406. 'handler' => 'views_handler_filter_date',
  407. ),
  408. );
  409. // End Time
  410. $data['tripal_jobs']['end_time'] = array(
  411. 'title' => t('End Time'),
  412. 'help' => t('The time the job ended.'),
  413. 'field' => array(
  414. 'handler' => 'views_handler_field_date',
  415. 'click sortable' => TRUE,
  416. ),
  417. 'sort' => array(
  418. 'handler' => 'views_handler_sort_date',
  419. ),
  420. 'filter' => array(
  421. 'handler' => 'views_handler_filter_date',
  422. ),
  423. );
  424. // Error Message
  425. $data['tripal_jobs']['error_msg'] = array(
  426. 'title' => t('Error Message '),
  427. 'help' => t('A short description of any error the job might have had.'),
  428. 'field' => array(
  429. 'handler' => 'views_handler_field',
  430. 'click sortable' => TRUE, // This is use by the table display plugin.
  431. ),
  432. 'sort' => array(
  433. 'handler' => 'views_handler_sort',
  434. ),
  435. 'filter' => array(
  436. 'handler' => 'views_handler_filter_string',
  437. ),
  438. 'argument' => array(
  439. 'handler' => 'views_handler_argument_string',
  440. ),
  441. );
  442. // Unix Pid of the job
  443. $data['tripal_jobs']['pid'] = array(
  444. 'title' => t('Job PID'),
  445. 'help' => t('The Unix PID of the job.'),
  446. 'field' => array(
  447. 'handler' => 'views_handler_field_numeric',
  448. 'click sortable' => TRUE,
  449. ),
  450. 'filter' => array(
  451. 'handler' => 'views_handler_filter_numeric',
  452. ),
  453. 'sort' => array(
  454. 'handler' => 'views_handler_sort',
  455. ),
  456. );
  457. // Priority
  458. $data['tripal_jobs']['priority'] = array(
  459. 'title' => t('Priority'),
  460. 'help' => t('The priority of this job.'),
  461. 'field' => array(
  462. 'handler' => 'views_handler_field_numeric',
  463. 'click sortable' => TRUE,
  464. ),
  465. 'filter' => array(
  466. 'handler' => 'views_handler_filter_numeric',
  467. ),
  468. 'sort' => array(
  469. 'handler' => 'views_handler_sort',
  470. ),
  471. );
  472. }