TripalDataUIController.inc 11 KB

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