You are here

function conditional_fields_forms_submit in Conditional Fields 5

Same name and namespace in other branches
  1. 6.2 conditional_fields.module \conditional_fields_forms_submit()
  2. 6 conditional_fields.module \conditional_fields_forms_submit()

Handle saving of conditional field settings. The controlled field can be either a field or a group

File

./conditional_fields.module, line 430

Code

function conditional_fields_forms_submit($form_id, &$form, $type, $controlled_field) {
  foreach ($form['conditional_fields'] as $controlling_field => $trigger_values) {

    // If the row already exists
    if (db_result(db_query("SELECT COUNT(*) FROM {conditional_fields} WHERE control_field_name = '%s' AND field_name = '%s' AND type = '%s'", $controlling_field, $controlled_field, $type))) {

      // If no value is set, delete the entry, else update it
      if (empty($trigger_values) || $trigger_values['conditional_field_no_value']) {
        db_query("DELETE FROM {conditional_fields} WHERE control_field_name = '%s' AND field_name = '%s' AND type = '%s'", $controlling_field, $controlled_field, $type);
      }
      else {
        db_query("UPDATE {conditional_fields} SET trigger_values = '%s' WHERE control_field_name = '%s' AND field_name = '%s' AND type = '%s'", serialize($trigger_values), $controlling_field, $controlled_field, $type);
      }
    }
    else {

      // If values are set, create new entry
      if (!empty($trigger_values) && !$trigger_values['conditional_field_no_value']) {
        db_query("INSERT INTO {conditional_fields} (control_field_name, field_name, type, trigger_values) VALUES ('%s', '%s', '%s', '%s')", $controlling_field, $controlled_field, $type, serialize($trigger_values));
      }
    }
  }
}