This commit is contained in:
Jerome Nadaud
2013-09-11 20:41:14 +02:00
parent ab8d5bc265
commit e9dbadcabf
20 changed files with 587 additions and 285 deletions
+13 -1
View File
@@ -89,7 +89,18 @@ class HelperFormCore extends Helper
case 'categories':
if ($categories)
{
$this->context->smarty->assign('categories_tree', $params['categories_tree']);
$tree = new HelperTreeCategories($params['tree']['id'], isset($params['tree']['title']) ? $params['tree']['title'] : null);
if (isset($params['tree']['selected_categories']))
$tree->setSelectedCategories($params['tree']['selected_categories']);
if (isset($params['tree']['disabled_categories']))
$tree->setDisabledCategories($params['tree']['disabled_categories']);
if (isset($params['tree']['root_category']))
$tree->setRootCategory($params['tree']['root_category']);
$this->context->smarty->assign('categories_tree', $tree->render());
$categories = false;
}
break;
@@ -216,6 +227,7 @@ class HelperFormCore extends Helper
}
}
$tpl = $this->createTemplate('assoshop.tpl');
$tree = Shop::getTree();
$nb_shop = 0;
+265
View File
@@ -0,0 +1,265 @@
<?php
/*
* 2007-2013 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 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/osl-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 <contact@prestashop.com>
* @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 HelperTreeCategories extends TreeCore
{
const DEFAULT_TEMPLATE = 'tree_categories.tpl';
const DEFAULT_NODE_FOLDER_TEMPLATE = 'tree_node_folder_radio.tpl';
const DEFAULT_NODE_ITEM_TEMPLATE = 'tree_node_item_radio.tpl';
private $_disabled_categories;
private $_lang;
private $_root_category;
private $_selected_categories;
private $_shop;
private $_use_checkbox;
private $_use_search;
public function __construct($id, $title = null, $root_category = null,
$lang = null, $shop = null)
{
parent::__construct($id);
if (isset($title))
$this->setTitle($title);
if (isset($root_category))
$this->setRootCategory($root_category);
$this->setLang($lang);
$this->setShop($shop);
}
public function getData()
{
if (!isset($this->_data))
$this->setData(Category::getNestedCategories(
$this->getRootCategory(), $this->getLang()));
return $this->_data;
}
public function setDisabledCategories($value)
{
$this->_disabled_categories = $value;
return $this;
}
public function getDisabledCategories()
{
return $this->_disabled_categories;
}
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 setRootCategory($value)
{
if (!Validate::isInt($value))
throw new PrestaShopException('Root category must be an integer value');
$this->_root_category = $value;
return $this;
}
public function getRootCategory()
{
if (!isset($this->_root_category))
$this->setRootCategory(Category::getRootCategory($this->getLang())
->id);
return $this->_root_category;
}
public function setSelectedCategories($value)
{
if (!is_array($value))
throw new PrestaShopException('Selected categories value must be an array');
$this->_selected_categories = $value;
return $this;
}
public function getSelectedCatgories()
{
if (!isset($this->_selected_categories))
$this->_selected_categories = array();
return $this->_selected_categories;
}
public function setShop($value)
{
$this->_shop = $value;
return $this;
}
public function getShop()
{
if (!isset($this->_shop))
{
if (Tools::isSubmit('id_shop'))
$this->setShop(new Shop(Tools::getValue('id_shop')));
else
if ($this->getContext()->shop->id)
$this->setShop(new Shop($this->getContext()->shop->id));
else
if (!Shop::isFeatureActive())
$this->setShop(new Shop(Configuration::get('PS_SHOP_DEFAULT')));
else
$this->setShop(new Shop(0));
}
return $this->_shop;
}
public function getTemplate()
{
if (!isset($this->_template))
$this->setTemplate(self::DEFAULT_TEMPLATE);
return $this->_template;
}
public function setUseCheckBox($value)
{
$this->_use_checkbox = (bool)$value;
return $this;
}
public function setUseSearch($value)
{
$this->_use_search = (bool)$value;
return $this;
}
public function useCheckBox()
{
return (isset($this->_use_checkbox) && $this->_use_checkbox);
}
public function useSearch()
{
return (isset($this->_use_search) && $this->_use_search);
}
public function render($data = NULL)
{
if (!isset($data))
$data = $this->getData();
if (isset($this->_disabled_categories)
&& !empty($this->_disabled_categories))
$this->_disableCategories($data, $this->getDisabledCategories());
$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')
));
if ($this->useCheckBox())
{
$this->addAction(new TreeToolbarLink(
'Check All',
'#',
'checkAllAssociatedCategories($(\'#'.$this->getId().'\'));',
'icon-check-sign')
);
$this->addAction(new TreeToolbarLink(
'Uncheck All',
'#',
'uncheckAllAssociatedCategories($(\'#'.$this->getId().'\'));',
'icon-check-empty')
);
$this->setNodeFolderTemplate('tree_node_folder_checkbox.tpl');
$this->setNodeItemTemplate('tree_node_item_checkbox.tpl');
$this->setAttribute('use_checkbox', $this->useCheckBox());
}
if ($this->useSearch())
{
$this->addAction(new TreeToolbarSearch(
'Find a category:',
$this->getId().'-categories-search')
);
$this->setAttribute('use_search', $this->useSearch());
}
$this->setAttribute('selected_categories', $this->getSelectedCatgories());
return parent::render($data);
}
private function _disableCategories(&$categories, $disabled_categories = null)
{
foreach ($categories as &$category)
{
if (!isset($disabled_categories) || in_array($category['id_category'], $disabled_categories))
{
$category['disabled'] = true;
if (array_key_exists('children', $category) && is_array($category['children']))
self::_disableCategories($category['children']);
}
else if (array_key_exists('children', $category) && is_array($category['children']))
self::_disableCategories($category['children'], $disabled_categories);
}
}
}
@@ -24,7 +24,7 @@
* International Registered Trademark & Property of PrestaShop SA
*/
interface HelperITreeToolbarCore
interface ITreeToolbarCore
{
public function __toString();
public function setActions($value);
@@ -24,7 +24,7 @@
* International Registered Trademark & Property of PrestaShop SA
*/
interface HelperITreeToolbarButtonCore
interface ITreeToolbarButtonCore
{
public function __toString();
public function setAttribute($name, $value);
@@ -24,7 +24,7 @@
* International Registered Trademark & Property of PrestaShop SA
*/
class HelperTreeCore
class TreeCore
{
const DEFAULT_TEMPLATE_DIRECTORY = 'helpers/tree';
const DEFAULT_TEMPLATE = 'tree.tpl';
@@ -32,15 +32,16 @@ class HelperTreeCore
const DEFAULT_NODE_FOLDER_TEMPLATE = 'tree_node_folder.tpl';
const DEFAULT_NODE_ITEM_TEMPLATE = 'tree_node_item.tpl';
private $_context;
private $_data;
private $_headerTemplate;
private $_id;
private $_node_folder_template;
private $_node_item_template;
private $_template;
private $_template_directory;
private $_title;
protected $_attributes;
private $_context;
protected $_data;
protected $_headerTemplate;
private $_id;
protected $_node_folder_template;
protected $_node_item_template;
protected $_template;
private $_template_directory;
private $_title;
public function __construct($id, $data = null)
{
@@ -58,7 +59,7 @@ class HelperTreeCore
public function setActions($value)
{
if (!isset($this->_toolbar))
$this->setToolbar(new HelperTreeToolbarCore());
$this->setToolbar(new TreeToolbarCore());
$this->getToolbar()->setActions($value);
return $this;
@@ -67,7 +68,7 @@ class HelperTreeCore
public function getActions()
{
if (!isset($this->_toolbar))
$this->setToolbar(new HelperTreeToolbarCore());
$this->setToolbar(new TreeToolbarCore());
return $this->getToolbar()->getActions();
}
@@ -75,12 +76,43 @@ class HelperTreeCore
public function addAction($action)
{
if (!isset($this->_toolbar))
$this->setToolbar(new HelperTreeToolbarCore());
$this->setToolbar(new TreeToolbarCore());
$this->getToolbar()->addAction($action);
return $this;
}
public function setAttribute($name, $value)
{
if (!isset($this->_attributes))
$this->_attributes = array();
$this->_attributes[$name] = $value;
return $this;
}
public function getAttribute($name)
{
return $this->hasAttribute($name) ? $this->_attributes[$name] : null;
}
public function setAttributes($value)
{
if (!is_array($value) && !$value instanceof Traversable)
throw new PrestaShopException('Data value must be an traversable array');
$this->_attributes = $value;
return $this;
}
public function getAttributes()
{
if (!isset($this->_attributes))
$this->_attributes = array();
return $this->_attributes;
}
public function setContext($value)
{
$this->_context = $value;
@@ -250,7 +282,7 @@ class HelperTreeCore
$reflection = new ReflectionClass($value);
if (!$reflection->implementsInterface('HelperITreeToolbarCore'))
if (!$reflection->implementsInterface('ITreeToolbarCore'))
throw new PrestaShopException('Toolbar class must implements ITreeToolbarCore interface');
$this->_toolbar = $value;
@@ -315,7 +347,9 @@ class HelperTreeCore
}
//Assign Tree nodes
$template->assign(array(
$template
->assign($this->getAttributes())
->assign(array(
'id' => $this->getId(),
'nodes' => $this->renderNodes($data)
));
@@ -24,7 +24,7 @@
* International Registered Trademark & Property of PrestaShop SA
*/
class HelperTreeToolbarCore implements HelperITreeToolbarCore
class TreeToolbarCore implements ITreeToolbarCore
{
const DEFAULT_TEMPLATE_DIRECTORY = 'helpers/tree';
const DEFAULT_TEMPLATE = 'tree_toolbar.tpl';
@@ -159,7 +159,7 @@ class HelperTreeToolbarCore implements HelperITreeToolbarCore
$reflection = new ReflectionClass($action);
if (!$reflection->implementsInterface('HelperITreeToolbarButtonCore'))
if (!$reflection->implementsInterface('ITreeToolbarButtonCore'))
throw new PrestaShopException('Action class must implements ITreeToolbarButtonCore interface');
if (!isset($this->_actions))
@@ -173,7 +173,7 @@ class HelperTreeToolbarCore implements HelperITreeToolbarCore
{
foreach ($this->getActions() as $action)
{
if ($action instanceof HelperTreeToolbarSearchCore)
if ($action instanceof TreeToolbarSearchCore)
$action->setAttribute('data', $this->getData());
}
@@ -24,7 +24,7 @@
* International Registered Trademark & Property of PrestaShop SA
*/
abstract class HelperTreeToolbarButtonCore
abstract class TreeToolbarButtonCore
{
const DEFAULT_TEMPLATE_DIRECTORY = 'helpers/tree';
@@ -24,8 +24,8 @@
* International Registered Trademark & Property of PrestaShop SA
*/
class HelperTreeToolbarLinkCore extends HelperTreeToolbarButtonCore implements
HelperITreeToolbarButtonCore
class TreeToolbarLinkCore extends TreeToolbarButtonCore implements
ITreeToolbarButtonCore
{
private $_action;
private $_icon_class;
@@ -24,8 +24,8 @@
* International Registered Trademark & Property of PrestaShop SA
*/
class HelperTreeToolbarSearchCore extends HelperTreeToolbarButtonCore implements
HelperITreeToolbarButtonCore
class TreeToolbarSearchCore extends TreeToolbarButtonCore implements
ITreeToolbarButtonCore
{
protected $_template = 'tree_toolbar_search.tpl';