// Fix lot of multishop bugs (updateAssoShop reworked, remove multishop property for country / zone / currency / carrier, getList())

This commit is contained in:
rMalie
2012-05-18 14:54:37 +00:00
parent ba435ff3de
commit 7a6b54268e
10 changed files with 58 additions and 57 deletions
@@ -92,7 +92,7 @@ function check_all_shop() {
<label class="t"> <label class="t">
<input class="input_shop_group" <input class="input_shop_group"
type="checkbox" type="checkbox"
name="checkBoxShopGroupAsso_{$table}[{$form_id}][{$groupID}]" name="checkBoxShopGroupAsso_{$table}[{$groupID}]"
value="{$groupID}" value="{$groupID}"
{if $groupChecked} checked="checked"{/if} /> {if $groupChecked} checked="checked"{/if} />
<b>{l s='Group:'} {$groupData['name']}</b> <b>{l s='Group:'} {$groupData['name']}</b>
@@ -116,7 +116,7 @@ function check_all_shop() {
type="checkbox" type="checkbox"
value="{$groupID}" value="{$groupID}"
shop_id="{$shopID}" shop_id="{$shopID}"
name="checkBoxShopAsso_{$table}[{$form_id}][{$shopID}]" name="checkBoxShopAsso_{$table}[{$shopID}]"
id="checkedBox_{$shopID}" id="checkedBox_{$shopID}"
{if $checked} checked="checked"{/if} /> {if $checked} checked="checked"{/if} />
{$shopData['name']} {$shopData['name']}
-1
View File
@@ -115,7 +115,6 @@ class CarrierCore extends ObjectModel
'primary' => 'id_carrier', 'primary' => 'id_carrier',
'multilang' => true, 'multilang' => true,
'multilang_shop' => true, 'multilang_shop' => true,
'multishop' => true,
'fields' => array( 'fields' => array(
/* Classic fields */ /* Classic fields */
'id_reference' => array('type' => self::TYPE_INT), 'id_reference' => array('type' => self::TYPE_INT),
-1
View File
@@ -71,7 +71,6 @@ class CountryCore extends ObjectModel
'table' => 'country', 'table' => 'country',
'primary' => 'id_country', 'primary' => 'id_country',
'multilang' => true, 'multilang' => true,
'multishop' => true,
'fields' => array( 'fields' => array(
'id_zone' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true), 'id_zone' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'id_currency' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'), 'id_currency' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
-1
View File
@@ -66,7 +66,6 @@ class CurrencyCore extends ObjectModel
'table' => 'currency', 'table' => 'currency',
'primary' => 'id_currency', 'primary' => 'id_currency',
'multilang_shop' => true, 'multilang_shop' => true,
'multishop' => true,
'fields' => array( 'fields' => array(
'name' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 32), 'name' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 32),
'iso_code' => array('type' => self::TYPE_STRING, 'validate' => 'isLanguageIsoCode', 'required' => true, 'size' => 3), 'iso_code' => array('type' => self::TYPE_STRING, 'validate' => 'isLanguageIsoCode', 'required' => true, 'size' => 3),
+10 -3
View File
@@ -135,6 +135,8 @@ abstract class ObjectModelCore
*/ */
protected $update_fields = null; protected $update_fields = null;
public $insert_missing_shop = true;
/** /**
* Returns object validation rules (fields validity) * Returns object validation rules (fields validity)
* *
@@ -546,7 +548,11 @@ abstract class ObjectModelCore
$fields['id_shop'] = (int)$id_shop; $fields['id_shop'] = (int)$id_shop;
$all_fields['id_shop'] = (int)$id_shop; $all_fields['id_shop'] = (int)$id_shop;
$where = $this->def['primary'].' = '.(int)$this->id.' AND id_shop = '.(int)$id_shop; $where = $this->def['primary'].' = '.(int)$this->id.' AND id_shop = '.(int)$id_shop;
if (Db::getInstance()->getValue('SELECT '.$this->def['primary'].' FROM '._DB_PREFIX_.$this->def['table'].'_shop WHERE '.$where)) $shop_exists = Db::getInstance()->getValue('SELECT '.$this->def['primary'].' FROM '._DB_PREFIX_.$this->def['table'].'_shop WHERE '.$where);
if (!$this->insert_missing_shop && !$shop_exists)
continue;
if ($shop_exists)
$result &= Db::getInstance()->update($this->def['table'].'_shop', $fields, $where, 0, $null_values); $result &= Db::getInstance()->update($this->def['table'].'_shop', $fields, $where, 0, $null_values);
else else
$result &= Db::getInstance()->insert($this->def['table'].'_shop', $all_fields, $null_values); $result &= Db::getInstance()->insert($this->def['table'].'_shop', $all_fields, $null_values);
@@ -678,12 +684,13 @@ abstract class ObjectModelCore
// Object must have a variable called 'active' // Object must have a variable called 'active'
if (!array_key_exists('active', $this)) if (!array_key_exists('active', $this))
throw new PrestaShopException('property "active is missing in object '.get_class($this)); throw new PrestaShopException('property "active is missing in object '.get_class($this));
// Update active status on object // Update active status on object
$this->active = !(int)$this->active; $this->active = !(int)$this->active;
// Change status to active/inactive // Change status to active/inactive
return $this->update(); $this->insert_missing_shop = false;
return $this->update(false);
} }
/** /**
-1
View File
@@ -39,7 +39,6 @@ class ZoneCore extends ObjectModel
public static $definition = array( public static $definition = array(
'table' => 'zone', 'table' => 'zone',
'primary' => 'id_zone', 'primary' => 'id_zone',
'multishop' => true,
'fields' => array( 'fields' => array(
'name' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 64), 'name' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 64),
'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'), 'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
+42 -41
View File
@@ -1982,17 +1982,21 @@ class AdminControllerCore extends Controller
$filter_shop = ''; $filter_shop = '';
if ($this->multishop_context && Shop::isTableAssociated($this->table)) if ($this->multishop_context && Shop::isTableAssociated($this->table))
{ {
$idenfier_shop = Shop::getContextListShopID(); $def = ObjectModel::getDefinition($this->className);
if (!$this->_group) if (Shop::getContext() != Shop::CONTEXT_ALL || !empty($def['multishop']))
$this->_group = ' GROUP BY a.'.pSQL($this->identifier);
elseif (!preg_match('#(\s|,)\s*a\.`?'.pSQL($this->identifier).'`?(\s|,|$)#', $this->_group))
$this->_group .= ', a.'.pSQL($this->identifier);
$test_join = !preg_match('#`?'.preg_quote(_DB_PREFIX_.$this->table.'_shop').'`? *sa#', $this->_join);
if (Shop::isFeatureActive() && $test_join)
{ {
$filter_shop = ' JOIN `'._DB_PREFIX_.$this->table.'_shop` sa '; $idenfier_shop = Shop::getContextListShopID();
$filter_shop .= 'ON (sa.'.$this->identifier.' = a.'.$this->identifier.' AND sa.id_shop IN ('.implode(', ', $idenfier_shop).'))'; if (!$this->_group)
$this->_group = ' GROUP BY a.'.pSQL($this->identifier);
elseif (!preg_match('#(\s|,)\s*a\.`?'.pSQL($this->identifier).'`?(\s|,|$)#', $this->_group))
$this->_group .= ', a.'.pSQL($this->identifier);
$test_join = !preg_match('#`?'.preg_quote(_DB_PREFIX_.$this->table.'_shop').'`? *sa#', $this->_join);
if (Shop::isFeatureActive() && $test_join)
{
$filter_shop = ' JOIN `'._DB_PREFIX_.$this->table.'_shop` sa ';
$filter_shop .= 'ON (sa.'.$this->identifier.' = a.'.$this->identifier.' AND sa.id_shop IN ('.implode(', ', $idenfier_shop).'))';
}
} }
} }
@@ -2332,33 +2336,26 @@ class AdminControllerCore extends Controller
* *
* @param string $table * @param string $table
* @param int $id_object * @param int $id_object
* @return array
*/ */
protected function getAssoShop($table, $id_object = false) protected function getSelectedAssoShop($table, $id_object = false)
{ {
if (Shop::isTableAssociated($table)) if (!Shop::isFeatureActive() || !Shop::isTableAssociated($table))
$type = 'shop'; return array();
else
return;
$shops = Shop::getShops(true, null, true); $shops = Shop::getShops(true, null, true);
if (count($shops) == 1 && isset($shops[0])) if (count($shops) == 1 && isset($shops[0]))
return array($shops[0], 'shop'); return array($shops[0], 'shop');
$assos = array(); $assos = array();
if (Tools::isSubmit('checkBox'.Tools::toCamelCase($type, true).'Asso_'.$table)) if (Tools::isSubmit('checkBoxShopAsso_'.$table))
{ {
$check_box = Tools::getValue('checkBox'.Tools::toCamelCase($type, true).'Asso_'.$table); foreach (Tools::getValue('checkBoxShopAsso_'.$table) as $id_shop => $value)
foreach ($check_box as $id_asso_object => $row) $assos[] = (int)$id_shop;
{
if ($id_object)
$id_asso_object = $id_object;
foreach ($row as $id_shop => $value)
$assos[] = array('id_object' => (int)$id_asso_object, 'id_'.$type => (int)$id_shop);
}
} }
else // if we do not have the checkBox multishop, we can have an admin with only one shop and being in multishop else if (Shop::getTotalShops(false) == 1)// if we do not have the checkBox multishop, we can have an admin with only one shop and being in multishop
$assos[] = array('id_object' => (int)$id_object, 'id_'.$type => (int)Shop::getContextShopID()); $assos[] = (int)Shop::getContextShopID();
return array($assos, $type); return $assos;
} }
/** /**
@@ -2367,30 +2364,34 @@ class AdminControllerCore extends Controller
* @param int $id_object * @param int $id_object
* @param int $new_id_object * @param int $new_id_object
*/ */
protected function updateAssoShop($id_object = false, $new_id_object = false) protected function updateAssoShop($id_object, $new_id_object = false)
{ {
if (!Shop::isFeatureActive()) if (!Shop::isFeatureActive())
return; return;
$def = ObjectModel::getDefinition($this->className); $def = ObjectModel::getDefinition($this->className);
if (empty($def['multishop'])) if (!empty($def['multishop']))
return; return;
$assos_data = $this->getAssoShop($this->table, $id_object); if (!Shop::isTableAssociated($this->table))
$assos = $assos_data[0];
$type = $assos_data[1];
if (!$type)
return; return;
Db::getInstance()->execute(' $assos_data = $this->getSelectedAssoShop($this->table, $id_object);
DELETE FROM '._DB_PREFIX_.$this->table.'_'.$type.($id_object ? '
WHERE `'.$this->identifier.'`='.(int)$id_object : ''));
foreach ($assos as $asso) // Get list of shop id we want to exclude from asso deletion
Db::getInstance()->execute(' $exclude_ids = $assos_data;
INSERT INTO '._DB_PREFIX_.$this->table.'_'.$type.' (`'.pSQL($this->identifier).'`, id_'.$type.') foreach (Db::getInstance()->executeS('SELECT id_shop FROM '._DB_PREFIX_.'shop') as $row)
VALUES('.($new_id_object ? $new_id_object : (int)$asso['id_object']).', '.(int)$asso['id_'.$type].')'); if (!$this->context->employee->hasAuthOnShop($row['id_shop']))
$exclude_ids[] = $row['id_shop'];
Db::getInstance()->delete($this->table.'_shop', '`'.$this->identifier.'` = '.(int)$id_object.($exclude_ids ? ' AND id_shop NOT IN ('.implode(', ', $exclude_ids).')' : ''));
$insert = array();
foreach ($assos_data as $id_shop)
$insert[] = array(
$this->identifier => $new_id_object ? $new_id_object : $id_object,
'id_shop' => (int)$id_shop,
);
Db::getInstance()->insert($this->table.'_shop', $insert, false, true, Db::INSERT_IGNORE);
} }
protected function validateField($value, $field) protected function validateField($value, $field)
+1 -1
View File
@@ -139,7 +139,7 @@ class HelperFormCore extends Helper
case 'shop' : case 'shop' :
$params['html'] = $this->renderAssoShop($params['type']); $params['html'] = $this->renderAssoShop($params['type']);
$shops = Shop::getShops(true, null, true); $shops = Shop::getShops(true, null, true);
if (count($shops) == 1) if (Shop::getTotalShops(false) == 1)
unset($this->fields_form[$fieldset_key]['form']['input'][$key]); unset($this->fields_form[$fieldset_key]['form']['input'][$key]);
break; break;
} }
+1 -3
View File
@@ -445,7 +445,6 @@ class ShopCore extends ObjectModel
self::$shops = array(); self::$shops = array();
$select = '';
$from = ''; $from = '';
$where = ''; $where = '';
@@ -454,12 +453,11 @@ class ShopCore extends ObjectModel
// If the profile isn't a superAdmin // If the profile isn't a superAdmin
if (Validate::isLoadedObject($employee) && $employee->id_profile != _PS_ADMIN_PROFILE_) if (Validate::isLoadedObject($employee) && $employee->id_profile != _PS_ADMIN_PROFILE_)
{ {
$select .= ', es.id_employee';
$from .= 'LEFT JOIN '._DB_PREFIX_.'employee_shop es ON es.id_shop = s.id_shop'; $from .= 'LEFT JOIN '._DB_PREFIX_.'employee_shop es ON es.id_shop = s.id_shop';
$where .= 'AND es.id_employee = '.(int)$employee->id; $where .= 'AND es.id_employee = '.(int)$employee->id;
} }
$sql = 'SELECT gs.*, s.*, gs.name AS group_name, s.name AS shop_name, s.active, su.domain, su.domain_ssl, su.physical_uri, su.virtual_uri'.$select.' $sql = 'SELECT gs.*, s.*, gs.name AS group_name, s.name AS shop_name, s.active, su.domain, su.domain_ssl, su.physical_uri, su.virtual_uri
FROM '._DB_PREFIX_.'shop_group gs FROM '._DB_PREFIX_.'shop_group gs
LEFT JOIN '._DB_PREFIX_.'shop s LEFT JOIN '._DB_PREFIX_.'shop s
ON s.id_shop_group = gs.id_shop_group ON s.id_shop_group = gs.id_shop_group
@@ -405,9 +405,8 @@ class AdminEmployeesControllerCore extends AdminController
return false; return false;
} }
$assos = $this->getAssoShop($this->table); $assos = $this->getSelectedAssoShop($this->table);
if (!$assos && $this->table = 'employee')
if (count($assos[0]) == 0 && $this->table = 'employee')
if (Shop::isFeatureActive() && _PS_ADMIN_PROFILE_ != $_POST['id_profile']) if (Shop::isFeatureActive() && _PS_ADMIN_PROFILE_ != $_POST['id_profile'])
$this->errors[] = Tools::displayError('The employee must be associated with at least one shop'); $this->errors[] = Tools::displayError('The employee must be associated with at least one shop');
} }