You are here

function uc_object_attributes_form_submit in Ubercart 6.2

Same name and namespace in other branches
  1. 5 uc_attribute/uc_attribute.module \uc_object_attributes_form_submit()
  2. 7.3 uc_attribute/uc_attribute.admin.inc \uc_object_attributes_form_submit()

Form submission handler for uc_object_attributes_form().

See also

uc_object_attributes_form()

File

uc_attribute/uc_attribute.admin.inc, line 847
Attribute administration menu items.

Code

function uc_object_attributes_form_submit($form, &$form_state) {
  if ($form_state['values']['type'] == 'product') {
    $attr_table = '{uc_product_attributes}';
    $opt_table = '{uc_product_options}';
    $id = 'nid';
    $sql_type = '%d';
  }
  elseif ($form_state['values']['type'] == 'class') {
    $attr_table = '{uc_class_attributes}';
    $opt_table = '{uc_class_attribute_options}';
    $id = 'pcid';
    $sql_type = "'%s'";
  }
  $changed = FALSE;
  if ($form_state['values']['view'] == 'overview' && is_array($form_state['values']['attributes'])) {
    foreach ($form_state['values']['attributes'] as $aid => $attribute) {
      if ($attribute['remove']) {
        $remove_aids[] = $aid;
      }
      else {
        db_query("UPDATE {$attr_table} SET label = '%s', ordering = %d, required = %d, display = %d WHERE aid = %d AND {$id} = {$sql_type}", $attribute['label'], $attribute['ordering'], $attribute['required'], $attribute['display'], $aid, $form_state['values']['id']);
        $changed = TRUE;
      }
    }
    if (isset($remove_aids)) {
      $id_value = $form_state['values']['id'];
      $remove_aids_value = implode(', ', $remove_aids);
      db_query("DELETE FROM {$opt_table} WHERE EXISTS (SELECT * FROM {uc_attribute_options} AS ao WHERE {$opt_table}.oid = ao.oid AND ao.aid IN (%s)) AND {$opt_table}.{$id} = {$sql_type}", $remove_aids_value, $id_value);
      db_query("DELETE FROM {$attr_table} WHERE {$id} = {$sql_type} AND aid IN (%s)", $id_value, $remove_aids_value);
      if ($form_state['values']['type'] == 'product') {
        db_query("DELETE FROM {uc_product_adjustments} WHERE nid = %d", $id_value);
      }
      drupal_set_message(format_plural(count($remove_aids), '@count attribute has been removed.', '@count attributes have been removed.'));
    }
    if ($changed) {
      drupal_set_message(t('The changes have been saved.'));
    }
  }
  elseif ($form_state['values']['view'] == 'add') {
    foreach ($form_state['values']['add_attributes'] as $aid) {

      // Enable all options for added attributes.
      $attribute = uc_attribute_load($aid);
      foreach ($attribute->options as $option) {
        db_query("INSERT INTO {$opt_table} ({$id}, oid, cost, price, weight, ordering) VALUES ({$sql_type}, %d, %f, %f, %f, %d)", $form_state['values']['id'], $option->oid, $option->cost, $option->price, $option->weight, $option->ordering);
      }

      // Make the first option (if any) the default.
      $option = reset($attribute->options);
      if ($option) {
        $oid = $option->oid;
      }
      else {
        $oid = 0;
      }
      db_query("INSERT INTO {$attr_table} ({$id}, aid, label, ordering, default_option, required, display) SELECT {$sql_type}, aid, label, ordering, %d, required, display FROM {uc_attributes} WHERE aid = %d", $form_state['values']['id'], $oid, $aid);
    }
    if (count($form_state['values']['add_attributes']) > 0) {
      if ($form_state['values']['type'] == 'product') {
        db_query("DELETE FROM {uc_product_adjustments} WHERE nid = %d", $form_state['values']['id']);
      }
      drupal_set_message(format_plural(count($form_state['values']['add_attributes']), '@count attribute has been added.', '@count attributes have been added.'));
    }
  }
  if ($form_state['values']['type'] == 'product') {
    $form_state['redirect'] = 'node/' . $form_state['values']['id'] . '/edit/attributes';
  }
  else {
    $form_state['redirect'] = 'admin/store/products/classes/' . $form_state['values']['id'] . '/attributes';
  }
}