Browse Source

Convert add instance form to provide edit capabilities.

Lacey Sanderson 5 years ago
parent
commit
86ed58b66b

File diff suppressed because it is too large
+ 317 - 211
composer.lock


+ 132 - 60
tripal_jbrowse_mgmt/includes/tripal_jbrowse_mgmt_add.form.inc

@@ -19,6 +19,21 @@ function tripal_jbrowse_mgmt_add_form($form, &$form_state) {
     return $form;
   }
 
+  // Check to see if this is an edit form.
+  $edit_form = FALSE;
+  if (isset($form_state['build_info']['args'][0]) AND is_numeric($form_state['build_info']['args'][0])) {
+    $instance_id = $form_state['build_info']['args'][0];
+    $result = tripal_jbrowse_mgmt_get_instances(['id' => $instance_id]);
+    if (!empty($result)) {
+      $instance = $result[0];
+      $edit_form = TRUE;
+    }
+    else {
+      drupal_set_message('Unable to access the instance you would like to edit.', 'error');
+      return $form;
+    }
+  }
+
   $organisms = tripal_jbrowse_mgmt_get_organisms_list();
   $mapped_organisms = [];
   foreach ($organisms as $organism) {
@@ -27,12 +42,14 @@ function tripal_jbrowse_mgmt_add_form($form, &$form_state) {
     );
   }
 
+  $form_description = 'Create a new JBrowse instance for a given organism. Submitting this form
+  creates all the necessary files for a new JBrowse instance.';
+  if ($edit_form) {
+    $form_description = 'Edit details regarding the current JBrowse instance.';
+  }
   $form['description_of_form'] = [
     '#type' => 'item',
-    '#markup' => t(
-      'Create a new JBrowse instance for a given organism. Submitting this form
-      creates all the necessary files for a new JBrowse instance.'
-    ),
+    '#markup' => t($form_description),
   ];
 
   $form['organism'] = [
@@ -41,12 +58,14 @@ function tripal_jbrowse_mgmt_add_form($form, &$form_state) {
     '#type' => 'select',
     '#options' => $mapped_organisms,
     '#required' => TRUE,
+    '#default_value' => ($edit_form) ? $instance->organism_id : NULL,
   ];
 
   $form['description'] = [
     '#title' => t('Description'),
     '#description' => t('Optional description for the instance.'),
     '#type' => 'textarea',
+    '#default_value' => ($edit_form) ? $instance->description : NULL,
   ];
 
   $form['data'] = [
@@ -66,6 +85,7 @@ function tripal_jbrowse_mgmt_add_form($form, &$form_state) {
   $form['data']['ref_seq_file'] = [
     '#type' => 'file',
     '#title' => t('Reference Sequence FASTA File'),
+    '#disabled' => ($edit_form) ? TRUE : FALSE,
   ];
 
   $form['data']['ref_seq_path'] = [
@@ -74,11 +94,16 @@ function tripal_jbrowse_mgmt_add_form($form, &$form_state) {
     '#description' => t(
       'This path will be ignored if a file is provided above. Ex: sites/default/files/file.fasta or /data/file.fasta'
     ),
+    '#disabled' => ($edit_form) ? TRUE : FALSE,
   ];
 
+  $button = 'Create New Instance';
+  if ($edit_form) {
+    $button = 'Save Changes';
+  }
   $form['submit'] = [
     '#type' => 'submit',
-    '#value' => 'Create New Instance',
+    '#value' => $button,
   ];
 
   return $form;
@@ -93,37 +118,49 @@ function tripal_jbrowse_mgmt_add_form($form, &$form_state) {
 function tripal_jbrowse_mgmt_add_form_validate($form, &$form_state) {
   $values = $form_state['values'];
   $organism = isset($values['organism']) ? $values['organism'] : NULL;
-  $file = $_FILES['files']['tmp_name']['ref_seq_file'];
-  $local_file = isset($values['ref_seq_path']) ? $values['ref_seq_path'] : NULL;
 
-  if (empty($file) && empty($local_file)) {
-    form_set_error(
-      'ref_seq_file',
-      'Please provide a local file path or upload a new file.'
-    );
+  // Check if this is an add or edit form.
+  $edit_form = FALSE;
+  if (isset($form_state['build_info']['args'][0]) AND is_numeric($form_state['build_info']['args'][0])) {
+    $instance_id = $form_state['build_info']['args'][0];
+    $edit_form = TRUE;
   }
-  elseif (empty($file) && !empty($local_file)) {
-    if (!file_exists($local_file)) {
-      form_set_error('ref_seq_path', 'The file path provided does not exist.');
+
+  if (!$edit_form) {
+    $file = $_FILES['files']['tmp_name']['ref_seq_file'];
+    $local_file = isset($values['ref_seq_path']) ? $values['ref_seq_path'] : NULL;
+
+    if (empty($file) && empty($local_file)) {
+      form_set_error(
+        'ref_seq_file',
+        'Please provide a local file path or upload a new file.'
+      );
     }
-  }
-  else {
-    $uploaded = tripal_jbrowse_mgmt_upload_file('ref_seq_file');
-    if (!$uploaded) {
-      form_set_error('ref_seq_file', 'Unable to upload file');
+    elseif (empty($file) && !empty($local_file)) {
+      if (!file_exists($local_file)) {
+        form_set_error('ref_seq_path', 'The file path provided does not exist.');
+      }
     }
     else {
-      $uploaded = tripal_jbrowse_mgmt_move_file($uploaded);
-      $form_state['values']['uploaded_file'] = $uploaded;
+      $uploaded = tripal_jbrowse_mgmt_upload_file('ref_seq_file');
+      if (!$uploaded) {
+        form_set_error('ref_seq_file', 'Unable to upload file');
+      }
+      else {
+        $uploaded = tripal_jbrowse_mgmt_move_file($uploaded);
+        $form_state['values']['uploaded_file'] = $uploaded;
+      }
     }
   }
 
   $instances = tripal_jbrowse_mgmt_get_instances(['organism_id' => $organism]);
   if (!empty($instances)) {
-    form_set_error(
-      'organism',
-      'A JBrowse instance for the selected organism already exists. You can edit the instance from the instances page.'
-    );
+    if (!$edit_form OR ($edit_form AND $instances[0]->id != $instance_id)) {
+      form_set_error(
+        'organism',
+        'A JBrowse instance for the selected organism already exists. You can edit the instance from the instances page.'
+      );
+    }
   }
 
   $organism = db_select('chado.organism', 'CO')
@@ -150,44 +187,79 @@ function tripal_jbrowse_mgmt_add_form_submit($form, &$form_state) {
   $organism_id = $values['organism'];
   $description = isset($values['description']) ? $values['description'] : '';
 
-  if (empty($values['uploaded_file'])) {
-    $file = $values['ref_seq_path'];
+  // Check if this is an add or edit form.
+  $edit_form = FALSE;
+  if (isset($form_state['build_info']['args'][0]) AND is_numeric($form_state['build_info']['args'][0])) {
+    $instance_id = $form_state['build_info']['args'][0];
+    $edit_form = TRUE;
   }
-  else {
-    $file = $values['uploaded_file'];
+
+  if ($edit_form) {
+
+    $organism = db_select('chado.organism', 'CO')
+      ->fields('CO')
+      ->condition('organism_id', $organism_id)
+      ->execute()
+      ->fetchObject();
+
+    $title = tripal_jbrowse_mgmt_construct_organism_name($organism);
+
+    $success = db_update('tripal_jbrowse_mgmt_instances')
+      ->fields([
+        'organism_id' => $organism_id,
+        'title' => $title,
+        'description' => $description,
+      ])
+      ->condition('id', $instance_id)
+      ->execute();
+
+    if ($success) {
+      drupal_set_message("Successfully updated $title JBrowse instance.");
+      $form_state['redirect'] = 'admin/tripal/extension/tripal_jbrowse/management/instances';
+    }
+    else {
+      drupal_set_message('Failed to update the current instance!', 'error');
+    }
   }
+  else {
+    if (empty($values['uploaded_file'])) {
+      $file = $values['ref_seq_path'];
+    }
+    else {
+      $file = $values['uploaded_file'];
+    }
 
-  $organism = db_select('chado.organism', 'CO')
-    ->fields('CO')
-    ->condition('organism_id', $organism_id)
-    ->execute()
-    ->fetchObject();
+    $organism = db_select('chado.organism', 'CO')
+      ->fields('CO')
+      ->condition('organism_id', $organism_id)
+      ->execute()
+      ->fetchObject();
 
-  $instance_id = tripal_jbrowse_mgmt_create_instance(
-    [
-      'organism_id' => $organism_id,
-      'title' => tripal_jbrowse_mgmt_construct_organism_name($organism),
-      'description' => $description,
-      'created_at' => time(),
-      'file' => $file,
-    ]
-  );
-
-  if ($instance_id) {
-    drupal_set_message('Instance created successfully!');
-    $name = 'Create JBrowse instance for ';
-    $name .= tripal_jbrowse_mgmt_construct_organism_name($organism);
-
-    tripal_add_job(
-      $name,
-      'tripal_jbrowse_mgmt',
-      'tripal_jbrowse_mgmt_create_instance_files',
-      [$instance_id],
-      $user->uid
+    $instance_id = tripal_jbrowse_mgmt_create_instance(
+      [
+        'organism_id' => $organism_id,
+        'title' => tripal_jbrowse_mgmt_construct_organism_name($organism),
+        'description' => $description,
+        'created_at' => time(),
+        'file' => $file,
+      ]
     );
-    drupal_goto("admin/tripal/extension/tripal_jbrowse/management/instances/$instance_id");
-    return $form;
-  }
 
-  drupal_set_message('Failed to create instance!');
+    if ($instance_id) {
+      drupal_set_message('Instance created successfully!');
+      $name = 'Create JBrowse instance for ';
+      $name .= tripal_jbrowse_mgmt_construct_organism_name($organism);
+
+      tripal_add_job(
+        $name,
+        'tripal_jbrowse_mgmt',
+        'tripal_jbrowse_mgmt_create_instance_files',
+        [$instance_id],
+        $user->uid
+      );
+      $form_state['redirect'] = "admin/tripal/extension/tripal_jbrowse/management/instances/$instance_id";
+      return $form;
+    }
+    drupal_set_message('Failed to create instance!', 'error');
+  }
 }

+ 4 - 3
tripal_jbrowse_mgmt/includes/tripal_jbrowse_mgmt_list.page.inc

@@ -31,9 +31,9 @@ function tripal_jbrowse_mgmt_instances_page() {
     'Organism',
     'Submitter',
     'Description',
-    'Manage',
+    'Tracks',
     'Launch',
-    'Delete',
+    'Instance',
   ];
 
   $rows = [];
@@ -51,7 +51,8 @@ function tripal_jbrowse_mgmt_instances_page() {
         $settings['link'],
         ['query' => tripal_jbrowse_mgmt_build_http_query($instance)]
       ),
-      l('Delete Instance', 'admin/tripal/extension/tripal_jbrowse/management/instances/'.$instance->id.'/delete'),
+      l('Edit', 'admin/tripal/extension/tripal_jbrowse/management/instances/'.$instance->id.'/edit') . ' <br /> ' .
+      l('Delete', 'admin/tripal/extension/tripal_jbrowse/management/instances/'.$instance->id.'/delete'),
 
     ];
   }

+ 11 - 1
tripal_jbrowse_mgmt/tripal_jbrowse_mgmt.module

@@ -66,8 +66,18 @@ function tripal_jbrowse_mgmt_menu() {
     'type' => MENU_CALLBACK,
   ];
 
+  $items['admin/tripal/extension/tripal_jbrowse/management/instances/%/edit'] = [
+    'title' => 'Edit Instance',
+    'description' => 'Edit metadata for existing JBrowse instances.',
+    'page callback' => 'drupal_get_form',
+    'page arguments' => ['tripal_jbrowse_mgmt_add_form', 6],
+    'access arguments' => ['administer tripal_jbrowse_mgmt'],
+    'file' => 'includes/tripal_jbrowse_mgmt_add.form.inc',
+    'type' => MENU_LOCAL_ACTION,
+  ];
+
   $items['admin/tripal/extension/tripal_jbrowse/management/instances/%/delete'] = [
-    'title' => 'Delete an instance',
+    'title' => 'Delete Instance',
     'description' => 'Confirm deleting an instance.',
     'page callback' => 'drupal_get_form',
     'page arguments' => ['tripal_jbrowse_mgmt_delete_instance_form', 6],

Some files were not shown because too many files changed in this diff