TripalEntityUIController.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. 'access arguments' => array('administer tripal data'),
  30. );
  31. // Add a menu item for creating each bundle
  32. $bundles = array_keys($this->entityInfo['bundles']);
  33. foreach ($bundles as $bundle_name) {
  34. $matches = array();
  35. if (preg_match('/^bio-data_(.*?)$/', $bundle_name, $matches)) {
  36. $bundle = tripal_load_bundle_entity($bundle_name);
  37. // Get the term for this bundle
  38. $term = entity_load('TripalTerm', array('id' => $matches[1]));
  39. $term = reset($term);
  40. // Set a custom page for adding new tripal data entities.
  41. $items['bio-data/add/' . $term->id] = array(
  42. 'title' => ucfirst($bundle->label),
  43. 'description' => tripal_get_bundle_variable('description', $bundle->id, $term->definition),
  44. 'page callback' => 'drupal_get_form',
  45. 'page arguments' => array('tripal_entities_entity_form', 2),
  46. 'access callback' => 'tripal_entities_entity_access',
  47. 'access arguments' => array('edit'),
  48. );
  49. }
  50. }
  51. // Link for viewing a tripal data type.
  52. $items['bio-data/' . $wildcard] = array(
  53. 'title callback' => 'tripal_entities_entity_title',
  54. 'title arguments' => array(1),
  55. 'page callback' => 'tripal_entities_view_entity',
  56. 'page arguments' => array(1),
  57. 'access callback' => 'tripal_entities_entity_access',
  58. 'access arguments' => array('view', 1),
  59. 'type' => MENU_CALLBACK,
  60. );
  61. // 'View' tab for an individual entity page.
  62. $items['bio-data/' . $wildcard . '/view'] = array(
  63. 'title' => 'View',
  64. 'page callback' => 'tripal_entities_view_entity',
  65. 'page arguments' => array(1),
  66. 'access callback' => 'tripal_entities_entity_access',
  67. 'access arguments' => array('view', 1),
  68. 'type' => MENU_DEFAULT_LOCAL_TASK,
  69. 'weight' => -10,
  70. );
  71. // 'Edit' tab for an individual entity page.
  72. $items['bio-data/' . $wildcard . '/edit'] = array(
  73. 'title' => 'Edit',
  74. 'page callback' => 'drupal_get_form',
  75. 'page arguments' => array('tripal_entities_entity_form', NULL, 1),
  76. 'access callback' => 'tripal_entities_entity_access',
  77. 'access arguments' => array('edit', 1),
  78. 'type' => MENU_LOCAL_TASK,
  79. 'weight' => -8,
  80. );
  81. // Menu item for deleting tripal data entities.
  82. $items['bio-data/' . $wildcard . '/delete'] = array(
  83. 'title' => 'Delete',
  84. 'page callback' => 'drupal_get_form',
  85. 'page arguments' => array('tripal_entities_entity_delete_form', 1),
  86. 'access callback' => 'tripal_entities_entity_access',
  87. 'access arguments' => array('edit', 1),
  88. 'type' => MENU_CALLBACK,
  89. 'weight' => 10,
  90. );
  91. return $items;
  92. }
  93. }
  94. /**
  95. *
  96. * @param unknown $entity
  97. */
  98. function tripal_entity_manage_fields($entity) {
  99. drupal_goto('admin/structure/bio-data/manage/' . $entity->name . '/fields');
  100. return '';
  101. }
  102. /**
  103. * Menu callback to display an entity.
  104. *
  105. * As we load the entity for display, we're responsible for invoking a number
  106. * of hooks in their proper order.
  107. *
  108. * @see hook_entity_prepare_view()
  109. * @see hook_entity_view()
  110. * @see hook_entity_view_alter()
  111. */
  112. function tripal_entities_view_entity($entity, $view_mode = 'full') {
  113. $content = '';
  114. $controller = entity_get_controller($entity->type);
  115. $content = $controller->view(array($entity->id => $entity));
  116. drupal_set_title($entity->title);
  117. return $content;
  118. }
  119. /**
  120. * Provide a data listing for tripal entites (ie: biological data).
  121. *
  122. * This function is a callback in a menu item which is set in the
  123. * TripalEntityUIController class.
  124. */
  125. function tripal_entities_content_view() {
  126. // Retrieve our data listing form and render it.
  127. $form = drupal_get_form('tripal_entities_content_overview_form');
  128. $output = drupal_render($form);
  129. // Set the breadcrumb.
  130. $breadcrumb = array();
  131. $breadcrumb[] = l('Home', '<front>');
  132. $breadcrumb[] = l('Administration', 'admin');
  133. drupal_set_breadcrumb($breadcrumb);
  134. return $output;
  135. }
  136. /**
  137. * Display a listing of Tripal entities.
  138. *
  139. * @TODO Filters and bulk operations needed to be added to this form.
  140. *
  141. * @param array $form
  142. * @param array $form_state
  143. * @return
  144. * A form array describing this listing to the Form API.
  145. */
  146. function tripal_entities_content_overview_form($form, &$form_state) {
  147. // Set the title to be informative (defaults to content for some reason).
  148. drupal_set_title('Tripal Content');
  149. // Retrieve a pages list of all tripal entitles (ie: biological data).
  150. // This will return the 25 most recently created entities.
  151. $entities = db_select('tripal_entity', 'td')
  152. ->fields('td')
  153. ->orderBy('created', 'DESC')
  154. ->range(0,25)
  155. ->execute();
  156. $headers = array('Title', 'Vocabulary', 'Term', 'Author', 'Status', 'Updated', 'Operations');
  157. $rows = array();
  158. // For each entity retrieved add a row to the data listing.
  159. while ($entity = $entities->fetchObject()) {
  160. // Get the term
  161. $term = entity_load('TripalTerm', array('id' => $entity->term_id));
  162. $term = reset($term);
  163. $vocab = entity_load('TripalVocab', array('id' => $term->vocab_id));
  164. $vocab = reset($vocab);
  165. // Retrieve details about the user who created this data.
  166. $author = user_load($entity->uid);
  167. // Add information to the table.
  168. $rows[] = array(
  169. l($entity->title, 'bio-data/' . $entity->id),
  170. $vocab->namespace,
  171. $term->name,
  172. l($author->name, 'user/' . $entity->uid),
  173. $entity->status == 1 ? 'published' : 'unpublished',
  174. format_date($entity->changed, 'short'),
  175. l('edit', 'bio-data/' . $entity->id . '/edit') . '&nbsp;&nbsp;' .
  176. l('delete', 'bio-data/' . $entity->id . '/delete')
  177. );
  178. }
  179. // If there are no entites created yet then add a message to the table to
  180. // provide guidance to administrators.
  181. if (empty($rows)) {
  182. $rows[] = array(
  183. array(
  184. 'data' => t('No Tripal content available.'),
  185. 'colspan' => 7
  186. )
  187. );
  188. }
  189. // Render the data listing.
  190. $table_vars = array(
  191. 'header' => $headers,
  192. 'rows' => $rows,
  193. 'attributes' => array(),
  194. 'sticky' => TRUE,
  195. 'caption' => '',
  196. 'colgroups' => array(),
  197. 'empty' => '',
  198. );
  199. $form['results'] = array(
  200. '#type' => 'markup',
  201. '#markup' => theme('table', $table_vars),
  202. );
  203. return $form;
  204. }
  205. /**
  206. *
  207. */
  208. function tripal_entities_entity_form($form, &$form_state, $term_id = '', $entity = NULL) {
  209. $bundle_name = 'bio-data_' . $term_id;
  210. // Add a vertical tabs element
  211. $form['entity_form_vtabs'] = array(
  212. '#type' => 'vertical_tabs',
  213. '#weight' => 999,
  214. );
  215. // If the entity doesn't exist then create one.
  216. if (!$entity) {
  217. $entity = entity_get_controller('TripalEntity')->create(array('bundle' => $bundle_name, 'term_id' => $term_id));
  218. field_attach_form('TripalEntity', $entity, $form, $form_state);
  219. $form['add_button'] = array(
  220. '#type' => 'submit',
  221. '#value' => t('Save'),
  222. '#name' => 'add_data',
  223. '#weight' => 1000
  224. );
  225. }
  226. else {
  227. field_attach_form('TripalEntity', $entity, $form, $form_state);
  228. $form['update_button'] = array(
  229. '#type' => 'submit',
  230. '#value' => t('Update'),
  231. '#name' => 'update_data',
  232. '#weight' => 1000
  233. );
  234. $form['delete_button'] = array(
  235. '#type' => 'submit',
  236. '#value' => t('Delete'),
  237. '#name' => 'delete_data',
  238. '#weight' => 1001
  239. );
  240. }
  241. // The entity object must be added to the $form_state in order for
  242. // the Entity API to work. It must have a key of the entity name.
  243. $form_state['TripalEntity'] = $entity;
  244. $form['#prefix'] = "<div id='$bundle_name-entity-form'>";
  245. $form['#suffix'] = "</div>";
  246. return $form;
  247. }
  248. /**
  249. * An Ajax callback for the tripal_entities_entity_form.
  250. */
  251. function tripal_entities_entity_form_ajax_callback($form, $form_state) {
  252. // return the form so Drupal can update the content on the page
  253. return $form;
  254. }
  255. /**
  256. * Implements hook_validate() for the tripal_entities_entity_form.
  257. */
  258. function tripal_entities_entity_form_validate($form, &$form_state) {
  259. if (array_key_exists('clicked_button', $form_state) and
  260. $form_state['clicked_button']['#name'] =='add_data') {
  261. $entity = $form_state['TripalEntity'];
  262. field_attach_form_validate('TripalEntity', $entity, $form, $form_state);
  263. }
  264. }
  265. /**
  266. * Implements hook_submit() for the tripal_entities_entity_form.
  267. */
  268. function tripal_entities_entity_form_submit($form, &$form_state) {
  269. $entity = $form_state['TripalEntity'];
  270. if ($form_state['clicked_button']['#name'] =='cancel') {
  271. $form_state['redirect'] = "bio-data/" . $entity->id;
  272. }
  273. if ($form_state['clicked_button']['#name'] =='update_data' or
  274. $form_state['clicked_button']['#name'] =='add_data') {
  275. $entityform = entity_ui_controller('TripalEntity')->entityFormSubmitBuildEntity($form, $form_state);
  276. if ($entityform->save()) {
  277. $form_state['redirect'] = "bio-data/" . $entity->id;
  278. }
  279. else {
  280. drupal_set_message('Cannot save entity', 'error');
  281. }
  282. }
  283. if ($form_state['clicked_button']['#name'] =='delete_data') {
  284. $form_state['redirect'] = 'bio-data/' . $entity->id .'/delete';
  285. }
  286. }
  287. /**
  288. * Provides a list of TripalEntity types (bundles) for the user to add.
  289. *
  290. * This function is a callback in a menu item which is set in the
  291. * TripalEntityUIController class.
  292. */
  293. function tripal_entities_add_page() {
  294. $item = menu_get_item();
  295. $content = system_admin_menu_block($item);
  296. // Bypass the node/add listing if only one content type is available.
  297. if (count($content) == 1) {
  298. $item = array_shift($content);
  299. drupal_goto($item['href']);
  300. }
  301. return theme('tripal_entities_add_list', array('content' => $content));
  302. }
  303. /**
  304. * Returns HTML for a list of available node types for node creation.
  305. *
  306. * @param $variables
  307. * An associative array containing:
  308. * - content: An array of content types.
  309. *
  310. * @ingroup themeable
  311. */
  312. function theme_tripal_entities_add_list($variables) {
  313. $content = $variables['content'];
  314. $output = '';
  315. if ($content) {
  316. $output = '<dl class="node-type-list">';
  317. foreach ($content as $item) {
  318. $output .= '<dt>' . l($item['title'], $item['href'], $item['localized_options']) . '</dt>';
  319. $output .= '<dd>' . filter_xss_admin($item['description']) . '</dd>';
  320. }
  321. $output .= '</dl>';
  322. }
  323. else {
  324. $output = tripal_set_message(
  325. t('This page is for adding Tripal content to your site. However, before you can add data you have to specify what types of data your site will support. For example, if you want to add genes to be displayed to users, you must first create a data type "gene".'),
  326. TRIPAL_INFO,
  327. array('return_html' => TRUE)
  328. );
  329. $output .= '<p>' . t('You have not created any biological data types yet. ' .
  330. 'Go to the <a href="@create-content">data type creation page</a> to ' .
  331. 'add a new biological data type.',
  332. array('@create-content' => url('admin/structure/bio-data/add'))) . '</p>';
  333. }
  334. return $output;
  335. }
  336. /**
  337. * Form callback: confirmation form for deleting a tripal_entity.
  338. *
  339. * @param $tripal_entity The
  340. * tripal_entity to delete
  341. *
  342. * @see confirm_form()
  343. */
  344. function tripal_entities_entity_delete_form($form, &$form_state, $entity) {
  345. $form_state['entity'] = $entity;
  346. $form['#submit'][] = 'tripal_entities_entity_delete_form_submit';
  347. $form = confirm_form($form,
  348. t('Click the delete button below to confirm deletion of the record titled: %title',
  349. array('%title' => $entity->title)), 'admin/content/tripal_entity',
  350. '<p>' .t('This action cannot be undone.') .'</p>', t('Delete'), t('Cancel'), 'confirm');
  351. return $form;
  352. }
  353. /**
  354. * Submit callback for tripal_entity_delete_form
  355. */
  356. function tripal_entities_entity_delete_form_submit($form, &$form_state) {
  357. $entity = $form_state['entity'];
  358. $entity_controller = new TripalEntityController($entity->type);
  359. if ($entity_controller->delete(array($entity->id))) {
  360. drupal_set_message(t('The record title "%name" has been deleted.', array('%name' => $entity->title)));
  361. $form_state['redirect'] = 'admin/content/bio-data';
  362. }
  363. else {
  364. drupal_set_message(t('The tripal_entity %name was not deleted.', array('%name' => $entity->title)), "error");
  365. }
  366. }