TripalEntityUIController.inc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * UI controller.
  4. */
  5. class TripalEntityUIController extends EntityDefaultUIController {
  6. /**
  7. * Overrides hook_menu() defaults. Main reason for doing this is that
  8. * parent class hook_menu() is optimized for entity type administration.
  9. */
  10. public function hook_menu() {
  11. $items = array();
  12. // Set this on the object so classes that extend hook_menu() can use it.
  13. $this->id_count = count(explode('/', $this->path));
  14. $wildcard = isset($this->entityInfo['admin ui']['menu wildcard']) ? $this->entityInfo['admin ui']['menu wildcard'] : '%entity_object';
  15. $id_count = count(explode('/', $this->path));
  16. // The content menu.
  17. $items[$this->path] = array(
  18. 'title' => 'Tripal Content',
  19. 'page callback' => 'tripal_entities_content_view',
  20. 'file' => 'includes/tripal_entities.admin.inc',
  21. 'file path' => drupal_get_path('module', 'tripal_entities'),
  22. 'access arguments' => array('administer tripal data'),
  23. 'type' => MENU_LOCAL_TASK,
  24. 'weight' => -9
  25. );
  26. $items['bio-data/add'] = array(
  27. 'title' => 'Add Tripal Content',
  28. 'page callback' => 'tripal_entities_add_page',
  29. 'file' => 'includes/tripal_entities.entity_form.inc',
  30. 'file path' => drupal_get_path('module', 'tripal_entities'),
  31. 'access arguments' => array('administer tripal data'),
  32. );
  33. // Add a menu item for creating each bundle
  34. $bundles = array_keys($this->entityInfo['bundles']);
  35. foreach ($bundles as $bundle_name) {
  36. $matches = array();
  37. if (preg_match('/^bio-data_(.*?)$/', $bundle_name, $matches)) {
  38. $bundle = tripal_load_bundle_entity($bundle_name);
  39. // Get the term for this bundle
  40. $term = entity_load('TripalTerm', array('id' => $matches[1]));
  41. $term = reset($term);
  42. // Set a custom page for adding new tripal data entities.
  43. $items['bio-data/add/' . $term->id] = array(
  44. 'title' => ucfirst($bundle->label),
  45. 'description' => tripal_get_bundle_variable('description', $bundle->id, $term->definition),
  46. 'page callback' => 'drupal_get_form',
  47. 'page arguments' => array('tripal_entities_entity_form', 2),
  48. 'access callback' => 'tripal_entities_entity_access',
  49. 'access arguments' => array('edit'),
  50. 'file' => 'includes/tripal_entities.entity_form.inc',
  51. 'file path' => drupal_get_path('module', 'tripal_entities'),
  52. );
  53. }
  54. }
  55. // Link for viewing a tripal data type.
  56. $items['bio-data/' . $wildcard] = array(
  57. 'title callback' => 'tripal_entities_entity_title',
  58. 'title arguments' => array(1),
  59. 'page callback' => 'tripal_entities_view_entity',
  60. 'page arguments' => array(1),
  61. 'access callback' => 'tripal_entities_entity_access',
  62. 'access arguments' => array('view', 1),
  63. 'type' => MENU_CALLBACK,
  64. );
  65. // 'View' tab for an individual entity page.
  66. $items['bio-data/' . $wildcard . '/view'] = array(
  67. 'title' => 'View',
  68. 'page callback' => 'tripal_entities_view_entity',
  69. 'page arguments' => array(1),
  70. 'access callback' => 'tripal_entities_entity_access',
  71. 'access arguments' => array('view', 1),
  72. 'type' => MENU_DEFAULT_LOCAL_TASK,
  73. 'weight' => -10,
  74. );
  75. // 'Edit' tab for an individual entity page.
  76. $items['bio-data/' . $wildcard . '/edit'] = array(
  77. 'title' => 'Edit',
  78. 'page callback' => 'drupal_get_form',
  79. 'page arguments' => array('tripal_entities_entity_form', NULL, 1),
  80. 'access callback' => 'tripal_entities_entity_access',
  81. 'access arguments' => array('edit', 1),
  82. 'file' => 'includes/tripal_entities.entity_form.inc',
  83. 'file path' => drupal_get_path('module', 'tripal_entities'),
  84. 'type' => MENU_LOCAL_TASK,
  85. 'weight' => -8,
  86. );
  87. // Menu item for deleting tripal data entities.
  88. $items['bio-data/' . $wildcard . '/delete'] = array(
  89. 'title' => 'Delete',
  90. 'page callback' => 'drupal_get_form',
  91. 'page arguments' => array('tripal_entities_entity_delete_form', 1),
  92. 'access callback' => 'tripal_entities_entity_access',
  93. 'access arguments' => array('edit', 1),
  94. 'file' => 'includes/tripal_entities.entity_form.inc',
  95. 'file path' => drupal_get_path('module', 'tripal_entities'),
  96. 'type' => MENU_CALLBACK,
  97. 'weight' => 10,
  98. );
  99. return $items;
  100. }
  101. }
  102. /**
  103. *
  104. * @param unknown $entity
  105. */
  106. function tripal_entity_manage_fields($entity) {
  107. drupal_goto('admin/structure/bio-data/manage/' . $entity->bundle . '/fields');
  108. return '';
  109. }
  110. /**
  111. * Menu callback to display an entity.
  112. *
  113. * As we load the entity for display, we're responsible for invoking a number
  114. * of hooks in their proper order.
  115. *
  116. * @see hook_entity_prepare_view()
  117. * @see hook_entity_view()
  118. * @see hook_entity_view_alter()
  119. */
  120. function tripal_entities_view_entity($entity, $view_mode = 'full') {
  121. $content = '';
  122. $controller = entity_get_controller($entity->type);
  123. $content = $controller->view(array($entity->id => $entity));
  124. drupal_set_title($entity->title);
  125. return $content;
  126. }
  127. /**
  128. * Provide a data listing for tripal entites (ie: biological data).
  129. *
  130. * This function is a callback in a menu item which is set in the
  131. * TripalEntityUIController class.
  132. */
  133. function tripal_entities_content_view() {
  134. // Retrieve our data listing form and render it.
  135. $form = drupal_get_form('tripal_entities_content_overview_form');
  136. $output = drupal_render($form);
  137. // Set the breadcrumb.
  138. $breadcrumb = array();
  139. $breadcrumb[] = l('Home', '<front>');
  140. $breadcrumb[] = l('Administration', 'admin');
  141. drupal_set_breadcrumb($breadcrumb);
  142. return $output;
  143. }
  144. /**
  145. * Display a listing of Tripal entities.
  146. *
  147. * @TODO Filters and bulk operations needed to be added to this form.
  148. *
  149. * @param array $form
  150. * @param array $form_state
  151. * @return
  152. * A form array describing this listing to the Form API.
  153. */
  154. function tripal_entities_content_overview_form($form, &$form_state) {
  155. // Set the title to be informative (defaults to content for some reason).
  156. drupal_set_title('Tripal Content');
  157. // Retrieve a pages list of all tripal entitles (ie: biological data).
  158. // This will return the 25 most recently created entities.
  159. $entities = db_select('tripal_entity', 'td')
  160. ->fields('td')
  161. ->orderBy('created', 'DESC')
  162. ->range(0,25)
  163. ->execute();
  164. $headers = array('Title', 'Vocabulary', 'Term', 'Author', 'Status', 'Updated', 'Operations');
  165. $rows = array();
  166. // For each entity retrieved add a row to the data listing.
  167. while ($entity = $entities->fetchObject()) {
  168. // Get the term
  169. $term = entity_load('TripalTerm', array('id' => $entity->term_id));
  170. $term = reset($term);
  171. $vocab = entity_load('TripalVocab', array('id' => $term->vocab_id));
  172. $vocab = reset($vocab);
  173. // Retrieve details about the user who created this data.
  174. $author = user_load($entity->uid);
  175. // Add information to the table.
  176. $rows[] = array(
  177. l($entity->title, 'bio-data/' . $entity->id),
  178. $vocab->namespace,
  179. $term->name,
  180. l($author->name, 'user/' . $entity->uid),
  181. $entity->status == 1 ? 'published' : 'unpublished',
  182. format_date($entity->changed, 'short'),
  183. l('edit', 'bio-data/' . $entity->id . '/edit') . '&nbsp;&nbsp;' .
  184. l('delete', 'bio-data/' . $entity->id . '/delete')
  185. );
  186. }
  187. // If there are no entites created yet then add a message to the table to
  188. // provide guidance to administrators.
  189. if (empty($rows)) {
  190. $rows[] = array(
  191. array(
  192. 'data' => t('No Tripal content available.'),
  193. 'colspan' => 7
  194. )
  195. );
  196. }
  197. // Render the data listing.
  198. $table_vars = array(
  199. 'header' => $headers,
  200. 'rows' => $rows,
  201. 'attributes' => array(),
  202. 'sticky' => TRUE,
  203. 'caption' => '',
  204. 'colgroups' => array(),
  205. 'empty' => '',
  206. );
  207. $form['results'] = array(
  208. '#type' => 'markup',
  209. '#markup' => theme('table', $table_vars),
  210. );
  211. return $form;
  212. }