// Add copy data of group shops

This commit is contained in:
rMalie
2011-08-18 07:57:04 +00:00
parent 66d4533e68
commit 2e08bf999a
4 changed files with 105 additions and 28 deletions
+51 -5
View File
@@ -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;
<input type="submit" value="'.$this->l(' Save ').'" name="submitAdd'.$this->table.'" class="button" />
</div>
<div class="small"><sup>*</sup> '.$this->l('Required field').'</div>
</fieldset>
</form>';
</fieldset><br />';
$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 '<fieldset><legend>'.$this->l('Import data from another group shop').'</legend>';
echo '<label>'.$this->l('Import data from another group shop').'</label>';
echo '<div class="margin-form">';
echo '<input type="checkbox" value="1" '.(($checked) ? 'checked="checked"' : '').' name="useImportData" onclick="$(\'#importList\').slideToggle(\'slow\')" /> ';
echo $this->l('Duplicate data from group shop');
echo ' <select name="importFromShop">';
foreach (Shop::getTree() as $gID => $gData)
echo '<option value="'.(int)$gID.'" '.($gID == $defaultGroup ? 'selected="selected"' : '').'">'.$gData['name'].'</option>';
echo '</select>';
echo '<div id="importList" style="'.((!$checked) ? 'display: none' : '').'"><ul>';
foreach ($importData as $table => $lang)
echo '<li><label><input type="checkbox" name="importData['.$table.']" checked="checked" /> '.$lang.'</label></li>';
echo '</ul></div>';
echo '<p>'.$this->l('Use this option to associate data (products, modules, etc.) the same way as the selected shop').'</p>';
echo '</div><div class="margin-form">
<input type="submit" value="'.$this->l(' Save ').'" name="submitAdd'.$this->table.'" class="button" />
</div>';
echo '</fieldset>';
echo '</form>';
}
protected function displayAddButton()
-1
View File
@@ -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;
+52 -20
View File
@@ -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);
}
}
}
}
+2 -2
View File
@@ -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);
}
}
}