Browse Source

Save configuration values

Reynold Tan 3 years ago
parent
commit
bad74fa3e0
2 changed files with 191 additions and 55 deletions
  1. 3 2
      config/install/tripal_blast.settings.yml
  2. 188 53
      src/Form/TripalBlastConfigurationForm.php

+ 3 - 2
config/install/tripal_blast.settings.yml

@@ -4,7 +4,7 @@
 # GENERAL CONFIGURATIONS:
 tripal_blast_config_general:
   path:
-      threads: 1
+  threads: 1
   eval: 0.001
   qrange: 0
 
@@ -58,7 +58,8 @@ tripal_blast_config_jobs:
 
 # VISUALIZATION CONFIGURATIONS:
 tripal_blast_config_visualization:
-  cvitjs: FALSE
+  cvitjs_enabled: FALSE
+  cvitjs_config: 
 
 # NOTIFICATION CONFIGURATIONS
 tripal_blast_config_notification:

+ 188 - 53
src/Form/TripalBlastConfigurationForm.php

@@ -8,7 +8,6 @@ namespace Drupal\tripal_blast\Form;
 
 use Drupal\Core\Form\ConfigFormBase;
 use Drupal\Core\Form\FormStateInterface;
-use Drupal\Core\Messenger\Messenger;
 
 /**
  * Defines TripalBlastConfigurationForm class.
@@ -51,35 +50,35 @@ class TripalBlastConfigurationForm extends ConfigFormBase {
       '#open' => TRUE,
     ];
   
-      $form['general']['blast_path'] = [  
+      $form['general']['fld_text_blast_path'] = [  
         '#type' => 'textfield',
-        '#title' => t('Enter the path of the BLAST program'),
-        '#description' => t('You can ignore if your $PATH variable is set. 
+        '#title' => $this->t('Enter the path of the BLAST program'),
+        '#description' => $this->t('You can ignore if your $PATH variable is set. 
           Otherwise, enter the absoulte path to bin folder. For example, /opt/blast/2.2.29+/bin/'),
         '#default_value' => $config->get('tripal_blast_config_general.path')
       ];
 
-      $form['general']['blast_threads'] = [
+      $form['general']['fld_text_blast_threads'] = [
         '#type' => 'textfield',
-        '#title' => t('Enter the number of CPU threads to use in blast search.'),
-        '#description' => t('You can increase the number to reduce the search time. 
+        '#title' => $this->t('Enter the number of CPU threads to use in blast search.'),
+        '#description' => $this->t('You can increase the number to reduce the search time. 
           Before you increase, please check your hardware configurations. 
           A value of one(1) can result in a slower search for some programs eg. tblastn.'),
         '#default_value' => $config->get('tripal_blast_config_general.threads')
       ];
     
-      $form['general']['eVal'] = [
+      $form['general']['fld_text_blast_eval'] = [
         '#type' => 'textfield',
-        '#title' => t('Default e-value (Expected Threshold)'),
-        '#description' => t('Expected number of chance matches in a random model. 
+        '#title' => $this->t('Default e-value (Expected Threshold)'),
+        '#description' => $this->t('Expected number of chance matches in a random model. 
           This number should be give in a decimal format.'),
         '#default_value' => $config->get('tripal_blast_config_general.eval')
       ];
     
-      $form['general']['qRange'] = [
+      $form['general']['fld_text_blast_qrange'] = [
         '#type' => 'textfield',
-        '#title' => t('Default max matches in a query range'),
-        '#description' => t('Limit the number of matches to a query range. 
+        '#title' => $this->t('Default max matches in a query range'),
+        '#description' => $this->t('Limit the number of matches to a query range. 
           This option is useful if many strong matches to one part of a query may prevent 
           BLAST from presenting weaker matches to another part of the query.'),
         '#default_value' => $config->get('tripal_blast_config_general.qrange')
@@ -91,24 +90,24 @@ class TripalBlastConfigurationForm extends ConfigFormBase {
     $form['file_upload'] = [
       '#type' => 'details',
       '#open' => FALSE,
-      '#title' => t('Allow File Upload'),
-      '#description' => t('The following options allow you to control whether your users can
+      '#title' => $this->t('Allow File Upload'),
+      '#description' => $this->t('The following options allow you to control whether your users can
         upload files for the query or target respectively. The ability to upload files allows
         them to more conviently BLAST large sets of sequences. However, the size of the
         files could be problematic, storage-wise, on your server.<br />')
     ];
 
-      $form['file_upload']['query_upload'] = [
+      $form['file_upload']['fld_checkbox_blast_query_upload'] = [
         '#type' => 'checkbox',
-        '#title' => t('Enable Query Sequence Upload'),
-        '#description' => t('When checked, a query file upload field will be available on BLAST request forms.'),
+        '#title' => $this->t('Enable Query Sequence Upload'),
+        '#description' => $this->t('When checked, a query file upload field will be available on BLAST request forms.'),
         '#default_value' => $config->get('tripal_blast_config_upload.allow_query')
       ];
     
-      $form['file_upload']['target_upload'] = [
+      $form['file_upload']['fld_checkbox_blast_target_upload'] = [
         '#type' => 'checkbox',
-        '#title' => 'Enable Target Sequence Upload',
-        '#description' => 'When checked, a target file upload field will be available on BLAST request forms.',
+        '#title' => $this->t('Enable Target Sequence Upload'),
+        '#description' => $this->t('When checked, a target file upload field will be available on BLAST request forms.'),
         '#default_value' => $config->get('tripal_blast_config_upload.allow_target')
       ];
 
@@ -126,17 +125,17 @@ class TripalBlastConfigurationForm extends ConfigFormBase {
         ['@fasta-format-url' => 'https://www.ncbi.nlm.nih.gov/BLAST/blastcgihelp.shtml'])
       ];
 
-      $form['example_sequence']['nucleotide_example'] = [
+      $form['example_sequence']['fld_text_blast_nucleotide_example'] = [
         '#type' => 'textarea',
-        '#title' => t('Nucleotide Example'),
-        '#description' => t('Enter a complete nucleotide FASTA record including the header.'),
+        '#title' => $this->t('Nucleotide Example'),
+        '#description' => $this->t('Enter a complete nucleotide FASTA record including the header.'),
         '#default_value' => $config->get('tripal_blast_config_sequence.nucleotide')
       ];
 
-      $form['example_sequence']['protein_example'] = [
+      $form['example_sequence']['fld_text_blast_protein_example'] = [
         '#type' => 'textarea',
-        '#title' => 'Protein Example',
-        '#description' => t('Enter a complete protein FASTA record including the header.'),
+        '#title' => $this->t('Protein Example'),
+        '#description' => $this->t('Enter a complete protein FASTA record including the header.'),
         '#default_value' => $config->get('tripal_blast_config_sequence.protein')
       ];
 
@@ -146,16 +145,16 @@ class TripalBlastConfigurationForm extends ConfigFormBase {
     $form['protection'] = [
       '#type' => 'details',
       '#open' => FALSE,
-      '#title' => t('Protect against large jobs'),
-      '#description' => t('Depending on the size and nature of your target databases, 
+      '#title' => $this->t('Protect against large jobs'),
+      '#description' => $this->t('Depending on the size and nature of your target databases, 
         you may wish to constrain use of this module.'),
     ];
 
-      $form['protection']['max_results_displayed'] = [
+      $form['protection']['fld_text_blast_max_results'] = [
         '#type' => 'textfield',
-        '#title' => t('Maximum number of results to show on report page'),
-        '#description' => 'If there are more hits that this, the user is 
-          able to download but not visualize the results.',
+        '#title' => $this->t('Maximum number of results to show on report page'),
+        '#description' => $this->t('If there are more hits that this, the user is 
+          able to download but not visualize the results.'),
         '#default_value' => $config->get('tripal_blast_config_jobs.max_result')
       ];
 
@@ -169,8 +168,8 @@ class TripalBlastConfigurationForm extends ConfigFormBase {
     $form['visualization'] = [
       '#type' => 'details',
       '#open' => $config_cvitjs,
-      '#title' => t('Enable and configure genome visualization'),
-      '#description' => t('The JavaScript program CViTjs enables users to see BLAST hits on an
+      '#title' => $this->t('Enable and configure genome visualization'),
+      '#description' => $this->t('The JavaScript program CViTjs enables users to see BLAST hits on an
         entire genome assembly. See the help tab for information on how to download and set up CViTjs')
     ];
       
@@ -196,47 +195,183 @@ class TripalBlastConfigurationForm extends ConfigFormBase {
           </div>'
       ];
 
-      $form['visualization']['cvitjs_enabled'] = [
+      $form['visualization']['fld_checkbox_blast_cvitjs_enabled'] = [
         '#type' => 'checkbox',
-        '#title' => t('Enable CViTjs'),
-        '#description' => t('When checked, CViTjs will be enabled.'),
+        '#title' => $this->t('Enable CViTjs'),
+        '#description' => $this->t('When checked, CViTjs will be enabled.'),
         '#default_value' => $config_cvitjs,
       ];
     
       // Get CViTjs confuration text, if possible.
       // @TODO blast_ui_get_cvit_conf_text() goes in '' the condition below.
-      if (!$default_value = '') {
-        $default_value = 'Unable to get CViTjs configuration information. You will need to enable CViTjs and set and save the path to CViTjs before you can edit the CViTjs configuration text.';
+      $default_value = ''; // blast_ui_get_cvit_conf_text()
+      if (!$default_value) {
+        $default_value = 'Unable to get CViTjs configuration information. 
+You will need to enable CViTjs and set and save the path to CViTjs before 
+you can edit the CViTjs configuration text.';
 
-        $disabled = true;
+        $disabled = TRUE;
       }
       else {
-        $disabled = false;
+        $disabled = FALSE;
       }
 
-      $description = t('This is the contents of the file that defines data directories and 
-        backbone GFF files for each genome assembly target. It is named
-        cvit.conf and is in the root directory for the CViTjs javascript code.
-        This is NOT the config file that is used to build the display for each
-        individual genome. See the help tab for more information about
-        configuration files.');
-
-      $form['visualization']['cvitjs_config'] = [
+      $form['visualization']['fld_text_blast_cvitjs_config'] = [
         '#type' => 'textarea',
-        '#title' => t('CViTjs configuration'),
-        '#description' => $description,
-        '#default_value' => $default_value,
+        '#title' => $this->t('CViTjs configuration'),
+        '#description' => $this->t('This is the contents of the file that defines data directories and 
+          backbone GFF files for each genome assembly target. It is named
+          cvit.conf and is in the root directory for the CViTjs javascript code.
+          This is NOT the config file that is used to build the display for each
+          individual genome. See the help tab for more information about configuration files.'),
         '#rows' => 10,
         '#disabled' => $disabled,
+        '#default_value' => $default_value
       ];
 
+    
+    //
+    // # NOTIFICATION CONFIGURATIONS:
+    $form['notification'] = array(
+      '#type' => 'details',
+      '#open' => FALSE,
+      '#title' => $this->t('Show warning text'),
+      '#description' => $this->t('This permits display of a temporary warning message at the top of the
+        BLAST input form. Text can include HTML tags. Remember to remove the
+        message when it is no longer relevant.'),
+    );
+
+      $form['notification']['fld_text_blast_warning_text'] = array(
+        '#type' => 'textarea',
+        '#title' => $this->t('Text to be displayed'),
+        '#rows' => 10,
+        '#default_value' => $config->get('tripal_blast_config_notification.warning_text')
+      );
+
     return parent::buildForm($form, $form_state);
   }
+
+  /**
+   * {@inheritdoc}
+   * Validate configuration.
+   */
+  public function validateForm(array &$form, FormStateInterface $form_state) {
+    // Validate BLAST path.
+    $fld_name_blast_path = 'fld_text_blast_path';
+    $fld_value_blast_path = $form_state->getValue($fld_name_blast_path);
+    $blast_path = $fld_value_blast_path . 'blastn';
+
+    if(!empty($fld_value_blast_path)) {
+      if(!file_exists($blast_path) ) {  
+        $form_state->setErrorByName('fld_text_blast_path', $this->t('Please enter a valid path not including the name 
+          of the blast program (ie: /usr/bin/). You can leave this blank if you have your $PATH variable set appropriately.'));
+      }
+    }
+    
+    /* CVIT JS LOCATION/PATH referenced in this validation does not correspond to 
+       a form field above (form build).
+
+    // Check path to CViTjs executable and make sure cvit.conf is writable
+    $fld_name_cvitjs_enabled = 'fld_checkbox_blast_cvitjs_enabled';
+    $fld_value_cvitjs_enabled = getValue($fld_name_cvitjs_enabled);
+
+    if ($fld_value_cvitjs_enabled) {
+      $cvit_path = ''; //@TODO: blast_ui_get_cvit_conf();
+
+      if (!$cvit_path || !file_exists($cvit_path)) {
+        $form_state->setErrorByName('fld_text_cvitjs_path', $this->t('The CViTjs configuration file, cvit.conf, 
+          does not exist at the path given (@cvitjs_path). Please check your path. If you have not yet downloaded 
+          CViTjs, see the help tab for more information.', 
+          ['@cvitjs_path' => $fld_value_cvitjs_enabled]));        
+      }
+
+      if (!is_writable($cvit_path)) {
+        $form_state->setErrorByName('fld_text_cvitjs_path', $this->t('The file $cvit_path is not writable by this page.
+          Please enable write access for apache then try saving these settings again.'));
+      }
+    }
+    */ 
+    
+    // Validate contents of cvitjs configuration text.
+    $is_config_disabled = $form['visualization']['fld_text_blast_cvitjs_config']['#disabled'];
+
+    if (!$is_config_disabled) {
+      // Do this validation only when the textfield (cvitjs config) allows for data.
+      $fld_name_cvitjs_config = 'fld_text_blast_cvitjs_config';
+      $fld_value_cvitjs_config = $form_state->getValue($fld_name_cvitjs_config);
+
+      if ($fld_value_cvitjs_config && !preg_match('/\[general\]\s*\ndata_default =.*/m', $fld_value_cvitjs_config)) {
+        $form_state->setErrorByName($fld_name_cvitjs_config, $this->t('The CViTjs configuration text looks incorrect. 
+          It should contain a [general] section. See the help tab for more information.'));
+      }
+      
+      if ($fld_value_cvitjs_config && !preg_match('/\[.*\]\s*\nconf = .*\ndefaultData =.*/m', $fld_value_cvitjs_config)) {    
+        $form_state->setErrorByName($fld_name_cvitjs_config, $this->t('The CViTjs configuration text looks incorrect. 
+          It should contain one section for each genome target. See the help tab for more information.'));
+      }
+    }
+  }
+
   /**
    * {@inheritdoc}
    * Save configuration.
    */
   public function submitForm(array &$form, FormStateInterface $form_state) {
+    // Field Values: 
+    // GENERAL CONFIGURATIONS:
+    $fld_value_blast_path = $form_state->getValue('fld_text_blast_path');
+    $fld_value_blast_threads = $form_state->getValue('fld_text_blast_threads');
+    $fld_value_blast_eval = $form_state->getValue('fld_text_blast_eval');
+    $fld_value_blast_qrange = $form_state->getValue('fld_text_blast_qrange');
+
+    // UPLOAD CONFIGURATIONS:
+    $fld_value_blast_query_upload = $form_state->getValue('fld_checkbox_blast_query_upload');
+    $fld_value_blast_target_upload = $form_state->getValue('fld_checkbox_blast_target_upload');
+
+    // SEQUENCE CONFIGURATIONS:
+    $fld_value_blast_nucleotide_example = $form_state->getValue('fld_text_blast_nucleotide_example');
+    $fld_value_blast_protein_example = $form_state->getValue('fld_text_blast_protein_example');
+
+    // JOB CONFIGURATIONS:
+    $fld_value_blast_max_results = $form_state->getValue('fld_text_blast_max_results');
+
+    // VISUALIZATION CONFIGURATIONS:
+    $fld_value_blast_cvitjs_enabled = $form_state->getValue('fld_checkbox_blast_cvitjs_enabled');
+    $fld_value_blast_cvitjs_config = $form_state->getValue('fld_text_blast_cvitjs_config');
+
+    // NOTIFICATION CONFIGURATIONS:
+    $fld_value_blast_warning_text = $form_state->getValue('fld_text_blast_warning_text');
+    
+    // Set defined variables.
+    $this->configFactory->getEditable(static::SETTINGS)
+      ->set('tripal_blast_config_general.path', $fld_value_blast_path)
+      ->set('tripal_blast_config_general.threads', $fld_value_blast_threads)
+      ->set('tripal_blast_config_general.eval', $fld_value_blast_eval)
+      ->set('tripal_blast_config_general.qrange', $fld_value_blast_qrange)    
+      ->set('tripal_blast_config_upload.allow_query', $fld_value_blast_query_upload)
+      ->set('tripal_blast_config_upload.allow_target', $fld_value_blast_target_upload)
+      ->set('tripal_blast_config_sequence.nucleotide', $fld_value_blast_nucleotide_example)
+      ->set('tripal_blast_config_sequence.protein', $fld_value_blast_protein_example)
+      ->set('tripal_blast_config_jobs.max_result', $fld_value_blast_max_results)
+      ->set('tripal_blast_config_visualization.cvitjs_enabled', $fld_value_blast_cvitjs_enabled)
+      ->set('tripal_blast_config_visualization.cvitjs_config', $fld_value_blast_cvitjs_config)
+      ->set('tripal_blast_config_notification.warning_text', $fld_value_blast_warning_text)      
+      ->save();
+
+    // Save configuration to file.
+    if ($fld_value_blast_cvitjs_enabled && $fld_value_blast_cvitjs_config) {
+      $cvit_conf_path = getcwd() . DIRECTORY_SEPARATOR;
+       //@TODO . blast_ui_get_cvit_conf($form_state['values']['cvitjs_location']);
+      
+      if ($fh = fopen($cvit_conf_path, 'w')) {
+        fwrite($fh, $fld_value_blast_cvitjs_config);
+        fclose($fh);
+      }
+      else {
+        $form_state->setError('fld_text_blast_cvitjs_config', 
+          'Unable to open CViTjs conf file for writing: <pre>' . print_r(error_get_last(), true) . '</pre>');
+      }
+    }
 
     return parent::submitForm($form, $form_state);
   }