Browse Source

Working on prop adder fields

Stephen Ficklin 8 years ago
parent
commit
62ad0590e2

+ 27 - 27
tripal_chado/includes/TripalFields/chado_linker__prop.inc

@@ -48,14 +48,13 @@ class chado_linker__prop extends TripalField {
    */
   public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
 
-    $entity = $form['#entity'];
     $field_name = $this->field['field_name'];
+    $field_type = $this->field['type'];
+    $field_table = $this->field['settings']['chado_table'];
+    $field_column = $this->field['settings']['chado_column'];
 
-    // Get the record and table mapping info.
-    $chado_table = $entity->chado_table;
-    $chado_column = $entity->chado_column;
-    $chado_record = $entity->chado_record;
 
+    // Get the table name and cvterm that this field maps to.
     $matches = array();
     preg_match('/(.*?)__(\d+)/', $field_name, $matches);
     // If the field name is not properly formatted then we can't tell what
@@ -70,14 +69,14 @@ class chado_linker__prop extends TripalField {
     // of the FK field that links to the base table.
     $schema = chado_get_schema($table_name);
     $pkey = $schema['primary key'][0];
-    $lfkey_field = key($schema['foreign keys'][$chado_table]['columns']);
-    $rfkey_field = $schema['foreign keys'][$chado_table]['columns'][$lfkey_field];
+    $lfkey_field = key($schema['foreign keys'][$field_table]['columns']);
+    $rfkey_field = $schema['foreign keys'][$field_table]['columns'][$lfkey_field];
 
     // Get the field defaults.
     $fk_value = '';
     $propval = '';
     if (array_key_exists($delta, $items)) {
-      $propval = $items[$delta][$table_name . '__value'];
+      $propval = tripal_get_field_item_keyval($items, $delta, 'chado-' . $table_name . '__value', $propval);
     }
     if ($chado_record) {
       $fk_value = $chado_record->$rfkey_field;
@@ -100,23 +99,23 @@ class chado_linker__prop extends TripalField {
       '#value' => $items[$delta]['value'],
     );
 
-    $widget[$table_name . '__' . $pkey] = array(
+    $widget['chado-' . $table_name . '__' . $pkey] = array(
       '#type' => 'hidden',
       '#default_value' => !empty($items[$delta]['value']) ? $items[$delta]['value'] : '',
     );
-    $widget[$table_name . '__' . $lfkey_field] = array(
+    $widget['chado-' . $table_name . '__' . $lfkey_field] = array(
       '#type' => 'hidden',
       '#value' => $fk_value,
     );
-    $widget[$table_name . '__value'] = array(
+    $widget['chado-' . $table_name . '__value'] = array(
       '#type' => 'textfield',
       '#default_value' => $propval,
     );
-    $widget[$table_name . '__type_id'] = array(
+    $widget['chado-' . $table_name . '__type_id'] = array(
       '#type' => 'hidden',
       '#value' => $cvterm_id,
     );
-    $widget[$table_name . '__rank'] = array(
+    $widget['chado-' . $table_name . '__rank'] = array(
       '#type' => 'hidden',
       '#value' => $delta,
     );
@@ -180,10 +179,11 @@ class chado_linker__prop extends TripalField {
    */
   public function load($entity, $details = array()) {
 
-    $field_name = $details['field_name'];
-    $field_type = $details['type'];
-    $field_table = $details['settings']['chado_table'];
-    $field_column = $details['settings']['chado_column'];
+    $field_name = $this->field['field_name'];
+    $field_type = $this->field['type'];
+    $field_table = $this->field['settings']['chado_table'];
+    $field_column = $this->field['settings']['chado_column'];
+    $base_table = $this->field['settings']['base_table'];
 
     $matches = array();
     preg_match('/(.*?)__(\d+)/', $field_name, $matches);
@@ -199,11 +199,11 @@ class chado_linker__prop extends TripalField {
     // Set some defaults for the empty record.
     $entity->{$field_name}['und'][0] = array(
       'value' => '',
-      $field_table . '__' . $pkey => '',
-      $field_table . '__' . $fkey_lcolumn => '',
-      $field_table . '__value' => '',
-      $field_table . '__type_id' => '',
-      $field_table . '__rank' => '',
+      'chado-' . $field_table . '__' . $pkey => '',
+      'chado-' . $field_table . '__' . $fkey_lcolumn => '',
+      'chado-' . $field_table . '__value' => '',
+      'chado-' . $field_table . '__type_id' => '',
+      'chado-' . $field_table . '__rank' => '',
     );
 
     // Get the properties associated with this base record for this fields
@@ -223,11 +223,11 @@ class chado_linker__prop extends TripalField {
       foreach ($schema['fields'] as $fname => $details) {
         $entity->{$field_name}['und'][$i] = array(
           'value' => array(),
-          $field_table . '__' . $pkey => $property->$pkey,
-          $field_table . '__' . $fkey_lcolumn => $property->$fkey_lcolumn,
-          $field_table . '__value' => $property->value,
-          $field_table . '__type_id' => $property->type_id,
-          $field_table . '__rank' => $property->rank,
+          'chado-' . $field_table . '__' . $pkey => $property->$pkey,
+          'chado-' . $field_table . '__' . $fkey_lcolumn => $property->$fkey_lcolumn,
+          'chado-' . $field_table . '__value' => $property->value,
+          'chado-' . $field_table . '__type_id' => $property->type_id,
+          'chado-' . $field_table . '__rank' => $property->rank,
         );
       }
     }

+ 102 - 84
tripal_chado/includes/TripalFields/chado_linker__prop_adder.inc

@@ -80,95 +80,113 @@ class chado_linker__prop_adder extends TripalField {
   /**
    * @see TripalField::widgetFormValidate
    */
-  public function widgetFormValidate($entity_type, $entity, $field, $items, &$errors) {
+  public function widgetFormValidate($entity_type, $entity, $langcode, $items, &$errors) {
 
+    // We will never have more than one item for this field at a time, so
+    // delta is always zero.
+    $delta = 0;
+
+    // Make sure the cvterm for this property is uniquely identified.
+    $kvproperty = tripal_get_field_item_keyval($items, $delta, 'value', '');
+    $cvterms = chado_generate_var('cvterm', array('name' => $kvproperty), $options = array('return_array' => TRUE));
+
+    if (count(cvterms) > 1) {
+      $errors[$this->field['field_name']][$langcode][$delta][] = array(
+        'error' => 'chado_linker__prop_adder',
+        'message' => t("This term is present in multiple vocabularies. Please select the appropriate one."),
+      );
+    }
+    if (count(cvterms) == 0) {
+      $errors[$this->field['field_name']][$langcode][$delta][] = array(
+        'error' => 'chado_linker__prop_adder',
+        'message' => t("Please provide a property type to add."),
+      );
+    }
   }
 
+  /**
+   * @see TripalField::widgetFormSubmit()
+   */
   public function widgetFormSubmit($entity_type, $entity, $langcode, &$items, $form, &$form_state) {
+    // We will never have more than one item for this field at a time, so
+    // delta is always zero.
+    $delta = 0;
+
+    // Add the new field to the entity but only if the property adder button
+    // was clicked
+    if (!array_key_exists('triggering_element', $form_state) or
+      $form_state['triggering_element']['#name'] != 'kvproperty_adder_button') {
+      return;
+    }
+
+    // Because we're going to add a new property we want to rebuild the form
+    // rather than have it fully submit.
+    $form_state['rebuild'] = TRUE;
+
+    // Get the table and base table.
+    $base_table = $this->field['settings']['base_table'];
+
+    // Get the term for the property
+    $kvproperty = tripal_get_field_item_keyval($items, $delta, 'value', '');
+    $cvterm = chado_generate_var('cvterm', array('name' => $kvproperty));
+
+    // Generate the name for the property table and the field name that we'll
+    // be creating.
+    $prop_table = $base_table . 'prop';
+    $field_name = $prop_table . '__' . $cvterm->cvterm_id;
+
+    // The field name is the table name in this case. We want to get the
+    // primary key as this should be the field that maps th the value.
+    $schema = chado_get_schema($prop_table);
+    $pkey = $schema['primary key'][0];
+
+    // Add the field if it doesn't already exists.
+    $field = field_info_field($field_name);
+    if (!$field) {
+      $field = field_create_field(array(
+        'field_name' => $field_name,
+        'type' => 'chado_linker__prop',
+        'cardinality' => FIELD_CARDINALITY_UNLIMITED,
+        'locked' => FALSE,
+        'storage' => array(
+          'type' => 'field_chado_storage',
+        ),
+        'settings' => array(
+          'chado_table' => $prop_table,
+          'chado_column' => $pkey,
+          'base_table' => $base_table,
+          'semantic_web' => $cvterm->dbxref_id->db_id->name . ':' . $cvterm->dbxref_id->accession,
+        ),
+      ));
+    }
 
-    // Add the new field to the entity
-    if (array_key_exists('triggering_element', $form_state) and
-      $form_state['triggering_element']['#name'] == 'kvproperty_adder_button') {
-
-      $form_state['rebuild'] = TRUE;
-      $field_name = $this->field['field_name'];
-      $bundle = $entity->bundle;
-
-      // Get the base table name from the field properties.
-      $field =$this->field;
-      $base_table = $field['settings']['base_table'];
-
-      // Get the term for the property
-      foreach ($items as $delta => $values) {
-        $kvproperty = tripal_get_field_item_keyval($items, $delta, 'value', '');
-        $term = chado_generate_var('cvterm', array('name' => $kvproperty), $options = array('return_array' => TRUE));
-
-        if (count($term) == 1) {
-          $prop_field_name = $field_name . '__' . $term[0]->cvterm_id;
-
-          // The field name is the table name in this case. We want to get the
-          // primary key as this should be the field that maps th the value.
-          $schema = chado_get_schema($field_name);
-          $pkey = $schema['primary key'][0];
-  
-          // Add the field if it doesn't already exists.
-          $field = field_info_field('cvterm');      dpm('property_' . $term[0]->cvterm_id);
-          if (!$field) {
-            $create_info = array(
-              'field_name' => 'property_' . $term[0]->cvterm_id,
-              'type' => 'tripal_chado_kvproperty_widget',
-              'cardinality' => FIELD_CARDINALITY_UNLIMITED,
-              'locked' => FALSE,
-              'storage' => array(
-                'type' => 'field_chado_storage',
-              ),
-              'settings' => array(
-                'chado_table' => $field_name,
-                'chado_column' => $pkey,
-                'base_table' => $base_table,
-                'semantic_web' => '',
-              ),
-            );
-            $field = field_create_field($create_info);
-          }
-
-          // Attach the field to the bundle if it isn't already.
-          if (!$field and array_key_exists('bundles', $field) or
-              !array_key_exists('TripalEntity', $field['bundles']) or
-              !in_array($bundle_name, $field['bundles']['TripalEntity'])) {
-  
-                $createInstanceInfo = array(
-                  'field_name' => 'property_' . $term[0]->cvterm_id,
-                  'entity_type' => 'TripalEntity',
-                  'bundle' => $this->bundle->name,
-                  'label' => ucfirst(preg_replace('/_/', ' ', $term[0]->name)),
-                  'description' => $term[0]->definition ? $term[0]->definition : '',
-                  'required' => FALSE,
-                  'settings' => array(),
-                  'widget' => array(
-                    'type' => 'tripal_chado_kvproperty_widget',
-                    'settings' => array(
-                      'display_label' => 1,
-                    ),
-                  ),
-                  'display' => array(
-                    'default' => array(
-                      'label' => 'inline',
-                      'type' => 'tripal_chado_kvproperty_formatter',
-                      'settings' => array(),
-                    ),
-                  ),
-                );
-                //$instance = field_create_instance($createInstanceInfo);
-              }
-        }
-        else if (count($term) > 1) {
-          form_set_error(implode('][', $element ['#parents']) . '][value', t("This term is present in multiple vocabularies. Please select the appropriate one."));
-        }
-        else {
-          form_set_error(implode('][', $element ['#parents']) . '][value', t("Please provide a property type to add."));
-        }
-      }
+    // Create an instance of the field.
+    if (!$field and array_key_exists('bundles', $field) or
+        !array_key_exists('TripalEntity', $field['bundles']) or
+        !in_array($bundle_name, $field['bundles']['TripalEntity'])) {
+
+      $instance = field_create_instance(array(
+        'field_name' => $field_name,
+        'entity_type' => 'TripalEntity',
+        'bundle' => $entity->bundle,
+        'label' => ucfirst(preg_replace('/_/', ' ', $cvterm->name)),
+        'description' => $cvterm->definition ? $cvterm->definition : '',
+        'required' => FALSE,
+        'settings' => array(),
+        'widget' => array(
+          'type' => $field_name . '_widget',
+          'settings' => array(
+            'display_label' => 1,
+          ),
+        ),
+        'display' => array(
+          'default' => array(
+            'label' => 'inline',
+            'type' => $field_name . '_formatter',
+            'settings' => array(),
+          ),
+        ),
+      ));
     }
   }
 }