* @copyright 2007-2011 PrestaShop SA * @version Release: $Revision: 7466 $ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * International Registered Trademark & Property of PrestaShop SA */ class AdminModulesPositions extends AdminTab { private $displayKey = 0; public function postProcess() { // Getting key value for display if (Tools::getValue('show_modules') AND strval(Tools::getValue('show_modules')) != 'all') $this->displayKey = (int)(Tools::getValue('show_modules')); // Change position in hook if (array_key_exists('changePosition', $_GET)) { if ($this->tabAccess['edit'] === '1') { $id_module = (int)(Tools::getValue('id_module')); $id_hook = (int)(Tools::getValue('id_hook')); $module = Module::getInstanceById($id_module); if (Validate::isLoadedObject($module)) { $module->updatePosition($id_hook, (int)(Tools::getValue('direction'))); Tools::redirectAdmin(self::$currentIndex.($this->displayKey ? '&show_modules='.$this->displayKey : '').'&token='.$this->token); } else $this->_errors[] = Tools::displayError('module cannot be loaded'); } else $this->_errors[] = Tools::displayError('You do not have permission to edit here.'); } // Add new module in hook elseif (Tools::isSubmit('submitAddToHook')) { if ($this->tabAccess['add'] === '1') { // Getting vars... $id_module = (int)(Tools::getValue('id_module')); $module = Module::getInstanceById($id_module); $id_hook = (int)(Tools::getValue('id_hook')); $hook = new Hook($id_hook); $excepts = explode(',', str_replace(' ', '', Tools::getValue('exceptions'))); // Checking vars... foreach ($excepts AS $except) if (!Validate::isFileName($except)) $this->_errors[] = Tools::displayError('No valid value for field exceptions'); if (!$id_module OR !Validate::isLoadedObject($module)) $this->_errors[] = Tools::displayError('module cannot be loaded'); elseif (!$id_hook OR !Validate::isLoadedObject($hook)) $this->_errors[] = Tools::displayError('Hook cannot be loaded.'); elseif (Hook::getModulesFromHook($id_hook, $id_module)) $this->_errors[] = Tools::displayError('This module is already transplanted to this hook.'); elseif (!$module->isHookableOn($hook->name)) $this->_errors[] = Tools::displayError('This module can\'t be transplanted to this hook.'); // Adding vars... else { if (!$module->registerHook($hook->name, Context::getContext()->shop->getListOfID())) $this->_errors[] = Tools::displayError('An error occurred while transplanting module to hook.'); elseif (!$module->registerExceptions($id_hook, $excepts, Context::getContext()->shop->getListOfID())) $this->_errors[] = Tools::displayError('An error occurred while transplanting module to hook.'); else Tools::redirectAdmin(self::$currentIndex.'&conf=16'.($this->displayKey ? '&show_modules='.$this->displayKey : '').'&token='.$this->token); } } else $this->_errors[] = Tools::displayError('You do not have permission to add here.'); } // Edit module from hook elseif (Tools::isSubmit('submitEditGraft')) { if ($this->tabAccess['add'] === '1') { // Getting vars... $id_module = (int)(Tools::getValue('id_module')); $module = Module::getInstanceById($id_module); $id_hook = (int)(Tools::getValue('id_hook')); $hook = new Hook($id_hook); if (!$id_module OR !Validate::isLoadedObject($module)) $this->_errors[] = Tools::displayError('module cannot be loaded'); elseif (!$id_hook OR !Validate::isLoadedObject($hook)) $this->_errors[] = Tools::displayError('Hook cannot be loaded.'); else { $exceptions = Tools::getValue('exceptions'); if (is_array($exceptions)) { foreach ($exceptions as $id => $exception) { $exception = explode(',', str_replace(' ', '', $exception)); // Check files name foreach ($exception AS $except) if (!Validate::isFileName($except)) $this->_errors[] = Tools::displayError('No valid value for field exceptions'); // Add files exceptions if (!$module->editExceptions($id_hook, $exception, Context::getContext()->shop->getListOfID())) $this->_errors[] = Tools::displayError('An error occurred while transplanting module to hook.'); } if (!$this->_errors) Tools::redirectAdmin(self::$currentIndex.'&conf=16'.($this->displayKey ? '&show_modules='.$this->displayKey : '').'&token='.$this->token); } else { $exceptions = explode(',', str_replace(' ', '', $exceptions)); // Check files name foreach ($exceptions AS $except) if (!Validate::isFileName($except)) $this->_errors[] = Tools::displayError('No valid value for field exceptions'); // Add files exceptions if (!$module->editExceptions($id_hook, $exceptions, Context::getContext()->shop->getListOfID())) $this->_errors[] = Tools::displayError('An error occurred while transplanting module to hook.'); else Tools::redirectAdmin(self::$currentIndex.'&conf=16'.($this->displayKey ? '&show_modules='.$this->displayKey : '').'&token='.$this->token); } } } else $this->_errors[] = Tools::displayError('You do not have permission to add here.'); } // Delete module from hook elseif (array_key_exists('deleteGraft', $_GET)) { if ($this->tabAccess['delete'] === '1') { $id_module = (int)(Tools::getValue('id_module')); $module = Module::getInstanceById($id_module); $id_hook = (int)(Tools::getValue('id_hook')); $hook = new Hook($id_hook); if (!Validate::isLoadedObject($module)) $this->_errors[] = Tools::displayError('module cannot be loaded'); elseif (!$id_hook OR !Validate::isLoadedObject($hook)) $this->_errors[] = Tools::displayError('Hook cannot be loaded.'); else { if (!$module->unregisterHook($id_hook, Context::getContext()->shop->getListOfID()) OR !$module->unregisterExceptions($id_hook, Context::getContext()->shop->getListOfID())) $this->_errors[] = Tools::displayError('An error occurred while deleting module from hook.'); else Tools::redirectAdmin(self::$currentIndex.'&conf=17'.($this->displayKey ? '&show_modules='.$this->displayKey : '').'&token='.$this->token); } } else $this->_errors[] = Tools::displayError('You do not have permission to delete here.'); } elseif (Tools::isSubmit('unhookform')) { if (!($unhooks = Tools::getValue('unhooks')) OR !is_array($unhooks)) $this->_errors[] = Tools::displayError('Select a module to unhook.'); else { foreach ($unhooks as $unhook) { $explode = explode('_', $unhook); $id_hook = $explode[0]; $id_module = $explode[1]; $module = Module::getInstanceById((int)($id_module)); $hook = new Hook((int)($id_hook)); if (!Validate::isLoadedObject($module)) $this->_errors[] = Tools::displayError('module cannot be loaded'); elseif (!$id_hook OR !Validate::isLoadedObject($hook)) $this->_errors[] = Tools::displayError('Hook cannot be loaded.'); else { if (!$module->unregisterHook((int)($id_hook)) OR !$module->unregisterExceptions((int)($id_hook))) $this->_errors[] = Tools::displayError('An error occurred while deleting module from hook.'); } } if (!sizeof($this->_errors)) Tools::redirectAdmin(self::$currentIndex.'&conf=17'.($this->displayKey ? '&show_modules='.$this->displayKey : '').'&token='.$this->token); } } } public function display() { if (array_key_exists('addToHook', $_GET) OR array_key_exists('editGraft', $_GET) OR (Tools::isSubmit('submitAddToHook') AND $this->_errors)) $this->displayForm(); else $this->displayList(); } public function displayList() { $context = Context::getContext(); $admin_dir = dirname($_SERVER['PHP_SELF']); $admin_dir = substr($admin_dir, strrpos($admin_dir,'/') + 1); echo ' '; echo ' '.$this->l('Transplant a module').'

'; // Print select list echo '
'.$this->l('Show').' :

 
'.$this->l('Live edit').''; if (Tools::isMultiShopActivated() && $context->shop->getContextType() != Shop::CONTEXT_SHOP) echo '

'.$this->l('You have to select a shop to use live edit').'

'; else echo '

'.$this->l('By clicking here you will be redirected to the front office of your shop to move and delete modules directly.').'


'.$this->l('Run LiveEdit').''; echo '
'; // Print hook list echo '
'; $irow = 0; $hooks = Hook::getHooks(!(int)(Tools::getValue('hook_position'))); echo '
'; $canMove = (Tools::isMultiShopActivated() && $context->shop->getContextType() != Shop::CONTEXT_SHOP) ? false : true; if (!$canMove) echo '
'.$this->l('If you want to order / move following data, please go in shop context (select a shop in shop list)').'
'; foreach ($hooks AS $hook) { $modules = Hook::getModulesFromHook($hook['id_hook'], $this->displayKey); $nbModules = count($modules); echo ' '; // Print modules list if ($nbModules) { $instances = array(); foreach ($modules AS $module) if ($tmpInstance = Module::getInstanceById((int)($module['id_module']))) $instances[] = $tmpInstance; foreach ($instances AS $position => $instance) { $position = $position + 1; echo ' '; if (!$this->displayKey) { echo ' = 2) ? ' class="dragHandle"' : '').' id="td_'.$hook['id_hook'].'_'.$instance->id.'" width="40"> '.(($canMove) ? ''.$this->l('Up').'
'.$this->l('Down').'' : '').'
'; } } else echo ''; echo '
'.$hook['title'].' - '.$nbModules.' '.(($nbModules > 1) ? $this->l('modules') : $this->l('module')); if ($nbModules && $canMove) echo ''; if (!empty($hook['description'])) echo ' ['.$hook['description'].']'; echo ' ('.$this->l('Technical name: ').$hook['name'].')
'.(int)($position).' '; echo ' '.$this->l('Edit').' '.$this->l('Delete').' '; echo '
'.$this->l('No module for this hook').'
'; } echo '
'; } public function displayForm($isMainTab = true) { parent::displayForm(); $id_module = (int)(Tools::getValue('id_module')); $id_hook = (int)(Tools::getValue('id_hook')); if (Tools::isSubmit('editGraft')) { // Check auth for this page if (!$id_module || !$id_hook) Tools::redirectAdmin(self::$currentIndex . '&token='.$this->token); $sql = 'SELECT id_module FROM '._DB_PREFIX_.'hook_module WHERE id_module = '.$id_module.' AND id_hook = '.$id_hook.' AND id_shop IN('.implode(', ', Context::getContext()->shop->getListOfID()).')'; if (!Db::getInstance()->getValue($sql)) Tools::redirectAdmin(self::$currentIndex . '&token='.$this->token); $slModule = Module::getInstanceById($id_module); $exceptsList = $slModule->getExceptions($id_hook, true); $exceptsDiff = false; $excepts = ''; if ($exceptsList) { $first = current($exceptsList); foreach ($exceptsList as $k => $v) if (array_diff($v, $first)) $exceptsDiff = true; if (!$exceptsDiff) $excepts = implode(', ', $first); } } else { $exceptsDiff = false; $excepts = strval(Tools::getValue('exceptions')); } $modules = Module::getModulesInstalled(0); $instances = array(); foreach ($modules AS $module) if ($tmpInstance = Module::getInstanceById($module['id_module'])) $instances[$tmpInstance->displayName] = $tmpInstance; ksort($instances); $modules = $instances; $hooks = Hook::getHooks(0); echo '
'; if ($this->displayKey) echo ''; echo '
'.$this->l('Transplant a module').'
*
*
'; echo << // EOF; // Manage exceptions if (!$exceptsDiff) { echo '
'; $this->displayModuleExceptionList($excepts, 0); echo $this->l('Please specify those files for which you do not want the module to be displayed').'.
'.$this->l('Please type each filename separated by a comma').'.

'; } else { echo '
'; foreach ($exceptsList as $shopID => $fileList) { $this->displayModuleExceptionList($fileList, $shopID); } echo $this->l('Please specify those files for which you do not want the module to be displayed').'.
'.$this->l('Please type each filename separated by a comma').'.

'; } echo '
'; if (Tools::isSubmit('editGraft')) { echo ' '; } echo '
* '.$this->l('Required field').'
'; } public function displayModuleExceptionList($fileList, $shopID) { if (!is_array($fileList)) $fileList = ($fileList) ? array($fileList) : array(); echo ''; if ($shopID) echo ' ('.Shop::getInstance($shopID)->name.')'; echo '


'; } }