TripalDataUIController.inc 15 KB

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