diff --git a/admin-dev/tabs/AdminModulesPositions.php b/admin-dev/tabs/AdminModulesPositions.php index 740768bbf..c3b254059 100644 --- a/admin-dev/tabs/AdminModulesPositions.php +++ b/admin-dev/tabs/AdminModulesPositions.php @@ -258,7 +258,7 @@ class AdminModulesPositions extends AdminTab else echo '

'.$this->l('By clicking here you will be redirected to the front office of your shop to move and delete modules directly.').'


- '.$this->l('Run LiveEdit').''; + '.$this->l('Run LiveEdit').''; echo ''; // Print hook list diff --git a/admin-dev/tabs/AdminPayment.php b/admin-dev/tabs/AdminPayment.php index 808f2e3ee..a24e89251 100644 --- a/admin-dev/tabs/AdminPayment.php +++ b/admin-dev/tabs/AdminPayment.php @@ -84,7 +84,7 @@ class AdminPayment extends AdminTab private function saveRestrictions($type) { - Db::getInstance()->Execute('DELETE FROM '._DB_PREFIX_.'module_'.$type.' WHERE id_shop='.Context::getContext()->shop->getID()); + Db::getInstance()->Execute('DELETE FROM '._DB_PREFIX_.'module_'.$type.' WHERE id_shop = '.Context::getContext()->shop->getID(true)); foreach ($this->paymentModules as $module) if ($module->active AND isset($_POST[$module->name.'_'.$type.''])) foreach ($_POST[$module->name.'_'.$type.''] as $selected) diff --git a/admin-dev/tabs/AdminProducts.php b/admin-dev/tabs/AdminProducts.php index 6f555238f..1f7bbc05b 100644 --- a/admin-dev/tabs/AdminProducts.php +++ b/admin-dev/tabs/AdminProducts.php @@ -694,7 +694,7 @@ class AdminProducts extends AdminTab if ($this->_validateSpecificPrice($id_shops[$key], $id_currencies[$key], $id_countries[$key], $id_groups[$key], $prices[$key], $from_quantities[$key], $reductions[$key], $reduction_types[$key], $froms[$key], $tos[$key])) { $specificPrice = new SpecificPrice((int)($id_specific_price)); - $specificPrice->id_shop = (int)($id_shops[$key]); + $specificPrice->id_shop = (int)$id_shops[$key]; $specificPrice->id_currency = (int)($id_currencies[$key]); $specificPrice->id_country = (int)($id_countries[$key]); $specificPrice->id_group = (int)($id_groups[$key]); @@ -732,7 +732,7 @@ class AdminProducts extends AdminTab { $specificPrice = new SpecificPrice(); $specificPrice->id_product = $id_product; - $specificPrice->id_shop = (int)($id_shop); + $specificPrice->id_shop = (int)$id_shop; $specificPrice->id_currency = (int)($id_currency); $specificPrice->id_country = (int)($id_country); $specificPrice->id_group = (int)($id_group); diff --git a/admin-dev/tabs/AdminStockMvt.php b/admin-dev/tabs/AdminStockMvt.php index dcbe56e4f..7f0ad05dc 100755 --- a/admin-dev/tabs/AdminStockMvt.php +++ b/admin-dev/tabs/AdminStockMvt.php @@ -48,7 +48,7 @@ class AdminStockMvt extends AdminTab $this->_select = 'CONCAT(pl.name, \' \', GROUP_CONCAT(IFNULL(al.name, \'\'), \'\')) product_name, CONCAT(e.lastname, \' \', e.firstname) employee, mrl.name reason'; $this->_join = 'INNER JOIN '._DB_PREFIX_.'stock stock ON a.id_stock = stock.id_stock '.$context->shop->sqlSharedStock('stock').' - LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (stock.id_product = pl.id_product AND pl.id_lang = '.(int)$context->language->id.' AND pl.id_shop = '.$context->shop->getID(true).') + LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (stock.id_product = pl.id_product AND pl.id_lang = '.(int)$context->language->id.$context->shop->sqlLang('pl').') LEFT JOIN `'._DB_PREFIX_.'stock_mvt_reason_lang` mrl ON (a.id_stock_mvt_reason = mrl.id_stock_mvt_reason AND mrl.id_lang = '.(int)$context->language->id.') LEFT JOIN `'._DB_PREFIX_.'employee` e ON (e.id_employee = a.id_employee) LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON (pac.id_product_attribute = stock.id_product_attribute) diff --git a/classes/Cart.php b/classes/Cart.php index 6b666c40b..e494b128b 100644 --- a/classes/Cart.php +++ b/classes/Cart.php @@ -1255,9 +1255,8 @@ class CartCore extends ObjectModel return Tools::displayError('This voucher is not yet valid'); if (strtotime($discountObj->date_to) < time()) return Tools::displayError('This voucher has expired.'); - if (!$context->shop->inGlobalContext()) - if (!$discountObj->availableWithShop($context)) - return Tools::displayError('This voucher is not available with this shop.'); + if (!$discountObj->availableWithShop($context->shop)) + return Tools::displayError('This voucher is not available with this shop.'); if (sizeof($discounts) >= 1 AND $checkCartDiscount) { if (!$discountObj->cumulable) diff --git a/classes/Context.php b/classes/Context.php index 5b8d8ea0b..f45186619 100644 --- a/classes/Context.php +++ b/classes/Context.php @@ -30,43 +30,69 @@ */ class ContextCore { - /** @var Context */ + /** + * @var Context + */ protected static $instance; - /** @var Cart */ + /** + * @var Cart + */ public $cart; - /** @var Customer */ + /** + * @var Customer + */ public $customer; - /** @var Cookie */ + /** + * @var Cookie + */ public $cookie; - /** @var Link */ + /** + * @var Link + */ public $link; - /** @var Country */ + /** + * @var Country + */ public $country; - /** @var Employee */ + /** + * @var Employee + */ public $employee; - /** @var Controller */ + /** + * @var Controller + */ public $controller; - /** @var Language */ + /** + * @var Language + */ public $language; - /** @var Currency */ + /** + * @var Currency + */ public $currency; - /** @var AdminTab */ + /** + * @var AdminTab + */ public $tab; - /** @var Shop */ + /** + * @var Shop + */ public $shop; - /** @var Smarty */ + /** + * @var Smarty + */ public $smarty; /** diff --git a/classes/Currency.php b/classes/Currency.php index 0728cf217..2a335768b 100644 --- a/classes/Currency.php +++ b/classes/Currency.php @@ -231,10 +231,11 @@ class CurrencyCore extends ObjectModel if (is_null($id_shop)) $id_shop = Context::getContext()->shop->getID(); - return Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' - SELECT mc.* - FROM `'._DB_PREFIX_.'module_currency` mc - WHERE mc.`id_module` = '.(int)$id_module.' AND mc.`id_shop`='.(int)$id_shop); + $sql = 'SELECT * + FROM `'._DB_PREFIX_.'module_currency` + WHERE `id_module` = '.(int)$id_module.' + AND `id_shop`='.(int)$id_shop; + return Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql); } static public function getCurrency($id_currency) diff --git a/classes/Discount.php b/classes/Discount.php index 5f8fbeed7..c6e7e6ae4 100644 --- a/classes/Discount.php +++ b/classes/Discount.php @@ -276,7 +276,7 @@ class DiscountCore extends ObjectModel * @param boolean $order_total_products Total cart products amount * @return mixed Return a float value or '!' if reduction is 'Shipping free' */ - public function getValue($nb_discounts = 0, $order_total_products = 0, $shipping_fees = 0, $idCart = false, $useTax = true, $id_group_shop = false, $id_shop = false, Context $context = null) + public function getValue($nb_discounts = 0, $order_total_products = 0, $shipping_fees = 0, $idCart = false, $useTax = true, Context $context = null) { $totalAmount = 0; @@ -294,11 +294,10 @@ class DiscountCore extends ObjectModel $date_end = strtotime($this->date_to); if ((time() < $date_start OR time() > $date_end) AND !$cart->OrderExists()) return 0; - if ($id_group_shop AND $id_shop) - if (!$this->availableWithShop($id_group_shop, $id_shop)) - return 0; + if (!$this->availableWithShop(Context::getContext()->shop)) + return 0; $products = $cart->getProducts(); - $categories = Discount::getCategories((int)($this->id)); + $categories = Discount::getCategories((int)$this->id); $in_category = false; foreach ($products AS $product) @@ -511,16 +510,16 @@ class DiscountCore extends ObjectModel return Db::getInstance()->getRow('SELECT * FROM `'._DB_PREFIX_.'discount` WHERE `id_discount` = '.(int)$id_discount); } - public function availableWithShop(Context $context = null) + public function availableWithShop(Shop $shop = null) { - if (!$context) - $context = Context::getContext(); + if (!$shop) + $shop = Context::getContext()->shop; // @todo share datas on discount ? Utility of this function ? $sql = 'SELECT id_discount FROM '._DB_PREFIX_.'discount WHERE id_discount='.(int)$this->id - .$context->shop->sqlRestriction(true); + .$shop->sqlRestriction(true); return Db::getInstance()->getValue($sql); } } diff --git a/classes/Meta.php b/classes/Meta.php index e23ef10e6..73b45ef9e 100644 --- a/classes/Meta.php +++ b/classes/Meta.php @@ -95,29 +95,33 @@ class MetaCore extends ObjectModel ORDER BY page ASC'); } - static public function getMetasByIdLang($id_lang, $id_shop = false) + static public function getMetasByIdLang($id_lang, Context $context = null) { - if (!$id_shop) - $id_shop = (int)Configuration::get('PS_SHOP_DEFAULT'); - return Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' - SELECT * - FROM `'._DB_PREFIX_.'meta` m - LEFT JOIN `'._DB_PREFIX_.'meta_lang` ml ON m.`id_meta` = ml.`id_meta` - WHERE ml.`id_lang` = '.(int)($id_lang). - ($id_shop ? ' AND ml.id_shop='.(int)$id_shop : '').' - ORDER BY page ASC'); + if (!$context) + $context = Context::getContext(); + + $sql = 'SELECT * + FROM `'._DB_PREFIX_.'meta` m + LEFT JOIN `'._DB_PREFIX_.'meta_lang` ml ON m.`id_meta` = ml.`id_meta` + WHERE ml.`id_lang` = '.(int)$id_lang + .$context->shop->sqlLang('ml'). + 'ORDER BY page ASC'; + return Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql); } - static public function getMetaByPage($page, $id_lang, $id_shop = false) + static public function getMetaByPage($page, $id_lang, Context $context = null) { - if (!$id_shop) - $id_shop = (int)Configuration::get('PS_SHOP_DEFAULT'); - return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' - SELECT * - FROM '._DB_PREFIX_.'meta m - LEFT JOIN '._DB_PREFIX_.'meta_lang ml on (m.id_meta = ml.id_meta) - WHERE m.page = \''.pSQL($page).'\' AND ml.id_lang = '.(int)($id_lang)); + if (!$context) + $context = Context::getContext(); + + $sql = 'SELECT * + FROM '._DB_PREFIX_.'meta m + LEFT JOIN '._DB_PREFIX_.'meta_lang ml on (m.id_meta = ml.id_meta) + WHERE m.page = \''.pSQL($page).'\' + AND ml.id_lang = '.(int)$id_lang + .$context->shop->sqlLang('ml'); + return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql); } public function update($nullValues = false) diff --git a/classes/Module.php b/classes/Module.php index 7d7cb1bf8..0c912c383 100644 --- a/classes/Module.php +++ b/classes/Module.php @@ -804,8 +804,8 @@ abstract class ModuleCore LEFT JOIN `'._DB_PREFIX_.'hook` h ON hm.`id_hook` = h.`id_hook` WHERE h.`name` = \'payment\' AND mc.id_country = '.(int)($billing->id_country).' - AND mc.id_shop = '.$context->shop->getID().' - AND mg.id_shop = '.$context->shop->getID().' + AND mc.id_shop = '.$context->shop->getID(true).' + AND mg.id_shop = '.$context->shop->getID(true).' AND (SELECT COUNT(*) FROM '._DB_PREFIX_.'module_shop ms WHERE ms.id_module = m.id_module AND ms.id_shop IN('.implode(', ', $list).')) = '.count($list).' AND hm.id_shop IN('.implode(', ', $list).') GROUP BY hm.id_hook, hm.id_module @@ -895,8 +895,7 @@ abstract class ModuleCore */ public function updatePosition($id_hook, $way, $position = NULL) { - $list = $this->context->shop->getListOfID(); - foreach ($list as $shopID) + foreach ($this->context->shop->getListOfID() as $shopID) { $sql = 'SELECT hm.`id_module`, hm.`position`, hm.`id_hook` FROM `'._DB_PREFIX_.'hook_module` hm diff --git a/classes/ObjectModel.php b/classes/ObjectModel.php index 74d347d66..39ac5fcab 100644 --- a/classes/ObjectModel.php +++ b/classes/ObjectModel.php @@ -142,7 +142,7 @@ abstract class ObjectModelCore FROM `'._DB_PREFIX_.$this->table.'` a '. ($id_lang ? ('LEFT JOIN `'.pSQL(_DB_PREFIX_.$this->table).'_lang` b ON (a.`'.$this->identifier.'` = b.`'.$this->identifier).'` AND `id_lang` = '.(int)($id_lang).')' : '') .' WHERE 1 AND a.`'.$this->identifier.'` = '.(int)$id. - (($this->id_shop AND $id_lang) ? ' AND b.id_shop='.$this->id_shop : ''); + (($this->id_shop AND $id_lang) ? ' AND b.id_shop = '.$this->id_shop : ''); self::$_cache[$this->table][(int)($id)][(int)$id_shop][(int)$id_lang] = $db->getRow($sql); } @@ -768,7 +768,8 @@ abstract class ObjectModelCore $sql = 'SELECT id_shop FROM `'.pSQL(_DB_PREFIX_.$this->table).'_shop` - WHERE `'.$this->identifier.'`='.(int)$this->id.' AND id_shop='.(int)$id_shop; + WHERE `'.$this->identifier.'` = '.(int)$this->id.' + AND id_shop = '.(int)$id_shop; return (bool)Db::getInstance()->getValue($sql); } @@ -789,7 +790,7 @@ abstract class ObjectModelCore foreach ($id_shops as $id_shop) { - if (!$this->isAssociatedToShop((int)$id_shop)) + if (!$this->isAssociatedToShop($id_shop)) $sql .= '('.(int)$this->id.','.(int)$id_shop.'),'; } diff --git a/classes/PaymentModule.php b/classes/PaymentModule.php index 3d4a9d2c9..51113721d 100644 --- a/classes/PaymentModule.php +++ b/classes/PaymentModule.php @@ -88,7 +88,7 @@ abstract class PaymentModuleCore extends Module * @param string $paymentMethod Payment method (eg. 'Credit card') * @param string $message Message to attach to order */ - public function validateOrder($id_cart, $id_order_state, $amountPaid, $paymentMethod = 'Unknown', $message = NULL, $extraVars = array(), $currency_special = NULL, $dont_touch_amount = false, $secure_key = false, $id_group_shop = false, $id_shop = false) + public function validateOrder($id_cart, $id_order_state, $amountPaid, $paymentMethod = 'Unknown', $message = NULL, $extraVars = array(), $currency_special = NULL, $dont_touch_amount = false, $secure_key = false, Shop $shop = null) { $cart = new Cart((int)($id_cart)); // Does order already exists ? @@ -108,8 +108,8 @@ abstract class PaymentModuleCore extends Module $order->id_lang = (int)($cart->id_lang); $order->id_cart = (int)($cart->id); - $order->id_group_shop = (int)($id_group_shop ? $id_group_shop : $cart->id_group_shop); - $order->id_shop = (int)($id_shop ? $id_shop : $cart->id_shop); + $order->id_shop = (int)($shop->getID() ? $shop->getID() : $cart->id_shop); + $order->id_group_shop = (int)($shop->getID() ? $shop->getGroupID() : $cart->id_group_shop); $customer = new Customer((int)($order->id_customer)); $order->secure_key = ($secure_key ? pSQL($secure_key) : pSQL($customer->secure_key)); diff --git a/classes/Product.php b/classes/Product.php index 0953984a4..9589a5118 100644 --- a/classes/Product.php +++ b/classes/Product.php @@ -783,8 +783,11 @@ class ProductCore extends ObjectModel * @param string $orderWay Way for ordering (ASC or DESC) * @return array Products details */ - public static function getProducts($id_lang, $start, $limit, $orderBy, $orderWay, $id_category = false, $only_active = false, $id_shop = false) + public static function getProducts($id_lang, $start, $limit, $orderBy, $orderWay, $id_category = false, $only_active = false, Context $context = null) { + if (!$context) + $context = Context::getContext(); + if (!Validate::isOrderBy($orderBy) OR !Validate::isOrderWay($orderWay)) die (Tools::displayError()); if ($orderBy == 'id_product' OR $orderBy == 'price' OR $orderBy == 'date_add') @@ -794,39 +797,40 @@ class ProductCore extends ObjectModel elseif ($orderBy == 'position') $orderByPrefix = 'c'; - $rq = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' - SELECT p.*, pl.* , t.`rate` AS tax_rate, m.`name` AS manufacturer_name, s.`name` AS supplier_name - FROM `'._DB_PREFIX_.'product` p - '.($id_shop ? 'LEFT JOIN '._DB_PREFIX_.'product_shop ps ON (p.id_product = ps.id_product)' : '').' - LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product`) - LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group` - AND tr.`id_country` = '.(int)Context::getContext()->country->id.' - AND tr.`id_state` = 0) - LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`) - LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`) - LEFT JOIN `'._DB_PREFIX_.'supplier` s ON (s.`id_supplier` = p.`id_supplier`)'. - ($id_category ? 'LEFT JOIN `'._DB_PREFIX_.'category_product` c ON (c.`id_product` = p.`id_product`)' : '').' - WHERE pl.`id_lang` = '.(int)($id_lang). - ($id_shop ? ' AND ps.id_shop='.(int)$id_shop : ''). - ($id_category ? ' AND c.`id_category` = '.(int)($id_category) : ''). - ($only_active ? ' AND p.`active` = 1' : '').' - ORDER BY '.(isset($orderByPrefix) ? pSQL($orderByPrefix).'.' : '').'`'.pSQL($orderBy).'` '.pSQL($orderWay). - ($limit > 0 ? ' LIMIT '.(int)($start).','.(int)($limit) : '') - ); + $sql = 'SELECT p.*, pl.* , t.`rate` AS tax_rate, m.`name` AS manufacturer_name, s.`name` AS supplier_name + FROM `'._DB_PREFIX_.'product` p + '.$context->shop->sqlAsso('product', 'p', true).' + LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` '.$context->shop->sqlLang('pl').') + LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group` + AND tr.`id_country` = '.(int)Context::getContext()->country->id.' + AND tr.`id_state` = 0) + LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`) + LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`) + LEFT JOIN `'._DB_PREFIX_.'supplier` s ON (s.`id_supplier` = p.`id_supplier`)'. + ($id_category ? 'LEFT JOIN `'._DB_PREFIX_.'category_product` c ON (c.`id_product` = p.`id_product`)' : '').' + WHERE pl.`id_lang` = '.(int)$id_lang. + ($id_category ? ' AND c.`id_category` = '.(int)$id_category : ''). + ($only_active ? ' AND p.`active` = 1' : '').' + ORDER BY '.(isset($orderByPrefix) ? pSQL($orderByPrefix).'.' : '').'`'.pSQL($orderBy).'` '.pSQL($orderWay). + ($limit > 0 ? ' LIMIT '.(int)$start.','.(int)$limit : ''); + $rq = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql); if($orderBy == 'price') Tools::orderbyPrice($rq,$orderWay); return ($rq); } - public static function getSimpleProducts($id_lang, $id_shop = false) + public static function getSimpleProducts($id_lang, Context $context = null) { - return Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' - SELECT p.`id_product`, pl.`name` - FROM `'._DB_PREFIX_.'product` p - '.($id_shop ? 'LEFT JOIN '._DB_PREFIX_.'product_shop ps ON (p.id_product = ps.id_product)' : '').' - LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product`) - WHERE pl.`id_lang` = '.(int)($id_lang).($id_shop ? ' AND ps.id_shop='.(int)$id_shop : '').' - ORDER BY pl.`name`'); + if (!$context) + $context = Context::getContext(); + + $sql = 'SELECT p.`id_product`, pl.`name` + FROM `'._DB_PREFIX_.'product` p + '.$context->shop->sqlAsso('product', 'p').' + LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` '.$context->shop->sqlLang('pl').') + WHERE pl.`id_lang` = '.(int)($id_lang).' + ORDER BY pl.`name`'; + return Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql); } public function isNew() @@ -1345,10 +1349,11 @@ class ProductCore extends ObjectModel * @param integer $nbProducts Number of products to return (optional) * @return array New products */ - public static function getNewProducts($id_lang, $pageNumber = 0, $nbProducts = 10, $count = false, $orderBy = NULL, $orderWay = NULL, $id_shop = null) + public static function getNewProducts($id_lang, $pageNumber = 0, $nbProducts = 10, $count = false, $orderBy = NULL, $orderWay = NULL, Context $context = null) { - if (is_null($id_shop)) - $id_shop = Context::getContext()->shop->getID(); + if (!$context) + $context = Context::getContext(); + if ($pageNumber < 0) $pageNumber = 0; if ($nbProducts < 1) $nbProducts = 10; if (empty($orderBy) || $orderBy == 'position') $orderBy = 'date_add'; @@ -1367,9 +1372,8 @@ class ProductCore extends ObjectModel { $sql = 'SELECT COUNT(p.`id_product`) AS nb FROM `'._DB_PREFIX_.'product` p - '.($id_shop ? 'LEFT JOIN '._DB_PREFIX_.'product_shop ps ON (ps.id_product = p.id_product)' : '').' + '.$context->shop->sqlAsso('product', 'p', true).' WHERE `active` = 1 - '.($id_shop ? ' AND ps.id_shop='.(int)$id_shop : '').' AND DATEDIFF(p.`date_add`, DATE_SUB(NOW(), INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).' DAY)) > 0 AND p.`id_product` IN ( SELECT cp.`id_product` @@ -1384,18 +1388,17 @@ class ProductCore extends ObjectModel i.`id_image`, il.`legend`, t.`rate`, m.`name` AS manufacturer_name, DATEDIFF(p.`date_add`, DATE_SUB(NOW(), INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).' DAY)) > 0 AS new, (p.`price` * ((100 + (t.`rate`))/100)) AS orderprice, pa.id_product_attribute FROM `'._DB_PREFIX_.'product` p - '.($id_shop ? 'LEFT JOIN '._DB_PREFIX_.'product_shop ps ON (ps.id_product = p.id_product)' : '').' - LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)($id_lang).(($id_shop) ? ' AND pl.id_shop = '.(int)$id_shop : '').') + '.$context->shop->sqlAsso('product', 'p', true).' + LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.$context->shop->sqlLang('pl').') LEFT OUTER JOIN `'._DB_PREFIX_.'product_attribute` pa ON (p.`id_product` = pa.`id_product` AND `default_on` = 1) LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1) - LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)($id_lang).') + LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.') LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group` - AND tr.`id_country` = '.(int)Context::getContext()->country->id.' + AND tr.`id_country` = '.(int)$context->country->id.' AND tr.`id_state` = 0) LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`) LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`) WHERE p.`active` = 1 - '.($id_shop ? ' AND ps.id_shop='.(int)$id_shop : '').' AND DATEDIFF(p.`date_add`, DATE_SUB(NOW(), INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).' DAY)) > 0 AND p.`id_product` IN ( SELECT cp.`id_product` @@ -1438,46 +1441,47 @@ class ProductCore extends ObjectModel * @param integer $id_lang Language id * @return array Special */ - public static function getRandomSpecial($id_lang, $beginning = false, $ending = false, $id_shop = false) + public static function getRandomSpecial($id_lang, $beginning = false, $ending = false, Context $context = null) { + if (!$context) + $context = Context::getContext(); + $currentDate = date('Y-m-d H:i:s'); - $ids_product = self::_getProductIdByDate((!$beginning ? $currentDate : $beginning), (!$ending ? $currentDate : $ending)); + $ids_product = self::_getProductIdByDate((!$beginning ? $currentDate : $beginning), (!$ending ? $currentDate : $ending), $context); $groups = FrontController::getCurrentCustomerGroups(); $sqlGroups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1'); // Please keep 2 distinct queries because RAND() is an awful way to achieve this result - $sql = ' - SELECT p.id_product - FROM `'._DB_PREFIX_.'product` p - '.($id_shop ? 'LEFT JOIN '._DB_PREFIX_.'product_shop ps ON (ps.id_product = p.id_product)' : '').' - WHERE 1 - AND p.`active` = 1 '.($id_shop ? ' AND ps.id_shop='.(int)$id_shop : '').' - AND p.`id_product` IN ('.implode(', ', $ids_product).') - AND p.`id_product` IN ( - SELECT cp.`id_product` - FROM `'._DB_PREFIX_.'category_group` cg - LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`) - WHERE cg.`id_group` '.$sqlGroups.' - ) - ORDER BY RAND()'; + $sql = 'SELECT p.id_product + FROM `'._DB_PREFIX_.'product` p + '.$context->shop->sqlAsso('product', 'p', true).' + WHERE p.`active` = 1 + AND p.`id_product` IN ('.implode(', ', $ids_product).') + AND p.`id_product` IN ( + SELECT cp.`id_product` + FROM `'._DB_PREFIX_.'category_group` cg + LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`) + WHERE cg.`id_group` '.$sqlGroups.' + ) + ORDER BY RAND()'; $id_product = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql); if (!$id_product) return false; - $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' - SELECT p.*, pl.`description`, pl.`description_short`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, p.`ean13`, p.`upc`, - i.`id_image`, il.`legend`, t.`rate` - FROM `'._DB_PREFIX_.'product` p - LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)($id_lang).') - LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1) - LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)($id_lang).') - LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group` - AND tr.`id_country` = '.(int)Context::getContext()->country->id.' - AND tr.`id_state` = 0) - LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`) - WHERE p.id_product = '.(int)$id_product); + $sql = 'SELECT p.*, pl.`description`, pl.`description_short`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, p.`ean13`, p.`upc`, + i.`id_image`, il.`legend`, t.`rate` + FROM `'._DB_PREFIX_.'product` p + LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.$context->shop->sqlLang('pl').') + LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1) + LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.') + LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group` + AND tr.`id_country` = '.(int)Context::getContext()->country->id.' + AND tr.`id_state` = 0) + LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`) + WHERE p.id_product = '.(int)$id_product; + $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql); return Product::getProductProperties($id_lang, $row); } @@ -1496,7 +1500,7 @@ class ProductCore extends ObjectModel if (!Validate::isBool($count)) die(Tools::displayError()); - if (is_null($context)) $context = Context::getContext(); + if (!$context) $context = Context::getContext(); if ($pageNumber < 0) $pageNumber = 0; if ($nbProducts < 1) $nbProducts = 10; if (empty($orderBy) || $orderBy == 'position') $orderBy = 'price'; @@ -1508,7 +1512,7 @@ class ProductCore extends ObjectModel if (!Validate::isOrderBy($orderBy) OR !Validate::isOrderWay($orderWay)) die (Tools::displayError()); $currentDate = date('Y-m-d H:i:s'); - $ids_product = self::_getProductIdByDate((!$beginning ? $currentDate : $beginning), (!$ending ? $currentDate : $ending)); + $ids_product = self::_getProductIdByDate((!$beginning ? $currentDate : $beginning), (!$ending ? $currentDate : $ending), $context); $groups = FrontController::getCurrentCustomerGroups(); $sqlGroups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1'); @@ -1615,15 +1619,18 @@ class ProductCore extends ObjectModel * @param integer $id_lang Language id for multilingual legends * @return array Product images and legends */ - public function getImages($id_lang, $id_shop = false) + public function getImages($id_lang, Context $context = null) { - return Db::getInstance()->ExecuteS(' - SELECT i.`cover`, i.`id_image`, il.`legend` - FROM `'._DB_PREFIX_.'image` i - '.($id_shop ? 'LEFT JOIN '._DB_PREFIX_.'image_shop iss ON (i.id_image = iss.id_image)' : '').' - LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)($id_lang).') - WHERE i.`id_product` = '.(int)($this->id).' '.($id_shop ? ' AND iss.id_shop='.(int)$id_shop : '').' - ORDER BY `position`'); + if (!$context) + $context = Context::getContext(); + + $sql = 'SELECT i.`cover`, i.`id_image`, il.`legend` + FROM `'._DB_PREFIX_.'image` i + '.$context->shop->sqlAsso('image', 'i', true).' + LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.') + WHERE i.`id_product` = '.(int)$this->id.' + ORDER BY `position`'; + return Db::getInstance()->ExecuteS($sql); } /** @@ -1631,14 +1638,17 @@ class ProductCore extends ObjectModel * * @return array Product cover image */ - public static function getCover($id_product, $id_shop = false) + public static function getCover($id_product, Context $context = null) { - return Db::getInstance()->getRow(' - SELECT i.`id_image` - FROM `'._DB_PREFIX_.'image` i - '.($id_shop ? 'LEFT JOIN '._DB_PREFIX_.'image_shop iss ON (i.id_image = iss.id_image)' : '').' - WHERE i.`id_product` = '.(int)($id_product).($id_shop ? ' AND iss.id_shop='.(int)$id_shop : '').' - AND i.`cover` = 1'); + if (!$context) + $context = Context::getContext(); + + $sql = 'SELECT i.`id_image` + FROM `'._DB_PREFIX_.'image` i + '.$context->shop->sqlAsso('image', 'i', true).' + WHERE i.`id_product` = '.(int)($id_product).' + AND i.`cover` = 1'; + return Db::getInstance()->getRow($sql); } /** @@ -2328,15 +2338,18 @@ class ProductCore extends ObjectModel * @param integer $id_product Product id * @return array Product accessories */ - public static function getAccessoriesLight($id_lang, $id_product, $id_shop = false) + public static function getAccessoriesLight($id_lang, $id_product, Context $context = null) { - return Db::getInstance()->ExecuteS(' - SELECT p.`id_product`, p.`reference`, pl.`name` - FROM `'._DB_PREFIX_.'accessory` - LEFT JOIN `'._DB_PREFIX_.'product` p ON (p.`id_product`= `id_product_2`) - '.($id_shop ? 'LEFT JOIN '._DB_PREFIX_.'product_shop ps ON (ps.id_product = p.id_product)' : '').' - LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)($id_lang).') - WHERE `id_product_1` = '.(int)$id_product.($id_shop ? ' AND ps.id_shop='.(int)$id_shop : '')); + if (!$context) + $context = Context::getContext(); + + $sql = 'SELECT p.`id_product`, p.`reference`, pl.`name` + FROM `'._DB_PREFIX_.'accessory` + LEFT JOIN `'._DB_PREFIX_.'product` p ON (p.`id_product`= `id_product_2`) + '.$context->shop->sqlAsso('product', 'p', true).' + LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.$context->shop->sqlLang('pl').') + WHERE `id_product_1` = '.(int)$id_product; + return Db::getInstance()->ExecuteS($sql); } /** @@ -2345,34 +2358,29 @@ class ProductCore extends ObjectModel * @param integer $id_lang Language id * @return array Product accessories */ - public function getAccessories($id_lang, $active = true, $id_shop = false) - { - if (!$id_shop) - $id_shop_lang = (int)Configuration::get('PS_SHOP_DEFAULT'); - else - $id_shop_lang = (int)$id_shop; + public function getAccessories($id_lang, $active = true, Context $context = null) + { + if (!$context) + $context = Context::getContext(); - $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' - SELECT p.*, pl.`description`, pl.`description_short`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, p.`ean13`, p.`upc`, - i.`id_image`, il.`legend`, t.`rate`, m.`name` as manufacturer_name, cl.`name` AS category_default, DATEDIFF(p.`date_add`, DATE_SUB(NOW(), - INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).' DAY)) > 0 AS new - FROM `'._DB_PREFIX_.'accessory` - LEFT JOIN `'._DB_PREFIX_.'product` p ON p.`id_product` = `id_product_2` - '.($id_shop ? 'LEFT JOIN '._DB_PREFIX_.'product_shop ps ON (ps.id_product = p.id_product)' : '').' - LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)($id_lang).' AND pl.id_shop='.(int)$id_shop_lang.') - LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (p.`id_category_default` = cl.`id_category` AND cl.`id_lang` = '.(int)($id_lang).' AND cl.id_shop='.(int)$id_shop_lang.') - LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1) - LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)($id_lang).') - LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (p.`id_manufacturer`= m.`id_manufacturer`) - LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group` - AND tr.`id_country` = '.(int)Context::getContext()->country->id.' - AND tr.`id_state` = 0) - LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`) - WHERE `id_product_1` = '.(int)($this->id). - ($id_shop ? ' AND ps.id_shop='.(int)$id_shop : ''). - ($active ? ' AND p.`active` = 1' : '')); - - if (!$result) + $sql = 'SELECT p.*, pl.`description`, pl.`description_short`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, p.`ean13`, p.`upc`, + i.`id_image`, il.`legend`, t.`rate`, m.`name` as manufacturer_name, cl.`name` AS category_default, DATEDIFF(p.`date_add`, DATE_SUB(NOW(), + INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).' DAY)) > 0 AS new + FROM `'._DB_PREFIX_.'accessory` + LEFT JOIN `'._DB_PREFIX_.'product` p ON p.`id_product` = `id_product_2` + '.$context->shop->sqlAsso('product', 'p', true).' + LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.$context->shop->sqlLang('pl').') + LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (p.`id_category_default` = cl.`id_category` AND cl.`id_lang` = '.(int)$id_lang.$context->shop->sqlLang('cl').') + LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1) + LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.') + LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (p.`id_manufacturer`= m.`id_manufacturer`) + LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group` + AND tr.`id_country` = '.(int)Context::getContext()->country->id.' + AND tr.`id_state` = 0) + LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`) + WHERE `id_product_1` = '.(int)($this->id). + ($active ? ' AND p.`active` = 1' : ''); + if (!$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql)) return false; return $this->getProductsProperties($id_lang, $result); @@ -2498,27 +2506,31 @@ class ProductCore extends ObjectModel * @param string $query Search query * @return array Matching products */ - public static function searchByName($id_lang, $query, $id_shop = false) + public static function searchByName($id_lang, $query, Context $context = null) { - $result = Db::getInstance()->ExecuteS(' - SELECT p.`id_product`, pl.`name`, pl.`link_rewrite`, p.`weight`, p.`active`, p.`ecotax`, i.`id_image`, p.`reference`, p.`cache_is_pack`, - il.`legend`, m.`name` AS manufacturer_name, tl.`name` AS tax_name - FROM `'._DB_PREFIX_.'category_product` cp - LEFT JOIN `'._DB_PREFIX_.'product` p ON p.`id_product` = cp.`id_product` - '.($id_shop ? 'LEFT JOIN '._DB_PREFIX_.'product_shop ps ON (ps.id_product = p.id_product)' : '').' - LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)($id_lang).') - LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group` - AND tr.`id_country` = '.(int)Context::getContext()->country->id.' - AND tr.`id_state` = 0) - LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`) - LEFT JOIN `'._DB_PREFIX_.'tax_lang` tl ON (t.`id_tax` = tl.`id_tax` AND tl.`id_lang` = '.(int)($id_lang).') - LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON m.`id_manufacturer` = p.`id_manufacturer` - LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product`) AND i.`cover` = 1 - LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)($id_lang).') - WHERE pl.`name` LIKE \'%'.pSQL($query).'%\' OR p.`reference` LIKE \'%'.pSQL($query).'%\' OR p.`supplier_reference` LIKE \'%'.pSQL($query).'%\' - '.($id_shop ? ' AND ps.id_shop='.(int)$id_shop : '').' - GROUP BY `id_product` - ORDER BY pl.`name` ASC'); + if (!$context) + $context = Context::getContext(); + + $sql = 'SELECT p.`id_product`, pl.`name`, pl.`link_rewrite`, p.`weight`, p.`active`, p.`ecotax`, i.`id_image`, p.`reference`, p.`cache_is_pack`, + il.`legend`, m.`name` AS manufacturer_name, tl.`name` AS tax_name + FROM `'._DB_PREFIX_.'category_product` cp + LEFT JOIN `'._DB_PREFIX_.'product` p ON p.`id_product` = cp.`id_product` + '.$context->shop->sqlAsso('product', 'p', true).' + LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.$context->shop->sqlLang('pl').') + LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group` + AND tr.`id_country` = '.(int)Context::getContext()->country->id.' + AND tr.`id_state` = 0) + LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`) + LEFT JOIN `'._DB_PREFIX_.'tax_lang` tl ON (t.`id_tax` = tl.`id_tax` AND tl.`id_lang` = '.(int)$id_lang.') + LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON m.`id_manufacturer` = p.`id_manufacturer` + LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product`) AND i.`cover` = 1 + LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.') + WHERE pl.`name` LIKE \'%'.pSQL($query).'%\' + OR p.`reference` LIKE \'%'.pSQL($query).'%\' + OR p.`supplier_reference` LIKE \'%'.pSQL($query).'%\' + GROUP BY `id_product` + ORDER BY pl.`name` ASC'; + $result = Db::getInstance()->ExecuteS($sql); if (!$result) return false; diff --git a/classes/ProductSale.php b/classes/ProductSale.php index 3ee4bcd86..405128545 100644 --- a/classes/ProductSale.php +++ b/classes/ProductSale.php @@ -44,15 +44,17 @@ class ProductSaleCore ** Get number of actives products sold ** @return int number of actives products listed in product_sales */ - static public function getNbSales($id_shop = false) + static public function getNbSales(Context $context = null) { - $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' - SELECT COUNT(ps.`id_product`) AS nb - FROM `'._DB_PREFIX_.'product_sale` ps - '.($id_shop ? 'LEFT JOIN '._DB_PREFIX_.'product_shop pss ON (pss.id_product = ps.id_product)' : '').' - LEFT JOIN `'._DB_PREFIX_.'product` p ON p.`id_product` = ps.`id_product` - WHERE p.`active` = 1'.($id_shop ? ' AND pss.id_shop='.(int)$id_shop : '')); - return (int)($result['nb']); + if (!$context) + $context = Context::getContext(); + + $sql = 'SELECT COUNT(ps.`id_product`) AS nb + FROM `'._DB_PREFIX_.'product_sale` ps + '.$context->shop->sqlAsso('product', 'p').' + LEFT JOIN `'._DB_PREFIX_.'product` p ON p.`id_product` = ps.`id_product` + WHERE p.`active` = 1'; + return (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql); } /* @@ -63,8 +65,11 @@ class ProductSaleCore ** @param integer $nbProducts Number of products to return (optional) ** @return array from Product::getProductProperties */ - static public function getBestSales($id_lang, $pageNumber = 0, $nbProducts = 10, $orderBy=NULL, $orderWay=NULL, $id_shop = false) + static public function getBestSales($id_lang, $pageNumber = 0, $nbProducts = 10, $orderBy = NULL, $orderWay = NULL, Context $context = null) { + if (!$context) + $context = Context::getContext(); + if ($pageNumber < 0) $pageNumber = 0; if ($nbProducts < 1) $nbProducts = 10; if (empty($orderBy) || $orderBy == 'position') $orderBy = 'sales'; @@ -73,33 +78,32 @@ class ProductSaleCore $groups = FrontController::getCurrentCustomerGroups(); $sqlGroups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1'); - $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(' - SELECT p.*, - pl.`description`, pl.`description_short`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, m.`name` AS manufacturer_name, p.`id_manufacturer` as id_manufacturer, - i.`id_image`, il.`legend`, - ps.`quantity` AS sales, t.`rate`, pl.`meta_keywords`, pl.`meta_title`, pl.`meta_description`, - DATEDIFF(p.`date_add`, DATE_SUB(NOW(), INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).' DAY)) > 0 AS new - FROM `'._DB_PREFIX_.'product_sale` ps - LEFT JOIN `'._DB_PREFIX_.'product` p ON ps.`id_product` = p.`id_product` - '.($id_shop ? 'LEFT JOIN `'._DB_PREFIX_.'product_shop` pss ON (pss.`id_product` = p.`id_product`)' : '').' - LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)($id_lang).') - LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1) - LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)($id_lang).') - LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`) - LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group` - AND tr.`id_country` = '.(int)Context::getContext()->country->id.' - AND tr.`id_state` = 0) - LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`) - WHERE p.`active` = 1 - '.($id_shop ? ' AND pss.id_shop='.(int)$id_shop : '').' - AND p.`id_product` IN ( - SELECT cp.`id_product` - FROM `'._DB_PREFIX_.'category_group` cg - LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`) - WHERE cg.`id_group` '.$sqlGroups.' - ) - ORDER BY '.(isset($orderByPrefix) ? $orderByPrefix.'.' : '').'`'.pSQL($orderBy).'` '.pSQL($orderWay).' - LIMIT '.(int)($pageNumber * $nbProducts).', '.(int)($nbProducts)); + $sql = 'SELECT p.*, + pl.`description`, pl.`description_short`, pl.`link_rewrite`, pl.`meta_description`, pl.`meta_keywords`, pl.`meta_title`, pl.`name`, m.`name` AS manufacturer_name, p.`id_manufacturer` as id_manufacturer, + i.`id_image`, il.`legend`, + ps.`quantity` AS sales, t.`rate`, pl.`meta_keywords`, pl.`meta_title`, pl.`meta_description`, + DATEDIFF(p.`date_add`, DATE_SUB(NOW(), INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).' DAY)) > 0 AS new + FROM `'._DB_PREFIX_.'product_sale` ps + LEFT JOIN `'._DB_PREFIX_.'product` p ON ps.`id_product` = p.`id_product` + '.$context->shop->sqlAsso('product', 'p').' + LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.$context->shop->sqlLang('pl').') + LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1) + LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.') + LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`) + LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON p.`id_tax_rules_group` = tr.`id_tax_rules_group` + AND tr.`id_country` = '.(int)Context::getContext()->country->id.' + AND tr.`id_state` = 0 + LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`) + WHERE p.`active` = 1 + AND p.`id_product` IN ( + SELECT cp.`id_product` + FROM `'._DB_PREFIX_.'category_group` cg + LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`) + WHERE cg.`id_group` '.$sqlGroups.' + ) + ORDER BY `'.pSQL($orderBy).'` '.pSQL($orderWay).' + LIMIT '.(int)($pageNumber * $nbProducts).', '.(int)$nbProducts; + $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql); if ($orderBy == 'price') Tools::orderbyPrice($result,$orderWay); @@ -116,13 +120,12 @@ class ProductSaleCore ** @param integer $nbProducts Number of products to return (optional) ** @return array keys : id_product, link_rewrite, name, id_image, legend, sales, ean13, upc, link */ - static public function getBestSalesLight($id_lang, $pageNumber = 0, $nbProducts = 10, $id_shop = null, Context $context = null) + static public function getBestSalesLight($id_lang, $pageNumber = 0, $nbProducts = 10, Context $context = null) { if (!$context) $context = Context::getContext(); if ($pageNumber < 0) $pageNumber = 0; if ($nbProducts < 1) $nbProducts = 10; - if (is_null($id_shop)) $id_shop = $context->shop->getID(); $groups = FrontController::getCurrentCustomerGroups(); $sqlGroups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1'); @@ -130,13 +133,12 @@ class ProductSaleCore $sql = 'SELECT p.id_product, pl.`link_rewrite`, pl.`name`, pl.`description_short`, i.`id_image`, il.`legend`, ps.`quantity` AS sales, p.`ean13`, p.`upc`, cl.`link_rewrite` AS category FROM `'._DB_PREFIX_.'product_sale` ps LEFT JOIN `'._DB_PREFIX_.'product` p ON ps.`id_product` = p.`id_product` - '.($id_shop ? 'LEFT JOIN `'._DB_PREFIX_.'product_shop` pss ON (pss.`id_product` = p.`id_product`)' : '').' - LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.(($id_shop) ? ' AND pl.id_shop = '.(int)$id_shop : '').') + '.$context->shop->sqlAsso('product', 'p', true).' + LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.$context->shop->sqlLang('pl').') LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1) LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.') - LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (cl.`id_category` = p.`id_category_default` AND cl.`id_lang` = '.(int)$id_lang.(($id_shop) ? ' AND cl.id_shop = '.(int)$id_shop : '').') + LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (cl.`id_category` = p.`id_category_default` AND cl.`id_lang` = '.(int)$id_lang.$context->shop->sqlLang('cl').') WHERE p.`active` = 1 - '.($id_shop ? ' AND pss.id_shop='.(int)$id_shop : '').' AND p.`id_product` IN ( SELECT cp.`id_product` FROM `'._DB_PREFIX_.'category_group` cg @@ -144,7 +146,7 @@ class ProductSaleCore WHERE cg.`id_group` '.$sqlGroups.' ) ORDER BY sales DESC - LIMIT '.(int)($pageNumber * $nbProducts).', '.(int)($nbProducts); + LIMIT '.(int)($pageNumber * $nbProducts).', '.(int)$nbProducts; if (!$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql)) return $result; diff --git a/classes/Search.php b/classes/Search.php index f3180661a..d3a750670 100644 --- a/classes/Search.php +++ b/classes/Search.php @@ -145,7 +145,7 @@ class SearchCore return $string; } - public static function find($id_lang, $expr, $pageNumber = 1, $pageSize = 1, $orderBy = 'position', $orderWay = 'desc', $ajax = false, $useCookie = true, $id_shop = false, Context $context = null) + public static function find($id_lang, $expr, $pageNumber = 1, $pageSize = 1, $orderBy = 'position', $orderWay = 'desc', $ajax = false, $useCookie = true, Context $context = null) { if (!$context) $context = Context::getContext(); @@ -156,9 +156,6 @@ class SearchCore $id_customer = $context->customer->id; else $id_customer = 0; - - if (!$id_shop) - $id_shop = $context->shop->getID(); // TODO : smart page management if ($pageNumber < 1) $pageNumber = 1; @@ -180,8 +177,8 @@ class SearchCore FROM '._DB_PREFIX_.'search_word sw LEFT JOIN '._DB_PREFIX_.'search_index si ON sw.id_word = si.id_word WHERE sw.id_lang = '.(int)$id_lang.' - AND sw.id_shop = '.(int)$id_shop.' - AND sw.word LIKE + AND sw.id_shop = '.$context->shop->getID(true).' + AND sw.word LIKE '.($word[0] == '-' ? ' \''.pSQL(Tools::substr($word, 1, PS_SEARCH_MAX_WORD_LENGTH)).'%\'' : '\''.pSQL(Tools::substr($word, 0, PS_SEARCH_MAX_WORD_LENGTH)).'%\'' @@ -203,23 +200,25 @@ class SearchCore FROM '._DB_PREFIX_.'search_word sw LEFT JOIN '._DB_PREFIX_.'search_index si ON sw.id_word = si.id_word WHERE sw.id_lang = '.(int)$id_lang.' - AND sw.id_shop = '.(int)$id_shop.' - AND si.id_product = p.id_product - AND ('.implode(' OR ', $scoreArray).') + AND sw.id_shop = '.$context->shop->getID(true).' + AND si.id_product = p.id_product + AND ('.implode(' OR ', $scoreArray).') ) position'; - $result = $db->ExecuteS(' - SELECT cp.`id_product` - FROM `'._DB_PREFIX_.'category_group` cg - INNER JOIN `'._DB_PREFIX_.'category_product` cp ON cp.`id_category` = cg.`id_category` - INNER JOIN `'._DB_PREFIX_.'category` c ON cp.`id_category` = c.`id_category` - INNER JOIN `'._DB_PREFIX_.'product` p ON cp.`id_product` = p.`id_product` - INNER JOIN `'._DB_PREFIX_.'product_shop` ps ON (p.id_product = ps.id_product) - WHERE c.`active` = 1 AND p.`active` = 1 AND indexed = 1 AND ps.id_shop='.(int)$id_shop.' - AND cg.`id_group` '.(!$id_customer ? '= 1' : 'IN ( - SELECT id_group FROM '._DB_PREFIX_.'customer_group - WHERE id_customer = '.(int)$id_customer.' - )'), false); + $sql = 'SELECT cp.`id_product` + FROM `'._DB_PREFIX_.'category_group` cg + INNER JOIN `'._DB_PREFIX_.'category_product` cp ON cp.`id_category` = cg.`id_category` + INNER JOIN `'._DB_PREFIX_.'category` c ON cp.`id_category` = c.`id_category` + INNER JOIN `'._DB_PREFIX_.'product` p ON cp.`id_product` = p.`id_product` + '.$context->shop->sqlAsso('product', 'p').' + WHERE c.`active` = 1 + AND p.`active` = 1 + AND indexed = 1 + AND cg.`id_group` '.(!$id_customer ? '= 1' : 'IN ( + SELECT id_group FROM '._DB_PREFIX_.'customer_group + WHERE id_customer = '.(int)$id_customer.' + )'); + $result = $db->ExecuteS($sql, false); $eligibleProducts = array(); while ($row = $db->nextRow($result)) @@ -247,44 +246,44 @@ class SearchCore if ($ajax) { - return $db->ExecuteS(' - SELECT DISTINCT p.id_product, pl.name pname, cl.name cname, - cl.link_rewrite crewrite, pl.link_rewrite prewrite '.$score.' - FROM '._DB_PREFIX_.'product p - INNER JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.' AND pl.id_shop='.(int)$id_shop.') - INNER JOIN `'._DB_PREFIX_.'category_lang` cl ON (p.`id_category_default` = cl.`id_category` AND cl.`id_lang` = '.(int)$id_lang.' AND cl.id_shop='.(int)$id_shop.') - WHERE p.`id_product` '.$productPool.' - ORDER BY position DESC LIMIT 10'); + $sql = 'SELECT DISTINCT p.id_product, pl.name pname, cl.name cname, + cl.link_rewrite crewrite, pl.link_rewrite prewrite '.$score.' + FROM '._DB_PREFIX_.'product p + INNER JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.$context->shop->sqlLang('pl').') + INNER JOIN `'._DB_PREFIX_.'category_lang` cl ON (p.`id_category_default` = cl.`id_category` AND cl.`id_lang` = '.(int)$id_lang.$context->shop->sqlLang('cl').') + WHERE p.`id_product` '.$productPool.' + ORDER BY position DESC LIMIT 10'; + return $db->ExecuteS($sql); } - $queryResults = ' - SELECT p.*, pl.`description_short`, pl.`available_now`, pl.`available_later`, pl.`link_rewrite`, pl.`name`, - tax.`rate`, i.`id_image`, il.`legend`, m.`name` manufacturer_name '.$score.', DATEDIFF(p.`date_add`, DATE_SUB(NOW(), INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).' DAY)) > 0 new - FROM '._DB_PREFIX_.'product p - INNER JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.' AND pl.id_shop='.(int)$id_shop.') - LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group` - AND tr.`id_country` = '.(int)$context->country->id.' - AND tr.`id_state` = 0) - LEFT JOIN `'._DB_PREFIX_.'tax` tax ON (tax.`id_tax` = tr.`id_tax`) - LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON m.`id_manufacturer` = p.`id_manufacturer` - LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1) - LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.') - WHERE p.`id_product` '.$productPool.' - '.($orderBy ? 'ORDER BY '.$orderBy : '').($orderWay ? ' '.$orderWay : '').' - LIMIT '.(int)(($pageNumber - 1) * $pageSize).','.(int)$pageSize; + $sql = 'SELECT p.*, pl.`description_short`, pl.`available_now`, pl.`available_later`, pl.`link_rewrite`, pl.`name`, + tax.`rate`, i.`id_image`, il.`legend`, m.`name` manufacturer_name '.$score.', DATEDIFF(p.`date_add`, DATE_SUB(NOW(), INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).' DAY)) > 0 new + FROM '._DB_PREFIX_.'product p + INNER JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.$context->shop->sqlLang('pl').') + LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group` + AND tr.`id_country` = '.(int)$context->country->id.' + AND tr.`id_state` = 0) + LEFT JOIN `'._DB_PREFIX_.'tax` tax ON (tax.`id_tax` = tr.`id_tax`) + LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON m.`id_manufacturer` = p.`id_manufacturer` + LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1) + LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.') + WHERE p.`id_product` '.$productPool.' + '.($orderBy ? 'ORDER BY '.$orderBy : '').($orderWay ? ' '.$orderWay : '').' + LIMIT '.(int)(($pageNumber - 1) * $pageSize).','.(int)$pageSize; + $result = $db->ExecuteS($sql); - $result = $db->ExecuteS($queryResults); - $total = $db->getValue('SELECT COUNT(*) - FROM '._DB_PREFIX_.'product p - INNER JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.' AND pl.id_shop='.(int)$id_shop.') - LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group` - AND tr.`id_country` = '.(int)Context::getContext()->country->id.' - AND tr.`id_state` = 0) - LEFT JOIN `'._DB_PREFIX_.'tax` tax ON (tax.`id_tax` = tr.`id_tax`) - LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON m.`id_manufacturer` = p.`id_manufacturer` - LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1) - LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.') - WHERE p.`id_product` '.$productPool); + $sql = 'SELECT COUNT(*) + FROM '._DB_PREFIX_.'product p + INNER JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.$context->shop->sqlLang('pl').') + LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group` + AND tr.`id_country` = '.(int)Context::getContext()->country->id.' + AND tr.`id_state` = 0) + LEFT JOIN `'._DB_PREFIX_.'tax` tax ON (tax.`id_tax` = tr.`id_tax`) + LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON m.`id_manufacturer` = p.`id_manufacturer` + LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1) + LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.') + WHERE p.`id_product` '.$productPool; + $total = $db->getValue($sql); if (!$result) $resultProperties = false; @@ -387,13 +386,13 @@ class SearchCore ); // All the product not yet indexed are retrieved - $products = $db->ExecuteS(' - SELECT p.id_product, pl.id_lang, pl.name pname, p.reference, p.ean13, p.upc, pl.description_short, pl.description, cl.name cname, m.name mname, pl.id_shop - FROM '._DB_PREFIX_.'product p - LEFT JOIN '._DB_PREFIX_.'product_lang pl ON p.id_product = pl.id_product - LEFT JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = p.id_category_default AND pl.id_lang = cl.id_lang AND pl.id_shop = cl.id_shop) - LEFT JOIN '._DB_PREFIX_.'manufacturer m ON m.id_manufacturer = p.id_manufacturer - WHERE p.indexed = 0', false); + $sql = 'SELECT p.id_product, pl.id_lang, pl.name pname, p.reference, p.ean13, p.upc, pl.description_short, pl.description, cl.name cname, m.name mname, pl.id_shop + FROM '._DB_PREFIX_.'product p + LEFT JOIN '._DB_PREFIX_.'product_lang pl ON p.id_product = pl.id_product + LEFT JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = p.id_category_default AND pl.id_lang = cl.id_lang AND pl.id_shop = cl.id_shop) + LEFT JOIN '._DB_PREFIX_.'manufacturer m ON m.id_manufacturer = p.id_manufacturer + WHERE p.indexed = 0'; + $products = $db->ExecuteS($sql, false); // Those are kind of global variables required to save the processed data in the database every X occurences, in order to avoid overloading MySQL $countWords = 0; @@ -532,14 +531,10 @@ class SearchCore $queryArray3 = array(); } - public static function searchTag($id_lang, $tag, $count = false, $pageNumber = 0, $pageSize = 10, $orderBy = false, $orderWay = false, $useCookie = true, $shops = null, Context $context = null) + public static function searchTag($id_lang, $tag, $count = false, $pageNumber = 0, $pageSize = 10, $orderBy = false, $orderWay = false, $useCookie = true, Context $context = null) { if (!$context) $context = Context::getContext(); - if (is_null($shops)) - $shops = array($context->shop->getID()); - if (!is_array($shops)) - $shops = array($shops); // Only use cookie if id_customer is not present if ($useCookie) @@ -558,7 +553,7 @@ class SearchCore { $sql = 'SELECT COUNT(DISTINCT pt.`id_product`) nb FROM `'._DB_PREFIX_.'product` p - INNER JOIN '._DB_PREFIX_.'product_shop ps ON p.id_product = ps.id_product AND ps.id_shop IN ('.implode(', ', $shops).') + '.$context->shop->sqlAsso('product', 'p', true).' LEFT JOIN `'._DB_PREFIX_.'product_tag` pt ON (p.`id_product` = pt.`id_product`) LEFT JOIN `'._DB_PREFIX_.'tag` t ON (pt.`id_tag` = t.`id_tag` AND t.`id_lang` = '.(int)$id_lang.') LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_product` = p.`id_product`) @@ -574,8 +569,8 @@ class SearchCore $sql = 'SELECT DISTINCT p.*, pl.`description_short`, pl.`link_rewrite`, pl.`name`, tax.`rate`, i.`id_image`, il.`legend`, m.`name` manufacturer_name, 1 position, DATEDIFF(p.`date_add`, DATE_SUB(NOW(), INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).' DAY)) > 0 new FROM `'._DB_PREFIX_.'product` p - INNER JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.' AND pl.id_shop IN ('.implode(', ', $shops).')) - INNER JOIN '._DB_PREFIX_.'product_shop ps ON p.id_product = ps.id_product AND ps.id_shop IN ('.implode(', ', $shops).') + INNER JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.$context->shop->sqlLang('pl').') + '.$context->shop->sqlAsso('product', 'p').' LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1) LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.') LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group` diff --git a/classes/ShopUrl.php b/classes/ShopUrl.php index 53ef1e546..3b05506e5 100755 --- a/classes/ShopUrl.php +++ b/classes/ShopUrl.php @@ -59,16 +59,17 @@ class ShopUrlCore extends ObjectModel public static function getShopUrls($id_shop = false) { - return Db::getInstance()->ExecuteS('SELECT * - FROM '._DB_PREFIX_.'shop_url - WHERE 1 - '.($id_shop ? ' AND id_shop='.(int)$id_shop : '')); + $sql = 'SELECT * + FROM '._DB_PREFIX_.'shop_url + WHERE 1 + '.($id_shop ? ' AND id_shop = '.(int)$id_shop : ''); + return Db::getInstance()->ExecuteS(); } public function setMain() { - $res = Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'shop_url SET main=0 WHERE id_shop='.(int)$this->id_shop); - $res &= Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'shop_url SET main=1 WHERE id_shop_url='.(int)$this->id); + $res = Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'shop_url SET main=0 WHERE id_shop = '.(int)$this->id_shop); + $res &= Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'shop_url SET main=1 WHERE id_shop_url = '.(int)$this->id); return $res; } @@ -87,16 +88,20 @@ class ShopUrlCore extends ObjectModel if (!self::$main_domain) self::$main_domain = Db::getInstance()->getValue('SELECT domain FROM '._DB_PREFIX_.'shop_url - WHERE main=1 AND id_shop='.Context::getContext()->shop->getID()); + WHERE main=1 AND id_shop = '.Context::getContext()->shop->getID()); return self::$main_domain; } public static function getMainShopDomainSSL() { if (!self::$main_domain) - self::$main_domain = Db::getInstance()->getValue('SELECT domain - FROM '._DB_PREFIX_.'shop_url - WHERE main=1 AND id_shop='.Context::getContext()->shop->getID()); + { + $sql = 'SELECT domain + FROM '._DB_PREFIX_.'shop_url + WHERE main = 1 + AND id_shop='.Context::getContext()->shop->getID(true); + self::$main_domain = Db::getInstance()->getValue($sql); + } return self::$main_domain_ssl; } } diff --git a/controllers/BestSalesController.php b/controllers/BestSalesController.php index 4108d7cea..04a79d001 100644 --- a/controllers/BestSalesController.php +++ b/controllers/BestSalesController.php @@ -37,11 +37,11 @@ class BestSalesControllerCore extends FrontController public function process() { $this->productSort(); - $nbProducts = (int)(ProductSale::getNbSales((int)$this->id_current_shop)); + $nbProducts = (int)ProductSale::getNbSales(); $this->pagination($nbProducts); $this->smarty->assign(array( - 'products' => ProductSale::getBestSales((int)(self::$cookie->id_lang), (int)($this->p) - 1, (int)($this->n), $this->orderBy, $this->orderWay, (int)$this->id_current_shop), + 'products' => ProductSale::getBestSales((int)self::$cookie->id_lang, (int)$this->p - 1, (int)$this->n, $this->orderBy, $this->orderWay), 'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'), 'nbProducts' => $nbProducts, 'homeSize' => Image::getSize('home') diff --git a/controllers/ContactController.php b/controllers/ContactController.php index a30981329..7646bac69 100644 --- a/controllers/ContactController.php +++ b/controllers/ContactController.php @@ -112,17 +112,17 @@ class ContactControllerCore extends FrontController $id_customer_thread = (int)Tools::getValue('id_customer_thread') AND (int)Db::getInstance()->getValue(' SELECT cm.id_customer_thread FROM '._DB_PREFIX_.'customer_thread cm - WHERE cm.id_customer_thread = '.(int)$id_customer_thread.' AND cm.id_shop='.(int)$this->id_current_shop.' AND token = \''.pSQL(Tools::getValue('token')).'\'') + WHERE cm.id_customer_thread = '.(int)$id_customer_thread.' AND cm.id_shop = '.(int)$this->id_current_shop.' AND token = \''.pSQL(Tools::getValue('token')).'\'') ) OR ( $id_customer_thread = (int)Db::getInstance()->getValue(' SELECT cm.id_customer_thread FROM '._DB_PREFIX_.'customer_thread cm - WHERE cm.email = \''.pSQL($from).'\' AND cm.id_shop='.(int)$this->id_current_shop.' AND cm.id_order = '.(int)(Tools::getValue('id_order')).'') + WHERE cm.email = \''.pSQL($from).'\' AND cm.id_shop = '.(int)$this->id_current_shop.' AND cm.id_order = '.(int)(Tools::getValue('id_order')).'') ))) { $fields = Db::getInstance()->ExecuteS(' SELECT cm.id_customer_thread, cm.id_contact, cm.id_customer, cm.id_order, cm.id_product, cm.email FROM '._DB_PREFIX_.'customer_thread cm - WHERE email = \''.pSQL($from).'\' AND cm.id_shop='.(int)$this->id_current_shop.' AND ('. + WHERE email = \''.pSQL($from).'\' AND cm.id_shop = '.(int)$this->id_current_shop.' AND ('. ($customer->id ? 'id_customer = '.(int)($customer->id).' OR ' : '').' id_order = '.(int)(Tools::getValue('id_order')).')'); $score = 0; @@ -148,7 +148,7 @@ class ContactControllerCore extends FrontController } $old_message = Db::getInstance()->getValue(' SELECT cm.message FROM '._DB_PREFIX_.'customer_message cm - WHERE cm.id_customer_thread = '.(int)($id_customer_thread).'AND cm.id_shop='.(int)$this->id_current_shop.' + WHERE cm.id_customer_thread = '.(int)($id_customer_thread).'AND cm.id_shop = '.(int)$this->id_current_shop.' ORDER BY date_add DESC'); if ($old_message == htmlentities($message, ENT_COMPAT, 'UTF-8')) { @@ -246,7 +246,7 @@ class ContactControllerCore extends FrontController { $customerThread = Db::getInstance()->getRow(' SELECT cm.* FROM '._DB_PREFIX_.'customer_thread cm - WHERE cm.id_customer_thread = '.(int)$id_customer_thread.' AND cm.id_shop='.(int)$this->id_current_shop.' AND token = \''.pSQL($token).'\''); + WHERE cm.id_customer_thread = '.(int)$id_customer_thread.' AND cm.id_shop = '.(int)$this->id_current_shop.' AND token = \''.pSQL($token).'\''); $this->smarty->assign('customerThread', $customerThread); } $this->smarty->assign(array('contacts' => Contact::getContacts((int)self::$cookie->id_lang), diff --git a/controllers/ProductController.php b/controllers/ProductController.php index 8959abe0c..403e5ae6f 100644 --- a/controllers/ProductController.php +++ b/controllers/ProductController.php @@ -231,7 +231,7 @@ class ProductControllerCore extends FrontController 'HOOK_PRODUCT_TAB_CONTENT' => Module::hookExec('productTabContent') )); - $images = $this->product->getImages((int)self::$cookie->id_lang, (int)$this->id_current_shop); + $images = $this->product->getImages((int)self::$cookie->id_lang); $productImages = array(); foreach ($images AS $k => $image) { diff --git a/controllers/SearchController.php b/controllers/SearchController.php index e30cb909c..bbd6b1ff1 100644 --- a/controllers/SearchController.php +++ b/controllers/SearchController.php @@ -47,7 +47,7 @@ class SearchControllerCore extends FrontController $query = urldecode(Tools::getValue('q')); if ($this->ajaxSearch) { - $searchResults = Search::find((int)(Tools::getValue('id_lang')), $query, 1, 10, 'position', 'desc', true, true, (int)$this->id_current_shop); + $searchResults = Search::find((int)(Tools::getValue('id_lang')), $query, 1, 10, 'position', 'desc', true); foreach ($searchResults AS &$product) $product['product_link'] = self::$link->getProductLink($product['id_product'], $product['prewrite'], $product['crewrite']); die(Tools::jsonEncode($searchResults)); @@ -58,7 +58,7 @@ class SearchControllerCore extends FrontController $this->productSort(); $this->n = abs((int)(Tools::getValue('n', Configuration::get('PS_PRODUCTS_PER_PAGE')))); $this->p = abs((int)(Tools::getValue('p', 1))); - $search = Search::find((int)(self::$cookie->id_lang), $query, $this->p, $this->n, $this->orderBy, $this->orderWay, false, true, (int)$this->id_current_shop); + $search = Search::find((int)(self::$cookie->id_lang), $query, $this->p, $this->n, $this->orderBy, $this->orderWay); Module::hookExec('search', array('expr' => $query, 'total' => $search['total'])); $nbProducts = $search['total']; $this->pagination($nbProducts); @@ -75,7 +75,7 @@ class SearchControllerCore extends FrontController $this->productSort(); $this->n = abs((int)(Tools::getValue('n', Configuration::get('PS_PRODUCTS_PER_PAGE')))); $this->p = abs((int)(Tools::getValue('p', 1))); - $search = Search::find((int)(self::$cookie->id_lang), $query, false, $this->p, $this->n, $this->orderBy, $this->orderWay, true, (int)$this->id_current_shop); + $search = Search::find((int)(self::$cookie->id_lang), $query, false, $this->p, $this->n, $this->orderBy, $this->orderWay); Module::hookExec('search', array('expr' => $query, 'total' => $search['total'])); $nbProducts = $search['total']; $this->pagination($nbProducts); @@ -90,7 +90,7 @@ class SearchControllerCore extends FrontController { $nbProducts = (int)(Search::searchTag((int)(self::$cookie->id_lang), $tag, true)); $this->pagination($nbProducts); - $result = Search::searchTag((int)(self::$cookie->id_lang), $tag, false, $this->p, $this->n, $this->orderBy, $this->orderWay, true, (int)$this->id_current_shop); + $result = Search::searchTag((int)(self::$cookie->id_lang), $tag, false, $this->p, $this->n, $this->orderBy, $this->orderWay); Module::hookExec('search', array('expr' => $tag, 'total' => sizeof($result))); $this->smarty->assign(array( 'search_tag' => $tag, diff --git a/controllers/StoresController.php b/controllers/StoresController.php index ea9344e4c..457d441b3 100644 --- a/controllers/StoresController.php +++ b/controllers/StoresController.php @@ -51,7 +51,7 @@ class StoresControllerCore extends FrontController LEFT JOIN '._DB_PREFIX_.'store s ON (ss.id_store = s.id_store) LEFT JOIN '._DB_PREFIX_.'country_lang cl ON (cl.id_country = s.id_country) LEFT JOIN '._DB_PREFIX_.'state st ON (st.id_state = s.id_state) - WHERE s.active = 1 AND cl.id_lang = '.(int)$context->language->id.' AND ss.id_shop='.(int)$this->id_current_shop); + WHERE s.active = 1 AND cl.id_lang = '.(int)$context->language->id.' AND ss.id_shop = '.(int)$this->id_current_shop); foreach ($stores AS &$store) $store['has_picture'] = file_exists(_PS_STORE_IMG_DIR_.(int)($store['id_store']).'.jpg'); @@ -66,7 +66,7 @@ class StoresControllerCore extends FrontController LEFT JOIN '._DB_PREFIX_.'store s ON (ss.id_store = s.id_store) LEFT JOIN '._DB_PREFIX_.'country_lang cl ON (cl.id_country = s.id_country) LEFT JOIN '._DB_PREFIX_.'state st ON (st.id_state = s.id_state) - WHERE s.active = 1 AND cl.id_lang = '.(int)$context->language->id.' AND ss.id_shop='.(int)$this->id_current_shop); + WHERE s.active = 1 AND cl.id_lang = '.(int)$context->language->id.' AND ss.id_shop = '.(int)$this->id_current_shop); } else { @@ -80,7 +80,7 @@ class StoresControllerCore extends FrontController LEFT JOIN '._DB_PREFIX_.'store s ON (ss.id_store = s.id_store) LEFT JOIN '._DB_PREFIX_.'country_lang cl ON (cl.id_country = s.id_country) LEFT JOIN '._DB_PREFIX_.'state st ON (st.id_state = s.id_state) - WHERE s.active = 1 AND cl.id_lang = '.(int)$context->language->id.' AND ss.id_shop='.(int)$this->id_current_shop.' + WHERE s.active = 1 AND cl.id_lang = '.(int)$context->language->id.' AND ss.id_shop = '.(int)$this->id_current_shop.' HAVING distance < '.(int)($distance).' ORDER BY distance ASC LIMIT 0,20'); diff --git a/modules/blockcms/blockcms.php b/modules/blockcms/blockcms.php index 7f02a8cb0..a64ffa8fd 100755 --- a/modules/blockcms/blockcms.php +++ b/modules/blockcms/blockcms.php @@ -184,7 +184,7 @@ class BlockCms extends Module FROM `'._DB_PREFIX_.'cms_block` bc INNER JOIN `'._DB_PREFIX_.'cms_category_lang` ccl ON (bc.`id_cms_category` = ccl.`id_cms_category`) INNER JOIN `'._DB_PREFIX_.'cms_block_lang` bcl ON (bc.`id_cms_block` = bcl.`id_cms_block`) - WHERE bc.`location` = '.(int)($location).' AND ccl.`id_lang` = '.(int)$context->language->id.' AND bcl.`id_lang` = '.(int)$context->language->id.' AND bc.id_shop='.$context->shop->getID().' + WHERE bc.`location` = '.(int)($location).' AND ccl.`id_lang` = '.(int)$context->language->id.' AND bcl.`id_lang` = '.(int)$context->language->id.' AND bc.id_shop = '.$context->shop->getID(true).' ORDER BY `position`'); $content = array(); diff --git a/modules/blockmanufacturer/blockmanufacturer.php b/modules/blockmanufacturer/blockmanufacturer.php index 22d44fc25..0c0d80130 100644 --- a/modules/blockmanufacturer/blockmanufacturer.php +++ b/modules/blockmanufacturer/blockmanufacturer.php @@ -55,7 +55,7 @@ class BlockManufacturer extends Module function hookLeftColumn($params) { $context = Context::getContext(); - //$getNbProducts = false, $id_lang = 0, $active = true, $p = false, $n = false, $all_group = false, $id_shop = false + $context->smarty->assign(array( 'manufacturers' => Manufacturer::getManufacturers(false, 0, true, false, false, false, $this->context->shop->getGroupID()), 'link' => $context->link, diff --git a/modules/blocknewsletter/blocknewsletter.php b/modules/blocknewsletter/blocknewsletter.php index 268324f01..45837bb61 100644 --- a/modules/blocknewsletter/blocknewsletter.php +++ b/modules/blocknewsletter/blocknewsletter.php @@ -140,13 +140,13 @@ class Blocknewsletter extends Module { $sql = 'SELECT `email` FROM '._DB_PREFIX_.'newsletter WHERE `email` = \''.pSQL($customerEmail).'\' - AND id_shop = '.$this->context->shop->getID(); + AND id_shop = '.$this->context->shop->getID(true); if (Db::getInstance()->getRow($sql)) return 1; $sql = 'SELECT `newsletter` FROM '._DB_PREFIX_.'customer WHERE `email` = \''.pSQL($customerEmail).'\' - AND id_shop = '.$this->context->shop->getID(); + AND id_shop = '.$this->context->shop->getID(true); if (!$registered = Db::getInstance()->getRow($sql)) return -1; @@ -172,14 +172,14 @@ class Blocknewsletter extends Module /* If the user ins't a customer */ elseif ($registerStatus == 1) { - if (!Db::getInstance()->Execute('DELETE FROM '._DB_PREFIX_.'newsletter WHERE `email` = \''.pSQL($_POST['email']).'\' AND id_shop = '.$this->context->shop->getID())) + if (!Db::getInstance()->Execute('DELETE FROM '._DB_PREFIX_.'newsletter WHERE `email` = \''.pSQL($_POST['email']).'\' AND id_shop = '.$this->context->shop->getID(true))) return $this->error = $this->l('Error during unsubscription'); return $this->valid = $this->l('Unsubscription successful'); } /* If the user is a customer */ elseif ($registerStatus == 2) { - if (!Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'customer SET `newsletter` = 0 WHERE `email` = \''.pSQL($_POST['email']).'\' AND id_shop = '.$this->context->shop->getID())) + if (!Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'customer SET `newsletter` = 0 WHERE `email` = \''.pSQL($_POST['email']).'\' AND id_shop = '.$this->context->shop->getID(true))) return $this->error = $this->l('Error during unsubscription'); return $this->valid = $this->l('Unsubscription successful'); } @@ -210,7 +210,7 @@ class Blocknewsletter extends Module $sql = 'UPDATE '._DB_PREFIX_.'customer SET `newsletter` = 1, newsletter_date_add = NOW(), `ip_registration_newsletter` = \''.pSQL(Tools::getRemoteAddr()).'\' WHERE `email` = \''.pSQL($_POST['email']).'\' - AND id_shop = '.$this->context->shop->getID(); + AND id_shop = '.$this->context->shop->getID(true); if (!Db::getInstance()->Execute($sql)) return $this->error = $this->l('Error during subscription'); $this->sendVoucher(pSQL($_POST['email'])); diff --git a/modules/blockspecials/blockspecials.php b/modules/blockspecials/blockspecials.php index 953618b10..0ba1c874a 100644 --- a/modules/blockspecials/blockspecials.php +++ b/modules/blockspecials/blockspecials.php @@ -86,7 +86,7 @@ class BlockSpecials extends Module if (Configuration::get('PS_CATALOG_MODE')) return ; - if (!$special = Product::getRandomSpecial((int)($params['cookie']->id_lang), false, false, $this->context->shop->getID()) AND !Configuration::get('PS_BLOCK_SPECIALS_DISPLAY')) + if (!$special = Product::getRandomSpecial((int)$params['cookie']->id_lang) AND !Configuration::get('PS_BLOCK_SPECIALS_DISPLAY')) return; $this->context->smarty->assign(array( 'special' => $special, diff --git a/modules/paypal/paypal.php b/modules/paypal/paypal.php index e3d57e52f..554c39af0 100644 --- a/modules/paypal/paypal.php +++ b/modules/paypal/paypal.php @@ -476,7 +476,7 @@ class PayPal extends PaymentModule } - public function validateOrder($id_cart, $id_order_state, $amountPaid, $paymentMethod = 'Unknown', $message = NULL, $extraVars = array(), $currency_special = NULL, $dont_touch_amount = false, $secure_key = false, $id_group_shop = false, $id_shop = false) + public function validateOrder($id_cart, $id_order_state, $amountPaid, $paymentMethod = 'Unknown', $message = NULL, $extraVars = array(), $currency_special = NULL, $dont_touch_amount = false, $secure_key = false) { if (!$this->active) return; diff --git a/modules/themeinstallator/themeinstallator.php b/modules/themeinstallator/themeinstallator.php index 949579ed7..7c7f82709 100644 --- a/modules/themeinstallator/themeinstallator.php +++ b/modules/themeinstallator/themeinstallator.php @@ -460,7 +460,7 @@ class ThemeInstallator extends Module $msg .= '- '.pSQL($row).'
'; Db::getInstance()->Execute(' DELETE FROM `'._DB_PREFIX_.'hook_module` - WHERE `id_module` = '.pSQL($obj->id).' AND id_shop='.$shopID); + WHERE `id_module` = '.pSQL($obj->id).' AND id_shop = '.$shopID); $count = -1; while (isset($hookedModule[++$count])) if ($hookedModule[$count] == $row) @@ -483,7 +483,7 @@ class ThemeInstallator extends Module $msg .= '- '.pSQL($row).'
'; Db::getInstance()->Execute(' DELETE FROM `'._DB_PREFIX_.'module_shop` - WHERE `id_module` = '.pSQL($obj->id).' AND id_shop='.$shopID); + WHERE `id_module` = '.pSQL($obj->id).' AND id_shop = '.$shopID); } } $flag = 0; @@ -506,7 +506,7 @@ class ThemeInstallator extends Module $msg .= '- '.pSQL($row).'
'; Db::getInstance()->Execute(' DELETE FROM `'._DB_PREFIX_.'hook_module` - WHERE `id_module` = '.pSQL($obj->id).' AND id_shop='.$shopID); + WHERE `id_module` = '.pSQL($obj->id).' AND id_shop = '.$shopID); $count = -1; while (isset($hookedModule[++$count])) if ($hookedModule[$count] == $row)