TripalDataUIController.inc 15 KB

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