From 3a2bb555ebadc7b1b86114d8d3beae582e28d5d6 Mon Sep 17 00:00:00 2001 From: rMalie Date: Wed, 18 Apr 2012 09:57:19 +0000 Subject: [PATCH] // Some optimization #PI-121 --- admin-dev/init.php | 2 +- classes/Configuration.php | 164 ++++++++---------- classes/Employee.php | 4 +- classes/controller/AdminController.php | 2 +- classes/shop/Shop.php | 26 --- classes/stock/StockAvailable.php | 2 +- config/defines.inc.php | 2 +- .../upgrade/php/generate_tax_rules.php | 2 +- 8 files changed, 82 insertions(+), 122 deletions(-) diff --git a/admin-dev/init.php b/admin-dev/init.php index 569070271..7c85cd355 100644 --- a/admin-dev/init.php +++ b/admin-dev/init.php @@ -119,7 +119,7 @@ try } else if ($context->employee->id_profile == _PS_ADMIN_PROFILE_) $shop_id = ''; - else if ($context->shop->getTotalShopsWhoExists() != Employee::getTotalEmployeeShopById((int)$context->employee->id)) + else if (Shop::getTotalShops(false) != Employee::getTotalEmployeeShopById((int)$context->employee->id)) { $shops = Employee::getEmployeeShopById((int)$context->employee->id); if (count($shops)) diff --git a/classes/Configuration.php b/classes/Configuration.php index 6b0c88050..d529d40f1 100644 --- a/classes/Configuration.php +++ b/classes/Configuration.php @@ -88,16 +88,20 @@ class ConfigurationCore extends ObjectModel * Return ID a configuration key * * @param string $key - * @param int $shopGroupID - * @param int $shopID + * @param int $id_shop_group + * @param int $id_shop */ - public static function getIdByName($key, $shopGroupID = null, $shopID = null) + public static function getIdByName($key, $id_shop_group = null, $id_shop = null) { - Configuration::getShopFromContext($shopGroupID, $shopID); + if (is_null($id_shop)) + $id_shop = Shop::getContextShopID(); + if (is_null($id_shop_group)) + $id_shop_group = Shop::getContextShopGroupID(); + $sql = 'SELECT id_configuration FROM '._DB_PREFIX_.'configuration WHERE name = \''.pSQL($key).'\'' - .Configuration::sqlRestriction($shopGroupID, $shopID); + .Configuration::sqlRestriction($id_shop_group, $id_shop); return (int)Db::getInstance()->getValue($sql); } @@ -140,37 +144,41 @@ class ConfigurationCore extends ObjectModel * @param integer $id_lang Language ID * @return string Value */ - public static function get($key, $langID = null, $shopGroupID = null, $shopID = null) + public static function get($key, $id_lang = null, $id_shop_group = null, $id_shop = null) { - Configuration::getShopFromContext($shopGroupID, $shopID); - $langID = (int)$langID; - if (!isset(self::$_CONF[$langID])) - $langID = 0; + $id_lang = (int)$id_lang; + if (is_null($id_shop)) + $id_shop = Shop::getContextShopID(); + if (is_null($id_shop_group)) + $id_shop_group = Shop::getContextShopGroupID(); + + if (!isset(self::$_CONF[$id_lang])) + $id_lang = 0; // If conf if not initialized, try manual query if (!self::$_CONF) return Db::getInstance()->getValue('SELECT `value` FROM '._DB_PREFIX_.'configuration WHERE `name` = \''.pSQL($key).'\''); - if ($shopID && Configuration::hasKey($key, $langID, null, $shopID)) - return self::$_CONF[$langID]['shop'][$shopID][$key]; - else if ($shopGroupID && Configuration::hasKey($key, $langID, $shopGroupID)) - return self::$_CONF[$langID]['group'][$shopGroupID][$key]; - else if (Configuration::hasKey($key, $langID)) - return self::$_CONF[$langID]['global'][$key]; + if ($id_shop && Configuration::hasKey($key, $id_lang, null, $id_shop)) + return self::$_CONF[$id_lang]['shop'][$id_shop][$key]; + else if ($id_shop_group && Configuration::hasKey($key, $id_lang, $id_shop_group)) + return self::$_CONF[$id_lang]['group'][$id_shop_group][$key]; + else if (Configuration::hasKey($key, $id_lang)) + return self::$_CONF[$id_lang]['global'][$key]; return false; } - public static function getGlobalValue($key, $langID = null) + public static function getGlobalValue($key, $id_lang = null) { - return Configuration::get($key, $langID, 0, 0); + return Configuration::get($key, $id_lang, 0, 0); } /** * Get a single configuration value (in multiple languages) * * @param string $key Key wanted - * @param int $shopGroupID - * @param int $shopID + * @param int $id_shop_group + * @param int $id_shop * @return array Values in multiple languages */ public static function getInt($key, $id_shop_group = null, $id_shop = null) @@ -189,17 +197,20 @@ class ConfigurationCore extends ObjectModel * @param integer $id_lang Language ID * @return array Values */ - public static function getMultiple($keys, $langID = null, $shopGroupID = null, $shopID = null) + public static function getMultiple($keys, $id_lang = null, $id_shop_group = null, $id_shop = null) { if (!is_array($keys)) throw new PrestaShopException('keys var is not an array'); - $langID = (int)$langID; - Configuration::getShopFromContext($shopGroupID, $shopID); + $id_lang = (int)$id_lang; + if (is_null($id_shop)) + $id_shop = Shop::getContextShopID(); + if (is_null($id_shop_group)) + $id_shop_group = Shop::getContextShopGroupID(); $results = array(); foreach ($keys as $key) - $results[$key] = Configuration::get($key, $langID, $shopGroupID, $shopID); + $results[$key] = Configuration::get($key, $id_lang, $id_shop_group, $id_shop); return $results; } @@ -208,18 +219,18 @@ class ConfigurationCore extends ObjectModel * * @param string $key * @param int $id_lang - * @param int $shopGroupID - * @param int $shopID + * @param int $id_shop_group + * @param int $id_shop * @return bool */ - public static function hasKey($key, $langID = null, $shopGroupID = null, $shopID = null) + public static function hasKey($key, $id_lang = null, $id_shop_group = null, $id_shop = null) { - $langID = (int)$langID; - if ($shopID) - return isset(self::$_CONF[$langID]['shop'][$shopID]) && array_key_exists($key, self::$_CONF[$langID]['shop'][$shopID]); - else if ($shopGroupID) - return isset(self::$_CONF[$langID]['group'][$shopGroupID]) && array_key_exists($key, self::$_CONF[$langID]['group'][$shopGroupID]); - return isset(self::$_CONF[$langID]['global']) && array_key_exists($key, self::$_CONF[$langID]['global']); + $id_lang = (int)$id_lang; + if ($id_shop) + return isset(self::$_CONF[$id_lang]['shop'][$id_shop]) && array_key_exists($key, self::$_CONF[$id_lang]['shop'][$id_shop]); + else if ($id_shop_group) + return isset(self::$_CONF[$id_lang]['group'][$id_shop_group]) && array_key_exists($key, self::$_CONF[$id_lang]['group'][$id_shop_group]); + return isset(self::$_CONF[$id_lang]['global']) && array_key_exists($key, self::$_CONF[$id_lang]['global']); } /** @@ -227,14 +238,18 @@ class ConfigurationCore extends ObjectModel * * @param string $key Key wanted * @param mixed $values $values is an array if the configuration is multilingual, a single string else. - * @param int $shopGroupID - * @param int $shopID + * @param int $id_shop_group + * @param int $id_shop */ public static function set($key, $values, $id_shop_group = null, $id_shop = null) { if (!Validate::isConfigName($key)) die(Tools::displayError()); - Configuration::getShopFromContext($id_shop_group, $id_shop); + + if (is_null($id_shop)) + $id_shop = Shop::getContextShopID(); + if (is_null($id_shop_group)) + $id_shop_group = Shop::getContextShopGroupID(); if (!is_array($values)) $values = array($values); @@ -269,15 +284,19 @@ class ConfigurationCore extends ObjectModel * @param string $key Key * @param mixed $values $values is an array if the configuration is multilingual, a single string else. * @param boolean $html Specify if html is authorized in value - * @param int $shopGroupID - * @param int $shopID + * @param int $id_shop_group + * @param int $id_shop * @return boolean Update result */ - public static function updateValue($key, $values, $html = false, $shopGroupID = null, $shopID = null) + public static function updateValue($key, $values, $html = false, $id_shop_group = null, $id_shop = null) { if (!Validate::isConfigName($key)) die(Tools::displayError()); - Configuration::getShopFromContext($shopGroupID, $shopID); + + if (is_null($id_shop)) + $id_shop = Shop::getContextShopID(); + if (is_null($id_shop_group)) + $id_shop_group = Shop::getContextShopGroupID(); if (!is_array($values)) $values = array($values); @@ -285,11 +304,11 @@ class ConfigurationCore extends ObjectModel $result = true; foreach ($values as $lang => $value) { - if ($value === Configuration::get($key, $lang, $shopGroupID, $shopID)) + if ($value === Configuration::get($key, $lang, $id_shop_group, $id_shop)) continue; // If key already exists, update value - if (Configuration::hasKey($key, $lang, $shopGroupID, $shopID)) + if (Configuration::hasKey($key, $lang, $id_shop_group, $id_shop)) { if (!$lang) { @@ -297,7 +316,7 @@ class ConfigurationCore extends ObjectModel $result &= Db::getInstance()->update('configuration', array( 'value' => pSQL($value, $html), 'date_upd' => date('Y-m-d H:i:s'), - ), '`name` = \''.pSQL($key).'\''.Configuration::sqlRestriction($shopGroupID, $shopID), true, true); + ), '`name` = \''.pSQL($key).'\''.Configuration::sqlRestriction($id_shop_group, $id_shop), true, true); } else { @@ -310,7 +329,7 @@ class ConfigurationCore extends ObjectModel SELECT c.id_configuration FROM '._DB_PREFIX_.'configuration c WHERE c.name = \''.pSQL($key).'\'' - .Configuration::sqlRestriction($shopGroupID, $shopID) + .Configuration::sqlRestriction($id_shop_group, $id_shop) .')'; $result &= Db::getInstance()->execute($sql); } @@ -318,14 +337,14 @@ class ConfigurationCore extends ObjectModel // If key does not exists, create it else { - if (!$configID = Configuration::getIdByName($key, $shopGroupID, $shopID)) + if (!$configID = Configuration::getIdByName($key, $id_shop_group, $id_shop)) { $newConfig = new Configuration(); $newConfig->name = $key; - if ($shopID) - $newConfig->id_shop = (int)$shopID; - if ($shopGroupID) - $newConfig->id_shop_group = (int)$shopGroupID; + if ($id_shop) + $newConfig->id_shop = (int)$id_shop; + if ($id_shop_group) + $newConfig->id_shop_group = (int)$id_shop_group; if (!$lang) $newConfig->value = $value; $result &= $newConfig->add(true, true); @@ -343,7 +362,7 @@ class ConfigurationCore extends ObjectModel } } - Configuration::set($key, $value, $shopGroupID, $shopID); + Configuration::set($key, $value, $id_shop_group, $id_shop); } return $result; @@ -469,51 +488,18 @@ class ConfigurationCore extends ObjectModel } /** - * Fill $id_shop_group and $id_shop vars from correct context + * Add SQL restriction on shops for configuration table * * @param int $id_shop_group * @param int $id_shop - */ - protected static function getShopFromContext(&$id_shop_group, &$id_shop) - { - if (!Shop::isFeatureActive()) - return; - - if (Shop::getContext() == Shop::CONTEXT_ALL) - $shop_id = $shop_group_id = null; - else if (Shop::getContext() == Shop::CONTEXT_GROUP) - { - $shop_group_id = Shop::getContextShopGroupID(); - $shop_id = null; - } - else - { - $shop_group_id = Shop::getContextShopGroupID(); - $shop_id = Shop::getContextShopID(); - } - - if (is_null($id_shop)) - $id_shop = $shop_id; - if (is_null($id_shop_group)) - $id_shop_group = $shop_group_id; - - $id_shop = (int)$id_shop; - $id_shop_group = (int)$id_shop_group; - } - - /** - * Add SQL restriction on shops for configuration table - * - * @param int $shopGroupID - * @param int $shopID * @return string */ - protected static function sqlRestriction($shopGroupID, $shopID) + protected static function sqlRestriction($id_shop_group, $id_shop) { - if ($shopID) - return ' AND id_shop = '.$shopID; - else if ($shopGroupID) - return ' AND id_shop_group = '.$shopGroupID.' AND id_shop IS NULL'; + if ($id_shop) + return ' AND id_shop = '.$id_shop; + else if ($id_shop_group) + return ' AND id_shop_group = '.$id_shop_group.' AND id_shop IS NULL'; else return ' AND id_shop_group IS NULL AND id_shop IS NULL'; } diff --git a/classes/Employee.php b/classes/Employee.php index 761211dcc..64668088a 100644 --- a/classes/Employee.php +++ b/classes/Employee.php @@ -272,7 +272,7 @@ class EmployeeCore extends ObjectModel case Shop::CONTEXT_GROUP: if ($context->shop->checkIfShopGroupExist(Shop::getContextShopGroupID())) { - $shops = $context->shop->getIdShopsByIdShopGroup(Shop::getContextShopGroupID()); + $shops = Shop::getShops(false, Shop::getContextShopGroupID(), true); foreach ($shops as $shop) if (!in_array($shop, Employee::getEmployeeShopById($id_employee))) return false; @@ -283,7 +283,7 @@ class EmployeeCore extends ObjectModel case Shop::CONTEXT_ALL: if ($context->employee->id_profile == _PS_ADMIN_PROFILE_ || - $context->shop->getTotalShopsWhoExists() == Employee::getTotalEmployeeShopById($id_employee)) + Shop::getTotalShops(false) == Employee::getTotalEmployeeShopById($id_employee)) return true; else return false; diff --git a/classes/controller/AdminController.php b/classes/controller/AdminController.php index f04852724..717f9112d 100644 --- a/classes/controller/AdminController.php +++ b/classes/controller/AdminController.php @@ -1662,7 +1662,7 @@ class AdminControllerCore extends Controller } elseif ($this->context->employee->id_profile == _PS_ADMIN_PROFILE_) $shop_id = ''; - elseif ($this->context->shop->getTotalShopsWhoExists() != Employee::getTotalEmployeeShopById((int)$this->context->employee->id)) + elseif (Shop::getTotalShops(false) != Employee::getTotalEmployeeShopById((int)$this->context->employee->id)) { $shops = Employee::getEmployeeShopById((int)$this->context->employee->id); if (count($shops)) diff --git a/classes/shop/Shop.php b/classes/shop/Shop.php index 4de81ff6c..023c6ea0e 100644 --- a/classes/shop/Shop.php +++ b/classes/shop/Shop.php @@ -491,7 +491,6 @@ class ShopCore extends ObjectModel 'name' => $row['group_name'], 'share_customer' => $row['share_customer'], 'share_order' => $row['share_order'], - 'totalShops' => Shop::getTotalShopsByIdShopGroup($row['id_shop_group']), 'shops' => array(), ); @@ -573,31 +572,6 @@ class ShopCore extends ObjectModel return count(Shop::getShops($active)); } - /** - * @return int Total of shops - */ - public static function getTotalShopsWhoExists() - { - return (int)Db::getInstance()->getValue('SELECT COUNT(*) FROM `'._DB_PREFIX_.'shop`'); - } - - /** - * @return int Total of shops - */ - public static function getTotalShopsByIdShopGroup($id) - { - return (int)Db::getInstance()->getValue('SELECT COUNT(*) FROM `'._DB_PREFIX_.'shop` WHERE `id_shop_group` = '.(int)$id); - } - - public static function getIdShopsByIdShopGroup($id) - { - $result = Db::getInstance()->executeS('SELECT `id_shop`, `id_shop_group` FROM `'._DB_PREFIX_.'shop` WHERE `id_shop_group` = '.(int)$id); - $data = array(); - foreach ($result as $group_data) - $data[] = (int)$group_data['id_shop']; - return $data; - } - /** * Retrieve group ID of a shop * diff --git a/classes/stock/StockAvailable.php b/classes/stock/StockAvailable.php index 51c98a715..2e30feb55 100644 --- a/classes/stock/StockAvailable.php +++ b/classes/stock/StockAvailable.php @@ -474,7 +474,7 @@ class StockAvailableCore extends ObjectModel { if ($shop_group->share_stock) { - $shop_list = Shop::getIdShopsByIdShopGroup($shop_group->id); + $shop_list = Shop::getShops(false, $shop_group->id, true); if (count($shop_list) > 0) { diff --git a/config/defines.inc.php b/config/defines.inc.php index e61acc65c..e5fae2103 100755 --- a/config/defines.inc.php +++ b/config/defines.inc.php @@ -26,7 +26,7 @@ */ define('_PS_MODE_DEV_', true); -define('_PS_DEBUG_PROFILING_', false); +define('_PS_DEBUG_PROFILING_', true); define('_PS_MODE_DEMO_', false); $currentDir = dirname(__FILE__); diff --git a/install-dev/upgrade/php/generate_tax_rules.php b/install-dev/upgrade/php/generate_tax_rules.php index e3e60deb9..d8eced1e8 100644 --- a/install-dev/upgrade/php/generate_tax_rules.php +++ b/install-dev/upgrade/php/generate_tax_rules.php @@ -42,7 +42,7 @@ function generate_tax_rules() 'id_tax_rules_group' => $id_tax, 'name' => 'Rule '.$tax['rate'].'%', ); - $res &= Db::getInstance()->AutoExecute(_DB_PREFIX_.'tax_rules_group', $row, 'INSERT'); + $res &= Db::getInstance()->insert('tax_rules_group', $row); $id_tax_rules_group = Db::getInstance()->insert_id();