From 3ffa50d1ace7fd6f7e0e6cca24da2bf3c703e650 Mon Sep 17 00:00:00 2001 From: lLefevre Date: Tue, 1 Nov 2011 10:32:07 +0000 Subject: [PATCH] [*] BO : #PSFV-94 - added AdminTabsController git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@9765 b9a71923-0436-4b27-9f14-aed3839534dd --- .../themes/template/helper/form/form.tpl | 2 +- .../template/helper/list/list_footer.tpl | 2 - .../template/helper/list/list_header.tpl | 2 +- classes/AdminController.php | 10 +- classes/HelperList.php | 6 +- classes/Tab.php | 117 ++++++-- controllers/admin/AdminTabsController.php | 254 ++++++++++++++++++ 7 files changed, 353 insertions(+), 40 deletions(-) create mode 100644 controllers/admin/AdminTabsController.php diff --git a/admin-dev/themes/template/helper/form/form.tpl b/admin-dev/themes/template/helper/form/form.tpl index 7d35f5348..1ffb85a0a 100644 --- a/admin-dev/themes/template/helper/form/form.tpl +++ b/admin-dev/themes/template/helper/form/form.tpl @@ -248,7 +248,7 @@
{/foreach} {elseif $input.type == 'file'} - {if $input.display_image} + {if isset($input.display_image) && $input.display_image} {if isset($fields_value.image) && $fields_value.image}
{$fields_value.image} diff --git a/admin-dev/themes/template/helper/list/list_footer.tpl b/admin-dev/themes/template/helper/list/list_footer.tpl index db299698c..44e949266 100644 --- a/admin-dev/themes/template/helper/list/list_footer.tpl +++ b/admin-dev/themes/template/helper/list/list_footer.tpl @@ -35,10 +35,8 @@ -{if !$simple_header} -{/if} {* if (isset($this->_includeTab) AND sizeof($this->_includeTab)) echo '

'; diff --git a/admin-dev/themes/template/helper/list/list_header.tpl b/admin-dev/themes/template/helper/list/list_header.tpl index 601cf22aa..91ede11d9 100644 --- a/admin-dev/themes/template/helper/list/list_header.tpl +++ b/admin-dev/themes/template/helper/list/list_header.tpl @@ -72,8 +72,8 @@
  -
{/if}{* End if simple_header *} + {if !$simple_header} diff --git a/classes/AdminController.php b/classes/AdminController.php index b635300cd..bd89dad83 100644 --- a/classes/AdminController.php +++ b/classes/AdminController.php @@ -1154,9 +1154,9 @@ class AdminControllerCore extends Controller if (file_exists($this->context->smarty->template_dir[0].'/'.$this->tpl_folder.'list_header.tpl')) $helper->header_tpl = $this->tpl_folder.'list_header.tpl'; if (file_exists($this->context->smarty->template_dir[0].'/'.$this->tpl_folder.'list_content.tpl')) - $helper->header_tpl = $this->tpl_folder.'list_content.tpl'; + $helper->content_tpl = $this->tpl_folder.'list_content.tpl'; if (file_exists($this->context->smarty->template_dir[0].'/'.$this->tpl_folder.'list_footer.tpl')) - $helper->header_tpl = $this->tpl_folder.'list_footer.tpl'; + $helper->footer_tpl = $this->tpl_folder.'list_footer.tpl'; // For compatibility reasons, we have to check standard actions in class attributes foreach ($this->actions_available as $action) @@ -1538,10 +1538,10 @@ class AdminControllerCore extends Controller $use_limit = false; else if (empty($limit)) { - if (!isset($this->context->cookie->{$this->table.'_pagination'})) - $limit = $this->_pagination[1]; - else + if (isset($this->context->cookie->{$this->table.'_pagination'}) && $this->context->cookie->{$this->table.'_pagination'}) $limit = $this->context->cookie->{$this->table.'_pagination'}; + else + $limit = $this->_pagination[1]; } if (!Validate::isTableOrIdentifier($this->table)) diff --git a/classes/HelperList.php b/classes/HelperList.php index 714547ef3..18e34541e 100644 --- a/classes/HelperList.php +++ b/classes/HelperList.php @@ -96,7 +96,9 @@ class HelperListCore extends Helper 'id_cms_category' => 'id_cms_category_to_move', 'id_cms' => 'id_cms', 'id_attribute' => 'id_attribute', - 'id_carrier' => 'id_carrier' + 'id_attribute_group' => 'id_attribute_group', + 'id_carrier' => 'id_carrier', + 'id_tab' => 'id_tab' ); /** @var if not null, a title will be added on that list */ @@ -422,7 +424,7 @@ class HelperListCore extends Helper $token = $this->token; /* Determine total page number */ - if (isset($this->context->cookie->{$this->table.'_pagination'})) + if (isset($this->context->cookie->{$this->table.'_pagination'}) && $this->context->cookie->{$this->table.'_pagination'}) $default_pagination = $this->context->cookie->{$this->table.'_pagination'}; else $default_pagination = $this->_pagination[0]; diff --git a/classes/Tab.php b/classes/Tab.php index f73b7ec08..9c24b95d4 100644 --- a/classes/Tab.php +++ b/classes/Tab.php @@ -43,7 +43,11 @@ class TabCore extends ObjectModel protected $fieldsRequired = array('class_name', 'position'); protected $fieldsSize = array('class_name' => 64, 'module' => 64); - protected $fieldsValidate = array('id_parent' => 'isInt', 'position' => 'isUnsignedInt', 'module' => 'isTabName'); + protected $fieldsValidate = array( + 'id_parent' => 'isInt', + 'position' => 'isUnsignedInt', + 'module' => 'isTabName' + ); protected $fieldsRequiredLang = array('name'); protected $fieldsSizeLang = array('name' => 32); @@ -104,7 +108,11 @@ class TabCore extends ObjectModel if (!$context->employee->id_profile) return false; /* Profile selection */ - $profiles = Db::getInstance()->executeS('SELECT `id_profile` FROM '._DB_PREFIX_.'profile where `id_profile` != 1'); + $profiles = Db::getInstance()->executeS(' + SELECT `id_profile` + FROM '._DB_PREFIX_.'profile + WHERE `id_profile` != 1 + '); if (!$profiles || empty($profiles)) return false; /* Query definition */ @@ -147,7 +155,10 @@ class TabCore extends ObjectModel */ public static function getCurrentParentId() { - if ($result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('SELECT `id_parent` FROM `'._DB_PREFIX_.'tab` WHERE LOWER(class_name) = \''.pSQL(Tools::strtolower(Tools::getValue('controller'))).'\'')) + if ($result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' + SELECT `id_parent` + FROM `'._DB_PREFIX_.'tab` + WHERE LOWER(class_name) = \''.pSQL(Tools::strtolower(Tools::getValue('controller'))).'\'')) return $result['id_parent']; return -1; } @@ -161,10 +172,12 @@ class TabCore extends ObjectModel { /* Tabs selection */ return Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow(' - SELECT * - FROM `'._DB_PREFIX_.'tab` t - LEFT JOIN `'._DB_PREFIX_.'tab_lang` tl ON (t.`id_tab` = tl.`id_tab` AND tl.`id_lang` = '.(int)$id_lang.') - WHERE t.`id_tab` = '.(int)$id_tab); + SELECT * + FROM `'._DB_PREFIX_.'tab` t + LEFT JOIN `'._DB_PREFIX_.'tab_lang` tl + ON (t.`id_tab` = tl.`id_tab` AND tl.`id_lang` = '.(int)$id_lang.') + WHERE t.`id_tab` = '.(int)$id_tab + ); } /** @@ -179,10 +192,12 @@ class TabCore extends ObjectModel { self::$_cache_tabs[$id_lang] = array(); $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' - SELECT * - FROM `'._DB_PREFIX_.'tab` t - LEFT JOIN `'._DB_PREFIX_.'tab_lang` tl ON (t.`id_tab` = tl.`id_tab` AND tl.`id_lang` = '.(int)$id_lang.') - ORDER BY t.`position` ASC'); + SELECT * + FROM `'._DB_PREFIX_.'tab` t + LEFT JOIN `'._DB_PREFIX_.'tab_lang` tl + ON (t.`id_tab` = tl.`id_tab` AND tl.`id_lang` = '.(int)$id_lang.') + ORDER BY t.`position` ASC + '); foreach ($result as $row) { if (!isset(self::$_cache_tabs[$id_lang][$row['id_parent']])) @@ -221,9 +236,10 @@ class TabCore extends ObjectModel public static function getNbTabs($id_parent = null) { return (int)Db::getInstance()->getValue(' - SELECT COUNT(*) - FROM `'._DB_PREFIX_.'tab` t - '.(!is_null($id_parent) ? 'WHERE t.`id_parent` = '.(int)$id_parent : '')); + SELECT COUNT(*) + FROM `'._DB_PREFIX_.'tab` t + '.(!is_null($id_parent) ? 'WHERE t.`id_parent` = '.(int)$id_parent : '') + ); } /** @@ -234,7 +250,11 @@ class TabCore extends ObjectModel */ public static function getNewLastPosition($id_parent) { - return (Db::getInstance()->getValue('SELECT MAX(position)+1 FROM `'._DB_PREFIX_.'tab` WHERE `id_parent` = '.(int)($id_parent))); + return (Db::getInstance()->getValue(' + SELECT MAX(position)+1 + FROM `'._DB_PREFIX_.'tab` + WHERE `id_parent` = '.(int)$id_parent + )); } public function move($direction) @@ -250,9 +270,12 @@ class TabCore extends ObjectModel return false; $new_position = ($direction == 'l') ? $this->position - 1 : $this->position + 1; - Db::getInstance()->execute('UPDATE `'._DB_PREFIX_.'tab` t - SET position = '.(int)$this->position.' - WHERE id_parent = '.(int)$this->id_parent.' AND position = '.(int)$new_position); + Db::getInstance()->execute(' + UPDATE `'._DB_PREFIX_.'tab` t + SET position = '.(int)$this->position.' + WHERE id_parent = '.(int)$this->id_parent.' + AND position = '.(int)$new_position + ); $this->position = $new_position; return $this->update(); } @@ -260,28 +283,64 @@ class TabCore extends ObjectModel public function cleanPositions($id_parent) { $result = Db::getInstance()->executeS(' - SELECT `id_tab` - FROM `'._DB_PREFIX_.'tab` - WHERE `id_parent` = '.(int)$id_parent.' - ORDER BY `position`'); + SELECT `id_tab` + FROM `'._DB_PREFIX_.'tab` + WHERE `id_parent` = '.(int)$id_parent.' + ORDER BY `position` + '); $sizeof = count($result); for ($i = 0; $i < $sizeof; ++$i) Db::getInstance()->execute(' - UPDATE `'._DB_PREFIX_.'tab` - SET `position` = '.($i + 1).' - WHERE `id_tab` = '.(int)$result[$i]['id_tab']); + UPDATE `'._DB_PREFIX_.'tab` + SET `position` = '.($i + 1).' + WHERE `id_tab` = '.(int)$result[$i]['id_tab'] + ); return true; } + public function updatePosition($way, $position) + { + if (!$res = Db::getInstance()->executeS(' + SELECT t.`id_tab`, t.`position`, t.`id_parent` + FROM `'._DB_PREFIX_.'tab` t + WHERE t.`id_parent` = '.(int)$this->id_parent.' + ORDER BY t.`position` ASC' + )) + return false; + + foreach ($res as $tab) + if ((int)$tab['id_tab'] == (int)$this->id) + $moved_tab = $tab; + + if (!isset($moved_tab) || !isset($position)) + return false; + // < and > statements rather than BETWEEN operator + // since BETWEEN is treated differently according to databases + $result = (Db::getInstance()->execute(' + UPDATE `'._DB_PREFIX_.'tab` + SET `position`= `position` '.($way ? '- 1' : '+ 1').' + WHERE `position` + '.($way + ? '> '.(int)$moved_tab['position'].' AND `position` <= '.(int)$position + : '< '.(int)$moved_tab['position'].' AND `position` >= '.(int)$position).' + AND `id_parent`='.(int)$moved_tab['id_parent']) + && Db::getInstance()->execute(' + UPDATE `'._DB_PREFIX_.'tab` + SET `position` = '.(int)$position.' + WHERE `id_parent` = '.(int)$moved_tab['id_parent'].' + AND `id_tab`='.(int)$moved_tab['id_tab'])); + return $result; + } + public static function checkTabRights($id_tab) { - static $tabAccesses = NULL; + static $tabAccesses = null; - if ($tabAccesses === NULL) + if ($tabAccesses === null) $tabAccesses = Profile::getProfileAccesses(Context::getContext()->employee->id_profile); - if (isset($tabAccesses[(int)($id_tab)]['view'])) - return ($tabAccesses[(int)($id_tab)]['view'] === '1'); + if (isset($tabAccesses[(int)$id_tab]['view'])) + return ($tabAccesses[(int)$id_tab]['view'] === '1'); return false; } diff --git a/controllers/admin/AdminTabsController.php b/controllers/admin/AdminTabsController.php new file mode 100644 index 000000000..d155b3348 --- /dev/null +++ b/controllers/admin/AdminTabsController.php @@ -0,0 +1,254 @@ + +* @copyright 2007-2011 PrestaShop SA +* @version Release: $Revision: 7307 $ +* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*/ + +class AdminTabsControllerCore extends AdminController +{ + public function __construct() + { + $this->context = Context::getContext(); + $this->table = 'tab'; + $this->className = 'Tab'; + $this->lang = true; + + $this->fieldImageSettings = array( + 'name' => 'icon', + 'dir' => 't' + ); + + $this->imageType = 'gif'; + + $this->fieldsDisplay = array( + 'id_tab' => array( + 'title' => $this->l('ID'), + 'width' => 25 + ), + 'name' => array( + 'title' => $this->l('Name'), + 'width' => 200 + ), + 'logo' => array( + 'title' => $this->l('Icon'), + 'image' => 't', + 'image_id' => 'class_name', + 'orderby' => false, + 'search' => false + ), + 'module' => array( + 'title' => $this->l('Module') + ), + 'position' => array( + 'title' => $this->l('Position'), + 'width' => 40, + 'filter_key' => 'cp!position', + 'position' => 'position' + ) + ); + + parent::__construct(); + } + + /** + * AdminController::initForm() override + * @see AdminController::initForm() + */ + public function initForm() + { + $tabs = Tab::getTabs($this->context->language->id, 0); + + // added category "Home" in var $tabs + $tab_zero = array( + 'id_tab' => 0, + 'name' => $this->l('Home') + ); + array_unshift($tabs, $tab_zero); + + $this->fields_form = array( + 'legend' => array( + 'title' => $this->l('Tabs'), + 'image' => '../img/admin/tab.gif' + ), + 'input' => array( + array( + 'type' => 'hidden', + 'name' => 'position' + ), + array( + 'type' => 'text', + 'label' => $this->l('Name:'), + 'name' => 'name', + 'lang' => true, + 'attributeLang' => 'name', + 'size' => 33, + 'required' => true, + 'hint' => $this->l('Invalid characters:').' <>;=#{}' + ), + array( + 'type' => 'text', + 'label' => $this->l('Class:'), + 'name' => 'class_name', + 'required' => true + ), + array( + 'type' => 'text', + 'label' => $this->l('Module:'), + 'name' => 'module' + ), + array( + 'type' => 'file', + 'label' => $this->l('Icon:'), + 'name' => 'icon', + 'p' => $this->l('Upload logo from your computer').' (.gif, .jpg, .jpeg '.$this->l('or').' .png)' + ), + array( + 'type' => 'select', + 'label' => $this->l('Parent:'), + 'name' => 'id_parent', + 'options' => array( + 'query' => $tabs, + 'id' => 'id_tab', + 'name' => 'name', + 'default' => array( + 'value' => -1, + 'label' => $this->l('None') + ) + ) + ) + ), + 'submit' => array( + 'title' => $this->l(' Save '), + 'class' => 'button' + ) + ); + + return parent::initForm(); + } + + /** + * AdminController::initList() override + * @see AdminController::initList() + */ + public function initList() + { + $this->addRowAction('edit'); + $this->addRowAction('delete'); + $this->addRowAction('details'); + + $this->_where = 'AND a.`id_parent` = 0'; + $this->_orderBy = 'position'; + + return parent::initList(); + } + + /** + * method call when ajax request is made with the details row action + * @see AdminController::postProcess() + */ + public function ajaxProcess() + { + // test if an id is submit + if (($id = Tools::getValue('id')) && Tools::isSubmit('id')) + { + // override attributes + $this->display = 'list'; + $this->lang = false; + + $this->addRowAction('edit'); + $this->addRowAction('delete'); + + $this->_select = 'b.*'; + $this->_join = 'LEFT JOIN `'._DB_PREFIX_.'tab_lang` b ON (b.`id_tab` = a.`id_tab` AND b.`id_lang` = '.$this->context->language->id.')'; + $this->_where = 'AND a.`id_parent` = '.(int)$id; + $this->_orderBy = 'position'; + + // get list and force no limit clause in the request + $this->getList($this->context->language->id); + + // Render list + $helper = new HelperList(); + $helper->actions = $this->actions; + $helper->list_skip_actions = $this->list_skip_actions; + $helper->no_link = true; + $helper->shopLinkType = ''; + $helper->identifier = $this->identifier; + $helper->imageType = $this->imageType; + $helper->orderBy = 'position'; + $helper->orderWay = 'ASC'; + $helper->currentIndex = self::$currentIndex; + $helper->token = $this->token; + $helper->table = $this->table; + // Force render - no filter, form, js, sorting ... + $helper->simple_header = true; + $content = $helper->generateList($this->_list, $this->fieldsDisplay); + + echo Tools::jsonEncode(array('use_parent_structure' => false, 'data' => $content)); + } + + die; + } + + public function postProcess() + { + /* PrestaShop demo mode */ + if (_PS_MODE_DEMO_) + { + $this->_errors[] = Tools::displayError('This functionnality has been disabled.'); + return; + } + /* PrestaShop demo mode*/ + + if (($id_tab = (int)(Tools::getValue('id_tab'))) AND ($direction = Tools::getValue('move')) AND Validate::isLoadedObject($tab = new Tab($id_tab))) + { + if ($tab->move($direction)) + Tools::redirectAdmin(self::$currentIndex.'&token='.$this->token); + } + else if (Tools::getValue('position') && !Tools::isSubmit('submitAdd'.$this->table)) + { + if ($this->tabAccess['edit'] !== '1') + $this->_errors[] = Tools::displayError('You do not have permission to edit here.'); + else if (!Validate::isLoadedObject($object = new Tab((int)Tools::getValue($this->identifier)))) + $this->_errors[] = Tools::displayError('An error occurred while updating status for object.').' '.$this->table.' '.Tools::displayError('(cannot load object)'); + if (!$object->updatePosition((int)Tools::getValue('way'), (int)Tools::getValue('position'))) + $this->_errors[] = Tools::displayError('Failed to update the position.'); + else + Tools::redirectAdmin(self::$currentIndex.'&conf=5&token='.Tools::getAdminTokenLite('AdminTabs')); + } + else + parent::postProcess(); + } + + public function getList($id_lang, $order_by = null, $order_way = null, $start = 0, $limit = null, $id_lang_shop = false) + { + parent::getList($id_lang, 'position', $order_way, $start, $limit, $id_lang_shop); + } + + public function afterImageUpload() + { + if (!($obj = $this->loadObject(true))) + return; + @rename(_PS_IMG_DIR_.'t/'.$obj->id.'.gif', _PS_IMG_DIR_.'t/'.$obj->class_name.'.gif'); + } +} \ No newline at end of file