// Some optimization #PI-121

This commit is contained in:
rMalie
2012-04-18 09:57:19 +00:00
parent f045723c3c
commit 3a2bb555eb
8 changed files with 82 additions and 122 deletions
+1 -1
View File
@@ -119,7 +119,7 @@ try
} }
else if ($context->employee->id_profile == _PS_ADMIN_PROFILE_) else if ($context->employee->id_profile == _PS_ADMIN_PROFILE_)
$shop_id = ''; $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); $shops = Employee::getEmployeeShopById((int)$context->employee->id);
if (count($shops)) if (count($shops))
+75 -89
View File
@@ -88,16 +88,20 @@ class ConfigurationCore extends ObjectModel
* Return ID a configuration key * Return ID a configuration key
* *
* @param string $key * @param string $key
* @param int $shopGroupID * @param int $id_shop_group
* @param int $shopID * @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 $sql = 'SELECT id_configuration
FROM '._DB_PREFIX_.'configuration FROM '._DB_PREFIX_.'configuration
WHERE name = \''.pSQL($key).'\'' WHERE name = \''.pSQL($key).'\''
.Configuration::sqlRestriction($shopGroupID, $shopID); .Configuration::sqlRestriction($id_shop_group, $id_shop);
return (int)Db::getInstance()->getValue($sql); return (int)Db::getInstance()->getValue($sql);
} }
@@ -140,37 +144,41 @@ class ConfigurationCore extends ObjectModel
* @param integer $id_lang Language ID * @param integer $id_lang Language ID
* @return string Value * @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); $id_lang = (int)$id_lang;
$langID = (int)$langID; if (is_null($id_shop))
if (!isset(self::$_CONF[$langID])) $id_shop = Shop::getContextShopID();
$langID = 0; 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 conf if not initialized, try manual query
if (!self::$_CONF) if (!self::$_CONF)
return Db::getInstance()->getValue('SELECT `value` FROM '._DB_PREFIX_.'configuration WHERE `name` = \''.pSQL($key).'\''); return Db::getInstance()->getValue('SELECT `value` FROM '._DB_PREFIX_.'configuration WHERE `name` = \''.pSQL($key).'\'');
if ($shopID && Configuration::hasKey($key, $langID, null, $shopID)) if ($id_shop && Configuration::hasKey($key, $id_lang, null, $id_shop))
return self::$_CONF[$langID]['shop'][$shopID][$key]; return self::$_CONF[$id_lang]['shop'][$id_shop][$key];
else if ($shopGroupID && Configuration::hasKey($key, $langID, $shopGroupID)) else if ($id_shop_group && Configuration::hasKey($key, $id_lang, $id_shop_group))
return self::$_CONF[$langID]['group'][$shopGroupID][$key]; return self::$_CONF[$id_lang]['group'][$id_shop_group][$key];
else if (Configuration::hasKey($key, $langID)) else if (Configuration::hasKey($key, $id_lang))
return self::$_CONF[$langID]['global'][$key]; return self::$_CONF[$id_lang]['global'][$key];
return false; 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) * Get a single configuration value (in multiple languages)
* *
* @param string $key Key wanted * @param string $key Key wanted
* @param int $shopGroupID * @param int $id_shop_group
* @param int $shopID * @param int $id_shop
* @return array Values in multiple languages * @return array Values in multiple languages
*/ */
public static function getInt($key, $id_shop_group = null, $id_shop = null) 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 * @param integer $id_lang Language ID
* @return array Values * @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)) if (!is_array($keys))
throw new PrestaShopException('keys var is not an array'); throw new PrestaShopException('keys var is not an array');
$langID = (int)$langID; $id_lang = (int)$id_lang;
Configuration::getShopFromContext($shopGroupID, $shopID); if (is_null($id_shop))
$id_shop = Shop::getContextShopID();
if (is_null($id_shop_group))
$id_shop_group = Shop::getContextShopGroupID();
$results = array(); $results = array();
foreach ($keys as $key) 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; return $results;
} }
@@ -208,18 +219,18 @@ class ConfigurationCore extends ObjectModel
* *
* @param string $key * @param string $key
* @param int $id_lang * @param int $id_lang
* @param int $shopGroupID * @param int $id_shop_group
* @param int $shopID * @param int $id_shop
* @return bool * @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; $id_lang = (int)$id_lang;
if ($shopID) if ($id_shop)
return isset(self::$_CONF[$langID]['shop'][$shopID]) && array_key_exists($key, self::$_CONF[$langID]['shop'][$shopID]); return isset(self::$_CONF[$id_lang]['shop'][$id_shop]) && array_key_exists($key, self::$_CONF[$id_lang]['shop'][$id_shop]);
else if ($shopGroupID) else if ($id_shop_group)
return isset(self::$_CONF[$langID]['group'][$shopGroupID]) && array_key_exists($key, self::$_CONF[$langID]['group'][$shopGroupID]); 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[$langID]['global']) && array_key_exists($key, self::$_CONF[$langID]['global']); 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 string $key Key wanted
* @param mixed $values $values is an array if the configuration is multilingual, a single string else. * @param mixed $values $values is an array if the configuration is multilingual, a single string else.
* @param int $shopGroupID * @param int $id_shop_group
* @param int $shopID * @param int $id_shop
*/ */
public static function set($key, $values, $id_shop_group = null, $id_shop = null) public static function set($key, $values, $id_shop_group = null, $id_shop = null)
{ {
if (!Validate::isConfigName($key)) if (!Validate::isConfigName($key))
die(Tools::displayError()); 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)) if (!is_array($values))
$values = array($values); $values = array($values);
@@ -269,15 +284,19 @@ class ConfigurationCore extends ObjectModel
* @param string $key Key * @param string $key Key
* @param mixed $values $values is an array if the configuration is multilingual, a single string else. * @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 boolean $html Specify if html is authorized in value
* @param int $shopGroupID * @param int $id_shop_group
* @param int $shopID * @param int $id_shop
* @return boolean Update result * @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)) if (!Validate::isConfigName($key))
die(Tools::displayError()); 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)) if (!is_array($values))
$values = array($values); $values = array($values);
@@ -285,11 +304,11 @@ class ConfigurationCore extends ObjectModel
$result = true; $result = true;
foreach ($values as $lang => $value) 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; continue;
// If key already exists, update value // 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) if (!$lang)
{ {
@@ -297,7 +316,7 @@ class ConfigurationCore extends ObjectModel
$result &= Db::getInstance()->update('configuration', array( $result &= Db::getInstance()->update('configuration', array(
'value' => pSQL($value, $html), 'value' => pSQL($value, $html),
'date_upd' => date('Y-m-d H:i:s'), '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 else
{ {
@@ -310,7 +329,7 @@ class ConfigurationCore extends ObjectModel
SELECT c.id_configuration SELECT c.id_configuration
FROM '._DB_PREFIX_.'configuration c FROM '._DB_PREFIX_.'configuration c
WHERE c.name = \''.pSQL($key).'\'' WHERE c.name = \''.pSQL($key).'\''
.Configuration::sqlRestriction($shopGroupID, $shopID) .Configuration::sqlRestriction($id_shop_group, $id_shop)
.')'; .')';
$result &= Db::getInstance()->execute($sql); $result &= Db::getInstance()->execute($sql);
} }
@@ -318,14 +337,14 @@ class ConfigurationCore extends ObjectModel
// If key does not exists, create it // If key does not exists, create it
else else
{ {
if (!$configID = Configuration::getIdByName($key, $shopGroupID, $shopID)) if (!$configID = Configuration::getIdByName($key, $id_shop_group, $id_shop))
{ {
$newConfig = new Configuration(); $newConfig = new Configuration();
$newConfig->name = $key; $newConfig->name = $key;
if ($shopID) if ($id_shop)
$newConfig->id_shop = (int)$shopID; $newConfig->id_shop = (int)$id_shop;
if ($shopGroupID) if ($id_shop_group)
$newConfig->id_shop_group = (int)$shopGroupID; $newConfig->id_shop_group = (int)$id_shop_group;
if (!$lang) if (!$lang)
$newConfig->value = $value; $newConfig->value = $value;
$result &= $newConfig->add(true, true); $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; 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_group
* @param int $id_shop * @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 * @return string
*/ */
protected static function sqlRestriction($shopGroupID, $shopID) protected static function sqlRestriction($id_shop_group, $id_shop)
{ {
if ($shopID) if ($id_shop)
return ' AND id_shop = '.$shopID; return ' AND id_shop = '.$id_shop;
else if ($shopGroupID) else if ($id_shop_group)
return ' AND id_shop_group = '.$shopGroupID.' AND id_shop IS NULL'; return ' AND id_shop_group = '.$id_shop_group.' AND id_shop IS NULL';
else else
return ' AND id_shop_group IS NULL AND id_shop IS NULL'; return ' AND id_shop_group IS NULL AND id_shop IS NULL';
} }
+2 -2
View File
@@ -272,7 +272,7 @@ class EmployeeCore extends ObjectModel
case Shop::CONTEXT_GROUP: case Shop::CONTEXT_GROUP:
if ($context->shop->checkIfShopGroupExist(Shop::getContextShopGroupID())) if ($context->shop->checkIfShopGroupExist(Shop::getContextShopGroupID()))
{ {
$shops = $context->shop->getIdShopsByIdShopGroup(Shop::getContextShopGroupID()); $shops = Shop::getShops(false, Shop::getContextShopGroupID(), true);
foreach ($shops as $shop) foreach ($shops as $shop)
if (!in_array($shop, Employee::getEmployeeShopById($id_employee))) if (!in_array($shop, Employee::getEmployeeShopById($id_employee)))
return false; return false;
@@ -283,7 +283,7 @@ class EmployeeCore extends ObjectModel
case Shop::CONTEXT_ALL: case Shop::CONTEXT_ALL:
if ($context->employee->id_profile == _PS_ADMIN_PROFILE_ || if ($context->employee->id_profile == _PS_ADMIN_PROFILE_ ||
$context->shop->getTotalShopsWhoExists() == Employee::getTotalEmployeeShopById($id_employee)) Shop::getTotalShops(false) == Employee::getTotalEmployeeShopById($id_employee))
return true; return true;
else else
return false; return false;
+1 -1
View File
@@ -1662,7 +1662,7 @@ class AdminControllerCore extends Controller
} }
elseif ($this->context->employee->id_profile == _PS_ADMIN_PROFILE_) elseif ($this->context->employee->id_profile == _PS_ADMIN_PROFILE_)
$shop_id = ''; $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); $shops = Employee::getEmployeeShopById((int)$this->context->employee->id);
if (count($shops)) if (count($shops))
-26
View File
@@ -491,7 +491,6 @@ class ShopCore extends ObjectModel
'name' => $row['group_name'], 'name' => $row['group_name'],
'share_customer' => $row['share_customer'], 'share_customer' => $row['share_customer'],
'share_order' => $row['share_order'], 'share_order' => $row['share_order'],
'totalShops' => Shop::getTotalShopsByIdShopGroup($row['id_shop_group']),
'shops' => array(), 'shops' => array(),
); );
@@ -573,31 +572,6 @@ class ShopCore extends ObjectModel
return count(Shop::getShops($active)); 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 * Retrieve group ID of a shop
* *
+1 -1
View File
@@ -474,7 +474,7 @@ class StockAvailableCore extends ObjectModel
{ {
if ($shop_group->share_stock) 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) if (count($shop_list) > 0)
{ {
+1 -1
View File
@@ -26,7 +26,7 @@
*/ */
define('_PS_MODE_DEV_', true); define('_PS_MODE_DEV_', true);
define('_PS_DEBUG_PROFILING_', false); define('_PS_DEBUG_PROFILING_', true);
define('_PS_MODE_DEMO_', false); define('_PS_MODE_DEMO_', false);
$currentDir = dirname(__FILE__); $currentDir = dirname(__FILE__);
@@ -42,7 +42,7 @@ function generate_tax_rules()
'id_tax_rules_group' => $id_tax, 'id_tax_rules_group' => $id_tax,
'name' => 'Rule '.$tax['rate'].'%', '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(); $id_tax_rules_group = Db::getInstance()->insert_id();