// Changes to controller and AdminCustomers refactoring

This commit is contained in:
tDidierjean
2011-11-29 18:35:17 +00:00
parent b726023488
commit 6ef867bd5a
6 changed files with 209 additions and 238 deletions
+7 -2
View File
@@ -231,6 +231,9 @@ class AdminControllerCore extends Controller
/** @var instanciation of the class associated with the AdminController */
protected $object;
/** @var current object ID */
protected $id_object;
public function __construct()
{
$controller = get_class($this);
@@ -1696,6 +1699,8 @@ class AdminControllerCore extends Controller
if (Tools::isSubmit('submitFilter'.$this->table) || $this->context->cookie->{'submitFilter'.$this->table} !== false)
$this->filter = true;
$this->id_object = (int)Tools::getValue('id_'.$this->table);
/* Delete object image */
if (isset($_GET['deleteImage']))
{
@@ -1733,7 +1738,7 @@ class AdminControllerCore extends Controller
else if (Tools::getValue('submitAdd'.$this->table) || Tools::getValue('submitAdd'.$this->table.'AndStay'))
{
// case 1: updating existing entry
if ((int)(Tools::getValue('id_'.$this->table)))
if ($this->id_object)
{
if ($this->tabAccess['edit'] === '1')
{
@@ -1794,7 +1799,7 @@ class AdminControllerCore extends Controller
else if (is_array($this->bulk_actions))
foreach ($this->bulk_actions as $bulk_action => $params)
{
if (Tools::isSubmit('submitBulk'.$bulk_action.$this->table))
if (Tools::isSubmit('submitBulk'.$bulk_action.$this->table) || Tools::isSubmit('submitBulk'.$bulk_action))
{
$this->action = 'bulk'.$bulk_action;
$this->boxes = Tools::getValue($this->table.'Box');
+10 -4
View File
@@ -1090,7 +1090,7 @@ class CartCore extends ObjectModel
static $cache = false;
if ($cache !== false && !$flush)
return $cache;
$product_list = $this->getProducts();
// Step 1 : Get product informations (warehouse_list and carrier_list), count warehouse
// Determine the best warehouse to determine the packages
@@ -1331,7 +1331,7 @@ class CartCore extends ObjectModel
static $cache = false;
if ($cache !== false && !$flush)
return $cache;
$delivery_option_list = array();
$carriers_price = array();
$carrier_collection = array();
@@ -1559,7 +1559,7 @@ class CartCore extends ObjectModel
public function getDeliveryOption($default_country = null)
{
$delivery_option_list = $this->getDeliveryOptionList($default_country);
// The delivery option was selected
if (isset($this->delivery_option) && $this->delivery_option != '')
{
@@ -1574,7 +1574,7 @@ class CartCore extends ObjectModel
if ($validated)
return $delivery_option;
}
// No delivery option selected or delivery option selected is not valid, get the better for all options
$delivery_option = array();
foreach ($delivery_option_list as $id_address => $options)
@@ -2028,6 +2028,12 @@ class CartCore extends ObjectModel
return self::$_isVirtualCart[$this->id];
}
/**
* Build cart object from provided id_order
*
* @param int $id_order
* @return Cart|bool
*/
public static function getCartByOrderId($id_order)
{
if ($id_cart = self::getCartIdByOrderId($id_order))
+6 -2
View File
@@ -939,7 +939,6 @@ class CategoryCore extends ObjectModel
return isset($row['id_category']);
}
public function cleanGroups()
{
Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'category_group` WHERE `id_category` = '.(int)$this->id);
@@ -1005,10 +1004,15 @@ class CategoryCore extends ObjectModel
return false;
}
/**
* Update customer groups associated to the object
*
* @param array $list groups
*/
public function updateGroup($list)
{
$this->cleanGroups();
if ($list && count($list))
if ($list && !empty($list))
$this->addGroups($list);
else
$this->addGroups(array(1));
+29 -20
View File
@@ -108,6 +108,7 @@ class CustomerCore extends ObjectModel
public $id_guest;
protected $tables = array ('customer');
public $groupBox;
protected $fieldsRequired = array('lastname', 'passwd', 'firstname', 'email');
protected $fieldsSize = array('lastname' => 32, 'passwd' => 32, 'firstname' => 32, 'email' => 128, 'note' => 65000);
@@ -125,7 +126,8 @@ class CustomerCore extends ObjectModel
'note' => 'isCleanHtml',
'is_guest' => 'isBool',
'id_shop' => 'isUnsignedId',
'id_group_shop' => 'isUnsignedId'
'id_group_shop' => 'isUnsignedId',
'groupBox' => 'isArrayWithIds'
);
protected $webserviceParameters = array(
@@ -189,14 +191,13 @@ class CustomerCore extends ObjectModel
$this->id_default_group = 2;
else
$this->id_default_group = 3;
/* Can't create a guest customer, if this feature is disabled */
if ($this->is_guest && !Configuration::get('PS_GUEST_CHECKOUT_ENABLED'))
return false;
if (!parent::add($autodate, $null_values))
return false;
$row = array('id_customer' => (int)$this->id, 'id_group' => (int)$this->id_default_group);
return Db::getInstance()->AutoExecute(_DB_PREFIX_.'customer_group', $row, 'INSERT');
$success = parent::add($autodate, $null_values);
$this->updateGroup($this->groupBox);
return $success;
}
public function update($nullValues = false)
@@ -204,6 +205,7 @@ class CustomerCore extends ObjectModel
$this->birthday = (empty($this->years) ? $this->birthday : (int)$this->years.'-'.(int)$this->months.'-'.(int)$this->days);
if ($this->newsletter && !$this->newsletter_date_add)
$this->newsletter_date_add = date('Y-m-d H:i:s');
$this->updateGroup($this->groupBox);
return parent::update(true);
}
@@ -419,7 +421,7 @@ class CustomerCore extends ObjectModel
)'.$shop->addSqlRestriction(Shop::SHARE_CUSTOMER);
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
}
/**
* Search for customers by ip address
*
@@ -499,6 +501,20 @@ class CustomerCore extends ObjectModel
return isset($row['id_customer']);
}
/**
* Update customer groups associated to the object
*
* @param array $list groups
*/
public function updateGroup($list)
{
$this->cleanGroups();
if ($list && !empty($list))
$this->addGroups($list);
else
$this->addGroups(array($this->id_default_group));
}
public function cleanGroups()
{
Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'customer_group` WHERE `id_customer` = '.(int)$this->id);
@@ -508,19 +524,8 @@ class CustomerCore extends ObjectModel
{
foreach ($groups as $group)
{
$groups_customers = $this->getGroups();
if (count($groups_customers) == 0)
{
$row = array('id_customer' => (int)$this->id, 'id_group' => (int)$group);
Db::getInstance()->AutoExecute(_DB_PREFIX_.'customer_group', $row, 'INSERT');
}
else
foreach ($groups_customers as $group_customers)
if ($group_customers != $group)
{
$row = array('id_customer' => (int)$this->id, 'id_group' => (int)$group);
Db::getInstance()->AutoExecute(_DB_PREFIX_.'customer_group', $row, 'INSERT');
}
$row = array('id_customer' => (int)$this->id, 'id_group' => (int)$group);
Db::getInstance()->AutoExecute(_DB_PREFIX_.'customer_group', $row, 'INSERT');
}
}
@@ -547,8 +552,12 @@ class CustomerCore extends ObjectModel
return self::getGroupsStatic((int)$this->id);
}
/**
* @deprecated since 1.5
*/
public function isUsed()
{
Tools::displayAsDeprecated();
return false;
}
+2 -5
View File
@@ -86,6 +86,8 @@ class FrontControllerCore extends Controller
return;
self::$initialized = true;
parent::init();
// For compatibility with globals, DEPRECATED as of version 1.5
$css_files = $this->css_files;
$js_files = $this->js_files;
@@ -242,11 +244,6 @@ class FrontControllerCore extends Controller
$navigationPipe = (Configuration::get('PS_NAVIGATION_PIPE') ? Configuration::get('PS_NAVIGATION_PIPE') : '>');
$this->context->smarty->assign('navigationPipe', $navigationPipe);
if (!defined('_PS_BASE_URL_'))
define('_PS_BASE_URL_', Tools::getShopDomain(true));
if (!defined('_PS_BASE_URL_SSL_'))
define('_PS_BASE_URL_SSL_', Tools::getShopDomainSsl(true));
// Automatically redirect to the canonical URL if needed
if (isset($this->php_self) && !empty($this->php_self) && !Tools::getValue('ajax'))
$this->canonicalRedirection($this->context->link->getPageLink($this->php_self, $this->ssl, $this->context->language->id));
+155 -205
View File
@@ -27,6 +27,8 @@
class AdminCustomersControllerCore extends AdminController
{
protected $delete_mode;
public function __construct()
{
$this->table = 'customer';
@@ -34,6 +36,11 @@ class AdminCustomersControllerCore extends AdminController
$this->lang = false;
$this->deleted = true;
$this->addRowAction('edit');
$this->addRowAction('view');
$this->addRowAction('delete');
$this->bulk_actions = array('delete' => array('text' => $this->l('Delete selected'), 'confirm' => $this->l('Delete selected items?')));
$this->context = Context::getContext();
$this->default_form_language = $this->context->language->id;
@@ -142,14 +149,41 @@ class AdminCustomersControllerCore extends AdminController
parent::__construct();
}
public function initContent()
{
if ($this->action == 'select_delete')
$this->context->smarty->assign(array(
'delete_form' => true,
'url_delete' => htmlentities($_SERVER['REQUEST_URI']),
'boxes' => $this->boxes,
));
parent::initContent();
}
public function initProcess()
{
$this->id_object = Tools::getValue('id_'.$this->table);
if (Tools::isSubmit('submitGuestToCustomer') && $this->id_object)
if ($this->tabAccess['edit'] === '1')
$this->action = 'guest_to_customer';
else
$this->_errors[] = Tools::displayError('You do not have permission to edit here.');
elseif (Tools::isSubmit('changeNewsletterVal') && $this->id_object)
$this->action = 'change_newsletter_val';
elseif (Tools::isSubmit('changeOptinVal') && $this->id_object)
$this->action = 'change_optin_val';
parent::initProcess();
// When deleting, first display a form to select the type of deletion
if ($this->action == 'delete' || $this->action == 'bulkdelete')
if (Tools::getValue('deleteMode') == 'real' || Tools::getValue('deleteMode') == 'deleted')
$this->delete_mode = Tools::getValue('deleteMode');
else
$this->action = 'select_delete';
}
public function initList()
{
$this->addRowAction('edit');
$this->addRowAction('view');
$this->addRowAction('delete');
$this->bulk_actions = array('delete' => array('text' => $this->l('Delete selected'), 'confirm' => $this->l('Delete selected items?')));
$this->_select = '(YEAR(CURRENT_DATE)-YEAR(`birthday`)) - (RIGHT(CURRENT_DATE, 5) < RIGHT(birthday, 5)) AS `age`, (
SELECT c.date_add FROM '._DB_PREFIX_.'guest g
LEFT JOIN '._DB_PREFIX_.'connections c ON c.id_guest = g.id_guest
@@ -546,211 +580,132 @@ class AdminCustomersControllerCore extends AdminController
return parent::initView();
}
public function postProcess()
public function processDelete($token)
{
/**
* Todo : Where it's used?
*/
if (Tools::isSubmit('submitDel'.$this->table) || Tools::isSubmit('delete'.$this->table))
if ($this->delete_mode == 'real')
{
$delete_form = '
<form action="'.htmlentities($_SERVER['REQUEST_URI']).'" method="post">
<fieldset><legend>'.$this->l('How do you want to delete your customer(s)?').'</legend>
'.$this->l('You have two ways to delete a customer, please choose what you want to do.').'
<p>
<input type="radio" name="deleteMode" value="real" id="deleteMode_real" />
<label for="deleteMode_real" style="float:none">'.
$this->l('I want to delete my customer(s) for real, all data will be removed from the database.
A customer with the same e-mail address will be able to register again.').'
</label>
</p>
<p>
<input type="radio" name="deleteMode" value="deleted" id="deleteMode_deleted" />
<label for="deleteMode_deleted" style="float:none">'.
$this->l('I don\'t want my customer(s) to register again.
The customer(s) will be removed from this list but all data will be kept in the database.').'
</label>
</p>';
foreach ($_POST as $key => $value)
if (is_array($value))
foreach ($value as $val)
$delete_form .= '<input type="hidden" name="'.htmlentities($key).'[]" value="'.htmlentities($val).'" />';
else
$delete_form .= '<input type="hidden" name="'.htmlentities($key).'" value="'.htmlentities($value).'" />';
$delete_form .= ' <br /><input type="submit" class="button" value="'.$this->l(' Delete ').'" />
</fieldset>
</form>
<div class="clear">&nbsp;</div>';
$this->deleted = false;
Discount::deleteByIdCustomer((int)Tools::getValue('id_customer'));
}
elseif ($this->delete_mode == 'deleted')
$this->deleted = true;
else
{
$this->_errors[] = Tools::displayError('Unknown delete mode:'.' '.$this->deleted);
return;
}
if (Tools::getValue('submitAdd'.$this->table))
{
/* Checking fields validity */
$this->validateRules();
if (!count($this->_errors))
{
$id = (int)Tools::getValue('id_'.$this->table);
$group_list = Tools::getValue('groupBox');
parent::processDelete($token);
}
//Update Object
if (isset($id) && !empty($id))
public function processBulkDelete($token)
{
if ($this->delete_mode == 'real')
{
$this->deleted = false;
foreach (Tools::getValue('customerBox') as $id_customer)
Discount::deleteByIdCustomer((int)$id_customer);
}
elseif ($this->delete_mode == 'deleted')
$this->deleted = true;
else
{
$this->_errors[] = Tools::displayError('Unknown delete mode:'.' '.$this->deleted);
return;
}
parent::processBulkDelete($token);
}
public function processSave($token)
{
// Check that the new email is not already in use
// Case add
if (!$this->id_object)
{
$customer_email = strval(Tools::getValue('email'));
$customer = new Customer();
$customer->getByEmail($customer_email);
if ($customer->id)
$this->_errors[] = Tools::displayError('An account already exists for this e-mail address:').' '.$customer_email;
}
// Case update
else
{
$object = new $this->className($this->id_object);
if (Validate::isLoadedObject($object))
{
$customer_email = strval(Tools::getValue('email'));
// check if e-mail already used
if ($customer_email != $object->email)
{
if ($this->tabAccess['edit'] !== '1')
$this->_errors[] = Tools::displayError('You do not have permission to edit here.');
else
{
$object = new $this->className($id);
if (Validate::isLoadedObject($object))
{
$customer_email = strval(Tools::getValue('email'));
// check if e-mail already used
if ($customer_email != $object->email)
{
$customer = new Customer();
$customer->getByEmail($customer_email);
if ($customer->id)
$this->_errors[] = Tools::displayError('An account already exists for this e-mail address:').' '.$customer_email;
}
if (!is_array($group_list) || count($group_list) == 0)
$this->_errors[] = Tools::displayError('Customer must be in at least one group.');
else
if (!in_array(Tools::getValue('id_default_group'), $group_list))
$this->_errors[] = Tools::displayError('Default customer group must be selected in group box.');
// Updating customer's group
if (!count($this->_errors))
{
$object->cleanGroups();
if (is_array($group_list) && count($group_list) > 0)
$object->addGroups($group_list);
}
}
else
$this->_errors[] = Tools::displayError('An error occurred while loading object.').'
<b>'.$this->table.'</b> '.Tools::displayError('(cannot load object)');
}
$customer = new Customer();
$customer->getByEmail($customer_email);
if ($customer->id)
$this->_errors[] = Tools::displayError('An account already exists for this e-mail address:').' '.$customer_email;
}
//Create Object
else
{
if ($this->tabAccess['add'] === '1')
{
$object = new $this->className();
$this->copyFromPost($object, $this->table);
$shop = new Shop((int)$object->id_shop);
$object->id_group_shop = (int)$shop->id_group_shop;
if (!$object->add())
$this->_errors[] = Tools::displayError('An error occurred while creating object.').'
<b>'.$this->table.' ('.Db::getInstance()->getMsgError().')</b>';
else if (($_POST[$this->identifier] = $object->id /* voluntary */) &&
$this->postImage($object->id) && !count($this->_errors) &&
$this->_redirect)
{
// Add Associated groups
$group_list = Tools::getValue('groupBox');
if (is_array($group_list) && count($group_list) > 0)
$object->addGroups($group_list);
$parent_id = (int)Tools::getValue('id_parent', 1);
// Save and stay on same form
if (Tools::isSubmit('submitAdd'.$this->table.'AndStay'))
Tools::redirectAdmin(self::$currentIndex.'&'.$this->identifier.'='.$object->id.'&conf=3&update'.$this->table.'&token='.$this->token);
// Save and back to parent
if (Tools::isSubmit('submitAdd'.$this->table.'AndBackToParent'))
Tools::redirectAdmin(self::$currentIndex.'&'.$this->identifier.'='.$parent_id.'&conf=3&token='.$this->token);
// Default behavior (save and back)
Tools::redirectAdmin(self::$currentIndex.($parent_id ? '&'.$this->identifier.'='.$object->id : '').'&conf=3&token='.$this->token);
}
}
else
$this->_errors[] = Tools::displayError('You do not have permission to add here.');
}
}
}
else if (Tools::isSubmit('delete'.$this->table) && $this->tabAccess['delete'] === '1')
{
switch (Tools::getValue('deleteMode'))
{
case 'real':
$this->deleted = false;
Discount::deleteByIdCustomer((int)Tools::getValue('id_customer'));
break;
case 'deleted':
$this->deleted = true;
break;
default:
echo $delete_form;
if (isset($_POST['delete'.$this->table]))
unset($_POST['delete'.$this->table]);
if (isset($_GET['delete'.$this->table]))
unset($_GET['delete'.$this->table]);
break;
}
}
else if (Tools::isSubmit('submitDel'.$this->table) && $this->tabAccess['delete'] === '1')
{
switch (Tools::getValue('deleteMode'))
{
case 'real':
$this->deleted = false;
foreach (Tools::getValue('customerBox') as $id_customer)
Discount::deleteByIdCustomer((int)$id_customer);
break;
case 'deleted':
$this->deleted = true;
break;
default:
echo $delete_form;
if (isset($_POST['submitDel'.$this->table]))
unset($_POST['submitDel'.$this->table]);
if (isset($_GET['submitDel'.$this->table]))
unset($_GET['submitDel'.$this->table]);
break;
}
}
else if (Tools::isSubmit('submitGuestToCustomer') && Tools::getValue('id_customer'))
{
if ($this->tabAccess['edit'] === '1')
{
$customer = new Customer((int)Tools::getValue('id_customer'));
if (!Validate::isLoadedObject($customer))
$this->_errors[] = Tools::displayError('This customer does not exist.');
if (Customer::customerExists($customer->email))
$this->_errors[] = Tools::displayError('This customer already exist as non-guest.');
else if ($customer->transformToCustomer(Tools::getValue('id_lang', Configuration::get('PS_LANG_DEFAULT'))))
Tools::redirectAdmin(self::$currentIndex.'&'.$this->identifier.'='.$customer->id.'&conf=3&token='.$this->token);
else
$this->_errors[] = Tools::displayError('An error occurred while updating customer.');
}
else
$this->_errors[] = Tools::displayError('You do not have permission to edit here.');
}
else if (Tools::isSubmit('changeNewsletterVal') && Tools::getValue('id_customer'))
{
$id_customer = (int)Tools::getValue('id_customer');
$customer = new Customer($id_customer);
if (!Validate::isLoadedObject($customer))
$this->_errors[] = Tools::displayError('An error occurred while updating customer.');
$update = Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'customer` SET newsletter = '.($customer->newsletter ? 0 : 1).' WHERE `id_customer` = '.(int)$customer->id);
if (!$update)
$this->_errors[] = Tools::displayError('An error occurred while updating customer.');
Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token);
}
else if (Tools::isSubmit('changeOptinVal') && Tools::getValue('id_customer'))
{
$id_customer = (int)Tools::getValue('id_customer');
$customer = new Customer($id_customer);
if (!Validate::isLoadedObject($customer))
$this->_errors[] = Tools::displayError('An error occurred while updating customer.');
$update = Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'customer` SET optin = '.($customer->optin ? 0 : 1).' WHERE `id_customer` = '.(int)$customer->id);
if (!$update)
$this->_errors[] = Tools::displayError('An error occurred while updating customer.');
Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token);
$this->_errors[] = Tools::displayError('An error occurred while loading object.').'
<b>'.$this->table.'</b> '.Tools::displayError('(cannot load object)');
}
return parent::postProcess();
// Check that default group is selected
if (!is_array(Tools::getValue('groupBox')) || !in_array(Tools::getValue('id_default_group'), Tools::getValue('groupBox')))
$this->_errors[] = $this->_errors[] = Tools::displayError('Default customer group must be selected in group box.');
parent::processSave($token);
}
/**
* Transform a guest account into a registered customer account
*
* @param string $token
*/
public function processGuestToCustomer($token)
{
$customer = new Customer((int)Tools::getValue('id_customer'));
if (!Validate::isLoadedObject($customer))
$this->_errors[] = Tools::displayError('This customer does not exist.');
if (Customer::customerExists($customer->email))
$this->_errors[] = Tools::displayError('This customer already exist as non-guest.');
else if ($customer->transformToCustomer(Tools::getValue('id_lang', $this->context->language->id)))
Tools::redirectAdmin(self::$currentIndex.'&'.$this->identifier.'='.$customer->id.'&conf=3&token='.$this->token);
else
$this->_errors[] = Tools::displayError('An error occurred while updating customer.');
}
/**
* Toggle the newsletter flag
*
* @param string $token
*/
public function processChangeNewsletterVal($token)
{
$customer = new Customer($this->id_object);
if (!Validate::isLoadedObject($customer))
$this->_errors[] = Tools::displayError('An error occurred while updating customer.');
$update = Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'customer` SET newsletter = '.($customer->newsletter ? 0 : 1).' WHERE `id_customer` = '.(int)$customer->id);
if (!$update)
$this->_errors[] = Tools::displayError('An error occurred while updating customer.');
Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token);
}
/**
* Toggle newsletter optin flag
*
* @param string $token
*/
public function processChangeOptinVal($token)
{
$customer = new Customer($this->id_object);
if (!Validate::isLoadedObject($customer))
$this->_errors[] = Tools::displayError('An error occurred while updating customer.');
$update = Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'customer` SET optin = '.($customer->optin ? 0 : 1).' WHERE `id_customer` = '.(int)$customer->id);
if (!$update)
$this->_errors[] = Tools::displayError('An error occurred while updating customer.');
Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token);
}
public function getList($id_lang, $order_by = null, $order_way = null, $start = 0, $limit = null, $id_lang_shop = null)
@@ -762,11 +717,6 @@ class AdminCustomersControllerCore extends AdminController
);
}
public function beforeDelete($object)
{
return $object->isUsed();
}
public static function printNewsIcon($id_customer, $tr)
{
$customer = new Customer($tr['id_customer']);