tripal_jbrowse_mgmt_add.form.inc 5.1 KB

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