tripal.views.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  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. // 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'] = array(
  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' => array(
  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 = array();
  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(array('name' => $bundle_name));
  81. $term = tripal_load_term_entity(array('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' => array(
  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(array('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'] = array(
  197. 'query class' => 'tripal_views_query',
  198. 'title' => t($bundle->label),
  199. 'help' => t('Tripal ' . $bundle->label . ' pages'),
  200. );
  201. $data[$table]['entity_id'] = array(
  202. 'title' => t('Entity ID'),
  203. 'help' => t('The unique entity ID for this content type.'),
  204. 'field' => array(
  205. 'handler' => 'tripal_views_handler_field_entity',
  206. ),
  207. 'filter' => array(
  208. 'handler' => 'tripal_views_handler_filter',
  209. ),
  210. 'sort' => array(
  211. 'handler' => 'tripal_views_handler_sort',
  212. ),
  213. );
  214. $data[$table]['link'] = array(
  215. 'title' => t('Link'),
  216. 'help' => t('Provide a simple link to the content.'),
  217. 'field' => array(
  218. 'handler' => 'tripal_views_handler_field_entity_link',
  219. ),
  220. );
  221. $data[$table]['edit_link'] = array(
  222. 'title' => t('Edit Link'),
  223. 'help' => t('Provide a simple link to edit the content.'),
  224. 'field' => array(
  225. 'handler' => 'tripal_views_handler_field_entity_link_edit',
  226. ),
  227. );
  228. $data[$table]['delete_link'] = array(
  229. 'title' => t('Delete Link'),
  230. 'help' => t('Provide a simple link to delete the content.'),
  231. 'field' => array(
  232. 'handler' => 'tripal_views_handler_field_entity_link_delete',
  233. ),
  234. );
  235. $data[$table]['status'] = array(
  236. 'title' => t('Published'),
  237. 'help' => t('Whether or not the content is published.'),
  238. 'field' => array(
  239. 'handler' => 'tripal_views_handler_field_boolean',
  240. 'click sortable' => TRUE,
  241. 'output formats' => array(
  242. 'published-notpublished' => array(t('Published'), t('Not published')),
  243. ),
  244. ),
  245. 'filter' => array(
  246. 'handler' => 'tripal_views_handler_filter_boolean_operator',
  247. 'label' => t('Published'),
  248. 'type' => 'yes-no',
  249. 'use equal' => TRUE, // Use status = 1 instead of status <> 0 in WHERE statment
  250. ),
  251. 'sort' => array(
  252. 'handler' => 'tripal_views_handler_sort',
  253. ),
  254. );
  255. }
  256. }
  257. /**
  258. * Provides the data array for the tripal job management system
  259. *
  260. * @param $data
  261. * Previously generated tripal views data array
  262. * return
  263. * $data array with job management system described
  264. *
  265. * @ingroup tripal
  266. */
  267. function tripal_views_data_jobs(&$data) {
  268. $data['tripal_jobs']['table']['group'] = t('Tripal Jobs');
  269. $data['tripal_jobs']['table']['base'] = array(
  270. 'field' => 'job_id', // This is the identifier field for the view.
  271. 'title' => t('Tripal Jobs'),
  272. 'help' => t('The Job Management system for Tripal.'),
  273. 'weight' => 10,
  274. );
  275. // Job ID
  276. $data['tripal_jobs']['job_id'] = array(
  277. 'title' => t('Job ID'),
  278. 'help' => t('The job primary key.'),
  279. 'field' => array(
  280. 'handler' => 'views_handler_field_numeric',
  281. 'click sortable' => TRUE,
  282. ),
  283. 'filter' => array(
  284. 'handler' => 'views_handler_filter_numeric',
  285. ),
  286. 'sort' => array(
  287. 'handler' => 'views_handler_sort',
  288. ),
  289. );
  290. // User ID: Submitter
  291. $data['tripal_jobs']['uid'] = array(
  292. 'title' => t('Job Submitter'),
  293. 'help' => t('The user who submitted the job.'),
  294. 'relationship' => array(
  295. 'base' => 'user', // The name of the table to join with.
  296. 'base field' => 'uid', // The name of the field on the joined table.
  297. 'handler' => 'views_handler_relationship',
  298. 'label' => t('Submitting User'),
  299. 'title' => t('Submitting User'),
  300. 'help' => t('The user who submitted the job'),
  301. ),
  302. );
  303. // Job Name
  304. $data['tripal_jobs']['job_name'] = array(
  305. 'title' => t('Job Name'),
  306. 'help' => t('The name of the job.'),
  307. 'field' => array(
  308. 'handler' => 'views_handler_field',
  309. 'click sortable' => TRUE, // This is use by the table display plugin.
  310. ),
  311. 'sort' => array(
  312. 'handler' => 'views_handler_sort',
  313. ),
  314. 'filter' => array(
  315. 'handler' => 'views_handler_filter_string',
  316. ),
  317. 'argument' => array(
  318. 'handler' => 'views_handler_argument_string',
  319. ),
  320. );
  321. // Module Name
  322. $data['tripal_jobs']['modulename'] = array(
  323. 'title' => t('Module Name'),
  324. 'help' => t('The name of the module that submitted the job.'),
  325. 'field' => array(
  326. 'handler' => 'views_handler_field',
  327. 'click sortable' => TRUE, // This is use by the table display plugin.
  328. ),
  329. 'sort' => array(
  330. 'handler' => 'views_handler_sort',
  331. ),
  332. 'filter' => array(
  333. 'handler' => 'views_handler_filter_string',
  334. ),
  335. 'argument' => array(
  336. 'handler' => 'views_handler_argument_string',
  337. ),
  338. );
  339. // Callback
  340. $data['tripal_jobs']['callback'] = array(
  341. 'title' => t('Callback'),
  342. 'help' => t('The callback executed when the job runs.'),
  343. 'field' => array(
  344. 'handler' => 'views_handler_field',
  345. 'click sortable' => TRUE, // This is use by the table display plugin.
  346. ),
  347. 'sort' => array(
  348. 'handler' => 'views_handler_sort',
  349. ),
  350. 'filter' => array(
  351. 'handler' => 'views_handler_filter_string',
  352. ),
  353. 'argument' => array(
  354. 'handler' => 'views_handler_argument_string',
  355. ),
  356. );
  357. // Arguments
  358. $data['tripal_jobs']['arguments'] = array(
  359. 'title' => t('Arguements'),
  360. 'help' => t('Any arguments passed to the callback.'),
  361. 'field' => array(
  362. 'handler' => 'views_handler_field',
  363. 'click sortable' => TRUE, // This is use by the table display plugin.
  364. ),
  365. 'sort' => array(
  366. 'handler' => 'views_handler_sort',
  367. ),
  368. 'filter' => array(
  369. 'handler' => 'views_handler_filter_string',
  370. ),
  371. 'argument' => array(
  372. 'handler' => 'views_handler_argument_string',
  373. ),
  374. );
  375. // Progress
  376. $data['tripal_jobs']['progress'] = array(
  377. 'title' => t('Progress'),
  378. 'help' => t('The current progress of the job.'),
  379. 'field' => array(
  380. 'handler' => 'views_handler_field_numeric',
  381. 'click sortable' => TRUE,
  382. ),
  383. 'filter' => array(
  384. 'handler' => 'views_handler_filter_numeric',
  385. ),
  386. 'sort' => array(
  387. 'handler' => 'views_handler_sort',
  388. ),
  389. );
  390. // Status
  391. $data['tripal_jobs']['status'] = array(
  392. 'title' => t('Status'),
  393. 'help' => t('The current status of the job.'),
  394. 'field' => array(
  395. 'handler' => 'views_handler_field',
  396. 'click sortable' => TRUE, // This is use by the table display plugin.
  397. ),
  398. 'sort' => array(
  399. 'handler' => 'views_handler_sort',
  400. ),
  401. 'filter' => array(
  402. 'handler' => 'views_handler_filter_string',
  403. ),
  404. 'argument' => array(
  405. 'handler' => 'views_handler_argument_string',
  406. ),
  407. );
  408. // Submit Data
  409. $data['tripal_jobs']['submit_date'] = array(
  410. 'title' => t('Submit Date'),
  411. 'help' => t('The date the job was submitted.'),
  412. 'field' => array(
  413. 'handler' => 'views_handler_field_date',
  414. 'click sortable' => TRUE,
  415. ),
  416. 'sort' => array(
  417. 'handler' => 'views_handler_sort_date',
  418. ),
  419. 'filter' => array(
  420. 'handler' => 'views_handler_filter_date',
  421. ),
  422. );
  423. // Start Time
  424. $data['tripal_jobs']['start_time'] = array(
  425. 'title' => t('Start Time'),
  426. 'help' => t('The time the job started.'),
  427. 'field' => array(
  428. 'handler' => 'views_handler_field_date',
  429. 'click sortable' => TRUE,
  430. ),
  431. 'sort' => array(
  432. 'handler' => 'views_handler_sort_date',
  433. ),
  434. 'filter' => array(
  435. 'handler' => 'views_handler_filter_date',
  436. ),
  437. );
  438. // End Time
  439. $data['tripal_jobs']['end_time'] = array(
  440. 'title' => t('End Time'),
  441. 'help' => t('The time the job ended.'),
  442. 'field' => array(
  443. 'handler' => 'views_handler_field_date',
  444. 'click sortable' => TRUE,
  445. ),
  446. 'sort' => array(
  447. 'handler' => 'views_handler_sort_date',
  448. ),
  449. 'filter' => array(
  450. 'handler' => 'views_handler_filter_date',
  451. ),
  452. );
  453. // Error Message
  454. $data['tripal_jobs']['error_msg'] = array(
  455. 'title' => t('Error Message '),
  456. 'help' => t('A short description of any error the job might have had.'),
  457. 'field' => array(
  458. 'handler' => 'views_handler_field',
  459. 'click sortable' => TRUE, // This is use by the table display plugin.
  460. ),
  461. 'sort' => array(
  462. 'handler' => 'views_handler_sort',
  463. ),
  464. 'filter' => array(
  465. 'handler' => 'views_handler_filter_string',
  466. ),
  467. 'argument' => array(
  468. 'handler' => 'views_handler_argument_string',
  469. ),
  470. );
  471. // Unix Pid of the job
  472. $data['tripal_jobs']['pid'] = array(
  473. 'title' => t('Job PID'),
  474. 'help' => t('The Unix PID of the job.'),
  475. 'field' => array(
  476. 'handler' => 'views_handler_field_numeric',
  477. 'click sortable' => TRUE,
  478. ),
  479. 'filter' => array(
  480. 'handler' => 'views_handler_filter_numeric',
  481. ),
  482. 'sort' => array(
  483. 'handler' => 'views_handler_sort',
  484. ),
  485. );
  486. // Priority
  487. $data['tripal_jobs']['priority'] = array(
  488. 'title' => t('Priority'),
  489. 'help' => t('The priority of this job.'),
  490. 'field' => array(
  491. 'handler' => 'views_handler_field_numeric',
  492. 'click sortable' => TRUE,
  493. ),
  494. 'filter' => array(
  495. 'handler' => 'views_handler_filter_numeric',
  496. ),
  497. 'sort' => array(
  498. 'handler' => 'views_handler_sort',
  499. ),
  500. );
  501. }