ChadoDataUIController.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. <?php
  2. /**
  3. * UI controller.
  4. */
  5. class ChadoDataUIController 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. $items[$this->path] = array(
  16. 'title' => 'Chado Data',
  17. 'description' => 'Add edit and update chado data.',
  18. 'page callback' => 'system_admin_menu_block_page',
  19. 'access arguments' => array('access administration pages'),
  20. 'file path' => drupal_get_path('module', 'system'),
  21. 'file' => 'system.admin.inc',
  22. );
  23. // Change the overview menu type for the list of models.
  24. $items[$this->path]['type'] = MENU_LOCAL_TASK;
  25. // Add an action link to the admin page for adding new data.
  26. $items[$this->path . '/add'] = array(
  27. 'title' => 'Add Chado Data',
  28. 'description' => 'Add a new chado data record',
  29. 'page callback' => 'drupal_get_form',
  30. 'page arguments' => array('chado_data_form'),
  31. 'access callback' => 'chado_data_access',
  32. 'access arguments' => array('edit'),
  33. 'type' => MENU_LOCAL_ACTION,
  34. 'weight' => 20,
  35. );
  36. // Set a custom page for adding new chado data entities.
  37. $items['data/add'] = array(
  38. 'title' => 'Add Chado data',
  39. 'description' => 'Add a new chado data record',
  40. 'page callback' => 'drupal_get_form',
  41. 'page arguments' => array('chado_data_form'),
  42. 'access callback' => 'chado_data_access',
  43. 'access arguments' => array('edit'),
  44. 'type' => MENU_NORMAL_ITEM,
  45. 'weight' => 20,
  46. );
  47. // Link for viewing a chado data type.
  48. $items['data/' . $wildcard] = array(
  49. 'title callback' => 'chado_data_title',
  50. 'title arguments' => array(1),
  51. 'page callback' => 'chado_data_view',
  52. 'page arguments' => array(1),
  53. 'access callback' => 'chado_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' => 'chado_data_view',
  61. 'page arguments' => array(1),
  62. 'access callback' => 'chado_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('chado_data_form', 1),
  72. 'access callback' => 'chado_data_access',
  73. 'access arguments' => array('edit', 1),
  74. 'type' => MENU_LOCAL_TASK,
  75. );
  76. // Menu item for deleting chado data entities.
  77. $items['data/' . $wildcard . '/delete'] = array(
  78. 'title' => 'Delete',
  79. 'page callback' => 'drupal_get_form',
  80. 'page arguments' => array('chado_data_delete_form', 1),
  81. 'access callback' => 'chado_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 chado 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 chado data entity or a chado 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 chado_data_access($op, $entity = NULL, $account = NULL) {
  104. if (user_access('administer chado 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 chado_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. $entity_id = $entity->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 and chado_data but only if they haven't
  144. // already selected a chado_data.
  145. $cvs = tripal_get_cv_select_options();
  146. if (!$term_name) {
  147. $form['cv_id'] = array(
  148. '#type' => 'select',
  149. '#title' => t('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' => "chado_data_form_ajax_callback",
  156. 'wrapper' => 'chado_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. $form['cvterm_select']['term_name'] = array(
  165. '#title' => t('Record Type'),
  166. '#type' => 'textfield',
  167. '#description' => t("Enter the name of a term within the selected vocabulary for the record type you want to enter."),
  168. '#required' => TRUE,
  169. '#default_value' => $term_name,
  170. '#autocomplete_path' => "admin/tripal/chado/tripal_cv/cvterm/auto_name/$cv_id",
  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['term_name'] = array(
  186. '#type' => 'hidden',
  187. '#value' => $term_name,
  188. );
  189. $form['cvterm_id'] = array(
  190. '#type' => 'hidden',
  191. '#value' => $cvterm->cvterm_id,
  192. );
  193. $form['type'] = array(
  194. '#type' => 'hidden',
  195. '#value' => $bundle_id,
  196. );
  197. $form['details'] = array(
  198. '#type' => 'fieldset',
  199. '#title' => 'Record Type',
  200. '#collapsable' => FALSE,
  201. '#weight' => -100,
  202. );
  203. $form['details']['cv_name_shown'] = array(
  204. '#type' => 'item',
  205. '#title' => 'Vocabulary',
  206. '#markup' => $cvterm->cv_id->name,
  207. );
  208. $form['details']['term_name_shown'] = array(
  209. '#type' => 'item',
  210. '#title' => 'Term',
  211. '#markup' => $cvterm->name,
  212. );
  213. /*
  214. // Create the Chado data type entity.
  215. $data_type_entity = chado_data_type_create(array(
  216. 'type' => $bundle_id,
  217. 'label' => $cvterm->name,
  218. 'module' => 'tripal_entities'
  219. ));
  220. $data_type_entity->save();
  221. */
  222. /*
  223. // Drupal field types and settings:
  224. // https://www.drupal.org/node/1879542
  225. $field = array(
  226. 'field_name' => 'feature__name',
  227. 'type' => 'text',
  228. 'cardinality' => 1,
  229. 'locked' => TRUE,
  230. 'storage' => array(
  231. 'type' => 'tripal_entities_storage'
  232. ),
  233. );
  234. field_create_field($field);
  235. $field_instance = array(
  236. 'field_name' => 'feature__name',
  237. 'label' => 'Name',
  238. 'widget' => array(
  239. 'type' => 'text_textfield'
  240. ),
  241. 'entity_type' => 'chado_data',
  242. 'required' => 'true',
  243. 'settings' => array(
  244. 'max_length' => 255
  245. ),
  246. 'bundle' => $bundle_id,
  247. );
  248. field_create_instance($field_instance);
  249. $field = array(
  250. 'field_name' => 'feature__uniquename',
  251. 'type' => 'text',
  252. 'cardinality' => 1,
  253. 'locked' => TRUE,
  254. 'storage' => array(
  255. 'type' => 'tripal_entities_storage'
  256. ),
  257. );
  258. field_create_field($field);
  259. $field_instance = array(
  260. 'field_name' => 'feature__uniquename',
  261. 'label' => 'Unique Name',
  262. 'widget' => array(
  263. 'type' => 'text_textfield'
  264. ),
  265. 'entity_type' => 'chado_data',
  266. 'required' => 'true',
  267. 'settings' => array(
  268. 'max_length' => 255
  269. ),
  270. 'bundle' => $bundle_id,
  271. );
  272. field_create_instance($field_instance);
  273. $field = array(
  274. 'field_name' => 'feature__organism_id',
  275. 'type' => 'organism_id',
  276. 'cardinality' => 1,
  277. 'locked' => TRUE,
  278. 'storage' => array(
  279. 'type' => 'tripal_entities_storage'
  280. ),
  281. );
  282. field_create_field($field);
  283. $field_instance = array(
  284. 'field_name' => 'feature__organism_id',
  285. 'label' => 'Organism',
  286. 'entity_type' => 'chado_data',
  287. 'required' => 'true',
  288. 'settings' => array(),
  289. 'bundle' => $bundle_id,
  290. );
  291. field_create_instance($field_instance);
  292. */
  293. // If the entity doesn't exist then create one.
  294. if (!$entity) {
  295. $entity = entity_get_controller('chado_data')->create(array('type' => $bundle_id));
  296. field_attach_form('chado_data', $entity, $form, $form_state);
  297. $form['submit'] = array(
  298. '#type' => 'submit',
  299. '#value' => t('Add a new ' . $cvterm->name),
  300. '#name' => 'add_data',
  301. '#weight' => 1000
  302. );
  303. }
  304. else {
  305. field_attach_form('chado_data', $entity, $form, $form_state);
  306. $form['submit'] = array(
  307. '#type' => 'submit',
  308. '#value' => t('Update'),
  309. '#name' => 'update_data',
  310. '#weight' => 1000
  311. );
  312. }
  313. // The entity object must be added to the $form_state in order for
  314. // the Entity API to work. It must have a key of the entity name.
  315. $form_state['chado_data'] = $entity;
  316. }
  317. $form['#prefix'] = '<div id="chado_data_form">';
  318. $form['#suffix'] = '</div>';
  319. return $form;
  320. }
  321. /**
  322. * An Ajax callback for the chado_data_form.
  323. */
  324. function chado_data_form_ajax_callback($form, $form_state) {
  325. // return the form so Drupal can update the content on the page
  326. return $form;
  327. }
  328. /**
  329. * Implements hook_validate() for the chado_data_form.
  330. */
  331. function chado_data_form_validate($form, &$form_state) {
  332. if ($form_state['clicked_button']['#name'] == 'add_data') {
  333. $chado_data = (object) $form_state['values'];
  334. field_attach_form_validate('chado_data', $chado_data, $form, $form_state);
  335. }
  336. }
  337. /**
  338. * Implements hook_submit() for the chado_data_form.
  339. *
  340. */
  341. function chado_data_form_submit($form, &$form_state) {
  342. if ($form_state['clicked_button']['#name'] == 'cancel') {
  343. if (array_key_exists('entity_id', $form_state['values'])){
  344. $entity = $form_state['values']['entity'];
  345. $form_state['redirect'] = "data/$entity->entity_id";
  346. }
  347. else {
  348. $form_state['redirect'] = "admin/structure/chado_data";
  349. }
  350. return;
  351. }
  352. if ($form_state['clicked_button']['#name'] == 'select_cvterm') {
  353. // don't do anything, we just need to know what the term name is.
  354. $form_state['rebuild'] = TRUE;
  355. }
  356. if ($form_state['clicked_button']['#name'] == 'update_data' or
  357. $form_state['clicked_button']['#name'] == 'add_data') {
  358. // Use the Entity API to get the entity from the form state, then
  359. // attach the fields and save.
  360. $entity = entity_ui_controller('chado_data')->entityFormSubmitBuildEntity($form, $form_state);
  361. $entity->save();
  362. $form_state['redirect'] = "data/$entity->entity_id";
  363. }
  364. }
  365. /**
  366. * Form API submit callback for the delete button.
  367. *
  368. * @todo Remove hard-coded path
  369. */
  370. function chado_data_form_submit_delete(&$form, &$form_state) {
  371. $form_state['redirect'] = 'admin/content/chado_datas/chado_data/' . $form_state['chado_data']->chado_data_id . '/delete';
  372. }
  373. /**
  374. * Form callback: confirmation form for deleting a chado_data.
  375. *
  376. * @param $chado_data
  377. * The chado_data to delete
  378. *
  379. * @see confirm_form()
  380. */
  381. function chado_data_delete_form($form, &$form_state, $chado_data) {
  382. $form_state['chado_data'] = $chado_data;
  383. $form['#submit'][] = 'chado_data_delete_form_submit';
  384. $form = confirm_form($form,
  385. t('Are you sure you want to delete chado_data %name?', array('%name' => $chado_data->name)),
  386. 'admin/content/chado_datas/chado_data',
  387. '<p>' . t('This action cannot be undone.') . '</p>',
  388. t('Delete'),
  389. t('Cancel'),
  390. 'confirm'
  391. );
  392. return $form;
  393. }
  394. /**
  395. * Submit callback for chado_data_delete_form
  396. */
  397. function chado_data_delete_form_submit($form, &$form_state) {
  398. $chado_data = $form_state['chado_data'];
  399. chado_data_delete($chado_data);
  400. drupal_set_message(t('The chado_data %name has been deleted.', array('%name' => $chado_data->name)));
  401. watchdog('chado_data', 'Deleted chado_data %name.', array('%name' => $chado_data->name));
  402. $form_state['redirect'] = 'admin/content/chado_datas';
  403. }
  404. /**
  405. * Sets the breadcrumb for administrative chado_data pages.
  406. */
  407. function chado_data_set_breadcrumb() {
  408. $breadcrumb = array(
  409. l(t('Home'), '<front>'),
  410. l(t('Administration'), 'admin'),
  411. l(t('Content'), 'admin/content'),
  412. l(t('Chado Data'), 'admin/content/chado_data'),
  413. );
  414. drupal_set_breadcrumb($breadcrumb);
  415. }
  416. /**
  417. * Menu callback to display an entity.
  418. *
  419. * As we load the entity for display, we're responsible for invoking a number
  420. * of hooks in their proper order.
  421. *
  422. * @see hook_entity_prepare_view()
  423. * @see hook_entity_view()
  424. * @see hook_entity_view_alter()
  425. */
  426. function chado_data_view($entity, $view_mode = 'full') {
  427. $controller = entity_get_controller('chado_data');
  428. $content = $controller->view(array($entity->entity_id => $entity));
  429. drupal_set_title($entity->title);
  430. return $content;
  431. }
  432. /**
  433. * Menu title callback for showing individual entities
  434. */
  435. function chado_data_title(ChadoData $entity){
  436. return $entity->title;
  437. }