tripal_jbrowse_mgmt_register.form.inc 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. $settings = tripal_jbrowse_mgmt_get_settings();
  15. $path = $settings['data_dir'] . '/genus_species/data/';
  16. $msg = '<p>Register an already existing JBrowse instance to be managed by this module.</p>'
  17. . '<div class="messages warning">It is expected that the data directory for the existing JBrowse instance is <code>@path</code> where <code>genus_species</code> matches what you select for the organism. If this is not the location of your JBrowse, you should create a symbolic link between the current instance location and the expected one mentioned above.</div>';
  18. $form['description_of_form']['#markup'] = t($msg, ['@path' => $path]);
  19. // Remove the file upload.
  20. unset($form['data']);
  21. // Change the submit button.
  22. $form['submit']['#value'] = 'Register Instance';
  23. return $form;
  24. }
  25. /**
  26. * Validate the form.
  27. *
  28. * @param $form
  29. * @param $form_state
  30. */
  31. function tripal_jbrowse_mgmt_register_form_validate($form, &$form_state) {
  32. $values = $form_state['values'];
  33. $organism = isset($values['organism']) ? $values['organism'] : NULL;
  34. // Check an instance does not already exist.
  35. $instances = tripal_jbrowse_mgmt_get_instances(['organism_id' => $organism]);
  36. if (!empty($instances)) {
  37. form_set_error(
  38. 'organism',
  39. 'A JBrowse instance for the selected organism already exists. You can edit the instance from the instances page.'
  40. );
  41. }
  42. // Check that the organism does exist.
  43. $organism = db_select('chado.organism', 'CO')
  44. ->fields('CO')
  45. ->condition('organism_id', $organism)
  46. ->execute()
  47. ->fetchObject();
  48. if (empty($organism)) {
  49. form_set_error('organism', 'Invalid organism selected ' . $organism);
  50. }
  51. // Check that the directory we assume contains the instance does exist.
  52. $title = tripal_jbrowse_mgmt_construct_organism_name($organism);
  53. $settings = tripal_jbrowse_mgmt_get_settings();
  54. $path = $settings['data_dir'];
  55. $path .= '/' . tripal_jbrowse_mgmt_make_slug($title);
  56. $path .= '/data/trackList.json';
  57. if (!file_exists($path)) {
  58. form_set_error('organism', "We expect there is already an existing instane at the following path: $path");
  59. }
  60. }
  61. /**
  62. * @param $form
  63. * @param $form_state
  64. *
  65. * @throws \Exception
  66. */
  67. function tripal_jbrowse_mgmt_register_form_submit($form, &$form_state) {
  68. global $user;
  69. $values = $form_state['values'];
  70. $organism_id = $values['organism'];
  71. $description = isset($values['description']) ? $values['description'] : '';
  72. $organism = db_select('chado.organism', 'CO')
  73. ->fields('CO')
  74. ->condition('organism_id', $organism_id)
  75. ->execute()
  76. ->fetchObject();
  77. $instance_id = tripal_jbrowse_mgmt_create_instance(
  78. [
  79. 'organism_id' => $organism_id,
  80. 'title' => tripal_jbrowse_mgmt_construct_organism_name($organism),
  81. 'description' => $description,
  82. 'created_at' => time(),
  83. 'file' => '',
  84. ]
  85. );
  86. if ($instance_id) {
  87. drupal_set_message('Instance registered successfully!');
  88. $form_state['redirect'] = "admin/tripal/extension/tripal_jbrowse/management/instances/$instance_id";
  89. }
  90. else {
  91. drupal_set_message('Failed to create instance!', 'error');
  92. return;
  93. }
  94. // Now save the instance properties.
  95. tripal_jbrowse_mgmt_save_instance_properties(
  96. $instance_id,
  97. $form_state['values']['page']
  98. );
  99. }