Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #516: Convert product classes to CMI #518

Open
wants to merge 4 commits into
base: 1.x-3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions uc_attribute/tests/uc_attribute.test
Original file line number Diff line number Diff line change
Expand Up @@ -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().
*/
Expand Down
8 changes: 0 additions & 8 deletions uc_attribute/uc_attribute.install
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down Expand Up @@ -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'),
Expand Down
6 changes: 3 additions & 3 deletions uc_order/uc_order.rules.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion uc_product/config/uc_product.settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
}
52 changes: 14 additions & 38 deletions uc_product/uc_product.admin.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
Expand Down
94 changes: 44 additions & 50 deletions uc_product/uc_product.install
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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 <em>products</em> 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().
*/
Expand Down Expand Up @@ -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 <em>products</em> 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');
}
104 changes: 75 additions & 29 deletions uc_product/uc_product.module
Original file line number Diff line number Diff line change
Expand Up @@ -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().
*
Expand Down Expand Up @@ -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.
Expand All @@ -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;
}

Expand Down Expand Up @@ -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();
}
5 changes: 3 additions & 2 deletions uc_store/tests/test_helper.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down