TripalDataUIController.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. $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_data_form'),
  31. 'access callback' => 'tripal_data_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_data_form'),
  42. 'access callback' => 'tripal_data_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_data_title',
  50. 'title arguments' => array(1),
  51. 'page callback' => 'tripal_data_view',
  52. 'page arguments' => array(1),
  53. 'access callback' => 'tripal_data_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_data_view',
  61. 'page arguments' => array(1),
  62. 'access callback' => 'tripal_data_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_data_form', 1),
  72. 'access callback' => 'tripal_data_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_data_delete_form', 1),
  81. 'access callback' => 'tripal_data_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_data_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_data_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_data but only if they haven't
  144. // already selected a tripal_data.
  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_data_form_ajax_callback",
  156. 'wrapper' => 'tripal_data_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['entity_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['submit'] = 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['submit'] = array(
  231. '#type' => 'submit',
  232. '#value' => t('Update'),
  233. '#name' => 'update_data',
  234. '#weight' => 1000
  235. );
  236. }
  237. // The entity object must be added to the $form_state in order for
  238. // the Entity API to work. It must have a key of the entity name.
  239. $form_state[$cvterm->dbxref_id->db_id->name] = $entity;
  240. }
  241. $form['#prefix'] = '<div id="tripal_data_form">';
  242. $form['#suffix'] = '</div>';
  243. return $form;
  244. }
  245. /**
  246. * An Ajax callback for the tripal_data_form.
  247. */
  248. function tripal_data_form_ajax_callback($form, $form_state) {
  249. // return the form so Drupal can update the content on the page
  250. return $form;
  251. }
  252. /**
  253. * Implements hook_validate() for the tripal_data_form.
  254. */
  255. function tripal_data_form_validate($form, &$form_state) {
  256. if ($form_state['clicked_button']['#name'] == 'add_data') {
  257. $tripal_data = (object) $form_state['values'];
  258. $entity_type = $form_state['values']['entity_type'];
  259. field_attach_form_validate($entity_type, $tripal_data, $form, $form_state);
  260. }
  261. }
  262. /**
  263. * Implements hook_submit() for the tripal_data_form.
  264. *
  265. */
  266. function tripal_data_form_submit($form, &$form_state) {
  267. if ($form_state['clicked_button']['#name'] == 'cancel') {
  268. if (array_key_exists('id', $form_state['values'])){
  269. $entity = $form_state['values']['entity'];
  270. $form_state['redirect'] = "data/$entity->id";
  271. }
  272. else {
  273. $form_state['redirect'] = "admin/structure/tripal_data";
  274. }
  275. return;
  276. }
  277. if ($form_state['clicked_button']['#name'] == 'select_cvterm') {
  278. // don't do anything, we just need to know what the term name is.
  279. $form_state['rebuild'] = TRUE;
  280. }
  281. if ($form_state['clicked_button']['#name'] == 'update_data' or
  282. $form_state['clicked_button']['#name'] == 'add_data') {
  283. // Use the Entity API to get the entity from the form state, then
  284. // attach the fields and save.
  285. $entity_type = $form_state['values']['entity_type'];
  286. $entity = entity_ui_controller($entity_type)->entityFormSubmitBuildEntity($form, $form_state);
  287. $entity->save();
  288. $form_state['redirect'] = "data/$entity->id";
  289. }
  290. }
  291. /**
  292. * Form API submit callback for the delete button.
  293. *
  294. * @todo Remove hard-coded path
  295. */
  296. function tripal_data_form_submit_delete(&$form, &$form_state) {
  297. $form_state['redirect'] = 'admin/content/tripal_datas/tripal_data/' . $form_state['tripal_data']->tripal_data_id . '/delete';
  298. }
  299. /**
  300. * Form callback: confirmation form for deleting a tripal_data.
  301. *
  302. * @param $tripal_data
  303. * The tripal_data to delete
  304. *
  305. * @see confirm_form()
  306. */
  307. function tripal_data_delete_form($form, &$form_state, $entity) {
  308. $form_state['entity'] = $entity;
  309. $form['#submit'][] = 'tripal_data_delete_form_submit';
  310. $form = confirm_form($form,
  311. t('Click the delete button below to confirm deltion of the record titled: %title', array('%title' => $entity->title)),
  312. 'admin/content/tripal_data',
  313. '<p>' . t('This action cannot be undone.') . '</p>',
  314. t('Delete'),
  315. t('Cancel'),
  316. 'confirm'
  317. );
  318. return $form;
  319. }
  320. /**
  321. * Submit callback for tripal_data_delete_form
  322. */
  323. function tripal_data_delete_form_submit($form, &$form_state) {
  324. $entity = $form_state['entity'];
  325. tripal_data_delete($entity);
  326. drupal_set_message(t('The tripal_data %name has been deleted.', array('%name' => $tripal_data->name)));
  327. watchdog('tripal_data', 'Deleted tripal_data %name.', array('%name' => $tripal_data->name));
  328. $form_state['redirect'] = 'admin/content/tripal_datas';
  329. }
  330. /**
  331. * Menu callback to display an entity.
  332. *
  333. * As we load the entity for display, we're responsible for invoking a number
  334. * of hooks in their proper order.
  335. *
  336. * @see hook_entity_prepare_view()
  337. * @see hook_entity_view()
  338. * @see hook_entity_view_alter()
  339. */
  340. function tripal_data_view($entity, $view_mode = 'full') {
  341. $controller = entity_get_controller($entity->type);
  342. $content = $controller->view(array($entity->id => $entity));
  343. drupal_set_title($entity->title);
  344. return $content;
  345. }
  346. /**
  347. * Menu title callback for showing individual entities
  348. */
  349. function tripal_data_title(TripalData $entity){
  350. return $entity->title;
  351. }