From e63ce0a86d7f37be808f976c2824d99fc71a7965 Mon Sep 17 00:00:00 2001 From: Jerome Nadaud Date: Thu, 12 Sep 2013 14:47:47 +0200 Subject: [PATCH] // Tree --- .../tree/tree_node_folder_checkbox.tpl | 34 ++++ .../helpers/tree/tree_node_item_checkbox.tpl | 31 ++++ .../template/helpers/tree/tree_shops.tpl | 111 +++++++++++ .../template/helpers/tree/tree_toolbar.tpl | 2 +- classes/helper/HelperForm.php | 25 +-- classes/helper/HelperTreeCategories.php | 4 +- classes/helper/HelperTreeShops.php | 174 ++++++++++++++++++ classes/tree/Tree.php | 30 +-- classes/tree/TreeToolbar.php | 5 +- classes/tree/TreeToolbarSearch.php | 2 +- classes/tree/TreeToolbarSearchCategories.php | 83 +++++++++ .../admin/AdminCategoriesController.php | 4 + 12 files changed, 464 insertions(+), 41 deletions(-) create mode 100644 admin-dev/themes/default/template/controllers/categories/helpers/tree/tree_node_folder_checkbox.tpl create mode 100644 admin-dev/themes/default/template/controllers/categories/helpers/tree/tree_node_item_checkbox.tpl create mode 100644 admin-dev/themes/default/template/helpers/tree/tree_shops.tpl create mode 100644 classes/helper/HelperTreeShops.php create mode 100644 classes/tree/TreeToolbarSearchCategories.php diff --git a/admin-dev/themes/default/template/controllers/categories/helpers/tree/tree_node_folder_checkbox.tpl b/admin-dev/themes/default/template/controllers/categories/helpers/tree/tree_node_folder_checkbox.tpl new file mode 100644 index 000000000..737a619dd --- /dev/null +++ b/admin-dev/themes/default/template/controllers/categories/helpers/tree/tree_node_folder_checkbox.tpl @@ -0,0 +1,34 @@ +{* +* 2007-2013 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-2013 PrestaShop SA +* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*} +
  • + + + + + +
      + {$children} +
    +
  • \ No newline at end of file diff --git a/admin-dev/themes/default/template/controllers/categories/helpers/tree/tree_node_item_checkbox.tpl b/admin-dev/themes/default/template/controllers/categories/helpers/tree/tree_node_item_checkbox.tpl new file mode 100644 index 000000000..a9130e2a4 --- /dev/null +++ b/admin-dev/themes/default/template/controllers/categories/helpers/tree/tree_node_item_checkbox.tpl @@ -0,0 +1,31 @@ +{* +* 2007-2013 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-2013 PrestaShop SA +* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*} +
  • + +
  • \ No newline at end of file diff --git a/admin-dev/themes/default/template/helpers/tree/tree_shops.tpl b/admin-dev/themes/default/template/helpers/tree/tree_shops.tpl new file mode 100644 index 000000000..dd391f8c9 --- /dev/null +++ b/admin-dev/themes/default/template/helpers/tree/tree_shops.tpl @@ -0,0 +1,111 @@ +{* +* 2007-2013 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-2013 PrestaShop SA +* @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*} +
    + {if isset($header)}{$header}{/if} + {if isset($nodes)} +
      + {$nodes} +
    + {/if} +
    + \ No newline at end of file diff --git a/admin-dev/themes/default/template/helpers/tree/tree_toolbar.tpl b/admin-dev/themes/default/template/helpers/tree/tree_toolbar.tpl index d036a7932..ad71c1d7d 100644 --- a/admin-dev/themes/default/template/helpers/tree/tree_toolbar.tpl +++ b/admin-dev/themes/default/template/helpers/tree/tree_toolbar.tpl @@ -22,4 +22,4 @@ * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA *} -
    {if isset($actions)}{foreach from=$actions item=action}{$action}{/foreach}{/if}
    \ No newline at end of file +
    {if isset($actions)}{foreach from=$actions item=action}{$action->render()}{/foreach}{/if}
    \ No newline at end of file diff --git a/classes/helper/HelperForm.php b/classes/helper/HelperForm.php index 99532925a..11f51cac4 100644 --- a/classes/helper/HelperForm.php +++ b/classes/helper/HelperForm.php @@ -139,7 +139,7 @@ class HelperFormCore extends Helper case 'shop' : $disable_shops = isset($params['disable_shared']) ? $params['disable_shared'] : false; - $params['html'] = $this->renderAssoShop($disable_shops); + $params['html'] = $this->renderAssoShop($params['tree'], $disable_shops); if (Shop::getTotalShops(false) == 1) unset($this->fields_form[$fieldset_key]['form']['input'][$key]); break; @@ -192,7 +192,7 @@ class HelperFormCore extends Helper * * @return string */ - public function renderAssoShop($disable_shared = false) + public function renderAssoShop($params, $disable_shared = false) { if (!Shop::isFeatureActive()) return; @@ -227,27 +227,16 @@ class HelperFormCore extends Helper } } - - $tpl = $this->createTemplate('assoshop.tpl'); - $tree = Shop::getTree(); $nb_shop = 0; foreach ($tree as &$value) { $value['disable_shops'] = (isset($value[$disable_shared]) && $value[$disable_shared]); $nb_shop += count($value['shops']); } - $tpl->assign(array( - 'input' => array( - 'type' => 'shop', - 'values' => $tree, - ), - 'fields_value' => array( - 'shop' => $assos - ), - 'form_id' => $this->id, - 'table' => $this->table, - 'nb_shop' => $nb_shop - )); - return $tpl->fetch(); + + $tree = new HelperTreeShops($params['id'], + isset($params['title']) ? $params['title'] : null); + $tree->setSelectedShops($assos); + return $tree->render(); } } diff --git a/classes/helper/HelperTreeCategories.php b/classes/helper/HelperTreeCategories.php index f08cafa02..b919ee3ca 100644 --- a/classes/helper/HelperTreeCategories.php +++ b/classes/helper/HelperTreeCategories.php @@ -24,7 +24,7 @@ * International Registered Trademark & Property of PrestaShop SA */ -class HelperTreeCategories extends TreeCore +class HelperTreeCategoriesCore extends TreeCore { const DEFAULT_TEMPLATE = 'tree_categories.tpl'; const DEFAULT_NODE_FOLDER_TEMPLATE = 'tree_node_folder_radio.tpl'; @@ -237,7 +237,7 @@ class HelperTreeCategories extends TreeCore if ($this->useSearch()) { - $this->addAction(new TreeToolbarSearch( + $this->addAction(new TreeToolbarSearchCategories( 'Find a category:', $this->getId().'-categories-search') ); diff --git a/classes/helper/HelperTreeShops.php b/classes/helper/HelperTreeShops.php new file mode 100644 index 000000000..9c1b0b29a --- /dev/null +++ b/classes/helper/HelperTreeShops.php @@ -0,0 +1,174 @@ + +* @copyright 2007-2013 PrestaShop SA +* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*/ + +class HelperTreeShopsCore extends TreeCore +{ + const DEFAULT_TEMPLATE = 'tree_shops.tpl'; + const DEFAULT_NODE_FOLDER_TEMPLATE = 'tree_node_folder_checkbox.tpl'; + const DEFAULT_NODE_ITEM_TEMPLATE = 'tree_node_item_checkbox.tpl'; + + private $_lang; + private $_selected_shops; + + public function __construct($id, $title = null, $lang = null) + { + parent::__construct($id); + + if (isset($title)) + $this->setTitle($title); + + $this->setLang($lang); + } + + public function getData() + { + if (!isset($this->_data)) + $this->setData(Shop::getTree()); + + return $this->_data; + } + + public function setLang($value) + { + $this->_lang = $value; + return $this; + } + + public function getLang() + { + if (!isset($this->_lang)) + $this->setLang($this->getContext()->employee->id_lang); + + return $this->_lang; + } + + public function getNodeFolderTemplate() + { + if (!isset($this->_node_folder_template)) + $this->setNodeFolderTemplate(self::DEFAULT_NODE_FOLDER_TEMPLATE); + + return $this->_node_folder_template; + } + + public function getNodeItemTemplate() + { + if (!isset($this->_node_item_template)) + $this->setNodeItemTemplate(self::DEFAULT_NODE_ITEM_TEMPLATE); + + return $this->_node_item_template; + } + + public function setSelectedShops($value) + { + if (!is_array($value)) + throw new PrestaShopException('Selected shops value must be an array'); + + $this->_selected_shops = $value; + return $this; + } + + public function getSelectedShops() + { + if (!isset($this->_selected_shops)) + $this->_selected_shops = array(); + + return $this->_selected_shops; + } + + public function getTemplate() + { + if (!isset($this->_template)) + $this->setTemplate(self::DEFAULT_TEMPLATE); + + return $this->_template; + } + + public function render($data = NULL) + { + if (!isset($data)) + $data = $this->getData(); + + $this->setActions(array( + new TreeToolbarLink( + 'Collapse All', + '#', + '$(\'#'.$this->getId().'\').tree(\'collapseAll\')', + 'icon-collapse-alt'), + new TreeToolbarLink( + 'Expand All', + '#', + '$(\'#'.$this->getId().'\').tree(\'expandAll\')', + 'icon-expand-alt'), + new TreeToolbarLink( + 'Check All', + '#', + 'checkAllAssociatedShops($(\'#'.$this->getId().'\'));', + 'icon-check-sign'), + new TreeToolbarLink( + 'Uncheck All', + '#', + 'uncheckAllAssociatedShops($(\'#'.$this->getId().'\'));', + 'icon-check-empty') + ) + ); + + $this->setAttribute('selected_shops', $this->getSelectedShops()); + return parent::render($data); + } + + public function renderNodes($data = null) + { + if (!isset($data)) + $data = $this->getData(); + + if (!is_array($data) && !$data instanceof Traversable) + throw new PrestaShopException('Data value must be an traversable array'); + + $html = ''; + + foreach ($data as $item) + { + if (array_key_exists('shops', $item) + && !empty($item['shops'])) + $html .= $this->getContext()->smarty->createTemplate( + $this->getTemplateFile($this->getNodeFolderTemplate()), + $this->getContext()->smarty + )->assign(array( + 'children' => $this->renderNodes($item['shops']), + 'node' => $item + ))->fetch(); + else + $html .= $this->getContext()->smarty->createTemplate( + $this->getTemplateFile($this->getNodeItemTemplate()), + $this->getContext()->smarty + )->assign(array( + 'node' => $item + ))->fetch(); + } + + return $html; + } +} \ No newline at end of file diff --git a/classes/tree/Tree.php b/classes/tree/Tree.php index b1eebae2e..a4bc77f45 100644 --- a/classes/tree/Tree.php +++ b/classes/tree/Tree.php @@ -73,15 +73,6 @@ class TreeCore return $this->getToolbar()->getActions(); } - public function addAction($action) - { - if (!isset($this->_toolbar)) - $this->setToolbar(new TreeToolbarCore()); - - $this->getToolbar()->addAction($action); - return $this; - } - public function setAttribute($name, $value) { if (!isset($this->_attributes)) @@ -297,14 +288,13 @@ class TreeCore return $this->_toolbar; } - public function useInput() + public function addAction($action) { - return isset($this->_input_type); - } + if (!isset($this->_toolbar)) + $this->setToolbar(new TreeToolbarCore()); - public function useToolbar() - { - return isset($this->_toolbar); + $this->getToolbar()->addAction($action); + return $this; } public function render($data = null) @@ -395,6 +385,16 @@ class TreeCore return $this->getToolbar()->render(); } + public function useInput() + { + return isset($this->_input_type); + } + + public function useToolbar() + { + return isset($this->_toolbar); + } + private function _normalizeDirectory($directory) { $last = $directory[strlen($directory) - 1]; diff --git a/classes/tree/TreeToolbar.php b/classes/tree/TreeToolbar.php index 09f09d0a0..195333560 100644 --- a/classes/tree/TreeToolbar.php +++ b/classes/tree/TreeToolbar.php @@ -172,10 +172,7 @@ class TreeToolbarCore implements ITreeToolbarCore public function render() { foreach ($this->getActions() as $action) - { - if ($action instanceof TreeToolbarSearchCore) - $action->setAttribute('data', $this->getData()); - } + $action->setAttribute('data', $this->getData()); return $this->getContext()->smarty->createTemplate( $this->getTemplateFile($this->getTemplate()), diff --git a/classes/tree/TreeToolbarSearch.php b/classes/tree/TreeToolbarSearch.php index 9f39f74ea..699a344b5 100644 --- a/classes/tree/TreeToolbarSearch.php +++ b/classes/tree/TreeToolbarSearch.php @@ -66,7 +66,7 @@ class TreeToolbarSearchCore extends TreeToolbarButtonCore implements private function _renderData($data) { if (!is_array($data) && !$data instanceof Traversable) - throw new PrestaShopException('Data value must be an traversable array'); + throw new PrestaShopException('Data value must be a traversable array'); $html = ''; diff --git a/classes/tree/TreeToolbarSearchCategories.php b/classes/tree/TreeToolbarSearchCategories.php new file mode 100644 index 000000000..e4311e8ad --- /dev/null +++ b/classes/tree/TreeToolbarSearchCategories.php @@ -0,0 +1,83 @@ + +* @copyright 2007-2013 PrestaShop SA +* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*/ + +class TreeToolbarSearchCategoriesCore extends TreeToolbarButtonCore implements + ITreeToolbarButtonCore +{ + protected $_template = 'tree_toolbar_search.tpl'; + + public function __construct($label, $id, $name = null, $class = null) + { + parent::__construct($label); + + $this->setId($id); + $this->setName($name); + $this->setClass($class); + } + + public function render() + { + if ($this->hasAttribute('data')) + $this->setAttribute('typeahead_source', + $this->_renderData($this->getAttribute('data'))); + + $admin_webpath = str_ireplace(_PS_ROOT_DIR_, '', _PS_ADMIN_DIR_); + $admin_webpath = preg_replace('/^'.preg_quote(DIRECTORY_SEPARATOR, '/').'/', '', $admin_webpath); + $bo_theme = ((Validate::isLoadedObject($this->getContext()->employee) + && $this->getContext()->employee->bo_theme) ? $this->getContext()->employee->bo_theme : 'default'); + + if (!file_exists(_PS_BO_ALL_THEMES_DIR_.$bo_theme.DIRECTORY_SEPARATOR + .'template')) + $bo_theme = 'default'; + + if ($this->getContext()->controller->ajax) + $html = ''; + else + $this->getContext()->controller->addJs(__PS_BASE_URI__.$admin_webpath + .'/themes/'.$bo_theme.'/js/vendor/typeahead.min.js'); + + return (isset($html)?$html:'').parent::render(); + } + + private function _renderData($data) + { + if (!is_array($data) && !$data instanceof Traversable) + throw new PrestaShopException('Data value must be a traversable array'); + + $html = ''; + + foreach ($data as $item) + { + $html .= '{id_category : '.$item['id_category'].', name : "'.$item['name'].'"},'; + + if (array_key_exists('children', $item) && !empty($item['children'])) + $html .= $this->_renderData($item['children']); + } + + return $html; + } +} \ No newline at end of file diff --git a/controllers/admin/AdminCategoriesController.php b/controllers/admin/AdminCategoriesController.php index 9919930b9..2314f46dc 100644 --- a/controllers/admin/AdminCategoriesController.php +++ b/controllers/admin/AdminCategoriesController.php @@ -484,6 +484,10 @@ class AdminCategoriesControllerCore extends AdminController if (Shop::isFeatureActive()) $this->fields_form['input'][] = array( 'type' => 'shop', + 'tree' => array( + 'id' => 'shops-tree', + 'title' => 'Shop' + ), 'label' => $this->l('Shop association:'), 'name' => 'checkBoxShopAsso', );