|
@@ -26,15 +26,38 @@ class schema__publication_widget extends ChadoFieldWidget {
|
|
|
|
|
|
// Get the field defaults.
|
|
|
$fkey_value = (array_key_exists('#entity', $element) and is_object($element['#entity'])) ? $element['#entity']->chado_record_id : NULL;
|
|
|
+ $pkey_value = '';
|
|
|
$pub_id = '';
|
|
|
$uname = '';
|
|
|
|
|
|
// If the field already has a value then it will come through the $items
|
|
|
// array. This happens when editing an existing record.
|
|
|
if (count($items) > 0 and array_key_exists($delta, $items)) {
|
|
|
+ $pkey_value = tripal_get_field_item_keyval($items, $delta, 'chado-' . $table_name . '__' . $pkey, $pkey_value);
|
|
|
$pub_id = tripal_get_field_item_keyval($items, $delta, 'chado-' . $table_name . '__pub_id', $pub_id);
|
|
|
$uname = tripal_get_field_item_keyval($items, $delta, 'uniquename', $uname);
|
|
|
}
|
|
|
+ // We also need to take into account the case where a field is changed by a user.
|
|
|
+ // In this case the info is in the form state.
|
|
|
+ if (isset($form_state['values']) AND !empty($form_state['values'])) {
|
|
|
+ // If the user entered a new publication then we only have the uniquename...
|
|
|
+ // so we need to query the db for the rest of the information.
|
|
|
+ if (empty($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id'])) {
|
|
|
+ $uname = $form_state['values'][$field_name][$langcode][$delta]['uniquename'];
|
|
|
+ $pub_id = chado_query('SELECT pub_id FROM {pub} WHERE uniquename=:uniquename',
|
|
|
+ array(':uniquename' => $uname))->fetchField();
|
|
|
+ }
|
|
|
+ // If the user changed an existing publication...
|
|
|
+ // then we have to update the pub_id and reset the pkey.
|
|
|
+ // We can tell if this element was changed by comparing it to the triggering element.
|
|
|
+ $current_value = $form_state['values'][$field_name][$langcode][$delta]['value'];
|
|
|
+ $triggering_value = $form_state['triggering_element']['#value'];
|
|
|
+ if ($triggering_value == $current_value) {
|
|
|
+ $uname = $form_state['values'][$field_name][$langcode][$delta]['uniquename'];
|
|
|
+ $pub_id = chado_query('SELECT pub_id FROM {pub} WHERE uniquename=:uniquename',
|
|
|
+ array(':uniquename' => $uname))->fetchField();
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
$schema = chado_get_schema('pub');
|
|
|
|
|
@@ -49,16 +72,16 @@ class schema__publication_widget extends ChadoFieldWidget {
|
|
|
);
|
|
|
|
|
|
$widget['chado-' . $table_name . '__' . $pkey] = array(
|
|
|
- '#type' => 'value',
|
|
|
- '#default_value' => '',
|
|
|
+ '#type' => 'hidden',
|
|
|
+ '#value' => $pkey_value,
|
|
|
);
|
|
|
$widget['chado-' . $table_name . '__' . $fkey] = array(
|
|
|
- '#type' => 'value',
|
|
|
- '#default_value' => $fkey_value,
|
|
|
+ '#type' => 'hidden',
|
|
|
+ '#value' => $fkey_value,
|
|
|
);
|
|
|
$widget['chado-' . $table_name . '__pub_id'] = array(
|
|
|
- '#type' => 'value',
|
|
|
- '#default_value' => $pub_id,
|
|
|
+ '#type' => 'hidden',
|
|
|
+ '#value' => $pub_id,
|
|
|
);
|
|
|
|
|
|
$widget['uniquename'] = array(
|
|
@@ -76,47 +99,33 @@ class schema__publication_widget extends ChadoFieldWidget {
|
|
|
);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
/**
|
|
|
*
|
|
|
- * @see TripalFieldWidget::submit()
|
|
|
+ * @see TripalFieldWidget::validate()
|
|
|
*/
|
|
|
- public function submit($form, &$form_state, $entity_type, $entity, $langcode, $delta) {
|
|
|
- // Get the FK column that links to the base table.
|
|
|
- $table_name = $this->instance['settings']['chado_table'];
|
|
|
+ public function validate($element, $form, &$form_state, $langcode, $delta) {
|
|
|
+ $field_name = $this->field['field_name'];
|
|
|
+ $field_type = $this->field['type'];
|
|
|
$base_table = $this->instance['settings']['base_table'];
|
|
|
- $schema = chado_get_schema($table_name);
|
|
|
+ $field_table = $this->instance['settings']['chado_table'];
|
|
|
+ $chado_column = $this->instance['settings']['chado_column'];
|
|
|
+ $instance = $this->instance;
|
|
|
+
|
|
|
+ $schema = chado_get_schema($field_table);
|
|
|
$pkey = $schema['primary key'][0];
|
|
|
$fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
|
|
|
$fkey = $fkeys[0];
|
|
|
- $field_name = $this->field['field_name'];
|
|
|
-
|
|
|
- // Get the field values.
|
|
|
- $fkey_value = isset($form_state['values'][$field_name][$langcode][$delta]['value']) ? $form_state['values'][$field_name][$langcode][$delta]['value'] : '';
|
|
|
- $pub_id = isset($form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id']) ? $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id'] : '';
|
|
|
- $uname = isset($form_state['values'][$field_name][$langcode][$delta]['uniquename']) ? $form_state['values'][$field_name][$langcode][$delta]['uniquename'] : '';
|
|
|
|
|
|
- // If the user provided a uniquename then we want to set the foreign key
|
|
|
- // value to be the chado_record_id
|
|
|
- if ($uname and !$pub_id) {
|
|
|
- $pub = chado_generate_var('pub', array('uniquename' => $uname));
|
|
|
- $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id'] = $pub->pub_id;
|
|
|
- }
|
|
|
-
|
|
|
- // In the widgetFrom function we automatically add the foreign key
|
|
|
- // record. But if the user did not provide a publication we want to take
|
|
|
- // it out so that the Chado field_storage infrastructure won't try to
|
|
|
- // write a record.
|
|
|
- if (!$uname and !$pub_id) {
|
|
|
- $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] = '';
|
|
|
- }
|
|
|
+ $value = $form_state['values'][$field_name]['und'][$delta]['uniquename'];
|
|
|
+ $form_state['values'][$field_name]['und'][$delta]['value'] = $value;
|
|
|
|
|
|
- // If the user removed the publication from the pub_uniquename field
|
|
|
- // then we want to clear out the rest of the hidden values.
|
|
|
- // Leave the primary key so the record can be deleted.
|
|
|
- if (!$uname and $pub_id) {
|
|
|
- $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__' . $fkey] = '';
|
|
|
- $form_state['values'][$field_name][$langcode][$delta]['chado-' . $table_name . '__pub_id'] = '';
|
|
|
+ // If the user removed the publication then we want to clear out the other
|
|
|
+ // fields except the pkey value. If the pkey field is present and the value
|
|
|
+ // is present then the chado storage backend will delete the record.
|
|
|
+ if (!$value) {
|
|
|
+ $form_state['values'][$field_name]['und'][$delta]['value'] = 'delete_me';
|
|
|
+ $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__' . $fkey] = '';
|
|
|
+ $form_state['values'][$field_name]['und'][$delta]['chado-' . $field_table . '__pub_id'] = '';
|
|
|
}
|
|
|
}
|
|
|
|