You are here

function uc_object_attributes_form_submit in Ubercart 5

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

File

uc_attribute/uc_attribute.module, line 873

Code

function uc_object_attributes_form_submit($form_id, $form_values) {
  if ($form_values['type'] == 'product') {
    $attr_table = '{uc_product_attributes}';
    $opt_table = '{uc_product_options}';
    $id = 'nid';
    $sql_type = '%d';
  }
  else {
    if ($form_values['type'] == 'class') {
      $attr_table = '{uc_class_attributes}';
      $opt_table = '{uc_class_attribute_options}';
      $id = 'pcid';
      $sql_type = "'%s'";
    }
  }
  if ($form_values['view'] == 'overview' && is_array($form_values['attributes'])) {
    foreach ($form_values['attributes'] as $aid => $attribute) {
      if ($attribute['remove']) {
        $remove_aids[] = $aid;
      }
      else {
        db_query("UPDATE {$attr_table} SET ordering = %d, required = %d, display = %d WHERE aid = %d AND {$id} = {$sql_type}", $attribute['ordering'], $attribute['required'], $attribute['display'], $aid, $form_values['id']);
        $changed = TRUE;
      }
    }
    if (count($remove_aids) > 0) {
      $values = array(
        $form_values['id'],
        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}", $values);
      db_query("DELETE FROM {$attr_table} WHERE {$id} = {$sql_type} AND aid IN (%s)", $values);
      if ($form_values['type'] == 'product') {
        db_query("DELETE FROM {uc_product_adjustments} WHERE nid = %d", $form_values['id']);
      }
      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.'));
    }
  }
  else {
    if ($form_values['view'] == 'add') {
      foreach ($form_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_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, ordering, default_option, required, display) SELECT {$sql_type}, aid, ordering, %d, required, display FROM {uc_attributes} WHERE aid = %d", $form_values['id'], $oid, $aid);
      }
      if (count($form_values['add_attributes']) > 0) {
        if ($form_values['type'] == 'product') {
          db_query("DELETE FROM {uc_product_adjustments} WHERE nid = %d", $form_values['id']);
        }
        drupal_set_message(format_plural(count($form_values['add_attributes']), '@count attribute has been added.', '@count attributes have been added.'));
      }
    }
  }
  if ($form_values['type'] == 'product') {
    return 'node/' . $form_values['id'] . '/edit/attributes';
  }
  else {
    return 'admin/store/products/classes/' . $form_values['id'] . '/attributes';
  }
}