From 3c2af0ca2c36c01ce4e2103d5a149468f96a3200 Mon Sep 17 00:00:00 2001 From: lLefevre Date: Wed, 7 Dec 2011 17:30:32 +0000 Subject: [PATCH] // fix norm && added PDO::FETCH_ASSOC in method fetch() for PDO --- classes/Image.php | 175 ++++++++++++++++++---------------- classes/Product.php | 18 ++-- classes/db/DbPDO.php | 2 +- modules/statsstock/config.xml | 2 +- 4 files changed, 105 insertions(+), 92 deletions(-) diff --git a/classes/Image.php b/classes/Image.php index 0c545ec26..a2000ee43 100644 --- a/classes/Image.php +++ b/classes/Image.php @@ -75,7 +75,7 @@ class ImageCore extends ObjectModel protected static $_cacheGetSize = array(); - public function __construct($id = NULL, $id_lang = NULL) + public function __construct($id = null, $id_lang = null) { parent::__construct($id, $id_lang); $this->image_dir = _PS_PROD_IMG_DIR_; @@ -85,9 +85,9 @@ class ImageCore extends ObjectModel public function getFields() { $this->validateFields(); - $fields['id_product'] = (int)($this->id_product); - $fields['position'] = (int)($this->position); - $fields['cover'] = (int)($this->cover); + $fields['id_product'] = (int)$this->id_product; + $fields['position'] = (int)$this->position; + $fields['cover'] = (int)$this->cover; return $fields; } @@ -114,17 +114,23 @@ class ImageCore extends ObjectModel // update positions $result = Db::getInstance()->executeS(' - SELECT * - FROM `'._DB_PREFIX_.'image` - WHERE `id_product` = '.(int)$this->id_product.' - ORDER BY `position`'); + SELECT * + FROM `'._DB_PREFIX_.'image` + WHERE `id_product` = '.(int)$this->id_product.' + ORDER BY `position` + '); $i = 1; if ($result) - foreach ($result AS $row) - { - $row['position'] = $i++; - Db::getInstance()->AutoExecute(_DB_PREFIX_.$this->table, $row, 'UPDATE', '`id_image` = '.(int)($row['id_image']), 1); - } + foreach ($result as $row) + { + $row['position'] = $i++; + Db::getInstance()->AutoExecute( + _DB_PREFIX_.$this->table, + $row, + 'UPDATE', + '`id_image` = '.(int)$row['id_image'], 1 + ); + } return true; } @@ -170,7 +176,7 @@ class ImageCore extends ObjectModel $result = Db::getInstance()->getRow(' SELECT COUNT(`id_image`) AS total FROM `'._DB_PREFIX_.'image` - WHERE `id_product` = '.(int)($id_product)); + WHERE `id_product` = '.(int)$id_product); return $result['total']; } @@ -185,7 +191,7 @@ class ImageCore extends ObjectModel $result = Db::getInstance()->getRow(' SELECT MAX(`position`) AS max FROM `'._DB_PREFIX_.'image` - WHERE `id_product` = '.(int)($id_product)); + WHERE `id_product` = '.(int)$id_product); return $result['max']; } @@ -202,10 +208,12 @@ class ImageCore extends ObjectModel if (file_exists(_PS_TMP_IMG_DIR_.'product_'.$id_product.'.jpg')) unlink(_PS_TMP_IMG_DIR_.'product_'.$id_product.'.jpg'); + return Db::getInstance()->execute(' - UPDATE `'._DB_PREFIX_.'image` - SET `cover` = 0 - WHERE `id_product` = '.(int)($id_product)); + UPDATE `'._DB_PREFIX_.'image` + SET `cover` = 0 + WHERE `id_product` = '.(int)$id_product + ); } /** @@ -217,9 +225,10 @@ class ImageCore extends ObjectModel public static function getCover($id_product) { return Db::getInstance()->getRow(' - SELECT * FROM `'._DB_PREFIX_.'image` - WHERE `id_product` = '.(int)($id_product).' - AND `cover`= 1'); + SELECT * FROM `'._DB_PREFIX_.'image` + WHERE `id_product` = '.(int)$id_product.' + AND `cover`= 1 + '); } /** @@ -228,51 +237,51 @@ class ImageCore extends ObjectModel * @param integer $id_product_old Source product ID * @param boolean $id_product_new Destination product ID */ - public static function duplicateProductImages($id_product_old, $id_product_new, $combinationImages) + public static function duplicateProductImages($id_product_old, $id_product_new, $combination_images) { - $imagesTypes = ImageType::getImagesTypes('products'); + $images_types = ImageType::getImagesTypes('products'); $result = Db::getInstance()->executeS(' SELECT `id_image` FROM `'._DB_PREFIX_.'image` - WHERE `id_product` = '.(int)($id_product_old)); + WHERE `id_product` = '.(int)$id_product_old); foreach ($result as $row) { - $imageOld = new Image($row['id_image']); - $imageNew = clone $imageOld; - $imageNew->id_product = (int)($id_product_new); + $image_old = new Image($row['id_image']); + $image_new = clone $image_old; + $image_new->id_product = (int)$id_product_new; // A new id is generated for the cloned image when calling add() - if ($imageNew->add()) + if ($image_new->add()) { - $new_path = $imageNew->getPathForCreation(); - foreach ($imagesTypes AS $imageType) + $new_path = $image_new->getPathForCreation(); + foreach ($images_types as $image_type) { - if (file_exists(_PS_PROD_IMG_DIR_.$imageOld->getExistingImgPath().'-'.$imageType['name'].'.jpg')) + if (file_exists(_PS_PROD_IMG_DIR_.$image_old->getExistingImgPath().'-'.$image_type['name'].'.jpg')) { if (!Configuration::get('PS_LEGACY_IMAGES')) - $imageNew->createImgFolder(); - copy(_PS_PROD_IMG_DIR_.$imageOld->getExistingImgPath().'-'.$imageType['name'].'.jpg', - $new_path.'-'.$imageType['name'].'.jpg'); + $image_new->createImgFolder(); + copy(_PS_PROD_IMG_DIR_.$image_old->getExistingImgPath().'-'.$image_type['name'].'.jpg', + $new_path.'-'.$image_type['name'].'.jpg'); } } - if (file_exists(_PS_PROD_IMG_DIR_.$imageOld->getExistingImgPath().'.jpg')) - copy(_PS_PROD_IMG_DIR_.$imageOld->getExistingImgPath().'.jpg', $new_path.'.jpg'); + if (file_exists(_PS_PROD_IMG_DIR_.$image_old->getExistingImgPath().'.jpg')) + copy(_PS_PROD_IMG_DIR_.$image_old->getExistingImgPath().'.jpg', $new_path.'.jpg'); - self::replaceAttributeImageAssociationId($combinationImages, (int)($imageOld->id), (int)($imageNew->id)); + self::replaceAttributeImageAssociationId($combination_images, (int)$image_old->id, (int)$image_new->id); } else return false; } - return self::duplicateAttributeImageAssociations($combinationImages); + return self::duplicateAttributeImageAssociations($combination_images); } - static protected function replaceAttributeImageAssociationId(&$combinationImages, $saved_id, $id_image) + protected static function replaceAttributeImageAssociationId(&$combination_images, $saved_id, $id_image) { - if (!isset($combinationImages['new']) OR !is_array($combinationImages['new'])) - return ; - foreach ($combinationImages['new'] AS $id_product_attribute => $imageIds) - foreach ($imageIds AS $key => $imageId) - if ((int)($imageId) == (int)($saved_id)) - $combinationImages['new'][$id_product_attribute][$key] = (int)($id_image); + if (!isset($combination_images['new']) || !is_array($combination_images['new'])) + return; + foreach ($combination_images['new'] as $id_product_attribute => $image_ids) + foreach ($image_ids as $key => $image_id) + if ((int)$image_id == (int)$saved_id) + $combination_images['new'][$id_product_attribute][$key] = (int)$id_image; } /** @@ -280,14 +289,14 @@ class ImageCore extends ObjectModel * @param integer $id_product_attribute_old * @return boolean */ - public static function duplicateAttributeImageAssociations($combinationImages) + public static function duplicateAttributeImageAssociations($combination_images) { - if (!isset($combinationImages['new']) OR !is_array($combinationImages['new'])) + if (!isset($combination_images['new']) || !is_array($combination_images['new'])) return true; $query = 'INSERT INTO `'._DB_PREFIX_.'product_attribute_image` (`id_product_attribute`, `id_image`) VALUES '; - foreach ($combinationImages['new'] AS $id_product_attribute => $imageIds) - foreach ($imageIds AS $imageId) - $query .= '('.(int)($id_product_attribute).', '.(int)($imageId).'), '; + foreach ($combination_images['new'] as $id_product_attribute => $image_ids) + foreach ($image_ids as $image_id) + $query .= '('.(int)$id_product_attribute.', '.(int)$image_id.'), '; $query = rtrim($query, ', '); return DB::getInstance()->Execute($query); } @@ -303,28 +312,28 @@ class ImageCore extends ObjectModel { Tools::displayAsDeprecated(); - $position = (int)($position); - $direction = (int)($direction); + $position = (int)$position; + $direction = (int)$direction; // temporary position $high_position = Image::getHighestPosition($this->id_product) + 1; Db::getInstance()->execute(' UPDATE `'._DB_PREFIX_.'image` - SET `position` = '.(int)($high_position).' - WHERE `id_product` = '.(int)($this->id_product).' + SET `position` = '.(int)$high_position.' + WHERE `id_product` = '.(int)$this->id_product.' AND `position` = '.($direction ? $position - 1 : $position + 1)); Db::getInstance()->execute(' UPDATE `'._DB_PREFIX_.'image` SET `position` = `position`'.($direction ? '-1' : '+1').' - WHERE `id_image` = '.(int)($this->id)); + WHERE `id_image` = '.(int)$this->id); Db::getInstance()->execute(' UPDATE `'._DB_PREFIX_.'image` SET `position` = '.$this->position.' - WHERE `id_product` = '.(int)($this->id_product).' - AND `position` = '.(int)($high_position)); + WHERE `id_product` = '.(int)$this->id_product.' + AND `position` = '.(int)$high_position); } /** @@ -346,21 +355,25 @@ class ImageCore extends ObjectModel 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)) + ? '> '.(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))); + 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) - self::$_cacheGetSize[$type] = Db::getInstance()->getRow('SELECT `width`, `height` FROM '._DB_PREFIX_.'image_type WHERE `name` = \''.pSQL($type).'\''); + if (!isset(self::$_cacheGetSize[$type]) || self::$_cacheGetSize[$type] === null) + self::$_cacheGetSize[$type] = Db::getInstance()->getRow(' + SELECT `width`, `height` + FROM '._DB_PREFIX_.'image_type + WHERE `name` = \''.pSQL($type).'\' + '); return self::$_cacheGetSize[$type]; } @@ -369,7 +382,7 @@ class ImageCore extends ObjectModel */ public static function clearTmpDir() { - foreach (scandir(_PS_TMP_IMG_DIR_) AS $d) + foreach (scandir(_PS_TMP_IMG_DIR_) as $d) if (preg_match('/(.*)\.jpg$/', $d)) unlink(_PS_TMP_IMG_DIR_.$d); } @@ -381,9 +394,9 @@ class ImageCore extends ObjectModel return Db::getInstance()->execute(' DELETE FROM `'._DB_PREFIX_.'product_attribute_image` - WHERE `id_image` = '.(int)($this->id) + WHERE `id_image` = '.(int)$this->id ); -} + } /** * Delete the product image from disk and remove the containing folder if empty @@ -403,9 +416,9 @@ class ImageCore extends ObjectModel $files_to_delete = array(); // Delete auto-generated images - $imageTypes = ImageType::getImagesTypes(); - foreach ($imageTypes AS $imageType) - $files_to_delete[] = $this->image_dir.$this->getExistingImgPath().'-'.$imageType['name'].'.'.$this->image_format; + $image_types = ImageType::getImagesTypes(); + foreach ($image_types as $image_type) + $files_to_delete[] = $this->image_dir.$this->getExistingImgPath().'-'.$image_type['name'].'.'.$this->image_format; // Delete watermark image $files_to_delete[] = $this->image_dir.$this->getExistingImgPath().'-watermark.'.$this->image_format; @@ -443,7 +456,7 @@ class ImageCore extends ObjectModel * @param string $format image format * @return bool success */ - public static function deleteAllImages($path, $format='jpg') + public static function deleteAllImages($path, $format = 'jpg') { if (!$path || !$format || !is_dir($path)) return false; @@ -460,7 +473,7 @@ class ImageCore extends ObjectModel { $remove_folder = true; foreach (scandir($path) as $file) - if (($file != '.' && $file != '..'&& $file != 'index.php')) + if (($file != '.' && $file != '..' && $file != 'index.php')) { $remove_folder = false; break; @@ -590,7 +603,7 @@ class ImageCore extends ObjectModel if (!$image || $image->id !== (int)$matches[2]) $image = new Image((int)$matches[2]); // image exists in DB and with the correct product? - if (Validate::isLoadedObject($image) && $image->id_product == (int)rtrim($matches[1], "-")) + if (Validate::isLoadedObject($image) && $image->id_product == (int)rtrim($matches[1], '-')) { // create the new folder if it does not exist if (!$image->createImgFolder()) @@ -600,16 +613,16 @@ class ImageCore extends ObjectModel // most likely the preexisting image is a demo image not linked to a product and it's ok to replace it $new_path = _PS_PROD_IMG_DIR_.$image->getImgPath().(isset($matches[3]) ? $matches[3] : '').'.jpg'; if (file_exists($new_path)) + { + if (!file_exists(_PS_PROD_IMG_DIR_.$tmp_folder)) { - if (!file_exists(_PS_PROD_IMG_DIR_.$tmp_folder)) - { - @mkdir(_PS_PROD_IMG_DIR_.$tmp_folder, self::$access_rights); - @chmod(_PS_PROD_IMG_DIR_.$tmp_folder, self::$access_rights); - } - $tmp_path = _PS_PROD_IMG_DIR_.$tmp_folder.basename($file); - if (!@rename($new_path, $tmp_path) || !file_exists($tmp_path)) - return false; + @mkdir(_PS_PROD_IMG_DIR_.$tmp_folder, self::$access_rights); + @chmod(_PS_PROD_IMG_DIR_.$tmp_folder, self::$access_rights); } + $tmp_path = _PS_PROD_IMG_DIR_.$tmp_folder.basename($file); + if (!@rename($new_path, $tmp_path) || !file_exists($tmp_path)) + return false; + } // move the image if (!@rename(_PS_PROD_IMG_DIR_.$file, $new_path) || !file_exists($new_path)) return false; diff --git a/classes/Product.php b/classes/Product.php index b78ca3c3c..d97919cac 100644 --- a/classes/Product.php +++ b/classes/Product.php @@ -677,7 +677,6 @@ class ProductCore extends ObjectModel return false; Hook::exec('deleteProduct', array('product' => $this)); - if (!parent::delete() || !$this->deleteCategories(true) || !$this->deleteImages() || @@ -888,17 +887,18 @@ class ProductCore extends ObjectModel public function deleteImages() { $result = Db::getInstance()->executeS(' - SELECT `id_image` - FROM `'._DB_PREFIX_.'image` - WHERE `id_product` = '.(int)$this->id); + SELECT `id_image` + FROM `'._DB_PREFIX_.'image` + WHERE `id_product` = '.(int)$this->id + ); $status = true; if ($result) - foreach ($result as $row) - { - $image = new Image($row['id_image']); - $status &= $image->delete(); - } + foreach ($result as $row) + { + $image = new Image($row['id_image']); + $status &= $image->delete(); + } return $status; } diff --git a/classes/db/DbPDO.php b/classes/db/DbPDO.php index df8d26283..96f8fc0dc 100644 --- a/classes/db/DbPDO.php +++ b/classes/db/DbPDO.php @@ -76,7 +76,7 @@ class DbPDOCore extends Db { if (!$result) $result = $this->result; - return $result->fetch(); + return $result->fetch(PDO::FETCH_ASSOC); } /** diff --git a/modules/statsstock/config.xml b/modules/statsstock/config.xml index 9d52db698..eab1e6071 100755 --- a/modules/statsstock/config.xml +++ b/modules/statsstock/config.xml @@ -1,7 +1,7 @@ statsstock - +