* @copyright 2007-2011 PrestaShop SA
* @version Release: $Revision: 7332 $
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
class AdminGroups extends AdminTab
{
public function __construct()
{
$this->table = 'group';
$this->className = 'Group';
$this->lang = true;
$this->edit = true;
$this->view = true;
$this->delete = true;
$this->_select = '
(SELECT COUNT(jcg.`id_customer`)
FROM `'._DB_PREFIX_.'customer_group` jcg
LEFT JOIN `'._DB_PREFIX_.'customer` jc ON (jc.`id_customer` = jcg.`id_customer`)
WHERE jc.`deleted` != 1
AND jcg.`id_group` = a.`id_group`) AS nb
';
$this->_listSkipDelete = array(1);
$this->fieldsDisplay = array(
'id_group' => array('title' => $this->l('ID'), 'align' => 'center', 'width' => 25),
'name' => array('title' => $this->l('Name'), 'width' => 80, 'filter_key' => 'b!name'),
'reduction' => array('title' => $this->l('Discount'), 'width' => 50, 'align' => 'right'),
'nb' => array('title' => $this->l('Members'), 'width' => 25, 'align' => 'center'),
'date_add' => array('title' => $this->l('Creation date'), 'width' => 60, 'type' => 'date', 'align' => 'right'));
parent::__construct();
}
public function displayForm($isMainTab = true)
{
parent::displayForm();
if (!($obj = $this->loadObject(true)))
return;
$groupReductions = $obj->id ? GroupReduction::getGroupReductions($obj->id, $this->context->language->id) : array();
$categories = Category::getSimpleCategories($this->context->language->id);
echo '
';
if ($obj->id)
{
echo '
';
}
}
/**
* display form client groups restrictions
*/
protected function displayFormRestrictions($id = false)
{
$modules = Module::getModulesInstalled();
$authorized_modules = '';
$auth_modules = array();
$unauth_modules = array();
if ($id)
$authorized_modules = Module::getAuthorizedModules($id);
else
$id = '';
if (is_array($authorized_modules))
{
foreach ($modules as $module)
{
$authorized = false;
foreach ($authorized_modules as $auth_module)
if ($module['name'] == $auth_module['name'])
$authorized = true;
if ($authorized)
$auth_modules[] = $module;
else
$unauth_modules[] = $module;
}
}
else
$auth_modules = $modules;
echo '
';
}
public function viewgroup()
{
$this->context = Context::getContext();
self::$currentIndex = 'index.php?tab=AdminGroups';
if (!($obj = $this->loadObject(true)))
return;
echo '
';
$this->fieldsDisplay = (array(
'ID' => array('title' => $this->l('ID')),
'sex' => array('title' => $this->l('Sex')),
'name' => array('title' => $this->l('Name')),
'e-mail' => array('title' => $this->l('e-mail')),
'birthdate' => array('title' => $this->l('Birth date')),
'register_date' => array('title' => $this->l('Registration date')),
'orders' => array('title' => $this->l('Orders')),
'status' => array('title' => $this->l('Status')),
'actions' => array('title' => $this->l('Actions'))
));
if ($nbCustomers = $obj->getCustomers(true))
{
echo ''.$this->l('Customer members of this group').' ('.$nbCustomers.')
';
// Pagination Begin
$customersPerPage = (Tools::getValue('customerPerPage') ? (int)Tools::getValue('customerPerPage') : 50);
$totalPages = ceil($nbCustomers / $customersPerPage);
$perPageOptions = array(20, 50, 100, 300);
$customerPageIndex = (Tools::getValue('customerPageIndex') && Tools::getValue('customerPageIndex') <= $totalPages ? (int)Tools::getValue('customerPageIndex') : 1);
$from = (Tools::getValue('customerPageIndex') ? ((int)$customerPageIndex - 1) * ((int)$customersPerPage) : 0);
$customers = $obj->getCustomers(false, $from, $customersPerPage);
echo '
';
// Pagination End
echo '
';
foreach ($this->fieldsDisplay as $field)
echo '| '.$field['title'].' | ';
echo '
';
$irow = 0;
foreach ($customers as $k => $customer)
{
echo '
| '.$customer['id_customer'].' |
.') |
'.stripslashes($customer['lastname']).' '.stripslashes($customer['firstname']).' |
'.stripslashes($customer['email']).'  |
'.Tools::displayDate($customer['birthday'], $this->context->language->id).' |
'.Tools::displayDate($customer['date_add'], $this->context->language->id).' |
'.Order::getCustomerNbOrders($customer['id_customer']).' |
.') |
|
';
}
echo '
';
}
else
echo '
'.$this->l('No user in this group.').'
';
}
public function postProcess()
{
$token = Tools::getValue('token') ? Tools::getValue('token') : $this->token;
if (Tools::isSubmit('deleteGroupReduction'))
{
if ($this->tabAccess['delete'] === '1')
{
if (!$id_group_reduction = Tools::getValue('id_group_reduction'))
$this->_errors[] = Tools::displayError('Invalid group reduction ID');
else
{
$groupReduction = new GroupReduction((int)($id_group_reduction));
if (!$groupReduction->delete())
$this->_errors[] = Tools::displayError('An error occurred while deleting the group reduction');
else
Tools::redirectAdmin(self::$currentIndex.'&update'.$this->table.'&id_group='.(int)(Tools::getValue('id_group')).'&conf=1&token='.$token);
}
}
else
$this->_errors[] = Tools::displayError('You do not have permission to delete here.');
}
if (Tools::isSubmit('submitAddGroupReduction'))
{
if ($this->tabAccess['add'] === '1')
{
if (!($obj = $this->loadObject()))
return;
$groupReduction = new GroupReduction();
if (!$id_category = Tools::getValue('id_category') || !Validate::isUnsignedId($id_category))
$this->_errors[] = Tools::displayError('Wrong category ID');
else if (!$reduction = Tools::getValue('reductionByCategory') || !Validate::isPrice($reduction))
$this->_errors[] = Tools::displayError('Invalid reduction (must be a percentage)');
else if (Tools::getValue('reductionByCategory') > 100 || Tools::getValue('reductionByCategory') < 0)
$this->_errors[] = Tools::displayError('Reduction value is incorrect');
else if (GroupReduction::doesExist((int)$obj->id, $id_category))
$this->_errors[] = Tools::displayError('A reduction already exists for this category.');
else
{
$reduction = Tools::getValue('reductionByCategory');
$id_category = Tools::getValue('id_category');
$category = new Category((int)($id_category));
$category->addGroupsIfNoExist(Tools::getValue('id_group'));
$groupReduction->id_category = (int)($id_category);
$groupReduction->id_group = (int)($obj->id);
$groupReduction->reduction = (float)($reduction) / 100;
if (!$groupReduction->add())
$this->_errors[] = Tools::displayError('An error occurred while adding a category group reduction.');
else
Tools::redirectAdmin(self::$currentIndex.'&update'.$this->table.'&id_group='.(int)(Tools::getValue('id_group')).'&conf=3&token='.$this->token);
}
}
else
$this->_errors[] = Tools::displayError('You do not have permission to add here.');
}
if (Tools::isSubmit('submitRestrictions'))
{
$this->updateRestrictions();
}
if (Tools::isSubmit('submitAddgroup'))
{
if ($this->tabAccess['add'] === '1')
{
if (Tools::getValue('reduction') > 100 || Tools::getValue('reduction') < 0)
$this->_errors[] = Tools::displayError('Reduction value is incorrect');
else
{
$id_group_reductions = Tools::getValue('gr_id_group_reduction');
$reductions = Tools::getValue('gr_reduction');
if ($id_group_reductions)
{
foreach ($id_group_reductions as $key => $id_group_reduction)
if (!Validate::isUnsignedId($id_group_reductions[$key]) || !Validate::isPrice($reductions[$key]))
$this->_errors[] = Tools::displayError('Invalid reduction (must be a percentage)');
else if ($reductions[$key] > 100 || $reductions[$key] < 0)
$this->_errors[] = Tools::displayError('Reduction value is incorrect');
else
{
$groupReduction = new GroupReduction((int)($id_group_reductions[$key]));
$groupReduction->reduction = $reductions[$key] / 100;
if (!$groupReduction->update())
$this->_errors[] = Tools::displayError('Cannot update group reductions');
}
}
if (!count($this->_errors))
{
parent::postProcess();
}
}
}
else
$this->_errors[] = Tools::displayError('You do not have permission to add here.');
}
else if (isset($_GET['delete'.$this->table]))
{
if ($this->tabAccess['delete'] === '1')
{
if (Validate::isLoadedObject($object = $this->loadObject()))
{
if ($object->id == 1)
$this->_errors[] = Tools::displayError('You cannot delete default group.');
else
{
if ($object->delete())
Tools::redirectAdmin(self::$currentIndex.'&conf=1&token='.$token);
$this->_errors[] = Tools::displayError('An error occurred during deletion.');
}
}
else
$this->_errors[] = Tools::displayError('An error occurred while deleting object.').' '.$this->table.' '.Tools::displayError('(cannot load object)');
}
else
$this->_errors[] = Tools::displayError('You do not have permission to delete here.');
}
else
parent::postProcess();
}
/**
* Update (or create) restrictions for modules by group
*/
protected function updateRestrictions()
{
$id_group = Tools::getValue('id_group');
$unauth_modules = Tools::getValue('unauthorized_modules');
$auth_modules = Tools::getValue('authorized_modules');
Group::truncateModulesRestrictions($id_group);
if (is_array($auth_modules))
Group::addModulesRestrictions($id_group, $auth_modules, 1);
if (is_array($unauth_modules))
Group::addModulesRestrictions($id_group, $unauth_modules, 0);
}
}