// 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">
<input class="input_shop_group"
type="checkbox"
name="checkBoxShopGroupAsso_{$table}[{$form_id}][{$groupID}]"
name="checkBoxShopGroupAsso_{$table}[{$groupID}]"
value="{$groupID}"
{if $groupChecked} checked="checked"{/if} />
<b>{l s='Group:'} {$groupData['name']}</b>
@@ -116,7 +116,7 @@ function check_all_shop() {
type="checkbox"
value="{$groupID}"
shop_id="{$shopID}"
name="checkBoxShopAsso_{$table}[{$form_id}][{$shopID}]"
name="checkBoxShopAsso_{$table}[{$shopID}]"
id="checkedBox_{$shopID}"
{if $checked} checked="checked"{/if} />
{$shopData['name']}
-1
View File
@@ -115,7 +115,6 @@ class CarrierCore extends ObjectModel
'primary' => 'id_carrier',
'multilang' => true,
'multilang_shop' => true,
'multishop' => true,
'fields' => array(
/* Classic fields */
'id_reference' => array('type' => self::TYPE_INT),
-1
View File
@@ -71,7 +71,6 @@ class CountryCore extends ObjectModel
'table' => 'country',
'primary' => 'id_country',
'multilang' => true,
'multishop' => true,
'fields' => array(
'id_zone' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
'id_currency' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
-1
View File
@@ -66,7 +66,6 @@ class CurrencyCore extends ObjectModel
'table' => 'currency',
'primary' => 'id_currency',
'multilang_shop' => true,
'multishop' => true,
'fields' => array(
'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),
+10 -3
View File
@@ -135,6 +135,8 @@ abstract class ObjectModelCore
*/
protected $update_fields = null;
public $insert_missing_shop = true;
/**
* Returns object validation rules (fields validity)
*
@@ -546,7 +548,11 @@ abstract class ObjectModelCore
$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;
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);
else
$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'
if (!array_key_exists('active', $this))
throw new PrestaShopException('property "active is missing in object '.get_class($this));
// Update active status on object
$this->active = !(int)$this->active;
// 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(
'table' => 'zone',
'primary' => 'id_zone',
'multishop' => true,
'fields' => array(
'name' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'required' => true, 'size' => 64),
'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
+42 -41
View File
@@ -1982,17 +1982,21 @@ class AdminControllerCore extends Controller
$filter_shop = '';
if ($this->multishop_context && Shop::isTableAssociated($this->table))
{
$idenfier_shop = Shop::getContextListShopID();
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)
$def = ObjectModel::getDefinition($this->className);
if (Shop::getContext() != Shop::CONTEXT_ALL || !empty($def['multishop']))
{
$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).'))';
$idenfier_shop = Shop::getContextListShopID();
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 int $id_object
* @return array
*/
protected function getAssoShop($table, $id_object = false)
protected function getSelectedAssoShop($table, $id_object = false)
{
if (Shop::isTableAssociated($table))
$type = 'shop';
else
return;
if (!Shop::isFeatureActive() || !Shop::isTableAssociated($table))
return array();
$shops = Shop::getShops(true, null, true);
if (count($shops) == 1 && isset($shops[0]))
return array($shops[0], 'shop');
$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 ($check_box as $id_asso_object => $row)
{
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);
}
foreach (Tools::getValue('checkBoxShopAsso_'.$table) as $id_shop => $value)
$assos[] = (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
$assos[] = array('id_object' => (int)$id_object, 'id_'.$type => (int)Shop::getContextShopID());
return array($assos, $type);
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[] = (int)Shop::getContextShopID();
return $assos;
}
/**
@@ -2367,30 +2364,34 @@ class AdminControllerCore extends Controller
* @param int $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())
return;
$def = ObjectModel::getDefinition($this->className);
if (empty($def['multishop']))
if (!empty($def['multishop']))
return;
$assos_data = $this->getAssoShop($this->table, $id_object);
$assos = $assos_data[0];
$type = $assos_data[1];
if (!$type)
if (!Shop::isTableAssociated($this->table))
return;
Db::getInstance()->execute('
DELETE FROM '._DB_PREFIX_.$this->table.'_'.$type.($id_object ? '
WHERE `'.$this->identifier.'`='.(int)$id_object : ''));
$assos_data = $this->getSelectedAssoShop($this->table, $id_object);
foreach ($assos as $asso)
Db::getInstance()->execute('
INSERT INTO '._DB_PREFIX_.$this->table.'_'.$type.' (`'.pSQL($this->identifier).'`, id_'.$type.')
VALUES('.($new_id_object ? $new_id_object : (int)$asso['id_object']).', '.(int)$asso['id_'.$type].')');
// Get list of shop id we want to exclude from asso deletion
$exclude_ids = $assos_data;
foreach (Db::getInstance()->executeS('SELECT id_shop FROM '._DB_PREFIX_.'shop') as $row)
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)
+1 -1
View File
@@ -139,7 +139,7 @@ class HelperFormCore extends Helper
case 'shop' :
$params['html'] = $this->renderAssoShop($params['type']);
$shops = Shop::getShops(true, null, true);
if (count($shops) == 1)
if (Shop::getTotalShops(false) == 1)
unset($this->fields_form[$fieldset_key]['form']['input'][$key]);
break;
}
+1 -3
View File
@@ -445,7 +445,6 @@ class ShopCore extends ObjectModel
self::$shops = array();
$select = '';
$from = '';
$where = '';
@@ -454,12 +453,11 @@ class ShopCore extends ObjectModel
// If the profile isn't a superAdmin
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';
$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
LEFT JOIN '._DB_PREFIX_.'shop s
ON s.id_shop_group = gs.id_shop_group
@@ -405,9 +405,8 @@ class AdminEmployeesControllerCore extends AdminController
return false;
}
$assos = $this->getAssoShop($this->table);
if (count($assos[0]) == 0 && $this->table = 'employee')
$assos = $this->getSelectedAssoShop($this->table);
if (!$assos && $this->table = 'employee')
if (Shop::isFeatureActive() && _PS_ADMIN_PROFILE_ != $_POST['id_profile'])
$this->errors[] = Tools::displayError('The employee must be associated with at least one shop');
}