tripal_jbrowse_mgmt_edit.form.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * @file
  4. * Instance Edit Form.
  5. */
  6. /**
  7. *
  8. */
  9. function tripal_jbrowse_mgmt_edit_form($form, &$form_state) {
  10. $instance_id = $form_state['build_info']['args'][0];
  11. $result = tripal_jbrowse_mgmt_get_instances(['id' => $instance_id]);
  12. if (!empty($result)) {
  13. $instance = $result[0];
  14. $edit_form = TRUE;
  15. }
  16. else {
  17. drupal_set_message('Unable to access the instance you would like to edit.', 'error');
  18. return $form;
  19. }
  20. // This is essentiall the add form with defaults.
  21. // We use a second function here for cleaner validation and submit.
  22. require_once 'tripal_jbrowse_mgmt_add.form.inc';
  23. $form = tripal_jbrowse_mgmt_add_form($form, $form_state);
  24. $form['description_of_form']['#markup'] = t('Edit details regarding the current JBrowse instance.');
  25. // Set Default Values.
  26. $form['organism']['#default_value'] = $instance->organism_id;
  27. $form['description']['#default_value'] = $instance->description;
  28. $form['page']['start-loc']['#default_value'] = tripal_jbrowse_mgmt_get_instance_property($instance_id, 'start-loc');
  29. $form['page']['start-tracks']['#default_value'] = tripal_jbrowse_mgmt_get_instance_property($instance_id, 'start-tracks');
  30. // Remove the file upload.
  31. unset($form['data']);
  32. // Change the submit button.
  33. $form['submit']['#value'] = 'Save Changes';
  34. return $form;
  35. }
  36. /**
  37. * Validate the form.
  38. *
  39. * @param $form
  40. * @param $form_state
  41. */
  42. function tripal_jbrowse_mgmt_edit_form_validate($form, &$form_state) {
  43. $values = $form_state['values'];
  44. $organism = isset($values['organism']) ? $values['organism'] : NULL;
  45. $instance_id = $form_state['build_info']['args'][0];
  46. $instances = tripal_jbrowse_mgmt_get_instances(['organism_id' => $organism]);
  47. if (!empty($instances)) {
  48. if ($instances[0]->id != $instance_id) {
  49. form_set_error(
  50. 'organism',
  51. 'A JBrowse instance for the selected organism already exists. You can edit the instance from the instances page.'
  52. );
  53. }
  54. }
  55. $organism = db_select('chado.organism', 'CO')
  56. ->fields('CO')
  57. ->condition('organism_id', $organism)
  58. ->execute()
  59. ->fetchObject();
  60. if (empty($organism)) {
  61. form_set_error('organism', 'Invalid organism selected ' . $organism);
  62. }
  63. }
  64. /**
  65. * @param $form
  66. * @param $form_state
  67. *
  68. * @throws \Exception
  69. */
  70. function tripal_jbrowse_mgmt_edit_form_submit($form, &$form_state) {
  71. global $user;
  72. $values = $form_state['values'];
  73. $organism_id = $values['organism'];
  74. $description = isset($values['description']) ? $values['description'] : '';
  75. // Check if this is an add or edit form.
  76. $edit_form = FALSE;
  77. if (isset($form_state['build_info']['args'][0]) AND is_numeric($form_state['build_info']['args'][0])) {
  78. $instance_id = $form_state['build_info']['args'][0];
  79. $edit_form = TRUE;
  80. }
  81. $organism = db_select('chado.organism', 'CO')
  82. ->fields('CO')
  83. ->condition('organism_id', $organism_id)
  84. ->execute()
  85. ->fetchObject();
  86. $title = tripal_jbrowse_mgmt_construct_organism_name($organism);
  87. $data = [
  88. 'organism_id' => $organism_id,
  89. 'title' => $title,
  90. 'description' => $description,
  91. ];
  92. $success = tripal_jbrowse_mgmt_update_instance($instance_id, $data);
  93. if ($success) {
  94. drupal_set_message("Successfully updated $title JBrowse instance.");
  95. $form_state['redirect'] = 'admin/tripal/extension/tripal_jbrowse/management/instances';
  96. }
  97. else {
  98. drupal_set_message('Failed to update the current instance!', 'error');
  99. return FALSE;
  100. }
  101. // Now save the instance properties.
  102. tripal_jbrowse_mgmt_save_instance_properties(
  103. $instance_id,
  104. $form_state['values']['page']
  105. );
  106. }