2017-07-20 14:28:43 +02:00

320 lines
15 KiB
PHP

<?php
class ConfiguratorStorage extends ObjectModel
{
/**
* Product with configuration
* @param int $id_product
* @return boolean
*/
public static function hasConfiguratorOpt($id_product)
{
$result = Db::getInstance()->getValue('
SELECT count(*) AS nb
FROM `'._DB_PREFIX_.'product_configurator_opt_group` pcog,
`'._DB_PREFIX_.'configurator_opt_group` cog
WHERE pcog.`id_product` = '.(int)$id_product.'
AND cog.`id_configurator_opt_group` = pcog.`id_configurator_opt_group`');
return ( $result > 0 ? true : false );
}
/**
* Retourne les groupes options
* @param int $id_product
* @return array
*/
public static function getConfiguratorOptGroup($id_product)
{
$context = Context::getContext();
$result = Db::getInstance()->executeS('
SELECT pcog.*, col.`name`, col.`description`, cog.`type`, cog.`reference`
FROM `'._DB_PREFIX_.'product_configurator_opt_group` pcog,
`'._DB_PREFIX_.'configurator_opt_group` cog,
`'._DB_PREFIX_.'configurator_opt_group_lang` col
WHERE pcog.`id_product` = '.(int)$id_product.'
AND cog.`id_configurator_opt_group` = col.`id_configurator_opt_group`
AND pcog.`id_configurator_opt_group` = col.`id_configurator_opt_group`
AND col.`id_lang` = '.(int)$context->language->id.'
ORDER BY pcog.`position` ASC');
return $result;
}
/**
* Retourne les impacts
* @param int $id_product
* @return array
*/
public static function getConfiguratorOptImpact($id_product)
{
$context = Context::getContext();
$result = Db::getInstance()->executeS('
SELECT pcoi.`id_product_configurator_opt_impact`, pcoi.`id_product_configurator_opt_group`, pcoi.`id_configurator_opt`, col.`name`, pcoi.`price`, co.`position`
FROM `'._DB_PREFIX_.'product_configurator_opt_impact` pcoi
LEFT JOIN `'._DB_PREFIX_.'configurator_opt` co ON co.`id_configurator_opt` = pcoi.`id_configurator_opt`
LEFT JOIN `'._DB_PREFIX_.'configurator_opt_lang` col
ON (col.`id_configurator_opt` = pcoi.`id_configurator_opt` AND col.`id_lang` = '.(int)$context->language->id.')
WHERE pcoi.`id_product` = '.(int)$id_product.'
ORDER BY co.`position` ASC');
return $result;
}
/**
* Group Label : values
* @param int $id_configurator
* @return array
*/
public static function getOptProductFlatten($id_configurator)
{
$context = Context::getContext();
$id_lang = $context->language->id;
$optFlatten = array();
$sql = 'SELECT cs.`opt_value`, cs.`id_configurator_opt_group`, cogl.`name` AS groupName, col.`name` AS optName, cog.`reference`, cog.`type`
FROM `'._DB_PREFIX_.'configurator_storage` cs,
`'._DB_PREFIX_.'configurator_opt_lang` col,
`'._DB_PREFIX_.'configurator_opt_group_lang` cogl,
`'._DB_PREFIX_.'configurator_opt_group` cog
WHERE col.`id_configurator_opt` = cs.`id_configurator_opt` AND col.`id_lang` = '.(int)$id_lang.'
AND cogl.`id_configurator_opt_group` = cs.`id_configurator_opt_group` AND cogl.`id_lang` = '.(int)$id_lang.'
AND cog.`id_configurator_opt_group` = cs.`id_configurator_opt_group`
AND cs.`id_configurator` = '.(int)$id_configurator;
$optList = Db::getInstance()->executeS($sql);
if (count($optList) > 0) {
foreach ($optList as $o) {
if (!array_key_exists($o['id_configurator_opt_group'], $optFlatten)) {
$optFlatten[$o['id_configurator_opt_group']] = array(
'name' => $o['groupName'],
'reference' => $o['reference'],
'type' => $o['type'],
'value' => array(),
);
}
if ($o['opt_value'] == '') {
$optFlatten[$o['id_configurator_opt_group']]['value'][] = $o['optName'];
} else {
$optFlatten[$o['id_configurator_opt_group']]['value'][] = $o['opt_value'];
}
}
}
return $optFlatten;
}
/**
* Get form values
* @param int $id_configurator
* @return array
*/
public static function getOptProductSelected($id_configurator)
{
$context = Context::getContext();
$id_lang = $context->language->id;
$optFlatten = array();
$sql = 'SELECT cs.`opt_value`, cs.`id_configurator_opt`, cs.`id_configurator_opt_group`, cogl.`name` AS groupName, col.`name` AS optName, cog.`reference`
FROM `'._DB_PREFIX_.'configurator_storage` cs,
`'._DB_PREFIX_.'configurator_opt_lang` col,
`'._DB_PREFIX_.'configurator_opt_group_lang` cogl,
`'._DB_PREFIX_.'configurator_opt_group` cog
WHERE col.`id_configurator_opt` = cs.`id_configurator_opt` AND col.`id_lang` = '.(int)$id_lang.'
AND cogl.`id_configurator_opt_group` = cs.`id_configurator_opt_group` AND cogl.`id_lang` = '.(int)$id_lang.'
AND cog.`id_configurator_opt_group` = cs.`id_configurator_opt_group`
AND cs.`id_configurator` = '.(int)$id_configurator;
$optList = Db::getInstance()->executeS($sql);
if (count($optList) > 0) {
foreach ($optList as $o) {
if (!array_key_exists($o['id_configurator_opt_group'], $optFlatten)) {
$optFlatten[$o['id_configurator_opt_group']] = array();
}
if ($o['opt_value'] == '') {
$optFlatten[$o['id_configurator_opt_group']][] = $o['id_configurator_opt'];
} else {
$optFlatten[$o['id_configurator_opt_group']][] = $o['opt_value'];
}
}
}
return $optFlatten;
}
/**
* Parse option send when adding to cart
* @param int $id_configurator
* @param int $id_product
* @param int $id_address_delivery
* @param int $id_product_attribute
* @param int $qty
* @return int
*/
public static function parseConfigurator($id_configurator, $id_product, $id_address_delivery, $id_product_attribute, $qty)
{
$detectOptGroup = 'optgroup-';
$postValues = Tools::getAllValues();
if (count($postValues) > 0) {
$sqlValues = array();
foreach($postValues as $p => $v) {
if (!empty($v)) {
if (substr($p, 0, strlen($detectOptGroup)) === $detectOptGroup) {
$ids = explode('-', substr($p, strlen($detectOptGroup)));
$id_configurator_opt_group = $ids[0];
$id_configurator_opt = $v;
$optValue = '';
if (isset($ids[1])) {
$id_configurator_opt = $ids[1];
$optValue = $v;
}
if (is_array($id_configurator_opt) && count($id_configurator_opt) > 0) {
foreach ($id_configurator_opt as $item) {
$price = Db::getInstance()->getValue('SELECT pcoi.`price` FROM `'._DB_PREFIX_.'configurator_opt` co,
`'._DB_PREFIX_.'product_configurator_opt_impact` pcoi
WHERE co.`id_configurator_opt` = pcoi.`id_configurator_opt` AND co.`id_configurator_opt` = '.(int)$item);
$sqlValues[] = '(@id_configurator, '.$id_product.', '.(int)$id_configurator_opt_group.', '.pSQL($item).', \''.pSQL($optValue).'\', '.$price.')';
}
} else {
if (is_array($optValue)) {
foreach($optValue as $v) {
$price = Db::getInstance()->getValue('SELECT pcoi.`price` FROM `'._DB_PREFIX_.'configurator_opt` co,
`'._DB_PREFIX_.'product_configurator_opt_impact` pcoi
WHERE co.`id_configurator_opt` = pcoi.`id_configurator_opt` AND co.`id_configurator_opt` = '.(int)$id_configurator_opt);
$sqlValues[] = '(@id_configurator, '.$id_product.', '.(int)$id_configurator_opt_group.', '.(int)$id_configurator_opt.', \''.pSQL($v).'\', '.$price.')';
}
} else {
$price = Db::getInstance()->getValue('SELECT pcoi.`price` FROM `'._DB_PREFIX_.'configurator_opt` co,
`'._DB_PREFIX_.'product_configurator_opt_impact` pcoi
WHERE co.`id_configurator_opt` = pcoi.`id_configurator_opt` AND co.`id_configurator_opt` = '.(int)$id_configurator_opt);
$sqlValues[] = '(@id_configurator, '.$id_product.', '.(int)$id_configurator_opt_group.', '.(int)$id_configurator_opt.', \''.pSQL($optValue).'\', '.$price.')';
}
}
}
}
}
if (count($sqlValues) > 0) {
if ($id_configurator === null) {
$id_configurator = 0;
}
if ($id_configurator == 0) {
// Get id_configurator max
$cmaxSql = 'SELECT IF(MAX(cp.id_configurator) IS NULL, 0, MAX(cp.id_configurator)) AS maxCp,
IF(MAX(cs.id_configurator) IS NULL, 0, MAX(cs.id_configurator)) AS maxCs
FROM `'._DB_PREFIX_.'configurator_storage` cs, `'._DB_PREFIX_.'cart_product` cp';
$max = Db::getInstance()->getRow($cmaxSql);
$id_configurator = ($max['maxCp'] > $max['maxCs'] ? $max['maxCp'] : $max['maxCs']) + 1;
$context = Context::getContext();
// Assign id on cart_product
Db::getInstance()->update('cart_product', array(
'id_configurator' => (int)$id_configurator),
'id_cart='.(int)$context->cart->id.
' AND id_product='.(int)$id_product.
' AND id_address_delivery='.(int)$id_address_delivery.
' AND id_shop='.(int)$context->cart->id_shop.
' AND id_product_attribute='.(int)$id_product_attribute.
' AND quantity='.(int)$qty
.' AND id_configurator = 0');
}
// Delete
else {
Db::getInstance()->delete('configurator_storage', 'id_configurator = '.(int)$id_configurator);
}
// Insert configuration
Db::getInstance()->execute('SET @id_configurator = '.$id_configurator.';
INSERT INTO `'._DB_PREFIX_.'configurator_storage`
(`id_configurator`, `id_product`, `id_configurator_opt_group`, `id_configurator_opt`, `opt_value`, `price`)
VALUES '.implode(',', $sqlValues)
);
return $id_configurator;
}
}
return 0;
}
/**
* Check if fields are required
* @param int $id_product
* @return boolean|array
*/
public static function checkRequirement($id_product)
{
$context = Context::getContext();
$id_lang = $context->language->id;
$errors = array();
$optIds = array();
$detectOptGroup = 'optgroup-';
$postValues = Tools::getAllValues();
if (count($postValues) > 0) {
foreach($postValues as $p => $v) {
if (substr($p, 0, strlen($detectOptGroup)) === $detectOptGroup) {
$ids = explode('-', substr($p, strlen($detectOptGroup)));
$optIds[$ids[0]] = $v;
}
}
$result = Db::getInstance()->executeS('
SELECT pcog.*, cog.`type`, col.`name`
FROM `'._DB_PREFIX_.'product_configurator_opt_group` pcog,
`'._DB_PREFIX_.'configurator_opt_group` cog,
`'._DB_PREFIX_.'configurator_opt_group_lang` col
WHERE pcog.`id_product` = '.(int)$id_product.'
AND cog.`id_configurator_opt_group` = col.`id_configurator_opt_group`
AND pcog.`id_configurator_opt_group` = col.`id_configurator_opt_group`
AND col.`id_lang` = '.(int)$id_lang.'
ORDER BY pcog.`position` ASC');
if (count($result) > 0) {
foreach($result as $item) {
if ($item['required'] == 1) {
if (!array_key_exists($item['id_configurator_opt_group'], $optIds)) {
$errors[] = Tools::displayError('Error : field '.$item['name'].' has no value');
}
else {
$value = $optIds[$item['id_configurator_opt_group']];
if (empty($value)) {
$errors[] = Tools::displayError('Error : field '.$item['name'].' has no value');
}
}
}
// Check quantities field value
if ($item['type'] == 'quantities') {
$product = new Product($id_product);
$id_product_attribute = $product->getDefaultIdProductAttribute();
if ($id_product_attribute > 0) {
$attributes = $product->getAttributeCombinations($context->language->id);
foreach ($attributes as $a) {
if ($a['id_product_attribute'] == $id_product_attribute) {
$qty = $a['minimal_quantity'];
break;
}
}
}
$values = $optIds[$item['id_configurator_opt_group']];
foreach($values as $v) {
if ($v <= $qty) {
$errors[] = Tools::displayError('You have set a quantity under minimal quantity');
break;
}
}
}
}
}
}
if (count($errors) > 0) {
return $errors;
}
return true;
}
}