Skip to content

Commit

Permalink
Fix position problem on a fresh install
Browse files Browse the repository at this point in the history
- AddPosition must add the position in category table
- updatePosition should test for negative position
  • Loading branch information
Pierre RAMBAUD committed Jul 8, 2019
1 parent 7c26c92 commit 1fb8008
Showing 1 changed file with 43 additions and 26 deletions.
69 changes: 43 additions & 26 deletions classes/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -1766,16 +1766,19 @@ public function updatePosition($way, $position)
}
// < and > statements rather than BETWEEN operator
// since BETWEEN is treated differently according to databases
$result = (Db::getInstance()->execute('
UPDATE `' . _DB_PREFIX_ . 'category` c ' . Shop::addSqlAssociation('category', 'c') . '
SET c.`position`= c.`position` ' . ($way ? '- 1' : '+ 1') . ',
category_shop.`position`= category_shop.`position` ' . ($way ? '- 1' : '+ 1') . ',
c.`date_upd` = "' . date('Y-m-d H:i:s') . '"
WHERE category_shop.`position`
' . ($way
$increment = ($way ? '- 1' : '+ 1');
$result = (Db::getInstance()->execute(
'UPDATE `' . _DB_PREFIX_ . 'category` c ' . Shop::addSqlAssociation('category', 'c') . ' ' .
'SET c.`position`= ' .
'IF(cast(c.`position` as signed) ' . $increment . ' > 0, c.`position` ' . $increment . ', 0), ' .
'category_shop.`position` = ' .
'IF(cast(category_shop.`position` as signed) ' . $increment . ' > 0, category_shop.`position` ' . $increment . ', 0), ' .
'c.`date_upd` = "' . date('Y-m-d H:i:s') . '" ' .
'WHERE category_shop.`position`' .
($way
? '> ' . (int) $movedCategory['position'] . ' AND category_shop.`position` <= ' . (int) $position
: '< ' . (int) $movedCategory['position'] . ' AND category_shop.`position` >= ' . (int) $position) . '
AND c.`id_parent`=' . (int) $movedCategory['id_parent'])
: '< ' . (int) $movedCategory['position'] . ' AND category_shop.`position` >= ' . (int) $position) . ' ' .
'AND c.`id_parent`=' . (int) $movedCategory['id_parent'])
&& Db::getInstance()->execute('
UPDATE `' . _DB_PREFIX_ . 'category` c ' . Shop::addSqlAssociation('category', 'c') . '
SET c.`position` = ' . (int) $position . ',
Expand Down Expand Up @@ -2224,30 +2227,44 @@ public static function getTopCategory($idLang = null)
*/
public function addPosition($position, $idShop = null)
{
$position = (int) $position;
$return = true;
if (null === $idShop) {

if (null !== $idShop) {
$shopIds = [(int) $idShop];
} else {
if (Shop::getContext() != Shop::CONTEXT_SHOP) {
foreach (Shop::getContextListShopID() as $idShop) {
$return &= Db::getInstance()->execute('
INSERT INTO `' . _DB_PREFIX_ . 'category_shop` (`id_category`, `id_shop`, `position`) VALUES
(' . (int) $this->id . ', ' . (int) $idShop . ', ' . (int) $position . ')
ON DUPLICATE KEY UPDATE `position` = ' . (int) $position);
}
$shopIds = Shop::getContextListShopID();
} else {
$id = Context::getContext()->shop->id;
$idShop = $id ? $id : Configuration::get('PS_SHOP_DEFAULT');
$return &= Db::getInstance()->execute('
INSERT INTO `' . _DB_PREFIX_ . 'category_shop` (`id_category`, `id_shop`, `position`) VALUES
(' . (int) $this->id . ', ' . (int) $idShop . ', ' . (int) $position . ')
ON DUPLICATE KEY UPDATE `position` = ' . (int) $position);
$shopIds = [$id ? $id : Configuration::get('PS_SHOP_DEFAULT')];
}
} else {
$return &= Db::getInstance()->execute('
INSERT INTO `' . _DB_PREFIX_ . 'category_shop` (`id_category`, `id_shop`, `position`) VALUES
(' . (int) $this->id . ', ' . (int) $idShop . ', ' . (int) $position . ')
ON DUPLICATE KEY UPDATE `position` = ' . (int) $position);
}

foreach ($shopIds as $idShop) {
$return &= Db::getInstance()->execute(
sprintf(
'INSERT INTO `' . _DB_PREFIX_ . 'category_shop` ' .
'(`id_category`, `id_shop`, `position`) VALUES ' .
'(%d, %d, %d) ' .
'ON DUPLICATE KEY UPDATE `position` = %d',
(int) $this->id,
(int) $idShop,
$position,
$position
)
);
}

$return &= Db::getInstance()->execute(
sprintf(
'UPDATE `' . _DB_PREFIX_ . 'category` c ' .
'SET c.`position`= %d WHERE c.id_category = %d',
$position,
(int) $this->id
)
);

return $return;
}

Expand Down

0 comments on commit 1fb8008

Please sign in to comment.