--- uc_attribute/uc_attribute.admin.inc 2009-06-09 15:01:27 +0000 +++ uc_attribute/uc_attribute.admin.inc 2009-06-11 07:32:06 +0000 @@ -627,78 +631,58 @@ return $output; } +/** + * Submit handler for the product/class attributes form. + */ 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'"; - } - + $type = $form_state['values']['type']; + $id = $form_state['values']['id']; + + // The attribute edit form. if ($form_state['values']['view'] == 'overview' && is_array($form_state['values']['attributes'])) { + + $remove_count = 0; foreach ($form_state['values']['attributes'] as $aid => $attribute) { + // Remove was checked. if ($attribute['remove']) { - $remove_aids[] = $aid; + uc_attribute_subject_delete($aid, $id, $type); + $remove_count++; } + + // Update the attribute. 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']); + uc_attribute_subject_save($attribute, $id, $type); $changed = TRUE; } } - - if (count($remove_aids) > 0) { - $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 ($remove_count) { + drupal_set_message(format_plural($remove_count, '@count attribute has been removed.', '@count attributes have been removed.')); } - + if ($changed) { drupal_set_message(t('The changes have been saved.')); } } + + // The attribute add form. elseif ($form_state['values']['view'] == 'add') { foreach ($form_state['values']['add_attributes'] as $aid) { - // Enable all options for added attributes. + // Load the base attribute, and save it with all its options. $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); + uc_attribute_subject_save($attribute, $id, $type, TRUE); } + 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'; + $form_state['redirect'] = 'node/'. $id .'/edit/attributes'; } else { - $form_state['redirect'] = 'admin/store/products/classes/'. $form_state['values']['id'] .'/attributes'; + $form_state['redirect'] = 'admin/store/products/classes/'. $id .'/attributes'; } } @@ -899,41 +883,36 @@ } } +/** + * Submit handler for the product/class attribute options form. + */ function uc_object_options_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'"; - } - + $id = $form_state['values']['id']; + $type = $form_state['values']['type']; + foreach ($form_state['values']['attributes'] as $attribute) { - db_query("UPDATE $attr_table SET default_option = %d WHERE $id = $sql_type AND aid = %d", $attribute['default'], $form_state['values']['id'], $attribute['aid']); - + // Save the default option. + uc_attribute_subject_save($attribute, $id, $type); + + // Deal with options if they exist. if (is_array($attribute['options'])) { foreach ($attribute['options'] as $oid => $option) { - db_query("DELETE FROM $opt_table WHERE $id = $sql_type AND oid = %d", $form_state['values']['id'], $oid); - + + // Checked? if ($option['select']) { - db_query("INSERT INTO $opt_table ($id, oid, cost, price, weight, ordering) VALUES ($sql_type, %d, %f, %f, %f, %d)", - $form_state['values']['id'], $oid, $option['cost'], $option['price'], $option['weight'], $option['ordering']); + $option['oid'] = $oid; + uc_attribute_subject_option_save($option, $id, $type); } - elseif ($form_state['values']['type'] == 'product') { - $aid = $attribute['aid']; - $match = 'i:'. $aid .';s:'. strlen($oid) .':"'. $oid .'";'; - db_query("DELETE FROM {uc_product_adjustments} WHERE nid = %d AND combination LIKE '%%%s%%'", $form_state['values']['id'], $match); + + // Unchecked means delete the option. + else { + uc_attribute_subject_option_delete($oid, $id, $type); } } } } - drupal_set_message(t('The !type options have been saved.', array('!type' => $form_state['values']['type'] == 'product' ? t('product') : t('product class')))); + drupal_set_message(t('The !type options have been saved.', array('!type' => $type == 'product' ? t('product') : t('product class')))); } /**