tripal.views.inc 16 KB

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