You are here

function uc_dropdown_attributes_product_submit in Dropdown Attributes 6

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

Write form values out to the database table.

File

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

Code

function uc_dropdown_attributes_product_submit($form, &$form_state) {
  if ($form_state['clicked_button']['#id'] == 'edit-submit') {
    $form_state['rebuild'] = FALSE;
    $nid = $form['#parameters'][2]->nid;
    $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_attributes} WHERE nid=%d';
    $result = db_query($query, $nid);
    drupal_set_message(t('Processing node @nid', array(
      '@nid' => $nid,
    )));
    foreach ($attributes as $aid => $attribute) {
      if (property_exists($attribute, 'parent_values') && count($attribute->parent_values) > 0) {
        $success = uc_dropdown_attributes_product_create_dependency($nid, $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;
}