tripal_jbrowse_mgmt_register.form.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. /**
  3. * @file
  4. * Instance Register Form.
  5. */
  6. /**
  7. *
  8. */
  9. function tripal_jbrowse_mgmt_register_form($form, &$form_state) {
  10. // This is essentiall the add form without create JBrowse functionality.
  11. // We use a second function here for cleaner validation and submit.
  12. require_once 'tripal_jbrowse_mgmt_add.form.inc';
  13. $form = tripal_jbrowse_mgmt_add_form($form, $form_state);
  14. $form['description_of_form']['#markup'] = t('Register an already existing JBrowse instance to be managed by this module.');
  15. // Remove the file upload.
  16. unset($form['data']);
  17. // Change the submit button.
  18. $form['submit']['#value'] = 'Save Changes';
  19. return $form;
  20. }
  21. /**
  22. * Validate the form.
  23. *
  24. * @param $form
  25. * @param $form_state
  26. */
  27. function tripal_jbrowse_mgmt_register_form_validate($form, &$form_state) {
  28. $values = $form_state['values'];
  29. $organism = isset($values['organism']) ? $values['organism'] : NULL;
  30. $instances = tripal_jbrowse_mgmt_get_instances(['organism_id' => $organism]);
  31. if (!empty($instances)) {
  32. form_set_error(
  33. 'organism',
  34. 'A JBrowse instance for the selected organism already exists. You can edit the instance from the instances page.'
  35. );
  36. }
  37. $organism = db_select('chado.organism', 'CO')
  38. ->fields('CO')
  39. ->condition('organism_id', $organism)
  40. ->execute()
  41. ->fetchObject();
  42. if (empty($organism)) {
  43. form_set_error('organism', 'Invalid organism selected ' . $organism);
  44. }
  45. }
  46. /**
  47. * @param $form
  48. * @param $form_state
  49. *
  50. * @throws \Exception
  51. */
  52. function tripal_jbrowse_mgmt_register_form_submit($form, &$form_state) {
  53. global $user;
  54. $values = $form_state['values'];
  55. $organism_id = $values['organism'];
  56. $description = isset($values['description']) ? $values['description'] : '';
  57. $organism = db_select('chado.organism', 'CO')
  58. ->fields('CO')
  59. ->condition('organism_id', $organism_id)
  60. ->execute()
  61. ->fetchObject();
  62. $instance_id = tripal_jbrowse_mgmt_create_instance(
  63. [
  64. 'organism_id' => $organism_id,
  65. 'title' => tripal_jbrowse_mgmt_construct_organism_name($organism),
  66. 'description' => $description,
  67. 'created_at' => time(),
  68. 'file' => '',
  69. ]
  70. );
  71. if ($instance_id) {
  72. $instance = tripal_jbrowse_mgmt_get_instance($instance_id);
  73. $settings = tripal_jbrowse_mgmt_get_settings();
  74. $slug = tripal_jbrowse_mgmt_make_slug($instance->title);
  75. if (isset($settings['data_path']) and !empty($settings['data_path'])) {
  76. $data_path = 'WEB_ROOT/' . $settings['data_path'] . '/' . $slug . '/data';
  77. }
  78. else {
  79. $data_path = 'JBROWSE_ROOT/data/' . $slug . '/data';
  80. }
  81. drupal_set_message('Instance registered successfully!');
  82. drupal_set_message(t('This assumes you have an existing JBrowse set-up at @path. If the link below does not work, perhaps you need to create, rather then register, a new JBrowse Instance.',
  83. ['@path' => $data_path]), 'warning');
  84. $form_state['redirect'] = "admin/tripal/extension/tripal_jbrowse/management/instances/$instance_id";
  85. }
  86. else {
  87. drupal_set_message('Failed to create instance!', 'error');
  88. return;
  89. }
  90. // Now save the instance properties.
  91. tripal_jbrowse_mgmt_save_instance_properties(
  92. $instance_id,
  93. $form_state['values']['page']
  94. );
  95. }