123 lines
4.2 KiB
PHP
123 lines
4.2 KiB
PHP
<?php
|
|
|
|
class ConfiguratorOpt extends ObjectModel
|
|
{
|
|
public $id_configurator_opt_group;
|
|
public $price;
|
|
public $position;
|
|
|
|
public $name;
|
|
|
|
public static $definition = array(
|
|
'table' => 'configurator_opt',
|
|
'primary' => 'id_configurator_opt',
|
|
'multilang' => true,
|
|
'fields' => array(
|
|
'id_configurator_opt_group' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
|
|
|
|
'price' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required' => true),
|
|
'position' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
|
|
|
|
/* Lang fields */
|
|
'name' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 255),
|
|
),
|
|
);
|
|
|
|
|
|
/**
|
|
* 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_configurator_opt_group = (int)Tools::getValue('id_configurator_opt_group')) {
|
|
$id_configurator_opt_group = (int)$this->id_configurator_opt_group;
|
|
}
|
|
|
|
$sql = '
|
|
SELECT a.`id_configurator_opt`, a.`position`, a.`id_configurator_opt_group`
|
|
FROM `'._DB_PREFIX_.'configurator_opt` a
|
|
WHERE a.`id_configurator_opt_group` = '.(int)$id_configurator_opt_group.'
|
|
ORDER BY a.`position` ASC';
|
|
|
|
if (!$res = Db::getInstance()->executeS($sql)) {
|
|
return false;
|
|
}
|
|
|
|
foreach ($res as $attribute) {
|
|
if ((int)$attribute['id_configurator_opt'] == (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_.'configurator_opt`
|
|
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_configurator_opt_group`='.(int)$moved_attribute['id_configurator_opt_group']
|
|
);
|
|
|
|
$res2 = Db::getInstance()->execute('
|
|
UPDATE `'._DB_PREFIX_.'configurator_opt`
|
|
SET `position` = '.(int)$position.'
|
|
WHERE `id_configurator_opt` = '.(int)$moved_attribute['id_configurator_opt'].'
|
|
AND `id_configurator_opt_group`='.(int)$moved_attribute['id_configurator_opt_group']
|
|
);
|
|
|
|
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_configurator_opt_group, $use_last = true)
|
|
{
|
|
Db::getInstance()->execute('SET @i = -1', false);
|
|
$sql = 'UPDATE `'._DB_PREFIX_.'configurator_opt` SET `position` = @i:=@i+1 WHERE';
|
|
|
|
if ($use_last) {
|
|
$sql .= ' `id_configurator_opt` != '.(int)$this->id.' AND';
|
|
}
|
|
|
|
$sql .= ' `id_configurator_opt_group` = '.(int)$id_configurator_opt_group.' ORDER BY `position` ASC';
|
|
|
|
$return = Db::getInstance()->execute($sql);
|
|
}
|
|
|
|
/**
|
|
* getHigherPosition
|
|
*
|
|
* Get the higher attribute position from a group attribute
|
|
*
|
|
* @param int $id_attribute_group
|
|
* @return int $position
|
|
*/
|
|
public static function getHigherPosition($id_configurator_opt_group)
|
|
{
|
|
$sql = 'SELECT MAX(`position`)
|
|
FROM `'._DB_PREFIX_.'configurator_opt`
|
|
WHERE id_configurator_opt_group = '.(int)$id_configurator_opt_group;
|
|
|
|
$position = DB::getInstance()->getValue($sql);
|
|
|
|
return (is_numeric($position)) ? $position : -1;
|
|
}
|
|
|
|
} |