diff --git a/admin-dev/ajax.php b/admin-dev/ajax.php index d58755762..3d0b4175f 100644 --- a/admin-dev/ajax.php +++ b/admin-dev/ajax.php @@ -335,6 +335,35 @@ if (array_key_exists('ajaxProductsPositions', $_POST)) } } +if (array_key_exists('ajaxProductImagesPositions', $_POST)) +{ + $id_image = (int)(Tools::getValue('id_image')); + $way = (int)(Tools::getValue('way')); + $positions = Tools::getValue('imageTable'); + + if (is_array($positions)) + foreach ($positions AS $key => $value) + { + $pos = explode('_', $value); + if ((isset($pos[1])) AND ($pos[1] == $id_image)) + { + $position = $key; + break; + } + } + $image = new Image($id_image); + if (Validate::isLoadedObject($image)) + { + if (isset($position) && $image->updatePosition($way, $position)) + die(true); + else + die('{"hasError" : true, "errors" : "Cannot update image position"}'); + } + else + die('{"hasError" : true, "errors" : "This image cannot be loaded"}'); +} + + if (isset($_GET['ajaxProductPackItems'])) { $jsonArray = array(); diff --git a/admin-dev/tabs/AdminProducts.php b/admin-dev/tabs/AdminProducts.php index d0122b8d8..713b83182 100644 --- a/admin-dev/tabs/AdminProducts.php +++ b/admin-dev/tabs/AdminProducts.php @@ -447,7 +447,7 @@ class AdminProducts extends AdminTab /* Choose product image position */ elseif (isset($_GET['imgPosition']) AND isset($_GET['imgDirection'])) { - $image->positionImage((int)(Tools::getValue('imgPosition')), (int)(Tools::getValue('imgDirection'))); + $image->updatePosition(Tools::getValue('imgDirection'), Tools::getValue('imgPosition')); Tools::redirectAdmin(self::$currentIndex.'&id_product='.$image->id_product.'&id_category='.(!empty($_REQUEST['id_category'])?$_REQUEST['id_category']:'1').'&add'.$this->table.'&tabs=1&token='.($token ? $token : $this->token)); } } @@ -3061,9 +3061,16 @@ class AdminProducts extends AdminTab
- - - +
+ + + + + @@ -3094,7 +3101,7 @@ class AdminProducts extends AdminTab echo ' - '; + '; echo $this->_positionJS(); foreach ($images AS $k => $image) @@ -3138,15 +3145,14 @@ class AdminProducts extends AdminTab $imgObj = new Image((int)$image['id_image']); $image_obj = new Image($image['id_image']); $img_path = $image_obj->getExistingImgPath(); - $html = ' - + - '; if (Shop::isMultiShopActivated()) diff --git a/classes/Image.php b/classes/Image.php index 853e4b4f1..2b95039bc 100644 --- a/classes/Image.php +++ b/classes/Image.php @@ -283,9 +283,12 @@ class ImageCore extends ObjectModel * * @param integer $position Position * @param boolean $direction Direction + * @deprecated since version 1.5.0.1 use Image::updatePosition() instead */ public function positionImage($position, $direction) { + Tools::displayAsDeprecated(); + $position = (int)($position); $direction = (int)($direction); @@ -310,6 +313,36 @@ class ImageCore extends ObjectModel AND `position` = '.(int)($high_position)); } + /** + * Change an image position and update relative positions + * + * @param int $way position is moved up if 0, moved down if 1 + * @param int $position new position of the moved image + * @return int success + */ + public function updatePosition($way, $position) + { + if (!isset($this->id) || !$position) + return false; + + // < and > statements rather than BETWEEN operator + // since BETWEEN is treated differently according to databases + $result = (Db::getInstance()->Execute(' + UPDATE `'._DB_PREFIX_.'image` + SET `position`= `position` '.($way ? '- 1' : '+ 1').' + WHERE `position` + '.($way + ? '> '.(int)($this->position).' AND `position` <= '.(int)($position) + : '< '.(int)($this->position).' AND `position` >= '.(int)($position)).' + AND `id_product`='.(int)($this->id_product)) + && Db::getInstance()->Execute(' + UPDATE `'._DB_PREFIX_.'image` + SET `position` = '.(int)($position).' + WHERE `id_image` = '.(int)($this->id_image))); + + return $result; + } + public static function getSize($type) { if (!isset(self::$_cacheGetSize[$type]) OR self::$_cacheGetSize[$type] === NULL) diff --git a/install-dev/sql/db.sql b/install-dev/sql/db.sql index c1f89507f..66fb70793 100644 --- a/install-dev/sql/db.sql +++ b/install-dev/sql/db.sql @@ -757,7 +757,6 @@ CREATE TABLE `PREFIX_image` ( `cover` tinyint(1) unsigned NOT NULL default '0', PRIMARY KEY (`id_image`), KEY `image_product` (`id_product`), - UNIQUE KEY `product_position` (`id_product`,`position`), KEY `id_product_cover` (`id_product`,`cover`) ) ENGINE=ENGINE_TYPE DEFAULT CHARSET=utf8; diff --git a/install-dev/sql/upgrade/1.5.0.1.sql b/install-dev/sql/upgrade/1.5.0.1.sql index 951d1b347..1fcf291e3 100644 --- a/install-dev/sql/upgrade/1.5.0.1.sql +++ b/install-dev/sql/upgrade/1.5.0.1.sql @@ -75,3 +75,6 @@ INSERT INTO `PREFIX_configuration` (`name`, `value`, `date_add`, `date_upd`) VAL ALTER TABLE `PREFIX_product` ADD `available_date` DATETIME NOT NULL AFTER `available_for_order`; ALTER TABLE `PREFIX_product_attribute` ADD `available_date` DATETIME NOT NULL; + +/* Index was only used by deprecated function Image::positionImage() */ +ALTER TABLE `PREFIX_image` DROP INDEX `product_position`; diff --git a/js/admin-dnd.js b/js/admin-dnd.js index d55c95f4f..8ca9bcf75 100644 --- a/js/admin-dnd.js +++ b/js/admin-dnd.js @@ -84,7 +84,15 @@ $(document).ready(function() { token: token }; } - + if (table.id == 'imageTable') { + params = { + ajaxProductImagesPositions: true, + id_image: ids[1], + way: way, + token: token + }; + } + $.ajax({ type: 'POST', async: false,
'.$this->l('Image').'  '.$this->l('Cover').' '.$this->l('Action').'
'.htmlentities(stripslashes($image['legend']), ENT_COMPAT, 'UTF-8').' '.(int)($image['position']).''; + '; if ($image['position'] == 1) { $html .= ' @@ -3156,16 +3162,16 @@ class AdminProducts extends AdminTab [ ]'; else $html .= ' - [ ]'; + [ ]'; } elseif ($image['position'] == $imagesTotal) $html .= ' - [ ] + [ ] [ ]'; else $html .= ' - [ ] - [ ]'; + [ ] + [ ]'; $html .= '