tripal_jbrowse_mgmt_add.form.inc 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. function tripal_jbrowse_mgmt_add_form($form, &$form_state) {
  3. $settings = tripal_jbrowse_mgmt_get_settings();
  4. if (empty(array_values($settings))) {
  5. $form['incomplete'] = [
  6. '#type' => 'item',
  7. '#prefix' => '<div style="color: red;">',
  8. '#markup' => t(
  9. 'You have not configured the module yet. Please visit the
  10. settings page and submit the form before continuing.'
  11. ),
  12. '#suffix' => '</div>',
  13. ];
  14. return;
  15. }
  16. $organisms = tripal_jbrowse_mgmt_get_organisms_list();
  17. $mapped_organisms = [];
  18. foreach ($organisms as $organism) {
  19. $mapped_organisms[$organism->organism_id] = tripal_jbrowse_mgmt_construct_organism_name(
  20. $organism
  21. );
  22. }
  23. $form['description_of_form'] = [
  24. '#type' => 'item',
  25. '#markup' => t(
  26. 'Create a new JBrowse instance for a given organism. Submitting this form
  27. creates all the necessary files for a new JBrowse instance.'
  28. ),
  29. ];
  30. $form['organism'] = [
  31. '#title' => t('Organism'),
  32. '#description' => t('Select the organism'),
  33. '#type' => 'select',
  34. '#options' => $mapped_organisms,
  35. '#required' => TRUE,
  36. ];
  37. $form['description'] = [
  38. '#title' => t('Description'),
  39. '#description' => t('Optional description for the instance.'),
  40. '#type' => 'textarea',
  41. ];
  42. $form['data'] = [
  43. '#type' => 'fieldset',
  44. '#title' => t('Reference Sequence File'),
  45. '#collabsible' => FALSE,
  46. ];
  47. $form['data']['data_desc'] = [
  48. '#type' => 'item',
  49. '#markup' => t(
  50. 'You may either upload a file below or provide
  51. the path to the reference sequence fasta file.'
  52. ),
  53. ];
  54. $form['data']['ref_seq_file'] = [
  55. '#type' => 'file',
  56. '#title' => t('Reference Sequence FASTA File'),
  57. ];
  58. $form['data']['ref_seq_path'] = [
  59. '#type' => 'textfield',
  60. '#title' => t('OR Path to File on Server'),
  61. '#description' => t(
  62. 'This path will be ignored if a file is provided above. Ex: sites/default/files/file.fasta or /data/file.fasta'
  63. ),
  64. ];
  65. $form['submit'] = [
  66. '#type' => 'submit',
  67. '#value' => 'Create New Instance',
  68. ];
  69. return $form;
  70. }
  71. /**
  72. * Validate the form.
  73. *
  74. * @param $form
  75. * @param $form_state
  76. */
  77. function tripal_jbrowse_mgmt_add_form_validate($form, &$form_state) {
  78. $values = $form_state['values'];
  79. $organism = isset($values['organism']) ? $values['organism'] : NULL;
  80. $file = $_FILES['files']['tmp_name']['ref_seq_file'];
  81. $local_file = isset($values['ref_seq_path']) ? $values['ref_seq_path'] : NULL;
  82. if (empty($file) && empty($local_file)) {
  83. form_set_error(
  84. 'ref_seq_file',
  85. 'Please provide a local file path or upload a new file.'
  86. );
  87. }
  88. elseif (empty($file) && !empty($local_file)) {
  89. if (!file_exists($local_file)) {
  90. form_set_error('ref_seq_path', 'The file path provided does not exist.');
  91. }
  92. }
  93. else {
  94. $uploaded = tripal_jbrowse_mgmt_upload_file('ref_seq_file');
  95. if (!$uploaded) {
  96. form_set_error('ref_seq_file', 'Unable to upload file');
  97. }
  98. else {
  99. $uploaded = tripal_jbrowse_mgmt_move_file($uploaded);
  100. $form_state['values']['uploaded_file'] = $uploaded;
  101. }
  102. }
  103. $instances = tripal_jbrowse_mgmt_get_instances(['organism_id' => $organism]);
  104. if (!empty($instances)) {
  105. form_set_error(
  106. 'organism',
  107. 'A JBrowse instance for the selected organism already exists. You can edit the instance from the instances page.'
  108. );
  109. }
  110. $organism = db_select('chado.organism', 'CO')
  111. ->fields('CO')
  112. ->condition('organism_id', $organism)
  113. ->execute()
  114. ->fetchObject();
  115. if (empty($organism)) {
  116. form_set_error('organism', 'Invalid organism selected ' . $organism);
  117. }
  118. }
  119. /**
  120. * @param $form
  121. * @param $form_state
  122. *
  123. * @throws \Exception
  124. */
  125. function tripal_jbrowse_mgmt_add_form_submit($form, &$form_state) {
  126. global $user;
  127. $values = $form_state['values'];
  128. $organism_id = $values['organism'];
  129. $description = isset($values['description']) ? $values['description'] : '';
  130. if (empty($values['uploaded_file'])) {
  131. $file = $values['ref_seq_path'];
  132. }
  133. else {
  134. $file = $values['uploaded_file'];
  135. }
  136. $organism = db_select('chado.organism', 'CO')
  137. ->fields('CO')
  138. ->condition('organism_id', $organism_id)
  139. ->execute()
  140. ->fetchObject();
  141. $instance_id = tripal_jbrowse_mgmt_create_instance(
  142. [
  143. 'organism_id' => $organism_id,
  144. 'title' => tripal_jbrowse_mgmt_construct_organism_name($organism),
  145. 'description' => $description,
  146. 'created_at' => time(),
  147. 'file' => $file,
  148. ]
  149. );
  150. if ($instance_id) {
  151. drupal_set_message('Instance created successfully!');
  152. $name = 'Create JBrowse instance for ';
  153. $name .= tripal_jbrowse_mgmt_construct_organism_name($organism);
  154. tripal_add_job(
  155. $name,
  156. 'tripal_jbrowse_mgmt',
  157. 'tripal_jbrowse_mgmt_create_instance_files',
  158. [$instance_id],
  159. $user->uid
  160. );
  161. drupal_goto("admin/tripal_jbrowse_mgmt/instances/$instance_id");
  162. return $form;
  163. }
  164. drupal_set_message('Failed to create instance!');
  165. }