From 3e268db6f9042fcc0ecefecf4e0dda8f4558c1a2 Mon Sep 17 00:00:00 2001 From: rMalie Date: Thu, 10 Nov 2011 13:45:41 +0000 Subject: [PATCH] [*] Classes: CMS::getCMSPages() now select only data from cms table if id_lang is null [*] Classes: Db::autoExecute() can now take INSERT IGNORE as third argument [*] Classes: Tag::addTags() can now take an array of tags as argument (instead of strings separated by comas) git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@10033 b9a71923-0436-4b27-9f14-aed3839534dd --- classes/CMS.php | 25 ++++++++++++++----------- classes/Tag.php | 13 +++++++------ classes/db/Db.php | 9 +++++---- 3 files changed, 26 insertions(+), 21 deletions(-) diff --git a/classes/CMS.php b/classes/CMS.php index 02c7ac83f..c9f5e2f47 100644 --- a/classes/CMS.php +++ b/classes/CMS.php @@ -127,9 +127,9 @@ class CMSCore extends ObjectModel public function updatePosition($way, $position) { if (!$res = Db::getInstance()->executeS(' - SELECT cp.`id_cms`, cp.`position`, cp.`id_cms_category` + SELECT cp.`id_cms`, cp.`position`, cp.`id_cms_category` FROM `'._DB_PREFIX_.'cms` cp - WHERE cp.`id_cms_category` = '.(int)$this->id_cms_category.' + WHERE cp.`id_cms_category` = '.(int)$this->id_cms_category.' ORDER BY cp.`position` ASC' )) return false; @@ -146,7 +146,7 @@ class CMSCore extends ObjectModel return (Db::getInstance()->execute(' UPDATE `'._DB_PREFIX_.'cms` SET `position`= `position` '.($way ? '- 1' : '+ 1').' - WHERE `position` + WHERE `position` '.($way ? '> '.(int)($movedCms['position']).' AND `position` <= '.(int)($position) : '< '.(int)($movedCms['position']).' AND `position` >= '.(int)($position)).' @@ -183,14 +183,17 @@ class CMSCore extends ObjectModel public static function getCMSPages($id_lang = null, $id_cms_category = null, $active = true) { - return Db::getInstance()->executeS(' - SELECT * - FROM `'._DB_PREFIX_.'cms` c - JOIN `'._DB_PREFIX_.'cms_lang` l ON (c.id_cms = l.id_cms)'. - (isset($id_cms_category) ? 'WHERE `id_cms_category` = '.(int)($id_cms_category) : ''). - ($active ? ' AND c.`active` = 1 ' : '').' - AND l.id_lang = '.(int)($id_lang).' - ORDER BY `position`'); + $sql = new DbQuery(); + $sql->select('*'); + $sql->from('cms c'); + if ($id_lang) + $sql->innerJoin('cms_lang l ON c.id_cms = l.id_cms AND l.id_lang = '.(int)$id_lang); + + if ($active) + $sql->where('c.active = 1'); + $sql->orderBy('position'); + + return Db::getInstance()->executeS($sql); } public static function getUrlRewriteInformations($id_cms) diff --git a/classes/Tag.php b/classes/Tag.php index b3f60917b..0e5712c28 100644 --- a/classes/Tag.php +++ b/classes/Tag.php @@ -87,18 +87,19 @@ class TagCore extends ObjectModel * * @param integer $id_lang Language id * @param integer $id_product Product id to link tags with - * @param string $string Tags separated by commas - * + * @param string|array $tag_list List of tags, as array or as a string with comas * @return boolean Operation success */ - public static function addTags($id_lang, $id_product, $string) + public static function addTags($id_lang, $id_product, $tag_list) { - if (!Validate::isUnsignedId($id_lang) || !Validate::isTagsList($string)) + if (!Validate::isUnsignedId($id_lang)) return false; - $tmp_tab = array_unique(array_map('trim', preg_split('/,/', $string, null, PREG_SPLIT_NO_EMPTY))); + if (!is_array($tag_list)) + $tag_list = array_unique(array_map('trim', preg_split('/,/', $tag_list, null, PREG_SPLIT_NO_EMPTY))); + $list = array(); - foreach ($tmp_tab as $tag) + foreach ($tag_list as $tag) { if (!Validate::isGenericName($tag)) return false; diff --git a/classes/db/Db.php b/classes/db/Db.php index a09642a88..d391c13ac 100644 --- a/classes/db/Db.php +++ b/classes/db/Db.php @@ -246,7 +246,7 @@ abstract class DbCore * * @param string $table Table where insert/update data * @param string $data Data to insert/update - * @param string $type INSERT or UPDATE + * @param string $type INSERT or INSERT IGNORE or UPDATE * @param string $where WHERE clause, only for UPDATE (optional) * @param string $limit LIMIT clause (optional) * @param bool $use_null If true, replace empty strings and NULL by a NULL value @@ -257,7 +257,8 @@ abstract class DbCore if (!$data) return true; - if (strtoupper($type) == 'INSERT') + $type = strtoupper($type); + if ($type == 'INSERT' || $type == 'INSERT IGNORE') { // Check if $data is a list of row $current = current($data); @@ -291,12 +292,12 @@ abstract class DbCore $values_stringified[] = '('.implode(', ', $values).')'; } - $sql = 'INSERT INTO `'.$table.'` ('.$keys_stringified.') VALUES '.implode(', ', $values_stringified); + $sql = $type.' INTO `'.$table.'` ('.$keys_stringified.') VALUES '.implode(', ', $values_stringified); if ($limit) $sql .= ' LIMIT '.(int)$limit; return $this->q($sql, $use_cache); } - else if (strtoupper($type) == 'UPDATE') + else if ($type == 'UPDATE') { $sql = 'UPDATE `'.$table.'` SET '; foreach ($data as $key => $value)