You are here

function uc_dropdown_attributes_class_submit in Dropdown Attributes 6

Same name and namespace in other branches
  1. 7 dependent_dropdown.inc \uc_dropdown_attributes_class_submit()

Write form values out to the database table.

File

./dependent_dropdown.inc, line 151
Administrative interface for specifying the attribute dependencies.

Code

function uc_dropdown_attributes_class_submit($form, &$form_state) {
  if ($form_state['clicked_button']['#id'] == 'edit-submit') {
    $form_state['rebuild'] = FALSE;
    $pcid = $form['#parameters'][2];
    $attributes = array();
    foreach ($form_state['values'] as $key => $value) {
      $keys = explode('-', $key);
      if (count($keys) > 1 && is_numeric($keys[1])) {
        if (!isset($attributes[$keys[1]])) {
          $attributes[$keys[1]] = new stdClass();
        }
        switch ($keys[0]) {
          case 'parent':
            $attributes[$keys[1]]->parent_aid = $value;
            break;
          case 'values':
            $attributes[$keys[1]]->parent_values = $value['values-' . $keys[1]];
            break;
          case 'required':
            $attributes[$keys[1]]->required = $value;
            break;
        }
      }
    }
    $query = 'DELETE FROM {uc_dropdown_classes} WHERE pcid="%s"';
    $result = db_query($query, $pcid);
    drupal_set_message(t('Processing class @pcid', array(
      '@pcid' => $pcid,
    )));
    foreach ($attributes as $aid => $attribute) {
      if (property_exists($attribute, 'parent_values') && count($attribute->parent_values) > 0) {
        $success = uc_dropdown_attributes_class_create_dependency($pcid, $aid, $attribute->parent_aid, $attribute->parent_values, $attribute->required);
        $attr = uc_attribute_load($aid);
        if ($success) {
          drupal_set_message(t('Saved @name', array(
            '@name' => $attr->name,
          )));
        }
        else {
          drupal_set_message(t('Database write failed for @name', array(
            '@name' => $attr->name,
          )), 'error');
        }
      }
    }
  }

  // edit-next or anything else will cause rebuild.
  $form_state['rebuild'] = TRUE;
}