Эх сурвалжийг харах

Save and retrieve instance properties.

Lacey Sanderson 5 жил өмнө
parent
commit
5cb2d869b3

+ 76 - 0
tripal_jbrowse_mgmt/includes/tripal_jbrowse_mgmt.api.inc

@@ -529,3 +529,79 @@ function tripal_jbrowse_mgmt_get_track_types() {
     'XYPlot',
     'XYPlot',
   ];
   ];
 }
 }
+
+/**
+ * Save properties for a given instance.
+ *
+ * @param $id
+ *   The instance ID that properties should be associated with.
+ * @param $data
+ *   An array of properties where each is
+ *   key: property_type, value: property value.
+ */
+function tripal_jbrowse_mgmt_save_instance_properties($id, $data) {
+
+  // For each property...
+  foreach ($data as $type => $value) {
+    tripal_jbrowse_mgmt_save_instance_property($id, $type, $value);
+  }
+
+}
+
+/**
+ * Save the details for a specific property of a given instance.
+ *
+ * @param $id
+ *   The instance ID that properties should be associated with.
+ * @param $type
+ *   The name of the property type to be saved.
+ * @param $value
+ *   The value of the property to be saved.
+ */
+function tripal_jbrowse_mgmt_save_instance_property($id, $type, $value) {
+
+  // Check to see if the property already exists.
+  $result = db_select('tripal_jbrowse_mgmt_instanceprop', 'p')
+    ->fields('p', ['value'])
+    ->condition('instance_id', $id)
+    ->condition('property_type', $type)
+    ->execute()->fetchObject();
+
+  // If the property doesn't already exist then insert it.
+  if (empty($result)) {
+    return db_insert('tripal_jbrowse_mgmt_instanceprop')
+      ->fields([
+        'instance_id' => $id,
+        'property_type' => $type,
+        'value' => $value,
+      ])->execute();
+  }
+  // Otherwise, we need to update it.
+  else {
+    return db_update('tripal_jbrowse_mgmt_instanceprop')
+      ->fields([
+        'value' => $value,
+      ])
+      ->condition('instance_id', $id)
+      ->condition('property_type', $type)
+      ->execute();
+  }
+}
+
+/**
+ * Retrieves the value for a given instance property.
+ *
+ * @param $id
+ *   The instance ID.
+ * @param $type
+ *   The name of the property type.
+ * @return
+ *   The value of the property for a given instance.
+ */
+function tripal_jbrowse_mgmt_get_instance_property($id, $type) {
+  return db_select('tripal_jbrowse_mgmt_instanceprop', 'p')
+    ->fields('p', ['value'])
+    ->condition('instance_id', $id)
+    ->condition('property_type', $type)
+    ->execute()->fetchField();
+}

+ 10 - 3
tripal_jbrowse_mgmt/includes/tripal_jbrowse_mgmt_add.form.inc

@@ -104,7 +104,7 @@ function tripal_jbrowse_mgmt_add_form($form, &$form_state) {
     '#description' => 'The following settings pertain to link directing users to this instance (either embedded or the original).',
     '#description' => 'The following settings pertain to link directing users to this instance (either embedded or the original).',
   ];
   ];
 
 
-  $form['page']['start_loc'] = [
+  $form['page']['start-loc'] = [
     '#type' => 'textfield',
     '#type' => 'textfield',
     '#title' => 'Start Location',
     '#title' => 'Start Location',
     '#description' => "<p>The initial genomic position which will be visible in
     '#description' => "<p>The initial genomic position which will be visible in
@@ -137,9 +137,10 @@ function tripal_jbrowse_mgmt_add_form($form, &$form_state) {
       position 0, starting position of the gene, to a certain end point.</p>\r\n
       position 0, starting position of the gene, to a certain end point.</p>\r\n
       <pre>     ctgA</pre>\r\n<p>Displays an arbitrary region from the ctgA
       <pre>     ctgA</pre>\r\n<p>Displays an arbitrary region from the ctgA
       reference.</p>",
       reference.</p>",
+    '#default_value' => ($edit_form) ? tripal_jbrowse_mgmt_get_instance_property($instance_id, 'start-loc') : NULL,
   ];
   ];
 
 
-  $form['page']['start_tracks'] = [
+  $form['page']['start-tracks'] = [
     '#type' => 'textarea',
     '#type' => 'textarea',
     '#rows' => 2,
     '#rows' => 2,
     '#title' => 'Tracks to Display',
     '#title' => 'Tracks to Display',
@@ -147,6 +148,7 @@ function tripal_jbrowse_mgmt_add_form($form, &$form_state) {
       each of which should correspond to the \"label\" element of the track
       each of which should correspond to the \"label\" element of the track
       information dictionaries that are currently viewed in the viewing field.</p>\r\n
       information dictionaries that are currently viewed in the viewing field.</p>\r\n
       <pre>     DNA,knownGene,ccdsGene,snp131,pgWatson,simpleRepeat</pre>",
       <pre>     DNA,knownGene,ccdsGene,snp131,pgWatson,simpleRepeat</pre>",
+    '#default_value' => ($edit_form) ? tripal_jbrowse_mgmt_get_instance_property($instance_id, 'start-tracks') : NULL,
   ];
   ];
 
 
   $button = 'Create New Instance';
   $button = 'Create New Instance';
@@ -308,8 +310,13 @@ function tripal_jbrowse_mgmt_add_form_submit($form, &$form_state) {
         $user->uid
         $user->uid
       );
       );
       $form_state['redirect'] = "admin/tripal/extension/tripal_jbrowse/management/instances/$instance_id";
       $form_state['redirect'] = "admin/tripal/extension/tripal_jbrowse/management/instances/$instance_id";
-      return $form;
     }
     }
     drupal_set_message('Failed to create instance!', 'error');
     drupal_set_message('Failed to create instance!', 'error');
   }
   }
+
+  // Now save the instance properties.
+  tripal_jbrowse_mgmt_save_instance_properties(
+    $instance_id,
+    $form_state['values']['page']
+  );
 }
 }