From eb6620fabef7b945ffd6c6b121ceef6de894d01a Mon Sep 17 00:00:00 2001 From: lLefevre Date: Fri, 4 Nov 2011 15:52:31 +0000 Subject: [PATCH] [*] BO #PSFV-94 : Added AdminAccessController git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@9896 b9a71923-0436-4b27-9f14-aed3839534dd --- admin-dev/ajax.php | 95 ++++- admin-dev/tabs/AdminAccess.php | 365 ------------------ admin-dev/themes/default/admin.css | 4 +- admin-dev/themes/template/access/form.tpl | 387 ++++++++++++++++++++ classes/AdminController.php | 21 +- classes/HelperForm.php | 4 + controllers/admin/AdminAccessController.php | 109 ++++++ js/ajax.js | 20 +- 8 files changed, 614 insertions(+), 391 deletions(-) delete mode 100644 admin-dev/tabs/AdminAccess.php create mode 100644 admin-dev/themes/template/access/form.tpl create mode 100644 controllers/admin/AdminAccessController.php diff --git a/admin-dev/ajax.php b/admin-dev/ajax.php index d1ceff13b..1b2e3b520 100644 --- a/admin-dev/ajax.php +++ b/admin-dev/ajax.php @@ -949,22 +949,22 @@ if (Tools::isSubmit('searchCategory')) 'SELECT c.`id_category`, cl.`name` FROM `'._DB_PREFIX_.'category` c LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category`'.$context->shop->addSqlRestrictionOnLang('cl').') - WHERE cl.`id_lang` = '.(int)($context->language->id).' AND c.`level_depth` <> 0 + WHERE cl.`id_lang` = '.(int)$context->language->id.' AND c.`level_depth` <> 0 AND cl.`name` LIKE \'%'.pSQL($q).'%\' GROUP BY c.id_category ORDER BY c.`position` LIMIT '.(int)$limit); if ($results) - foreach ($results AS $result) - echo trim($result['name']).'|'.(int)($result['id_category'])."\n"; + foreach ($results as $result) + echo trim($result['name']).'|'.(int)$result['id_category']."\n"; } -if (Tools::isSubmit('getParentCategoriesId') AND $id_category = Tools::getValue('id_category')) +if (Tools::isSubmit('getParentCategoriesId') && $id_category = Tools::getValue('id_category')) { $category = new Category((int)$id_category); $results = Db::getInstance()->executeS('SELECT `id_category` FROM `'._DB_PREFIX_.'category` c WHERE c.`nleft` < '.(int)$category->nleft.' AND c.`nright` > '.(int)$category->nright.''); $output = array(); - foreach($results as $result) + foreach ($results as $result) $output[] = $result; die(Tools::jsonEncode($output)); @@ -976,9 +976,90 @@ if (Tools::isSubmit('ajaxUpdateTaxRule')) $id_tax_rule = Tools::getValue('id_tax_rule'); $tax_rules = new TaxRule((int)$id_tax_rule); $output = array(); - foreach($tax_rules as $key => $result) + foreach ($tax_rules as $key => $result) $output[$key] = $result; die(Tools::jsonEncode($output)); - +} + +/* Update Access Tabs */ +if (Tools::isSubmit('submitAddAccess')) +{ + $perm = Tools::getValue('perm'); + if (!in_array($perm, array('view', 'add', 'edit', 'delete', 'all'))) + throw new PrestashopException('permission not exists'); + + $enabled = (int)Tools::getValue('enabled'); + $id_tab = (int)Tools::getValue('id_tab'); + $id_profile = (int)Tools::getValue('id_profile'); + $res = true; + + if ($id_tab == -1 && $perm == 'all' && $enabled == 0) + $res &= Db::getInstance()->execute(' + UPDATE `'._DB_PREFIX_.'access` + SET `view` = '.$enabled.', `add` = '.$enabled.', `edit` = '.$enabled.', `delete` = '.$enabled.' + WHERE `id_profile` = '.(int)$id_profile.' AND `id_tab` != 31 + '); + else if ($id_tab == -1 && $perm == 'all') + $res &= Db::getInstance()->execute(' + UPDATE `'._DB_PREFIX_.'access` + SET `view` = '.$enabled.', `add` = '.$enabled.', `edit` = '.$enabled.', `delete` = '.$enabled.' + WHERE `id_profile` = '.(int)$id_profile + ); + else if ($id_tab == -1) + $res &= Db::getInstance()->execute(' + UPDATE `'._DB_PREFIX_.'access` + SET `'.pSQL($perm).'` = '.$enabled.' + WHERE `id_profile` = '.(int)$id_profile + ); + else if ($perm == 'all') + $res &= Db::getInstance()->execute(' + UPDATE `'._DB_PREFIX_.'access` + SET `view` = '.$enabled.', `add` = '.$enabled.', `edit` = '.$enabled.', `delete` = '.$enabled.' + WHERE `id_tab` = '.(int)$id_tab.' + AND `id_profile` = '.(int)$id_profile + ); + else + $res &= Db::getInstance()->execute(' + UPDATE `'._DB_PREFIX_.'access` + SET `'.pSQL($perm).'` = '.$enabled.' + WHERE `id_tab` = '.(int)$id_tab.' + AND `id_profile` = '.(int)$id_profile + ); + $res = $res?'ok':'error'; + die(Tools::jsonEncode($res)); +} + +/* Update Access Modules */ +if (Tools::isSubmit('changeModuleAccess')) +{ + $perm = Tools::getValue('perm'); + $enabled = (int)Tools::getValue('enabled'); + $id_module = (int)Tools::getValue('id_module'); + $id_profile = (int)Tools::getValue('id_profile'); + $res = true; + + if (!in_array($perm, array('view', 'configure'))) + throw new PrestashopException('permission not exists'); + + if ($id_module == -1) + { + $res &= Db::getInstance()->execute(' + UPDATE `'._DB_PREFIX_.'module_access` + SET `'.pSQL($perm).'` = '.(int)$enabled.' + WHERE `id_profile` = '.(int)$id_profile + ); + } + else + { + $res &= Db::getInstance()->execute(' + UPDATE `'._DB_PREFIX_.'module_access` + SET `'.pSQL($perm).'` = '.(int)$enabled.' + WHERE `id_module` = '.(int)$id_module.' + AND `id_profile` = '.(int)$id_profile + ); + } + + $res = $res?'ok':'error'; + die(Tools::jsonEncode($res)); } diff --git a/admin-dev/tabs/AdminAccess.php b/admin-dev/tabs/AdminAccess.php deleted file mode 100644 index 37b531e4c..000000000 --- a/admin-dev/tabs/AdminAccess.php +++ /dev/null @@ -1,365 +0,0 @@ - -* @copyright 2007-2011 PrestaShop SA -* @version Release: $Revision: 6844 $ -* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) -* International Registered Trademark & Property of PrestaShop SA -*/ - -include_once(_PS_ADMIN_DIR_.'/../classes/AdminTab.php'); - -class AdminAccess extends AdminTab -{ - private $return_status; - private $return_message; - - public function processSubmitAddAccess() - { - $perm = Tools::getValue('perm') ; - if (!in_array($perm, array('view', 'add', 'edit', 'delete', 'all'))) - throw new PrestashopException('permission not exists'); - - $enabled = (int)Tools::getValue('enabled'); - $id_tab = (int)(Tools::getValue('id_tab')); - $id_profile = (int)(Tools::getValue('id_profile')); - $res = true; - - if ($id_tab == -1 AND $perm == 'all' AND $enabled == 0) - $res &= Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'access` SET `view` = '.$enabled.', `add` = '.$enabled.', `edit` = '.$enabled.', `delete` = '.$enabled.' WHERE `id_profile` = '.(int)($id_profile).' AND `id_tab` != 31'); - else if ($id_tab == -1 AND $perm == 'all') - $res &= Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'access` SET `view` = '.$enabled.', `add` = '.$enabled.', `edit` = '.$enabled.', `delete` = '.$enabled.' WHERE `id_profile` = '.(int)($id_profile)); - else if ($id_tab == -1) - $res &= Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'access` SET `'.pSQL($perm).'` = '.$enabled.' WHERE `id_profile` = '.(int)($id_profile)); - else if ($perm == 'all') - $res &= Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'access` SET `view` = '.$enabled.', `add` = '.$enabled.', `edit` = '.$enabled.', `delete` = '.$enabled.' WHERE `id_tab` = '.(int)($id_tab).' AND `id_profile` = '.(int)($id_profile)); - else - $res &= Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'access` SET `'.pSQL($perm).'` = '.$enabled.' WHERE `id_tab` = '.(int)($id_tab).' AND `id_profile` = '.(int)($id_profile)); - - $this->return_status = $res?'ok':'error'; - if ($res) - $this->return_message = $this->l('Access successfully updated'); - else - $this->return_message = $this->l('An error when updating access'); - } - - public function processChangeModuleAccess() - { - $perm = Tools::getValue('perm'); - $enabled = (int)Tools::getValue('enabled'); - $id_module = (int)Tools::getValue('id_module'); - $id_profile = (int)Tools::getValue('id_profile'); - $res = true; - - if (!in_array($perm, array('view', 'configure'))) - throw new PrestashopException('permission not exists'); - - if ($id_module == -1) - $res &= Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'module_access` SET `'.pSQL($perm).'` = '.(int)$enabled.' WHERE `id_profile` = '.(int)$id_profile); - else - $res &= Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'module_access` SET `'.pSQL($perm).'` = '.(int)$enabled.' WHERE `id_module` = '.(int)$id_module.' AND `id_profile` = '.(int)$id_profile); - - $this->return_status = $res?'ok':'error'; - if ($res) - $this->return_message = $this->l('Access successfully updated.'); - else - $this->return_message = $this->l('An error when updating access.'); - } - - - public function displayAjax() - { - $return = array('result'=>$this->return_status,'msg'=>$this->return_message); - - echo Tools::jsonEncode($return); - } - public function display() - { - $this->displayForm(); - echo ''; - - } - - /** - * Get the current profile id - * - * @return the $_GET['profile'] if valid, else 1 (the first profile id) - */ - function getCurrentProfileId() - { - return (isset($_GET['profile']) AND !empty($_GET['profile']) AND is_numeric($_GET['profile'])) ? (int)($_GET['profile']) : 1; - } - - public function displayForm($isMainTab = true) - { - parent::displayForm(); - $currentProfile = (int)($this->getCurrentProfileId()); - $tabs = Tab::getTabs($this->context->language->id); - $profiles = Profile::getProfiles($this->context->language->id); - $accesses = Profile::getProfileAccesses($currentProfile); - - echo ' - - - - '; - - if ($currentProfile != (int)(_PS_ADMIN_PROFILE_)) - echo ' - - - - - - '; - - if (!sizeof($tabs)) - echo ''; - elseif ($currentProfile == (int)(_PS_ADMIN_PROFILE_)) - echo ''; - else - foreach ($tabs AS $tab) - if (!$tab['id_parent'] OR (int)($tab['id_parent']) == -1) - { - $this->printTabAccess((int)($currentProfile), $tab, $accesses[$tab['id_tab']], false, $tabsize, sizeof($tabs)); - foreach ($tabs AS $child) - if ($child['id_parent'] === $tab['id_tab']) - if (isset($accesses[$child['id_tab']])) - { - - $this->printTabAccess($currentProfile, $child, $accesses[$child['id_tab']], true, $tabsize, sizeof($tabs)); - } - } - echo '
- - - tabAccess['edit'] == 1 ? ' rel="-1||'.$currentProfile.'||view||'.$tabsize.'||'.sizeof($tabs).'" class="ajaxPower"' : 'disabled="disabled"').' /> - '.$this->l('View').' - - tabAccess['edit'] == 1 ? ' rel="-1||'.$currentProfile.'||add||'.$tabsize.'||'.sizeof($tabs).'" class="ajaxPower"' : 'disabled="disabled"').' /> - '.$this->l('Add').' - - tabAccess['edit'] == 1 ? ' rel="-1||'.$currentProfile.'||edit||'.$tabsize.'||'.sizeof($tabs).'" class="ajaxPower"' : 'disabled="disabled"').' /> - '.$this->l('Edit').' - - tabAccess['edit'] == 1 ? ' rel="-1||'.$currentProfile.'||delete||'.$tabsize.'||'.sizeof($tabs).'" class="ajaxPower"' : 'disabled="disabled"').' /> - '.$this->l('Delete').' - - tabAccess['edit'] == 1 ? ' rel="-1||'.$currentProfile.'||all||'.$tabsize.'||'.sizeof($tabs).'" class="ajaxPower"' : 'disabled="disabled"').' /> - '.$this->l('All').' -
'.$this->l('No tab').'
'.$this->l('Administrator permissions can\'t be modified.').'
'; - - if ($currentProfile != (int)(_PS_ADMIN_PROFILE_)) - $this->displayModuleAccesses($currentProfile); - echo '
 
'; - } - - private function printTabAccess($currentProfile, $tab, $access, $is_child, $tabsize, $tabnumber) - { - $result_accesses = 0; - $perms = array('view', 'add', 'edit', 'delete'); - echo ''.($is_child ? ' » ' : '').$tab['name'].''; - foreach ($perms as $perm) - { - if ($this->tabAccess['edit'] == 1) - echo ''; - else - echo ''; - $result_accesses += $access[$perm]; - } - echo ' - tabAccess['edit'] == 1 ? ' rel="'.(int)($access['id_tab']).'||'.(int)($currentProfile).'||all||'.$tabsize.'||'.$tabnumber.'" class="ajaxPower all '.(int)($access['id_tab']).'"' : ' class="all '.(int)($access['id_tab']).'" disabled="disabled"').' - '.($result_accesses == 4 ? 'checked="checked"' : '').' - /> - '; - } - - public function ajaxProcess() - { - /* PrestaShop demo mode */ - if (_PS_MODE_DEMO_) - { - $this->_errors[] = Tools::displayError('This functionnality has been disabled.'); - return; - } - /* PrestaShop demo mode*/ - - if ($this->tabAccess['edit'] == 1) - { - if (Tools::isSubmit('submitAddaccess')) - $this->processSubmitAddAccess(); - if (Tools::isSubmit('changeModuleAccess')) - $this->processChangeModuleAccess(); - } - } - - private function displayModuleAccesses($currentProfile) - { - echo ' - - - - - - - '; - - $modules = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' - SELECT ma.id_module, m.name, ma.`view`, ma.`configure` - FROM '._DB_PREFIX_.'module_access ma - LEFT JOIN '._DB_PREFIX_.'module m ON ma.id_module = m.id_module - WHERE id_profile = '.(int)$currentProfile.' - ORDER BY m.name'); - if (!sizeof($modules)) - echo ''; - else - foreach ($modules AS $module) - echo ' - - - - '; - - echo '
'.$this->l('Modules').'tabAccess['edit'] == 1 ? 'class="changeModuleAccess" rel="-1||view"' : 'disabled="disabled"').' /> '.$this->l('View').'tabAccess['edit'] == 1 ? 'class="changeModuleAccess" rel="-1||configure"' : 'disabled="disabled"').' /> '.$this->l('Configure').'
'.$this->l('No modules installed').'
» '.$module['name'].' - tabAccess['edit'] == 1 ? 'class="ajax-ma-view changeModuleAccess" rel="'.(int)$module['id_module'].'||view"' : ' class="ajax-ma-view" disabled="disabled"').' - /> - - tabAccess['edit'] == 1 ? ' class="ajax-ma-configure changeModuleAccess" rel="'.(int)$module['id_module'].'||configure"' : ' class="ajax-ma-configure" disabled="disabled"').' - /> -
'; - } -} - - diff --git a/admin-dev/themes/default/admin.css b/admin-dev/themes/default/admin.css index 720faf539..ca2f3ae0b 100644 --- a/admin-dev/themes/default/admin.css +++ b/admin-dev/themes/default/admin.css @@ -154,14 +154,14 @@ a.module_toggle_all{color: #268CCD;} #productBox { position:relative; width:100%; float:left;} /*tab-pane*/ -form#product{ background-color:#ebedf4; border:1px solid #ccced7; min-height:300px; padding: 5px 10px 10px; margin-left:140px;} +form#product, form#access_form{ background-color:#ebedf4; border:1px solid #ccced7; min-height:300px; padding: 5px 10px 10px; margin-left:140px;} form.defaultForm{ background-color:#ebedf4; border:1px solid #ccced7; min-height:300px; padding: 5px 10px 10px;} .tab-row {} .tab-row .tab {background:#EFEFEF;width:100px;} .tab-row .tab-page.selected {} .tab-row .tab-page {} .productTabs{ background-color:#fafafa; border:1px solid #ccced7; border-right:none; float:left; width:140px;} -.productTabs ul{padding:0;} +.productTabs ul{padding:0;margin:0;} .productTabs ul li.tab-row { border:none; } .productTabs ul li.tab-row:first-child a {border-top:none;} .productTabs ul li.tab-row a { display:block; font-weight:normal; color:#666;padding: 7px 10px; border-top:1px solid #CCCED7; border-right:1px solid #CCCED7;} diff --git a/admin-dev/themes/template/access/form.tpl b/admin-dev/themes/template/access/form.tpl new file mode 100644 index 000000000..60d9764a5 --- /dev/null +++ b/admin-dev/themes/template/access/form.tpl @@ -0,0 +1,387 @@ +{* +* 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 +*} + + + +
+{if $show_toolbar} + {include file="toolbar.tpl" toolbar_btn=$toolbar_btn} +
+

{block name=pageTitle} + {$title|default:' '} + {/block}

+
+{/if} +
{block name="leadin"}{/block}
+
+ +
+ +
+ +
+ {if $form_id} + + {/if} + + {assign var=tabsize value=count($tabs)} + {foreach $tabs AS $tab} + {if $tab.id_tab > $tabsize} + {assign var=tabsize value=$tab.id_tab} + {/if} + {/foreach} + + {foreach $profiles as $profile} + +
+ + {if $profile.id_profile != $admin_profile} + + + + + + + + + + {if !count($tabs)} + + + + {else} + {foreach $tabs AS $tab} + + {assign var=access value=$accesses[$profile.id_profile]} + + {if !$tab.id_parent OR $tab.id_parent == -1} + {assign var=is_child value=false} + {assign var=result_accesses value=0} + + {if $is_child} » {/if}{$tab.name} + {foreach $perms as $perm} + {if $access_edit == 1} + + {else} + + {/if} + {assign var=result_accesses value=$result_accesses + $access[$tab.id_tab][$perm]} + {/foreach} + + + + {foreach $tabs AS $child} + {if $child.id_parent === $tab.id_tab} + {if isset($access[$child.id_tab])} + {assign var=is_child value=true} + {assign var=result_accesses value=0} + + {if $is_child} » {/if}{$child.name} + {foreach $perms as $perm} + {if $access_edit == 1} + + {else} + + {/if} + {assign var=result_accesses value=$result_accesses + $access[$child.id_tab][$perm]} + {/foreach} + + + {/if} + {/if} + {/foreach} + + {/if} + + {/foreach} + {/if} +
+ {l s='Tabs'} + + + {l s='View'} + + + {l s='Add'} + + + {l s='Edit'} + + + {l s='Delete'} + + + {l s='All'} +
{l s='No tab'}
+ + + + + +
+ + + + + +
+ + + + + + + + + {if !count($modules)} + + + + {else} + {foreach $modules[$profile.id_profile] AS $module} + + + + + + {/foreach} + {/if} +
{l s='Modules'} + {l s='View'} + + {l s='Configure'}
{l s='No modules installed'}
» {$module.name} + + + +
+ +
 
+ + {else} + {l s="Administrator permissions can't be modified."} + {/if} + +
+ + {/foreach} +
\ No newline at end of file diff --git a/classes/AdminController.php b/classes/AdminController.php index a7399e03b..a84da23bd 100644 --- a/classes/AdminController.php +++ b/classes/AdminController.php @@ -74,6 +74,8 @@ class AdminControllerCore extends Controller public $tpl_form_vars = array(); public $tpl_list_vars = array(); + public $fields_value = false; + /** @var array Errors displayed after post processing */ public $_errors = array(); @@ -1364,6 +1366,8 @@ class AdminControllerCore extends Controller $this->content .= $this->context->smarty->fetch($this->context->smarty->template_dir[0].'form_submit_ajax.tpl'); if ($this->fields_form && is_array($this->fields_form)) { + if (empty($this->toolbar_title)) + $this->initToolbarTitle(); $this->getlanguages(); $helper = new HelperForm($this); $helper->override_folder = $this->tpl_folder; @@ -1377,6 +1381,8 @@ class AdminControllerCore extends Controller $helper->allow_employee_form_lang = $this->allow_employee_form_lang; $helper->fields_value = $this->getFieldsValue($this->object); $helper->toolbar_btn = $this->toolbar_btn; + $helper->title = $this->toolbar_title; + $helper->show_toolbar = $this->show_toolbar; $helper->no_back = isset($this->no_back) ? $this->no_back : false; $helper->tpl_vars = $this->tpl_form_vars; if ($this->tabAccess['view']) @@ -1852,13 +1858,14 @@ class AdminControllerCore extends Controller public function getFieldsValue($obj) { - foreach ($this->fields_form['input'] as $input) - if (empty($this->fields_value[$input['name']])) - if (isset($input['lang']) && $input['lang']) - foreach ($this->_languages as $language) - $this->fields_value[$input['name']][$language['id_lang']] = $this->getFieldValue($obj, $input['name'], $language['id_lang']); - else - $this->fields_value[$input['name']] = $this->getFieldValue($obj, $input['name']); + if (isset($this->fields_form['input'])) + foreach ($this->fields_form['input'] as $input) + if (empty($this->fields_value[$input['name']])) + if (isset($input['lang']) && $input['lang']) + foreach ($this->_languages as $language) + $this->fields_value[$input['name']][$language['id_lang']] = $this->getFieldValue($obj, $input['name'], $language['id_lang']); + else + $this->fields_value[$input['name']] = $this->getFieldValue($obj, $input['name']); return $this->fields_value; } diff --git a/classes/HelperForm.php b/classes/HelperForm.php index 615d8274f..38df17da7 100644 --- a/classes/HelperForm.php +++ b/classes/HelperForm.php @@ -40,6 +40,9 @@ class HelperFormCore extends Helper public $fields_value = array(); public $table; + /** @var if not null, a title will be added on that list */ + public $title = null; + /** * Used to override default 'submitAdd' parameter in form action attribute * @var string @@ -83,6 +86,7 @@ class HelperFormCore extends Helper 'title' => $this->title, 'firstCall' => $this->first_call, 'current' => $this->currentIndex, + 'title' => $this->title, 'token' => $this->token, 'table' => $this->table, 'languages' => $this->languages, diff --git a/controllers/admin/AdminAccessController.php b/controllers/admin/AdminAccessController.php new file mode 100644 index 000000000..02a1bcac7 --- /dev/null +++ b/controllers/admin/AdminAccessController.php @@ -0,0 +1,109 @@ + +* @copyright 2007-2011 PrestaShop SA +* @version Release: $Revision: 8971 $ +* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*/ + +class AdminAccessController extends AdminController +{ + public function __construct() + { + $this->table = 'access'; + $this->className = 'Profile'; + $this->lang = false; + $this->context = Context::getContext(); + + parent::__construct(); + } + + /** + * AdminController::initForm() override + * @see AdminController::initForm() + */ + public function initForm() + { + $current_profile = (int)$this->getCurrentProfileId(); + $profiles = Profile::getProfiles($this->context->language->id); + $accesses = array(); + foreach ($profiles as $profile) + $accesses[$profile['id_profile']] = Profile::getProfileAccesses($profile['id_profile']); + + $modules = array(); + foreach ($profiles as $profile) + $modules[$profile['id_profile']] = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' + SELECT ma.`id_module`, m.`name`, ma.`view`, ma.`configure` + FROM '._DB_PREFIX_.'module_access ma + LEFT JOIN '._DB_PREFIX_.'module m + ON ma.id_module = m.id_module + WHERE id_profile = '.(int)$profile['id_profile'].' + ORDER BY m.name + '); + + $this->fields_form = array(''); + $this->tpl_form_vars = array( + 'profiles' => $profiles, + 'accesses' => $accesses, + 'tabs' => Tab::getTabs($this->context->language->id), + 'current_profile' => (int)$current_profile, + 'admin_profile' => (int)_PS_ADMIN_PROFILE_, + 'access_edit' => $this->tabAccess['edit'], + 'perms' => array('view', 'add', 'edit', 'delete'), + 'modules' => $modules + ); + + return parent::initForm(); + } + + /** + * AdminController::initContent() override + * @see AdminController::initContent() + */ + public function initContent() + { + $this->display = 'edit'; + // toolbar (save, cancel, new, ..) + $this->initToolbar(); + unset($this->toolbar_btn['save']); + + if (!$this->loadObject(true)) + return; + + $this->content .= $this->initForm(); + + $this->context->smarty->assign(array( + 'content' => $this->content, + 'url_post' => self::$currentIndex.'&token='.$this->token, + )); + } + + /** + * Get the current profile id + * + * @return the $_GET['profile'] if valid, else 1 (the first profile id) + */ + public function getCurrentProfileId() + { + return (isset($_GET['id_profile']) && !empty($_GET['id_profile']) && is_numeric($_GET['id_profile'])) ? (int)$_GET['id_profile'] : 1; + } +} \ No newline at end of file diff --git a/js/ajax.js b/js/ajax.js index 76f93f14d..c9f9b430e 100644 --- a/js/ajax.js +++ b/js/ajax.js @@ -87,23 +87,23 @@ function check_for_all_accesses(tabsize, tabnumber) } } -function perfect_access_js_gestion(src, action, id_tab, tabsize, tabnumber) +function perfect_access_js_gestion(src, action, id_tab, tabsize, tabnumber, table) { if (id_tab == '-1' && action == 'all') { - $('.add').attr('checked', src.checked); - $('.edit').attr('checked', src.checked); - $('.delete').attr('checked', src.checked); - $('.view').attr('checked', src.checked); - $('.all').attr('checked', src.checked); - $('.31').attr('checked', "checked"); + $(table+' .add').attr('checked', src.checked); + $(table+' .edit').attr('checked', src.checked); + $(table+' .delete').attr('checked', src.checked); + $(table+' .view').attr('checked', src.checked); + $(table+' .all').attr('checked', src.checked); + $(table+' .31').attr('checked', "checked"); } else if (action == 'all') - $('.'+id_tab).attr('checked', src.checked); + $(table+' .'+id_tab).attr('checked', src.checked); else if (id_tab == '-1') { - $('.'+action).attr('checked', src.checked); - $('#'+action+'31').attr('checked', "checked"); + $(table+' .'+action).attr('checked', src.checked); + $(table+' #'+action+'31').attr('checked', "checked"); } check_for_all_accesses(tabsize, tabnumber); }