TripalEntityUIController.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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' => 'Biological Data',
  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. );
  25. // Change the add page menu to multiple types of entities
  26. $items[$this->path . '/add'] = array(
  27. 'title' => 'Add new biological data',
  28. 'description' => 'Add new biological data',
  29. 'page callback' => 'drupal_get_form',
  30. 'page arguments' => array('tripal_entity_form'),
  31. 'access callback' => 'tripal_entity_access',
  32. 'access arguments' => array('administer tripal data'),
  33. 'type' => MENU_LOCAL_ACTION,
  34. 'weight' => 20,
  35. );
  36. // Set a custom page for adding new tripal data entities.
  37. $items['data/add'] = array(
  38. 'title' => 'Add Tripal data',
  39. 'description' => 'Add a new tripal data record',
  40. 'page callback' => 'drupal_get_form',
  41. 'page arguments' => array('tripal_entity_form'),
  42. 'access callback' => 'tripal_entity_access',
  43. 'access arguments' => array('edit'),
  44. 'type' => MENU_NORMAL_ITEM,
  45. 'weight' => 20,
  46. );
  47. // Link for viewing a tripal data type.
  48. $items['data/' . $wildcard] = array(
  49. 'title callback' => 'tripal_entity_title',
  50. 'title arguments' => array(1),
  51. 'page callback' => 'tripal_entity_view',
  52. 'page arguments' => array(1),
  53. 'access callback' => 'tripal_entity_access',
  54. 'access arguments' => array('view', 1),
  55. 'type' => MENU_CALLBACK,
  56. );
  57. // 'View' tab for an individual entity page.
  58. $items['data/' . $wildcard . '/view'] = array(
  59. 'title' => 'View',
  60. 'page callback' => 'tripal_entity_view',
  61. 'page arguments' => array(1),
  62. 'access callback' => 'tripal_entity_access',
  63. 'access arguments' => array('view', 1),
  64. 'type' => MENU_DEFAULT_LOCAL_TASK,
  65. 'weight' => -10,
  66. );
  67. // 'Edit' tab for an individual entity page.
  68. $items['data/' . $wildcard . '/edit'] = array(
  69. 'title' => 'Edit',
  70. 'page callback' => 'drupal_get_form',
  71. 'page arguments' => array('tripal_entity_form', 1),
  72. 'access callback' => 'tripal_entity_access',
  73. 'access arguments' => array('edit', 1),
  74. 'type' => MENU_LOCAL_TASK,
  75. );
  76. // Menu item for deleting tripal data entities.
  77. $items['data/' . $wildcard . '/delete'] = array(
  78. 'title' => 'Delete',
  79. 'page callback' => 'drupal_get_form',
  80. 'page arguments' => array('tripal_entity_delete_form', 1),
  81. 'access callback' => 'tripal_entity_access',
  82. 'access arguments' => array('edit', 1),
  83. 'type' => MENU_CALLBACK,
  84. 'weight' => 10,
  85. );
  86. return $items;
  87. }
  88. }
  89. /**
  90. * Determines whether the given user has access to a tripal data entity.
  91. *
  92. * @param $op
  93. * The operation being performed. One of 'view', 'update', 'create', 'delete'
  94. * or just 'edit' (being the same as 'create' or 'update').
  95. * @param $entity
  96. * Optionally a tripal data entity or a tripal data type to check access for.
  97. * If nothing is given, access for all types is determined.
  98. * @param $account
  99. * The user to check for. Leave it to NULL to check for the global user.
  100. * @return boolean
  101. * Whether access is allowed or not.
  102. */
  103. function tripal_entity_access($op, $entity = NULL, $account = NULL) {
  104. if (user_access('administer tripal data', $account)) {
  105. return TRUE;
  106. }
  107. if (isset($entity) && $type_name = $entity->type) {
  108. $op = ($op == 'view') ? 'view' : 'edit';
  109. if (user_access("$op any $type_name data", $account)) {
  110. return TRUE;
  111. }
  112. }
  113. return FALSE;
  114. }
  115. /**
  116. *
  117. */
  118. function tripal_entity_form($form, &$form_state, $entity = NULL) {
  119. // Set the defaults.
  120. $cv_id = NULL;
  121. $term_name = NULL;
  122. $cvterm = NULL;
  123. // Set defaults if an entity was provided.
  124. if ($entity) {
  125. drupal_set_title('Edit ' . $entity->title);
  126. $id = $entity->id;
  127. $values = array('cvterm_id' => $entity->cvterm_id);
  128. $cvterm = chado_generate_var('cvterm', $values);
  129. $cv_id = $cvterm->cv_id->cv_id;
  130. $term_name = $cvterm->name;
  131. }
  132. // Set defaults using the form state.
  133. if (array_key_exists('values', $form_state)) {
  134. $cv_id = array_key_exists('cv_id', $form_state['values']) ? $form_state['values']['cv_id'] : NULL;
  135. $term_name = array_key_exists('term_name', $form_state['values']) ? $form_state['values']['term_name'] : NULL;
  136. // Get the cvterm that matches
  137. $values = array(
  138. 'cv_id' => $cv_id,
  139. 'name' => $term_name
  140. );
  141. $cvterm = chado_generate_var('cvterm', $values);
  142. }
  143. // Let the user select the vocabulary defaut and tripal_entity but only if they haven't
  144. // already selected a tripal_entity.
  145. $cvs = tripal_entities_get_published_vocabularies_as_select_options();
  146. if (!$term_name) {
  147. $form['cv_id'] = array(
  148. '#type' => 'select',
  149. '#title' => t('Published vocabulary'),
  150. '#options' => $cvs,
  151. '#required' => TRUE,
  152. '#description' => t('Select a vocabulary that contains the term for the type of data you want to add.'),
  153. '#default_value' => $cv_id,
  154. '#ajax' => array(
  155. 'callback' => "tripal_entity_form_ajax_callback",
  156. 'wrapper' => 'tripal_entity_form',
  157. 'effect' => 'fade',
  158. 'method' => 'replace'
  159. )
  160. );
  161. }
  162. // If we have a CV ID then we want to provide an autocomplete field
  163. if ($cv_id and !$term_name) {
  164. $cvterms = tripal_entities_get_published_terms_as_select_options ($cv_id);
  165. $form['cvterm_select']['term_name'] = array(
  166. '#title' => t('Published term'),
  167. '#type' => 'select',
  168. '#options' => $cvterms,
  169. '#description' => t("Enter the name of a term within the selected vocabulary for the record type you want to enter."),
  170. '#required' => TRUE,
  171. );
  172. $form['cvterm_select']['select_button'] = array(
  173. '#type' => 'submit',
  174. '#value' => t('Use this term'),
  175. '#name' => 'select_cvterm',
  176. );
  177. }
  178. // Once the CV term is selected then provide the other fields.
  179. if ($cvterm) {
  180. $bundle_id = $cvterm->dbxref_id->db_id->name . '_' . $cvterm->dbxref_id->accession;
  181. $form['cv_id'] = array(
  182. '#type' => 'hidden',
  183. '#value' => $cv_id,
  184. );
  185. $form['type'] = array(
  186. '#type' => 'hidden',
  187. '#value' => $cvterm->dbxref_id->db_id->name,
  188. );
  189. $form['term_name'] = array(
  190. '#type' => 'hidden',
  191. '#value' => $term_name,
  192. );
  193. $form['cvterm_id'] = array(
  194. '#type' => 'hidden',
  195. '#value' => $cvterm->cvterm_id,
  196. );
  197. $form['bundle'] = array(
  198. '#type' => 'hidden',
  199. '#value' => $bundle_id,
  200. );
  201. $form['details'] = array(
  202. '#type' => 'fieldset',
  203. '#title' => 'Record Type',
  204. '#collapsable' => FALSE,
  205. '#weight' => -100,
  206. );
  207. $form['details']['cv_name_shown'] = array(
  208. '#type' => 'item',
  209. '#title' => 'Vocabulary',
  210. '#markup' => $cvterm->cv_id->name,
  211. );
  212. $form['details']['term_name_shown'] = array(
  213. '#type' => 'item',
  214. '#title' => 'Term',
  215. '#markup' => $cvterm->name,
  216. );
  217. // If the entity doesn't exist then create one.
  218. if (!$entity) {
  219. $entity = entity_get_controller($cvterm->dbxref_id->db_id->name)->create(array('bundle' => $bundle_id));
  220. field_attach_form($cvterm->dbxref_id->db_id->name, $entity, $form, $form_state);
  221. $form['add_button'] = array(
  222. '#type' => 'submit',
  223. '#value' => t('Add a new ' . $cvterm->name),
  224. '#name' => 'add_data',
  225. '#weight' => 1000
  226. );
  227. }
  228. else {
  229. field_attach_form($cvterm->dbxref_id->db_id->name, $entity, $form, $form_state);
  230. $form['entity_id'] = array(
  231. '#type' => 'hidden',
  232. '#value' => $entity->id,
  233. );
  234. $form['update_button'] = array(
  235. '#type' => 'submit',
  236. '#value' => t('Update'),
  237. '#name' => 'update_data',
  238. '#weight' => 1000
  239. );
  240. $form['delete_button'] = array(
  241. '#type' => 'submit',
  242. '#value' => t('Delete'),
  243. '#name' => 'delete_data',
  244. '#weight' => 1001,
  245. );
  246. }
  247. // The entity object must be added to the $form_state in order for
  248. // the Entity API to work. It must have a key of the entity name.
  249. $form_state[$cvterm->dbxref_id->db_id->name] = $entity;
  250. }
  251. $form['#prefix'] = '<div id="tripal_entity_form">';
  252. $form['#suffix'] = '</div>';
  253. return $form;
  254. }
  255. /**
  256. * An Ajax callback for the tripal_entity_form.
  257. */
  258. function tripal_entity_form_ajax_callback($form, $form_state) {
  259. // return the form so Drupal can update the content on the page
  260. return $form;
  261. }
  262. /**
  263. * Implements hook_validate() for the tripal_entity_form.
  264. */
  265. function tripal_entity_form_validate($form, &$form_state) {
  266. if (array_key_exists('clicked_button', $form_state) and
  267. $form_state['clicked_button']['#name'] == 'add_data') {
  268. $tripal_entity = (object) $form_state['values'];
  269. $entity_type = $form_state['values']['type'];
  270. field_attach_form_validate($entity_type, $tripal_entity, $form, $form_state);
  271. }
  272. }
  273. /**
  274. * Implements hook_submit() for the tripal_entity_form.
  275. *
  276. */
  277. function tripal_entity_form_submit($form, &$form_state) {
  278. if ($form_state['clicked_button']['#name'] == 'cancel') {
  279. if (array_key_exists('id', $form_state['values'])){
  280. $entity_id = $form_state['values']['entity_id'];
  281. $form_state['redirect'] = "data/$entity_id";
  282. }
  283. else {
  284. $form_state['redirect'] = "admin/structure/tripal_entity";
  285. }
  286. return;
  287. }
  288. if ($form_state['clicked_button']['#name'] == 'select_cvterm') {
  289. // don't do anything, we just need to know what the term name is.
  290. $form_state['rebuild'] = TRUE;
  291. }
  292. if ($form_state['clicked_button']['#name'] == 'update_data' or
  293. $form_state['clicked_button']['#name'] == 'add_data') {
  294. // Use the Entity API to get the entity from the form state, then
  295. // attach the fields and save.
  296. $entity_type = $form_state['values']['type'];
  297. $entity = entity_ui_controller($entity_type)->entityFormSubmitBuildEntity($form, $form_state);
  298. $entity->save();
  299. $form_state['redirect'] = "data/$entity->id";
  300. }
  301. if ($form_state['clicked_button']['#name'] == 'delete_data') {
  302. $entity_id = $form_state['values']['entity_id'];
  303. $form_state['redirect'] = 'data/' . $entity_id . '/delete';
  304. }
  305. }
  306. /**
  307. * Form callback: confirmation form for deleting a tripal_entity.
  308. *
  309. * @param $tripal_entity
  310. * The tripal_entity to delete
  311. *
  312. * @see confirm_form()
  313. */
  314. function tripal_entity_delete_form($form, &$form_state, $entity) {
  315. $form_state['entity'] = $entity;
  316. $form['#submit'][] = 'tripal_entity_delete_form_submit';
  317. $form = confirm_form($form,
  318. t('Click the delete button below to confirm deletion of the record titled: %title', array('%title' => $entity->title)),
  319. 'admin/content/tripal_entity',
  320. '<p>' . t('This action cannot be undone.') . '</p>',
  321. t('Delete'),
  322. t('Cancel'),
  323. 'confirm'
  324. );
  325. return $form;
  326. }
  327. /**
  328. * Submit callback for tripal_entity_delete_form
  329. */
  330. function tripal_entity_delete_form_submit($form, &$form_state) {
  331. $entity = $form_state['entity'];
  332. $entity_controller = new TripalEntityController($entity->type);
  333. if($entity_controller->delete($entity)) {
  334. drupal_set_message(t('The record title "%name" has been deleted.', array('%name' => $entity->title)));
  335. $form_state['redirect'] = 'admin/content/tripal_entitys';
  336. }
  337. else {
  338. drupal_set_message(t('The tripal_entity %name was not deleted.', array('%name' => $entity->title)), "error");
  339. }
  340. }
  341. /**
  342. * Menu callback to display an entity.
  343. *
  344. * As we load the entity for display, we're responsible for invoking a number
  345. * of hooks in their proper order.
  346. *
  347. * @see hook_entity_prepare_view()
  348. * @see hook_entity_view()
  349. * @see hook_entity_view_alter()
  350. */
  351. function tripal_entity_view($entity, $view_mode = 'full') {
  352. if ($entity) {
  353. $controller = entity_get_controller($entity->type);
  354. $content = $controller->view(array($entity->id => $entity));
  355. drupal_set_title($entity->title);
  356. return $content;
  357. }
  358. }
  359. /**
  360. * Menu title callback for showing individual entities
  361. */
  362. function tripal_entity_title($entity){
  363. if ($entity) {
  364. return $entity->title;
  365. }
  366. }