chocolatdemariage/www/modules/antadisconfigurator/classes/ProductConfiguratorOptGroup.php
2017-06-14 15:14:07 +02:00

141 lines
5.1 KiB
PHP

<?php
class ProductConfiguratorOptGroup extends ObjectModel
{
public $id_product_configurator_opt_group;
public $id_product;
public $id_configurator_opt_group;
public $required;
public $position;
public static $definition = array(
'table' => 'product_configurator_opt_group',
'primary' => 'id_product_configurator_opt_group',
'multilang' => false,
'fields' => array(
'id_product' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'id_configurator_opt_group' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'required' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'required' => true),
'position' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
),
);
public static function getProductConfiguratorOptGroup($id_product)
{
$id_lang = (int)Context::getContext()->language->id;
$opts = Db::getInstance()->executeS('
SELECT pcog.*, col.`name` FROM `'._DB_PREFIX_.'product_configurator_opt_group` pcog, `'._DB_PREFIX_.'configurator_opt_group_lang` col
WHERE pcog.`id_product` = '.(int)$id_product.'
AND pcog.`id_configurator_opt_group` = col.`id_configurator_opt_group`
AND col.`id_lang` = '.(int)$id_lang.'
ORDER BY pcog.`position` ASC');
return $opts;
}
public function getConfiguratorOptGroupName()
{
$id_lang = (int)Context::getContext()->language->id;
return Db::getInstance()->getValue('
SELECT `name`
FROM `'._DB_PREFIX_.'configurator_opt_group_lang`
WHERE `id_configurator_opt_group` = '.(int)$this->id_configurator_opt_group.' AND `id_lang` = '.(int)$id_lang);
}
/**
* Move an attribute inside its group
* @param bool $way Up (1) or Down (0)
* @param int $position
* @return bool Update result
*/
public function updatePosition($way, $position)
{
if (!$id_product = (int)Tools::getValue('id_product')) {
$id_product = (int)$this->id_product;
}
$sql = '
SELECT a.`id_product_configurator_opt_group`, a.`position`, a.`id_product`
FROM `'._DB_PREFIX_.'product_configurator_opt_group` a
WHERE a.`id_product` = '.(int)$id_product.'
ORDER BY a.`position` ASC';
if (!$res = Db::getInstance()->executeS($sql)) {
return false;
}
foreach ($res as $attribute) {
if ((int)$attribute['id_product_configurator_opt_group'] == (int)$this->id) {
$moved_attribute = $attribute;
}
}
if (!isset($moved_attribute) || !isset($position)) {
return false;
}
// < and > statements rather than BETWEEN operator
// since BETWEEN is treated differently according to databases
$res1 = Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'product_configurator_opt_group`
SET `position`= `position` '.($way ? '- 1' : '+ 1').'
WHERE `position`
'.($way
? '> '.(int)$moved_attribute['position'].' AND `position` <= '.(int)$position
: '< '.(int)$moved_attribute['position'].' AND `position` >= '.(int)$position).'
AND `id_product`='.(int)$moved_attribute['id_product']
);
$res2 = Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'product_configurator_opt_group`
SET `position` = '.(int)$position.'
WHERE `id_product_configurator_opt_group` = '.(int)$moved_attribute['id_product_configurator_opt_group'].'
AND `id_product`='.(int)$moved_attribute['id_product']
);
return ($res1 && $res2);
}
/**
* Reorder attribute position in group $id_attribute_group.
* Call it after deleting an attribute from a group.
*
* @param int $id_attribute_group
* @param bool $use_last_attribute
* @return bool $return
*/
public function cleanPositions($id_product, $use_last = true)
{
Db::getInstance()->execute('SET @i = -1', false);
$sql = 'UPDATE `'._DB_PREFIX_.'product_configurator_opt_group` SET `position` = @i:=@i+1 WHERE';
if ($use_last) {
$sql .= ' `id_product_configurator_opt_group` != '.(int)$this->id.' AND';
}
$sql .= ' `id_product` = '.(int)$id_product.' ORDER BY `position` ASC';
$return = Db::getInstance()->execute($sql);
}
/**
* getHigherPosition
*
* Get the higher position from a group
*
* @param int $id_product_configurator_opt_group
* @return int $position
*/
public static function getHigherPosition($id_product_configurator_opt_group)
{
$sql = 'SELECT MAX(`position`)
FROM `'._DB_PREFIX_.'product_configurator_opt_group`
WHERE id_prodcut_configurator_opt_group = '.(int)$id_product_configurator_opt_group;
$position = DB::getInstance()->getValue($sql);
return (is_numeric($position)) ? $position : -1;
}
}