tripal.views.inc 14 KB

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