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

Fix position problem on a fresh install #14529

Merged
merged 1 commit into from Jul 10, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 43 additions & 26 deletions classes/Category.php
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