From 2e08bf999a2897b2b1124a29bdb63bb85c90d6c8 Mon Sep 17 00:00:00 2001 From: rMalie Date: Thu, 18 Aug 2011 07:57:04 +0000 Subject: [PATCH] // Add copy data of group shops --- admin-dev/tabs/AdminGroupShop.php | 56 +++++++++++++++++++++--- admin-dev/tabs/AdminShop.php | 1 - classes/GroupShop.php | 72 ++++++++++++++++++++++--------- classes/Shop.php | 4 +- 4 files changed, 105 insertions(+), 28 deletions(-) diff --git a/admin-dev/tabs/AdminGroupShop.php b/admin-dev/tabs/AdminGroupShop.php index e0ac59eee..8907b5093 100755 --- a/admin-dev/tabs/AdminGroupShop.php +++ b/admin-dev/tabs/AdminGroupShop.php @@ -38,9 +38,10 @@ class AdminGroupShop extends AdminTab $this->deleted = false; $this->fieldsDisplay = array( - 'id_group_shop' => array('title' => $this->l('ID'), 'align' => 'center', 'width' => 25), - 'name' => array('title' => $this->l('County'), 'width' => 130, 'filter_key' => 'b!name'), - 'active' => array('title' => $this->l('Enabled'), 'align' => 'center', 'active' => 'status', 'type' => 'bool', 'orderby' => false, 'filter_key' => 'active')); + 'id_group_shop' => array('title' => $this->l('ID'), 'align' => 'center', 'width' => 25), + 'name' => array('title' => $this->l('County'), 'width' => 130, 'filter_key' => 'b!name'), + 'active' => array('title' => $this->l('Enabled'), 'align' => 'center', 'active' => 'status', 'type' => 'bool', 'orderby' => false, 'filter_key' => 'active'), + ); parent::__construct(); } @@ -60,6 +61,18 @@ class AdminGroupShop extends AdminTab } return parent::postProcess(); } + + public function afterAdd($newGroupShop) + { + if (Tools::getValue('useImportData') && ($importData = Tools::getValue('importData')) && is_array($importData)) + $newGroupShop->copyGroupShopData(Tools::getValue('importFromShop'), $importData); + } + + public function afterUpdate($newGroupShop) + { + if (Tools::getValue('useImportData') && ($importData = Tools::getValue('importData')) && is_array($importData)) + $newGroupShop->copyGroupShopData(Tools::getValue('importFromShop'), $importData); + } public function displayForm($isMainTab = true) { @@ -134,8 +147,41 @@ EOF;
* '.$this->l('Required field').'
- - '; +
'; + + $importData = array( + 'attribute_group' => $this->l('Attribute groups'), + 'attribute' => $this->l('Attributes'), + //'customer_group' => $this->l('Customer groups'), + 'feature' => $this->l('Features'), + 'group' => $this->l('Groups'), + 'manufacturer' => $this->l('Manufacturers'), + 'supplier' => $this->l('Suppliers'), + 'tax_rules_group' => $this->l('Tax rules groups'), + 'zone' => $this->l('Zones'), + ); + + $checked = (Tools::getValue('addgroup_shop') !== false) ? true : false; + $defaultGroup = Shop::getInstance(Configuration::get('PS_SHOP_DEFAULT'))->getGroupID(); + echo '
'.$this->l('Import data from another group shop').''; + echo ''; + echo '
'; + echo ' '; + echo $this->l('Duplicate data from group shop'); + echo ' '; + echo '
    '; + foreach ($importData as $table => $lang) + echo '
  • '; + echo '
'; + echo '

'.$this->l('Use this option to associate data (products, modules, etc.) the same way as the selected shop').'

'; + echo '
+ +
'; + echo '
'; + echo ''; } protected function displayAddButton() diff --git a/admin-dev/tabs/AdminShop.php b/admin-dev/tabs/AdminShop.php index cbf80a1d7..d4a71b395 100755 --- a/admin-dev/tabs/AdminShop.php +++ b/admin-dev/tabs/AdminShop.php @@ -200,7 +200,6 @@ class AdminShop extends AdminTab 'scene' => $this->l('Scenes'), 'stock' => $this->l('Stock'), 'store' => $this->l('Stores'), - 'zone' => $this->l('Zones'), ); $checked = (Tools::getValue('addshop') !== false) ? true : false; diff --git a/classes/GroupShop.php b/classes/GroupShop.php index e773c0035..9a14ae4cf 100755 --- a/classes/GroupShop.php +++ b/classes/GroupShop.php @@ -51,7 +51,7 @@ class GroupShopCore extends ObjectModel private static $assoTables = array( 'attribute_group' => array('type' => 'group_shop'), 'attribute' => array('type' => 'group_shop'), - 'customer_group' => array('type' => 'group_shop'), + //'customer_group' => array('type' => 'group_shop'), 'feature' => array('type' => 'group_shop'), 'group' => array('type' => 'group_shop'), 'manufacturer' => array('type' => 'group_shop'), @@ -87,10 +87,9 @@ class GroupShopCore extends ObjectModel foreach (Shop::getAssoTables() AS $table_name => $row) { - $id = 'id_'.$row['type']; - if ($row['type'] == 'fk_shop') - $id = 'id_shop'; + if ($row['type'] == 'fk_group_shop') + $id = 'id_group_shop'; else $table_name .= '_'.$row['type']; $res &= Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.$table_name.'` WHERE `'.$id.'`='.(int)$this->id); @@ -98,12 +97,12 @@ class GroupShopCore extends ObjectModel return $res; } - + public static function getAssoTables() { return self::$assoTables; } - + /** * @return int Total of groupshops */ @@ -111,24 +110,20 @@ class GroupShopCore extends ObjectModel { return sizeof(GroupShop::getGroupShops($active)); } - - public function copyGroupShopData($old_id) - { - //TODO - } - + public function haveShops() { - return (bool)$this->getNbShops(); + return (bool)$this->getTotalShops(); } - - public function getNbShops() + + public function getTotalShops() { - return (int)Db::getInstance()->getValue('SELECT COUNT(*) - FROM '._DB_PREFIX_.'shop s - WHERE id_group_shop='.(int)$this->id); + $sql = 'SELECT COUNT(*) + FROM '._DB_PREFIX_.'shop s + WHERE id_group_shop='.(int)$this->id; + return (int)Db::getInstance()->getValue($sql); } - + /** * Return a group shop ID from group shop name * @@ -137,8 +132,45 @@ class GroupShopCore extends ObjectModel */ public static function getIdByName($name) { - $sql = 'SELECT id_group_shop FROM '._DB_PREFIX_.'group_shop + $sql = 'SELECT id_group_shop + FROM '._DB_PREFIX_.'group_shop WHERE name = \''.pSQL($name).'\''; return (int)Db::getInstance()->getValue($sql); } + + public function copyGroupShopData($old_id, $tables_import = false, $deleted = false) + { + foreach (GroupShop::getAssoTables() AS $table_name => $row) + { + if ($tables_import && !isset($tables_import[$table_name])) + continue; + + $id = 'id_'.$row['type']; + if ($row['type'] == 'fk_group_shop') + $id = 'id_group_shop'; + else + $table_name .= '_'.$row['type']; + + if (!$deleted) + { + $res = Db::getInstance()->getRow('SELECT * FROM `'._DB_PREFIX_.$table_name.'` WHERE `'.$id.'` = '.(int)$old_id); + if ($res) + { + unset($res[$id]); + if (isset($row['primary'])) + unset($res[$row['primary']]); + + $keys = implode(', ', array_keys($res)); + $sql = 'INSERT IGNORE INTO `'._DB_PREFIX_.$table_name.'` ('.$keys.', '.$id.') + (SELECT '.$keys.', '.(int)$this->id.' FROM '._DB_PREFIX_.$table_name.' + WHERE `'.$id.'` = '.(int)$old_id.')'; + Db::getInstance()->Execute($sql); + } + } + else + { + //Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.$table_name.'` SET WHERE `'.$id.'`='.(int)$old_id); + } + } + } } diff --git a/classes/Shop.php b/classes/Shop.php index 4f2b58491..e7d21deac 100644 --- a/classes/Shop.php +++ b/classes/Shop.php @@ -165,7 +165,7 @@ class ShopCore extends ObjectModel if ($table_name == 'stock') { $group = new GroupShop($this->id_group_shop); - if ($group->share_stock && $group->getNbShops() > 1) + if ($group->share_stock && $group->getTotalShops() > 1) continue; } @@ -745,7 +745,7 @@ class ShopCore extends ObjectModel } else { - Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.$table_name.'` SET WHERE `'.$id.'`='.(int)$old_id); + //Db::getInstance()->Execute('UPDATE `'._DB_PREFIX_.$table_name.'` SET WHERE `'.$id.'`='.(int)$old_id); } } }