diff --git a/uc_attribute/tests/uc_attribute.test b/uc_attribute/tests/uc_attribute.test index b3786d9b..0f058e3d 100644 --- a/uc_attribute/tests/uc_attribute.test +++ b/uc_attribute/tests/uc_attribute.test @@ -14,14 +14,6 @@ require_once backdrop_get_path('module', 'uc_store') . '/tests/test_helper.inc'; */ class UbercartAttributeTestCase extends UbercartTestHelper { - public static function getInfo() { - return array( - 'name' => 'Attribute API', - 'description' => 'Test the attribute API.', - 'group' => 'Ubercart', - ); - } - /** * Overrides BackdropWebTestCase::setUp(). */ diff --git a/uc_attribute/uc_attribute.install b/uc_attribute/uc_attribute.install index 2fbc2903..7a1d30d8 100644 --- a/uc_attribute/uc_attribute.install +++ b/uc_attribute/uc_attribute.install @@ -189,10 +189,6 @@ function uc_attribute_schema() { ), 'primary key' => array('pcid', 'aid'), 'foreign keys' => array( - 'uc_product_classes' => array( - 'table' => 'uc_product_classes', - 'columns' => array('pcid' => 'pcid'), - ), 'uc_attributes' => array( 'table' => 'uc_attributes', 'columns' => array('aid' => 'aid'), @@ -249,10 +245,6 @@ function uc_attribute_schema() { ), 'primary key' => array('pcid', 'oid'), 'foreign keys' => array( - 'uc_product_classes' => array( - 'table' => 'uc_product_classes', - 'columns' => array('pcid' => 'pcid'), - ), 'uc_attribute_options' => array( 'table' => 'uc_attribute_options', 'columns' => array('oid' => 'oid'), diff --git a/uc_order/uc_order.rules.inc b/uc_order/uc_order.rules.inc index b78b56c3..f9984108 100644 --- a/uc_order/uc_order.rules.inc +++ b/uc_order/uc_order.rules.inc @@ -535,9 +535,9 @@ function uc_order_condition_has_product_class($order, $product_classes, $require function uc_order_condition_has_product_class_classes_options() { $options = array(); - $result = db_query('SELECT * FROM {uc_product_classes}'); - foreach ($result as $class) { - $options += array($class->pcid => $class->name); + $classes = uc_product_class_load(); + foreach ($classes as $class_id => $class) { + $options += array($class_id => $class['name']); } return $options; diff --git a/uc_product/config/uc_product.settings.json b/uc_product/config/uc_product.settings.json index 03f4baef..bf66d74a 100644 --- a/uc_product/config/uc_product.settings.json +++ b/uc_product/config/uc_product.settings.json @@ -3,5 +3,6 @@ "uc_product_image_widget": "", "uc_product_add_to_cart_qty": false, "uc_product_update_node_view": false, - "uc_product_shippable_product": true + "uc_product_shippable_product": true, + "classes": [] } diff --git a/uc_product/uc_product.admin.inc b/uc_product/uc_product.admin.inc index a2a21e48..b43d38d9 100644 --- a/uc_product/uc_product.admin.inc +++ b/uc_product/uc_product.admin.inc @@ -487,41 +487,12 @@ function uc_product_class_form_submit($form, &$form_state) { // Convert whitespace to underscores, and remove other non-alphanumeric characters. $pcid = preg_replace(array('/\s+/', '/\W/'), array('_', ''), strtolower($pcid)); - $result = db_merge('uc_product_classes') - ->key(array('pcid' => $pcid)) - ->fields(array( - 'name' => $form_state['values']['name'], - 'description' => $form_state['values']['description'], - )) - ->execute(); - - $type = new \stdClass(); - $type->type = $pcid; - $type->name = $form_state['values']['name']; - $type->base = 'uc_product'; - $type->module = 'uc_product'; - $type->description = $form_state['values']['description']; - $type = node_type_set_defaults($type); - node_type_save($type); - - uc_product_node_info(TRUE); - - if ($result == MergeQuery::STATUS_INSERT) { - module_invoke_all('uc_product_class', $pcid, 'insert'); - } - else { - module_invoke_all('uc_product_class', $pcid, 'update'); - } - - node_type_cache_reset(); - if ($is_new) { - $type = node_type_get_type($pcid); - node_add_body_field($type, t('Description')); - uc_product_add_default_image_field($pcid); - } - menu_rebuild(); - - backdrop_set_message(t('Product class saved.')); + $class = array( + 'pcid' => $pcid, + 'name' => $form_state['values']['name'], + 'description' => $form_state['values']['description'] + ); + uc_product_class_create($class, $is_new); } /** @@ -554,15 +525,20 @@ function uc_product_class_delete_confirm($form, &$form_state, $class) { * @see uc_product_class_delete_confirm() */ function uc_product_class_delete_confirm_submit($form, &$form_state) { + $config = config('uc_product.settings'); + $classes = $config->get('classes'); + if (($key = array_search($form_state['values']['pcid'], $classes)) !== false) { + unset($classes[$key]); + } + $config->set('classes', array_values(array_filter($classes))); + $config->save(); + $type = node_type_get_type($form_state['values']['pcid']); $type->base = 'node_content'; $type->module = 'node'; $type->custom = 1; node_type_save($type); - db_delete('uc_product_classes') - ->condition('pcid', $form_state['values']['pcid']) - ->execute(); module_invoke_all('uc_product_class', $form_state['values']['pcid'], 'delete'); // TODO migrate This needed? uc_product_node_info(TRUE); diff --git a/uc_product/uc_product.install b/uc_product/uc_product.install index 6e782e37..4b5ef168 100644 --- a/uc_product/uc_product.install +++ b/uc_product/uc_product.install @@ -10,31 +10,6 @@ function uc_product_schema() { $schema = array(); - $schema['uc_product_classes'] = array( - 'description' => 'The list of product node types.', - 'fields' => array( - 'pcid' => array( - 'description' => 'The node type identifier.', - 'type' => 'varchar', - 'length' => 32, - 'not null' => TRUE, - 'default' => '', - ), - 'name' => array( - 'description' => 'The human-readable name.', - 'type' => 'varchar', - 'length' => 255, - 'not null' => TRUE, - 'default' => '', - ), - 'description' => array( - 'description' => 'The description of the node type.', - 'type' => 'text', - ), - ), - 'primary key' => array('pcid'), - ); - $schema['uc_product_features'] = array( 'description' => 'Stores information of features added to products.', 'fields' => array( @@ -264,6 +239,37 @@ function uc_product_set_teaser_display($type) { field_bundle_settings('node', $type, $settings); } +/** + * Implements hook_install(). + */ +function uc_product_install() { + _uc_product_type_create(); +} + +/** + * Creates the product type. + */ +function _uc_product_type_create() { + // Create an additional node type. + $product_node_type = array( + 'type' => 'product', + 'name' => t('Product'), + 'base' => 'uc_product', + 'description' => t('Use products to represent items for sale on the website, including all the unique information that can be attributed to a specific model number.'), + 'custom' => 1, + 'modified' => 1, + 'locked' => 0, + 'is_new' => TRUE, + 'settings' => array( + 'promote_enabled' => FALSE, + ), + ); + + $product_node_type = node_type_set_defaults($product_node_type); + node_type_save($product_node_type); + node_add_body_field($product_node_type); +} + /** * Implements hook_update_last_removed(). */ @@ -455,32 +461,20 @@ function uc_product_update_1006() { } /** - * Implements hook_install(). + * Convert product classes to CMI. */ -function uc_product_install() { - _uc_product_type_create(); -} +function uc_product_update_1007() { + if (!db_table_exists('uc_product_classes')) { + return; + } + $classes_db = db_query("SELECT * FROM {uc_product_classes}"); + $config = config('uc_product.settings'); -/** - * Creates the product type. - */ -function _uc_product_type_create() { - // Create an additional node type. - $product_node_type = array( - 'type' => 'product', - 'name' => t('Product'), - 'base' => 'uc_product', - 'description' => t('Use products to represent items for sale on the website, including all the unique information that can be attributed to a specific model number.'), - 'custom' => 1, - 'modified' => 1, - 'locked' => 0, - 'is_new' => TRUE, - 'settings' => array( - 'promote_enabled' => FALSE, - ), - ); + foreach ($classes_db as $class) { + $classes[] = $class->pcid; + } + $config->set('classes', $classes); + $config->save(); - $product_node_type = node_type_set_defaults($product_node_type); - node_type_save($product_node_type); - node_add_body_field($product_node_type); + db_drop_table('uc_product_classes'); } diff --git a/uc_product/uc_product.module b/uc_product/uc_product.module index 94fa37f8..386cf1d7 100644 --- a/uc_product/uc_product.module +++ b/uc_product/uc_product.module @@ -262,28 +262,6 @@ function uc_product_node_info($reset = FALSE) { return $types; } -/** - * Implements hook_node_type_update(). - * - * Ensure product class names and descriptions are synchronised if the - * content type is updated. - */ -function uc_product_node_type_update($info) { - $existing_type = !empty($info->old_type) ? $info->old_type : $info->type; - - db_update('uc_product_classes') - ->fields(array( - 'pcid' => $info->type, - 'name' => $info->name, - 'description' => $info->description, - )) - ->condition('pcid', $existing_type) - ->execute(); - - // Reset static variable data and load new info. - uc_product_node_info(TRUE); -} - /** * Implements hook_forms(). * @@ -1586,16 +1564,33 @@ function uc_product_get_description($product) { /** * Load a product class or all classes. + * + * @param string|null $class_id + * @param bool $reset + * + * @return array + * Array of classes. */ function uc_product_class_load($class_id = NULL, $reset = FALSE) { static $classes; if (!isset($classes) || $reset) { - // Load classes from database. + // Load classes from config. $classes = array(); - $result = db_query("SELECT * FROM {uc_product_classes}"); - while ($class = $result->fetchObject()) { - $class->locked = FALSE; - $classes[$class->pcid] = $class; + + $config = config('uc_product.settings'); + $classes_config = $config->get('classes'); + + if (!$classes_config) { + return $classes; + } + + foreach($classes_config as $pcid) { + $type = node_type_get_type($pcid); + $class['locked'] = FALSE; + $class['pcid'] = $pcid; + $class['name'] = $type->name; + $class['description'] = $type->description; + $classes[$pcid] = (object) $class; } // Merge any module-defined classes. @@ -1616,8 +1611,8 @@ function uc_product_class_load($class_id = NULL, $reset = FALSE) { $classes[$pcid] = (object) array_merge($class, (array) $classes[$pcid]); } else { - // Ensure the class info is saved in the database. - backdrop_write_record('uc_product_classes', $class); + // Ensure the class info is saved. + uc_product_class_sync($pcid); $classes[$pcid] = (object) $class; } @@ -1923,3 +1918,54 @@ function uc_product_autoload_info() { 'uc_product_handler_filter_product' => 'views/uc_product_handler_filter_product.inc', ); } + +/** + * Create product class + * + * @param array $class + * @param bool $is_new + */ +function uc_product_class_create($class, $is_new = FALSE) { + uc_product_class_sync($class['pcid']); + + $type = new \stdClass(); + $type->type = $class['pcid']; + $type->name = $class['name']; + $type->base = 'uc_product'; + $type->module = 'uc_product'; + $type->description = $class['description']; + $type = node_type_set_defaults($type); + node_type_save($type); + + uc_product_node_info(TRUE); + + if ($is_new) { + module_invoke_all('uc_product_class', $class['pcid'], 'insert'); + } + else { + module_invoke_all('uc_product_class', $class['pcid'], 'update'); + } + + node_type_cache_reset(); + if ($is_new) { + $type = node_type_get_type($class['pcid']); + node_add_body_field($type, t('Description')); + uc_product_add_default_image_field($class['pcid']); + } + menu_rebuild(); + + backdrop_set_message(t('Product class saved.')); +} + +/** + * Save product class + * + * @param array $class_id + */ +function uc_product_class_sync($class_id) { + $config = config('uc_product.settings'); + $classes = $config->get('classes'); + $classes[] = $class_id; + $config->set('classes', array_values(array_filter($classes))); + $config->save(); +} diff --git a/uc_store/tests/test_helper.inc b/uc_store/tests/test_helper.inc index bc060070..806431e3 100644 --- a/uc_store/tests/test_helper.inc +++ b/uc_store/tests/test_helper.inc @@ -109,9 +109,10 @@ class UbercartTestHelper extends BackdropWebTestCase { 'name' => $this->randomName(8), 'description' => $this->randomName(8), ); - $product_class = (object) $product_class; - backdrop_write_record('uc_product_classes', $product_class); + uc_product_class_create($product_class, TRUE); + + $product_class = (object) $product_class; return $product_class; }