diff --git a/classes/AdminTab.php b/classes/AdminTab.php index dc6833724..9c6c80ee1 100644 --- a/classes/AdminTab.php +++ b/classes/AdminTab.php @@ -1305,10 +1305,10 @@ abstract class AdminTabCore $whereShop = Shop::addSqlRestriction($this->shopShareDatas, 'a', $this->shopLinkType); } - $assos = Shop::getAssoTables(); - if (isset($assos[$this->table]) && $assos[$this->table]['type'] == 'shop') + $asso = Shop::getAssoTable($this->table); + if ($asso !== false && $assos['type'] == 'shop') { - $filterKey = $assos[$this->table]['type']; + $filterKey = $asso['type']; $idenfierShop = Shop::getContextListShopID(); } diff --git a/classes/ObjectModel.php b/classes/ObjectModel.php index 1d5d11da5..e42f2455e 100644 --- a/classes/ObjectModel.php +++ b/classes/ObjectModel.php @@ -467,16 +467,15 @@ abstract class ObjectModelCore if (!$result) return false; - $assos = Shop::getAssoTables(); // Database insertion for multilingual fields related to the object if (!empty($this->def['multilang'])) { $fields = $this->getFieldsLang(); - - // @todo : try to do something better than this - $shops = Shop::getCompleteListOfShopsID(); if ($fields && is_array($fields)) + { + $shops = Shop::getCompleteListOfShopsID(); + $asso = Shop::getAssoTable($this->def['table'].'_lang'); foreach ($fields as $field) { foreach (array_keys($field) as $key) @@ -484,7 +483,7 @@ abstract class ObjectModelCore throw new PrestaShopException('key '.$key.' is not table or identifier, '); $field[$this->def['primary']] = (int)$this->id; - if (isset($assos[$this->def['table'].'_lang']) && $assos[$this->def['table'].'_lang']['type'] == 'fk_shop') + if ($asso !== false && $asso['type'] == 'fk_shop') { foreach ($shops as $id_shop) { @@ -495,6 +494,7 @@ abstract class ObjectModelCore else $result &= Db::getInstance()->insert($this->def['table'].'_lang', $field); } + } } // @hook actionObject*AddAfter @@ -1004,11 +1004,10 @@ abstract class ObjectModelCore public function getWebserviceObjectList($sql_join, $sql_filter, $sql_sort, $sql_limit) { - $assoc = Shop::getAssoTables(); - - if (array_key_exists($this->def['table'], $assoc)) + $assoc = Shop::getAssoTable($this->def['table']); + if ($assoc !== false) { - $multi_shop_join = ' LEFT JOIN `'._DB_PREFIX_.bqSQL($this->def['table']).'_'.bqSQL($assoc[$this->def['table']]['type']).'` + $multi_shop_join = ' LEFT JOIN `'._DB_PREFIX_.bqSQL($this->def['table']).'_'.bqSQL($assoc['type']).'` AS multi_shop_'.bqSQL($this->def['table']).' ON (main.'.bqSQL($this->def['primary']).' = multi_shop_'.bqSQL($this->def['table']).'.'.bqSQL($this->def['primary']).')'; $class_name = WebserviceRequest::$ws_current_classname; diff --git a/classes/shop/Shop.php b/classes/shop/Shop.php index 060ee9f77..7b7e2f105 100644 --- a/classes/shop/Shop.php +++ b/classes/shop/Shop.php @@ -85,44 +85,8 @@ class ShopCore extends ObjectModel /** @var array List of shops cached */ protected static $shops; - public static $asso_tables = array( - 'carrier' => array('type' => 'shop'), - 'carrier_lang' => array('type' => 'fk_shop'), - 'category' => array('type' => 'shop'), - 'category_lang' => array('type' => 'fk_shop'), - 'cms' => array('type' => 'shop'), - 'contact' => array('type' => 'shop'), - 'country' => array('type' => 'shop'), - 'currency' => array('type' => 'shop'), - 'employee' => array('type' => 'shop'), - 'hook_module' => array('type' => 'fk_shop'), - 'hook_module_exceptions' => array('type' => 'fk_shop', 'primary' => 'id_hook_module_exceptions'), - 'image' => array('type' => 'shop'), - 'lang' => array('type' => 'shop'), - 'meta_lang' => array('type' => 'fk_shop'), - 'module' => array('type' => 'shop'), - 'module_currency' => array('type' => 'fk_shop'), - 'module_country' => array('type' => 'fk_shop'), - 'module_group' => array('type' => 'fk_shop'), - 'product' => array('type' => 'shop'), - 'product_attribute' => array('type' => 'shop'), - 'product_lang' => array('type' => 'fk_shop'), - 'referrer' => array('type' => 'shop'), - 'scene' => array('type' => 'shop'), - 'store' => array('type' => 'shop'), - 'webservice_account' => array('type' => 'shop'), - 'warehouse' => array('type' => 'shop'), - 'stock_available' => array('type' => 'fk_shop'), - 'carrier_tax_rules_group_shop' => array('type' => 'fk_shop'), - 'attribute' => array('type' => 'shop'), - 'feature' => array('type' => 'shop'), - 'group' => array('type' => 'shop'), - 'attribute_group' => array('type' => 'shop'), - 'tax_rules_group' => array('type' => 'shop'), - 'zone' => array('type' => 'shop'), - 'manufacturer' => array('type' => 'shop'), - 'supplier' => array('type' => 'shop'), - ); + protected static $asso_tables = array(); + protected static $initialized = false; protected $webserviceParameters = array( 'fields' => array( @@ -167,6 +131,53 @@ class ShopCore extends ObjectModel if ($this->id) $this->setUrl(); } + + /** + * Initialize an array with all the multistore associations in the database + * You can override this method in order to add a new association + */ + protected static function init() + { + Shop::$asso_tables = array( + 'carrier' => array('type' => 'shop'), + 'carrier_lang' => array('type' => 'fk_shop'), + 'category' => array('type' => 'shop'), + 'category_lang' => array('type' => 'fk_shop'), + 'cms' => array('type' => 'shop'), + 'contact' => array('type' => 'shop'), + 'country' => array('type' => 'shop'), + 'currency' => array('type' => 'shop'), + 'employee' => array('type' => 'shop'), + 'hook_module' => array('type' => 'fk_shop'), + 'hook_module_exceptions' => array('type' => 'fk_shop', 'primary' => 'id_hook_module_exceptions'), + 'image' => array('type' => 'shop'), + 'lang' => array('type' => 'shop'), + 'meta_lang' => array('type' => 'fk_shop'), + 'module' => array('type' => 'shop'), + 'module_currency' => array('type' => 'fk_shop'), + 'module_country' => array('type' => 'fk_shop'), + 'module_group' => array('type' => 'fk_shop'), + 'product' => array('type' => 'shop'), + 'product_attribute' => array('type' => 'shop'), + 'product_lang' => array('type' => 'fk_shop'), + 'referrer' => array('type' => 'shop'), + 'scene' => array('type' => 'shop'), + 'store' => array('type' => 'shop'), + 'webservice_account' => array('type' => 'shop'), + 'warehouse' => array('type' => 'shop'), + 'stock_available' => array('type' => 'fk_shop'), + 'carrier_tax_rules_group_shop' => array('type' => 'fk_shop'), + 'attribute' => array('type' => 'shop'), + 'feature' => array('type' => 'shop'), + 'group' => array('type' => 'shop'), + 'attribute_group' => array('type' => 'shop'), + 'tax_rules_group' => array('type' => 'shop'), + 'zone' => array('type' => 'shop'), + 'manufacturer' => array('type' => 'shop'), + 'supplier' => array('type' => 'shop'), + ); + Shop::$initialized = true; + } public function setUrl() { @@ -480,6 +491,18 @@ class ShopCore extends ObjectModel return $this->id == Configuration::get('PS_SHOP_DEFAULT'); } + /** + * Get the associated table if available + * + * @return array + */ + public static function getAssoTable($table) + { + if (!Shop::$initialized) + Shop::init(); + return (isset(Shop::$asso_tables[$table]) ? Shop::$asso_tables[$table] : false); + } + /** * Get list of associated tables to shop * @@ -487,6 +510,8 @@ class ShopCore extends ObjectModel */ public static function getAssoTables() { + if (!Shop::$initialized) + Shop::init(); return Shop::$asso_tables; } @@ -498,6 +523,8 @@ class ShopCore extends ObjectModel */ public static function isTableAssociated($table) { + if (!Shop::$initialized) + Shop::init(); return isset(Shop::$asso_tables[$table]) && Shop::$asso_tables[$table]['type'] == 'shop'; } @@ -854,8 +881,8 @@ class ShopCore extends ObjectModel if (strpos($table, '.') !== false) list($table_alias, $table) = explode('.', $table); - $asso_tables = Shop::getAssoTables(); - if (!isset($asso_tables[$table]) || $asso_tables[$table]['type'] != 'shop') + $asso_table = Shop::getAssoTable($table); + if ($asso_table === false || $asso_table['type'] != 'shop') return; $sql = (($inner_join) ? ' INNER' : ' LEFT').' JOIN '._DB_PREFIX_.$table.'_shop '.$table_alias.' diff --git a/classes/webservice/WebserviceRequest.php b/classes/webservice/WebserviceRequest.php index 74fb8a0b3..15cedf246 100644 --- a/classes/webservice/WebserviceRequest.php +++ b/classes/webservice/WebserviceRequest.php @@ -1213,11 +1213,11 @@ class WebserviceRequestCore $this->fieldsToDisplay = 'full'; // Check if Object is accessible for this/those id_shop - $assoc = Shop::getAssoTables(); - if (array_key_exists($this->resourceConfiguration['retrieveData']['table'] ,$assoc)) + $assoc = Shop::getAssoTable($this->resourceConfiguration['retrieveData']['table']); + if ($assoc !== false) { $sql = 'SELECT 1 - FROM '.bqSQL(_DB_PREFIX_.$this->resourceConfiguration['retrieveData']['table'].'_'.$assoc[$this->resourceConfiguration['retrieveData']['table']]['type']).' '; + FROM '.bqSQL(_DB_PREFIX_.$this->resourceConfiguration['retrieveData']['table'].'_'.$assoc['type']).' '; foreach (self::$shopIDs as $id_shop) $OR[] = ' id_shop = '.(int)$id_shop.' '; $check = ' WHERE ('.implode('OR', $OR).') AND '.bqSQL($this->resourceConfiguration['fields']['id']['sqlId']).' = '.(int)$this->urlSegment[1]; @@ -1513,11 +1513,11 @@ class WebserviceRequestCore return false; } } - $assoc = Shop::getAssoTables(); - if (array_key_exists($this->resourceConfiguration['retrieveData']['table'] ,$assoc)) + $assoc = Shop::getAssoTable($this->resourceConfiguration['retrieveData']['table']); + if ($assoc !== false) { // PUT nor POST is destructive, no deletion - $sql = 'INSERT IGNORE INTO `'.bqSQL(_DB_PREFIX_.$this->resourceConfiguration['retrieveData']['table'].'_'.$assoc[$this->resourceConfiguration['retrieveData']['table']]['type']).'` (id_shop, '.pSQL($this->resourceConfiguration['fields']['id']['sqlId']).') VALUES '; + $sql = 'INSERT IGNORE INTO `'.bqSQL(_DB_PREFIX_.$this->resourceConfiguration['retrieveData']['table'].'_'.$assoc['type']).'` (id_shop, '.pSQL($this->resourceConfiguration['fields']['id']['sqlId']).') VALUES '; foreach (self::$shopIDs as $id) { $sql .= '('.(int)$id.','.(int)$object->id.')'; diff --git a/install-dev/classes/xmlLoader.php b/install-dev/classes/xmlLoader.php index 5462049ae..a45472d0c 100644 --- a/install-dev/classes/xmlLoader.php +++ b/install-dev/classes/xmlLoader.php @@ -468,8 +468,8 @@ class InstallXmlLoader } // Store INSERT queries for _shop associations - $asso_shop = Shop::getAssoTables(); - if (isset($asso_shop[$entity]) && $asso_shop[$entity]['type'] == 'shop') + $entity_asso = Shop::getAssoTable($entity); + if ($entity_asso !== false && $entity_asso['type'] == 'shop') $this->delayed_inserts[$entity.'_shop'][] = array( 'id_shop' => 1, 'id_'.$entity => $entity_id,