tripal.views.inc 14 KB

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