diff --git a/admin-dev/tabs/AdminGroups.php b/admin-dev/tabs/AdminGroups.php deleted file mode 100644 index fac957d38..000000000 --- a/admin-dev/tabs/AdminGroups.php +++ /dev/null @@ -1,558 +0,0 @@ - -* @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 ' -
- '.($obj->id ? '' : '').' -
'.$this->l('Customer group').' - -
'; - foreach ($this->_languages as $language) - echo ' -
- * - '.$this->l('Invalid characters:').' 0-9!<>,;?=+()@#"�{}_$%:  -
'; - $this->displayFlags($this->_languages, $this->_defaultFormLanguage, 'name', 'name'); - $reduction = htmlentities($this->getFieldValue($obj, 'reduction'), ENT_COMPAT, 'UTF-8'); - echo ' -
-
 
- -
- '.$this->l('%').' -

'.$this->l('Will automatically apply this value as a discount on ALL shop\'s products for this group\'s members.').'

-
'; - if ($obj->id) - { - echo ' - -
'; - if ($groupReductions) - { - - echo ' - - - - - - '; - foreach ($groupReductions as $groupReduction) - echo ' - - - - - '; - echo '
'.$this->l('Category').''.$this->l('Value').''.$this->l('Action').'
'.Tools::htmlentitiesUTF8($groupReduction['category_name']).''.$this->l('Delete').'
'; - } - else - echo $this->l('No discount'); - echo '
'; - } - echo ' - -
- -

'.$this->l('How the prices are displayed on order summary for this customer group (tax included or excluded).').'

-
-
 
'; - if (Shop::isFeatureActive()) - { - echo '
'; - $this->displayAssoShop('group_shop'); - echo '
'; - } - - echo '
- -
-
* '.$this->l('Required field').'
-
'; - $this->displayFormRestrictions($obj->id); - echo ' -

'; - - if ($obj->id) - { - echo ' -
- -
'.$this->l('New group discount').' -
'.$this->l('Beware the reduction applied to a category does not stack with the overall reduction but replaces it. The group is automatically added to the category.').'
- -
- * -

'.$this->l('Only products that have this category as default category will be affected').'.

-
- -
- * -
-
 
-
- -
-
* '.$this->l('Required field').'
-
-
'; - - } - } - - /** - * 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 ' -
- -
'.$this->l('Modules restrictions').' - - - - - -
-

'.$this->l('Unauthorized modules:').'

-

- - '.$this->l('Select All').' - - - '.$this->l('Authorize').' >> - -
-

'.$this->l('Authorized modules:').'

-

- - '.$this->l('Select All').' - - - << '.$this->l('Unauthorize').' - - -
-
 
-
- -
-
'; - } - - public function viewgroup() - { - $this->context = Context::getContext(); - self::$currentIndex = 'index.php?tab=AdminGroups'; - if (!($obj = $this->loadObject(true))) - return; - - echo ' -
-
- '.strval($obj->name[(int)$this->context->language->id]).' -
 
- '.$this->l('Discount:').' '.(float)($obj->reduction).$this->l('%').' -
-
 
'; - - $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 ' - - - - -
'; - - if ($customerPageIndex > 1) - { - echo '  '; - echo '  '; - } - - echo 'Page / '.$totalPages; - - if ($customerPageIndex < $totalPages) - { - echo ' '; - echo ' '; - } - - echo ' | Display - / '.$nbCustomers.' result(s) -
-
'; - // Pagination End - - echo ' - '; - foreach ($this->fieldsDisplay as $field) - echo ''.$field['title'].''; - echo ' - '; - $irow = 0; - - foreach ($customers as $k => $customer) - { - echo ' - - - - - - - - - - - '; - } - echo '
'.$customer['id_customer'].''.stripslashes($customer['lastname']).' '.stripslashes($customer['firstname']).''.stripslashes($customer['email']).' '.$this->l('Write to this customer').''.Tools::displayDate($customer['birthday'], $this->context->language->id).''.Tools::displayDate($customer['date_add'], $this->context->language->id).''.Order::getCustomerNbOrders($customer['id_customer']).' - - '.$this->l('View orders').' - - '.$this->l('Modify this customer').' - - '.$this->l('Delete this customer').' -
'; - } - 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); - } -} diff --git a/admin-dev/themes/template/groups/form.tpl b/admin-dev/themes/template/groups/form.tpl new file mode 100644 index 000000000..93c8958a7 --- /dev/null +++ b/admin-dev/themes/template/groups/form.tpl @@ -0,0 +1,275 @@ +{* +* 2007-2011 PrestaShop +* +* NOTICE OF LICENSE +* +* This source file is subject to the Academic Free License (AFL 3.0) +* that is bundled with this package in the file LICENSE.txt. +* It is also available through the world-wide-web at this URL: +* http://opensource.org/licenses/afl-3.0.php +* If you did not receive a copy of the license and are unable to +* obtain it through the world-wide-web, please send an email +* to license@prestashop.com so we can send you a copy immediately. +* +* DISCLAIMER +* +* Do not edit or add to this file if you wish to upgrade PrestaShop to newer +* versions in the future. If you wish to customize PrestaShop for your +* needs please refer to http://www.prestashop.com for more information. +* +* @author PrestaShop SA +* @copyright 2007-2011 PrestaShop SA +* @version Release: $Revision: 8971 $ +* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*} + +{extends file="helper/form/form.tpl"} + +{block name="label"} + {if $input['type'] == 'modules'} +
+
+ +
+ {elseif $input['type'] == 'group_discount_category'} +
+
+ +
+ {else} + + {/if} +{/block} + + +{block name="start_field_block"} + {if $input['type'] == 'group_discount_category'} +
+ +
+ {l s='Add a category discount'} + + {foreach $input['values'] key=key item=category } + + + + + + {/foreach} +
{$category.path}{l s='Discount:'} {$category.reduction}{l s='%'} + + +
+ + +
+ {elseif $input['type'] == 'modules'} +
+
+ + + + + + + + + + + + + + + + + + + + +
+ {$input['label']['auth_modules']} + + {$input['label']['unauth_modules']} +
+
    +
  • + {foreach $input['values']['auth_modules'] key=key item=module } +
  • + + + {$module->displayName} + +
  • + + {/foreach} +
+
+
    +
  • + {foreach $input['values']['unauth_modules'] key=key item=module } +
  • + + + {$module->displayName} + +
  • + {/foreach} +
+
+ + + + + +
+
+ {else} +
+ {/if} +{/block} \ No newline at end of file diff --git a/admin-dev/themes/template/groups/view.tpl b/admin-dev/themes/template/groups/view.tpl new file mode 100755 index 000000000..7d487cf1e --- /dev/null +++ b/admin-dev/themes/template/groups/view.tpl @@ -0,0 +1,55 @@ +{* +* 2007-2011 PrestaShop +* +* NOTICE OF LICENSE +* +* This source file is subject to the Academic Free License (AFL 3.0) +* that is bundled with this package in the file LICENSE.txt. +* It is also available through the world-wide-web at this URL: +* http://opensource.org/licenses/afl-3.0.php +* If you did not receive a copy of the license and are unable to +* obtain it through the world-wide-web, please send an email +* to license@prestashop.com so we can send you a copy immediately. +* +* DISCLAIMER +* +* Do not edit or add to this file if you wish to upgrade PrestaShop to newer +* versions in the future. If you wish to customize PrestaShop for your +* needs please refer to http://www.prestashop.com for more information. +* +* @author PrestaShop SA +* @copyright 2007-2011 PrestaShop SA +* @version Release: $Revision: 9596 $ +* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*} + +
+ {l s='Name:'} +
 
+ {l s='Discount:'} {$group->reduction} {l s='%'} +
 
+ {l s='Current category discount:'} + {if !$categorieReductions} + {l s='None'} + {else} + + {foreach $categorieReductions key=key item=category } + + + + + {/foreach} +
{$category.path}{l s='Discount:'} {$category.reduction}{l s='%'}
+ {/if} +
 
+ {l s='Price display method:'} + {if $group->price_display_method} + {l s='Tax excluded'} + {else} + {l s='Tax included'} + {/if} +
 
+
+

{l s='Customer members of this group'}

+{$customerList} \ No newline at end of file diff --git a/classes/Group.php b/classes/Group.php index 068ad84fd..83d9cd8d5 100644 --- a/classes/Group.php +++ b/classes/Group.php @@ -256,14 +256,16 @@ class GroupCore extends ObjectModel * @param integer authorized */ public static function addModulesRestrictions($id_group, $modules, $authorized) - { - if (!is_array($modules)) + { + if (!is_array($modules) AND !empty($modules)) return false; else { + //delete all record for this group + Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'group_module_restriction` WHERE `id_group` = '.(int)$id_group.' AND `authorized` = '.(int)$authorized); $sql = 'INSERT INTO `'._DB_PREFIX_.'group_module_restriction` (`id_group`, `id_module`, `authorized`) VALUES '; foreach ($modules as $mod) - $sql .= '("'.(int)$id_group.'", "'.(int)Module::getModuleIdByName($mod).'", "'.(int)$authorized.'"),'; + $sql .= '("'.(int)$id_group.'", "'.(int)$mod.'", "'.(int)$authorized.'"),'; // removing last comma to avoid SQL error $sql = substr($sql, 0, strlen($sql) - 1); Db::getInstance()->execute($sql); diff --git a/controllers/admin/AdminGroupsController.php b/controllers/admin/AdminGroupsController.php new file mode 100644 index 000000000..5ed377001 --- /dev/null +++ b/controllers/admin/AdminGroupsController.php @@ -0,0 +1,367 @@ + +* @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 AdminGroupsController extends AdminController +{ + public function __construct() + { + $this->table = 'group'; + $this->className = 'Group'; + $this->lang = true; + $this->addRowAction('edit'); + $this->addRowAction('view'); + $this->addRowAction('delete'); + + $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')); + + $this->addRowActionSkipList('delete', array(1)); + //$this->addRowActionSkipList('delete', array(Group::UNINDENTIFIED, Group::GUEST, Group::CUSTOMER)); + + parent::__construct(); + } + + public function setMedia() + { + parent::setMedia(); + $this->addJqueryPlugin('fancybox'); + $this->addJqueryUi('ui.sortable'); + } + + public function initView() + { + $this->context = Context::getContext(); + if (!($group = $this->loadObject(true))) + return; + + $this->tpl_view_vars = array( + 'group' => $group, + 'customerList' => $this->initCustomersList($group), + 'categorieReductions' => $this->formatCategoryDiscountList($group->id) + ); + + return parent::initView(); + } + + protected function initCustomersList($group) + { + $genders = array(0 => $this->l('?')); + foreach (Gender::getGenders() as $gender) + { + $genders_icon[$gender->id] = '../genders/'.(int)$gender->id.'.jpg'; + $genders[$gender->id] = $gender->name; + } + $customerFieldsDisplay = (array( + 'id_customer' => array('title' => $this->l('ID'), 'align' => 'center'), + 'id_gender' => array('title' => $this->l('Gender'), 'align' => 'center', 'icon' => $genders_icon, 'list' => $genders), + 'firstname' => array('title' => $this->l('Name'), 'align' => 'center'), + 'lastname' => array('title' => $this->l('Name'), 'align' => 'center'), + 'email' => array('title' => $this->l('E-mail address'), 'align' => 'center'), + 'birthday' => array('title' => $this->l('Birth date'), 'align' => 'center', 'type' => 'date'), + 'date_add' => array('title' => $this->l('Register date'), 'align' => 'center', 'type' => 'date'), + 'orders' => array('title' => $this->l('Orders'), 'align' => 'center'), + 'active' => array('title' => $this->l('Enabled'),'align' => 'center','active' => 'status','type' => 'bool') + )); + + $customerList = $group->getCustomers(false); + + $helper = new HelperList(); + $helper->currentIndex = self::$currentIndex; + $helper->token = $this->token; + $helper->shopLinkType = ''; + $helper->identifier = 'id_customer'; + $helper->actions = array('edit', 'view'); + $helper->show_toolbar = false; + + return $helper->generateList($customerList, $customerFieldsDisplay); + } + + public function initForm() + { + if (!($group = $this->loadObject(true))) + return; + + $this->fields_form = array( + 'legend' => array( + 'title' => $this->l('Customer group'), + 'image' => '../img/admin/tab-groups.gif' + ), + 'submit' => array( + 'title' => $this->l(' Save '), + 'class' => 'button' + ), + 'input' => array( + array( + 'type' => 'text', + 'label' => $this->l('Name:'), + 'name' => 'name', + 'size' => 33, + 'required' => true, + 'lang' => true, + 'hint' => $this->l('Forbidden characters:').' 0-9!<>,;?=+()@#"�{}_$%:' + ), + array( + 'type' => 'text', + 'label' => $this->l('Discount:'), + 'name' => 'reduction', + 'size' => 33, + 'p' => $this->l('Will automatically apply this value as a discount on ALL shop\'s products for this group\'s members.') + ), + array( + 'type' => 'select', + 'label' => $this->l('Price display method:'), + 'name' => 'price_display_method', + 'p' => $this->l('How the prices are displayed on order summary for this customer group (tax included or excluded).'), + 'options' => array( + 'query' => array(array('id_method' => PS_TAX_EXC, 'name' => $this->l('Tax excluded'), array('id_method' => PS_TAX_INC, 'name' => $this->l('Tax included')))), + 'id' => 'id_method', + 'name' => 'name' + ) + ), + array( + 'type' => 'group_discount_category', + 'label' => $this->l('Category discount:'), + 'name' => 'reduction', + 'size' => 33, + 'values' => ($group->id ? $this->formatCategoryDiscountList((int)$group->id) : array()) + ), + array( + 'type' => 'modules', + 'label' => array('auth_modules' => $this->l('Authorized modules :'), 'unauth_modules' => $this->l('Unauthorized modules :')), + 'name' => 'auth_modules', + 'values' => $this->formatModuleListAuth($group->id) + ) + ) + ); + + $trads = array( + 'Home' => $this->l('Home'), + 'selected' => $this->l('selected'), + 'Collapse All' => $this->l('Collapse All'), + 'Expand All' => $this->l('Expand All'), + 'Check All' => $this->l('Check All'), + 'Uncheck All' => $this->l('Uncheck All'), + 'search' => $this->l('Search a category') + ); + $this->tpl_form_vars['categoryTreeView'] = Helper::renderAdminCategorieTree($trads, array(), 'id_category', true); + + return parent::initForm(); + + } + + protected function formatCategoryDiscountList($id) + { + $categorie = GroupReduction::getGroupReductions((int)$id, $this->context->language->id); + $categorieReductions = array(); + $category_reduction = Tools::getValue('category_reduction'); + + foreach($categorie as $category) + { + if (is_array($category_reduction) AND array_key_exists($category['id_category'], $category_reduction)) + $category['reduction'] = $category_reduction[$category['id_category']]; + + $tmp = array(); + $tmp['path'] = getPath(self::$currentIndex.'?tab=AdminCatalog', (int)$category['id_category']); + $tmp['reduction'] = (float)$category['reduction'] * 100; + $tmp['id_category'] = (int)$category['id_category']; + $categorieReductions[(int)$category['id_category']] = $tmp; + } + + if (is_array($category_reduction)) + foreach($category_reduction as $key => $val) + { + if (!array_key_exists($key, $categorieReductions)) + { + $tmp = array(); + $tmp['path'] = getPath(self::$currentIndex.'?tab=AdminCatalog', (int)$key); + $tmp['reduction'] = (float)$val * 100; + $tmp['id_category'] = (int)$key; + $categorieReductions[(int)$category['id_category']] = $tmp; + } + } + + return $categorieReductions; + } + + public function formatModuleListAuth($id_group) + { + $modules = Module::getModulesInstalled(); + $authorized_modules = ''; + + $auth_modules = array(); + $unauth_modules = array(); + + if ($id_group) + $authorized_modules = Module::getAuthorizedModules($id_group); + + if (is_array($authorized_modules)) + { + foreach ($modules as $module) + { + $authorized = false; + foreach ($authorized_modules as $auth_module) + if ($module['id_module'] == $auth_module['id_module']) + $authorized = true; + + if ($authorized) + $auth_modules[] = $module; + else + $unauth_modules[] = $module; + } + } + else + $auth_modules = $modules; + $auth_modules_tmp = array(); + foreach($auth_modules as $key => $val) + $auth_modules_tmp[] = Module::getInstanceById($val['id_module']); + + $auth_modules = $auth_modules_tmp; + + $unauth_modules_tmp = array(); + foreach($unauth_modules as $key => $val) + $unauth_modules_tmp[] = Module::getInstanceById($val['id_module']); + + $unauth_modules = $unauth_modules_tmp; + + return array('unauth_modules' => $unauth_modules, 'auth_modules' => $auth_modules); + } + + public function postProcess() + { + if (Tools::isSubmit('submitAddgroup')) + { + if ($this->tabAccess['add'] === '1') + { + if (!$this->validateDiscount(Tools::getValue('reduction'))) + $this->_errors[] = Tools::displayError('Discount value is incorrect (must be a percentage)'); + else + { + $this->updateCategoryReduction(); + $this->updateRestrictions(); + parent::postProcess(); + } + } + else + $this->_errors[] = Tools::displayError('You do not have permission to add here.'); + } + else + parent::postProcess(); + } + + protected function validateDiscount($reduction) + { + if (!Validate::isPrice($reduction) || $reduction > 100 || $reduction < 0) + return false; + else + return true; + } + + public function ajaxProcessAddCategoryReduction() + { + $category_reduction = Tools::getValue('category_reduction'); + $id_category = Tools::getValue('id_category'); //no cast validation is done with Validate::isUnsignedId($id_category) + + $result = array(); + if (!Validate::isUnsignedId($id_category)) + { + $result['errors'][] = Tools::displayError('Wrong category ID'); + $result['hasError'] = true; + } + else if (!$this->validateDiscount($category_reduction)) + { + $result['errors'][] = Tools::displayError('Discount value is incorrect (must be a percentage)'); + $result['hasError'] = true; + } + else + { + $result['id_category'] = (int)$id_category; + $result['catPath'] = getPath(self::$currentIndex.'?tab=AdminCatalog', (int)$id_category); + $result['discount'] = $category_reduction; + $result['hasError'] = false; + } + die(Tools::jsonEncode($result)); + } + + + /** + * Update (or create) restrictions for modules by group + */ + protected function updateRestrictions() + { + $id_group = Tools::getValue('id_group'); + $unauth_modules = Tools::getValue('modulesBoxUnauth'); + $auth_modules = Tools::getValue('modulesBoxAuth'); + $return = true; + if ($id_group) + Group::truncateModulesRestrictions((int)$id_group); + + if (is_array($auth_modules)) + $return &= Group::addModulesRestrictions($id_group, $auth_modules, 1); + if (is_array($unauth_modules)) + $return &= Group::addModulesRestrictions($id_group, $unauth_modules, 0); + return $return; + } + + protected function updateCategoryReduction() + { + $category_reduction = Tools::getValue('category_reduction'); + if (is_array($category_reduction)) + { + foreach ($category_reduction as $cat => $reduction) + { + if (!Validate::isUnsignedId($cat) || !$this->validateDiscount($reduction)) + $this->_errors[] = Tools::displayError('Discount value is incorrect'); + else + { + Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'group_reduction` WHERE `id_group` = '.(int)Tools::getValue('id_group').' AND `id_category` = '.(int)$cat); + Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'product_group_reduction_cache` WHERE `id_group` = '.(int)Tools::getValue('id_group')); + $category = new Category((int)($cat)); + $category->addGroupsIfNoExist((int)Tools::getValue('id_group')); + $groupReduction = new GroupReduction(); + $groupReduction->id_group = (int)Tools::getValue('id_group'); + $groupReduction->reduction = (float)($reduction/100); + $groupReduction->id_category = (int)$cat; + if (!$groupReduction->save()) + $this->_errors[] = Tools::displayError('Cannot save group reductions'); + } + } + } + } +} diff --git a/css/admin.css b/css/admin.css index 57b99c07d..eb8397de6 100644 --- a/css/admin.css +++ b/css/admin.css @@ -1766,9 +1766,21 @@ div.progressBarImage color:#fff; background:url(../img/loader.gif); } - - +ul.connectedSortable +{ + padding-left: 0; +} +ul.connectedSortable li.module_list { + margin: 0 5px 5px 5px; + padding: 5px; + font-size: 1.2em; + border: 1px solid #CCC; + background: #F6F6F6; + font-weight: bold; + color: #1C94C4; + outline: none; +} /* Accounting css */ .formAccountingExport { display: none; -} \ No newline at end of file +}