// Categories Tree changes

This commit is contained in:
Jerome Nadaud
2013-09-10 18:40:05 +02:00
parent 1aba28ff6f
commit e0d33eae9e
14 changed files with 128 additions and 216 deletions
@@ -26,7 +26,7 @@
<span class="tree-folder-name"> <span class="tree-folder-name">
<input type="radio" name="id-category" value="{$node['id_category']}" /> <input type="radio" name="id-category" value="{$node['id_category']}" />
<i class="icon-folder-close"></i> <i class="icon-folder-close"></i>
<label class="tree-toggler ">{$name}</label> <label class="tree-toggler ">{$node['name']}</label>
</span> </span>
<ul class="tree"> <ul class="tree">
{$children} {$children}
@@ -26,7 +26,7 @@
<span class="tree-folder-name"> <span class="tree-folder-name">
<input type="checkbox" name="categoryBox[]" value="{$node['id_category']}" /> <input type="checkbox" name="categoryBox[]" value="{$node['id_category']}" />
<i class="icon-folder-close"></i> <i class="icon-folder-close"></i>
<label class="tree-toggler">{$name}</label> <label class="tree-toggler">{$node['name']}</label>
</span> </span>
<ul class="tree"> <ul class="tree">
{$children} {$children}
@@ -26,6 +26,6 @@
<label class="tree-item-name"> <label class="tree-item-name">
<input type="radio" name="id-category" value="{$node['id_category']}" /> <input type="radio" name="id-category" value="{$node['id_category']}" />
<i class="tree-dot"></i> <i class="tree-dot"></i>
{$name} {$node['name']}
<label> <label>
</li> </li>
@@ -26,6 +26,6 @@
<label class="tree-item-name"> <label class="tree-item-name">
<input type="checkbox" name="categoryBox[]" value="{$node['id_category']}" /> <input type="checkbox" name="categoryBox[]" value="{$node['id_category']}" />
<i class="tree-dot"></i> <i class="tree-dot"></i>
{$name} {$node['name']}
</label> </label>
</li> </li>
@@ -1,33 +0,0 @@
{*
* 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 <contact@prestashop.com>
* @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
*}
<div class="panel">
{if isset($header)}{$header}{/if}
{if isset($nodes)}
<ul id="{$id}" class="tree">
{$nodes}
</ul>
{/if}
</div>
@@ -1,28 +0,0 @@
{*
* 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 <contact@prestashop.com>
* @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
*}
<div class="panel-heading">
{l s=$title}
{if isset($toolbar)}{$toolbar}{/if}
</div>
@@ -26,7 +26,7 @@
<li class="tree-folder"> <li class="tree-folder">
<span class="tree-folder-name"> <span class="tree-folder-name">
<i class="icon-folder-close"></i> <i class="icon-folder-close"></i>
<label class="tree-toggler ">{$name}</label> <label class="tree-toggler ">{$node['name']}</label>
</span> </span>
<ul class="tree"> <ul class="tree">
{$children} {$children}
@@ -26,6 +26,6 @@
<li class="tree-item"> <li class="tree-item">
<label class="tree-item-name"> <label class="tree-item-name">
<i class="tree-dot"></i> <i class="tree-dot"></i>
{$name} {$node['name']}
<label> <label>
</li> </li>
@@ -1,25 +0,0 @@
{*
* 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 <contact@prestashop.com>
* @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
*}
<div class="pull-right">{if isset($actions)}{foreach from=$actions item=action}{$action}{/foreach}{/if}</div>
+3 -3
View File
@@ -477,11 +477,11 @@ class CategoryCore extends ObjectModel
return $categories; return $categories;
} }
public static function getNestedCategories($parent_category = 0, public static function getNestedCategories($root_category = 1,
$id_lang = false, $active = true, $sql_filter = '', $sql_sort = '', $id_lang = false, $active = true, $sql_filter = '', $sql_sort = '',
$sql_limit = '') $sql_limit = '')
{ {
if (!Validate::isInt($parent_category)) if (!Validate::isInt($root_category))
die(Tools::displayError()); die(Tools::displayError());
if (!Validate::isBool($active)) if (!Validate::isBool($active))
@@ -506,7 +506,7 @@ class CategoryCore extends ObjectModel
$current = &$buff[$row['id_category']]; $current = &$buff[$row['id_category']];
$current = $row; $current = $row;
if ($row['id_parent'] == $parent_category) if ($row['id_category'] == $root_category)
$categories[$row['id_category']] = &$current; $categories[$row['id_category']] = &$current;
else else
$buff[$row['id_parent']]['children'][$row['id_category']] = &$current; $buff[$row['id_parent']]['children'][$row['id_category']] = &$current;
+47 -58
View File
@@ -32,15 +32,13 @@ class HelperTreeCore
const DEFAULT_NODE_FOLDER_TEMPLATE = 'tree_node_folder.tpl'; const DEFAULT_NODE_FOLDER_TEMPLATE = 'tree_node_folder.tpl';
const DEFAULT_NODE_ITEM_TEMPLATE = 'tree_node_item.tpl'; const DEFAULT_NODE_ITEM_TEMPLATE = 'tree_node_item.tpl';
private $_children_key;
private $_context; private $_context;
private $_data; private $_data;
private $_headerTemplate; private $_headerTemplate;
private $_id; private $_id;
private $_id_key;
private $_name_key;
private $_node_folder_template; private $_node_folder_template;
private $_node_item_template; private $_node_item_template;
private $_template;
private $_template_directory; private $_template_directory;
private $_title; private $_title;
@@ -83,20 +81,6 @@ class HelperTreeCore
return $this; return $this;
} }
public function setChildrenKey($value)
{
$this->_children_key = (string)$value;
return $this;
}
public function getChildrenKey()
{
if (!isset($this->_children_key))
$this->setChildrenKey('children');
return $this->_children_key;
}
public function setContext($value) public function setContext($value)
{ {
$this->_context = $value; $this->_context = $value;
@@ -153,34 +137,6 @@ class HelperTreeCore
return $this->_id; return $this->_id;
} }
public function setIdKey($value)
{
$this->_id_key = (string)$value;
return $this;
}
public function getIdKey()
{
if (!isset($this->_id_key))
$this->setIdKey('id');
return $this->_id_key;
}
public function setNameKey($value)
{
$this->_name_key = (string)$value;
return $this;
}
public function getNameKey()
{
if (!isset($this->_name_key))
$this->setNameKey('name');
return $this->_name_key;
}
public function setNodeFolderTemplate($value) public function setNodeFolderTemplate($value)
{ {
$this->_node_folder_template = $value; $this->_node_folder_template = $value;
@@ -232,15 +188,50 @@ class HelperTreeCore
public function getTemplateDirectory() public function getTemplateDirectory()
{ {
if (!isset($this->_template_directory)) if (!isset($this->_template_directory))
{
$this->_template_directory = $this->_normalizeDirectory( $this->_template_directory = $this->_normalizeDirectory(
$this->getContext()->smarty->getTemplateDir(0) self::DEFAULT_TEMPLATE_DIRECTORY);
.self::DEFAULT_TEMPLATE_DIRECTORY);
}
return $this->_template_directory; return $this->_template_directory;
} }
public function getTemplateFile($template)
{
if (preg_match_all('/((?:^|[A-Z])[a-z]+)/', get_class($this->getContext()->controller), $matches) !== FALSE)
$controllerName = strtolower($matches[0][1]);
if ($this->getContext()->controller instanceof ModuleAdminController)
return $this->_normalizeDirectory(
$this->getContext()->controller->getTemplatePath())
.$this->getTemplateDirectory().$template;
else if ($this->getContext()->controller instanceof AdminController
&& isset($controllerName) && file_exists($this->_normalizeDirectory(
$this->getContext()->smarty->getTemplateDir(0)).'controllers'
.DIRECTORY_SEPARATOR
.$controllerName
.DIRECTORY_SEPARATOR
.$this->getTemplateDirectory().$template))
return $this->_normalizeDirectory(
$this->getContext()->smarty->getTemplateDir(0)).'controllers'
.DIRECTORY_SEPARATOR
.$controllerName
.DIRECTORY_SEPARATOR
.$this->getTemplateDirectory().$template;
else if (file_exists($this->_normalizeDirectory(
$this->getContext()->smarty->getTemplateDir(1))
.$this->getTemplateDirectory().$template))
return $this->_normalizeDirectory(
$this->getContext()->smarty->getTemplateDir(1))
.$this->getTemplateDirectory().$template;
else if (file_exists($this->_normalizeDirectory(
$this->getContext()->smarty->getTemplateDir(0))
.$this->getTemplateDirectory().$template))
return $this->_normalizeDirectory(
$this->getContext()->smarty->getTemplateDir(0))
.$this->getTemplateDirectory().$template;
else
return $this->getTemplateDirectory().$template;
}
public function setTitle($value) public function setTitle($value)
{ {
$this->_title = $value; $this->_title = $value;
@@ -305,7 +296,7 @@ class HelperTreeCore
//Create Tree Template //Create Tree Template
$template = $this->getContext()->smarty->createTemplate( $template = $this->getContext()->smarty->createTemplate(
$this->getTemplateDirectory().$this->getTemplate(), $this->getTemplateFile($this->getTemplate()),
$this->getContext()->smarty $this->getContext()->smarty
); );
@@ -313,7 +304,7 @@ class HelperTreeCore
{ {
//Create Tree Header Template //Create Tree Header Template
$headerTemplate = $this->getContext()->smarty->createTemplate( $headerTemplate = $this->getContext()->smarty->createTemplate(
$this->getTemplateDirectory().$this->getHeaderTemplate(), $this->getTemplateFile($this->getHeaderTemplate()),
$this->getContext()->smarty $this->getContext()->smarty
); );
$headerTemplate->assign(array( $headerTemplate->assign(array(
@@ -344,22 +335,20 @@ class HelperTreeCore
foreach ($data as $item) foreach ($data as $item)
{ {
if (array_key_exists($this->getChildrenKey(), $item) if (array_key_exists('children', $item)
&& !empty($item[$this->getChildrenKey()])) && !empty($item['children']))
$html .= $this->getContext()->smarty->createTemplate( $html .= $this->getContext()->smarty->createTemplate(
$this->getTemplateDirectory().$this->getNodeFolderTemplate(), $this->getTemplateFile($this->getNodeFolderTemplate()),
$this->getContext()->smarty $this->getContext()->smarty
)->assign(array( )->assign(array(
'name' => $item[$this->getNameKey()], 'children' => $this->renderNodes($item['children']),
'children' => $this->renderNodes($item[$this->getChildrenKey()]),
'node' => $item 'node' => $item
))->fetch(); ))->fetch();
else else
$html .= $this->getContext()->smarty->createTemplate( $html .= $this->getContext()->smarty->createTemplate(
$this->getTemplateDirectory().$this->getNodeItemTemplate(), $this->getTemplateFile($this->getNodeItemTemplate()),
$this->getContext()->smarty $this->getContext()->smarty
)->assign(array( )->assign(array(
'name' => $item[$this->getNameKey()],
'node' => $item 'node' => $item
))->fetch(); ))->fetch();
} }
+42 -4
View File
@@ -108,12 +108,50 @@ class HelperTreeToolbarCore implements HelperITreeToolbarCore
{ {
if (!isset($this->_template_directory)) if (!isset($this->_template_directory))
$this->_template_directory = $this->_normalizeDirectory( $this->_template_directory = $this->_normalizeDirectory(
$this->getContext()->smarty->getTemplateDir(0) self::DEFAULT_TEMPLATE_DIRECTORY);
.self::DEFAULT_TEMPLATE_DIRECTORY);
return $this->_template_directory; return $this->_template_directory;
} }
public function getTemplateFile($template)
{
if (preg_match_all('/((?:^|[A-Z])[a-z]+)/', get_class($this->getContext()->controller), $matches) !== FALSE)
$controllerName = strtolower($matches[0][1]);
if ($this->getContext()->controller instanceof ModuleAdminController)
return $this->_normalizeDirectory(
$this->getContext()->controller->getTemplatePath())
.$this->getTemplateDirectory().$template;
else if ($this->getContext()->controller instanceof AdminController
&& isset($controllerName) && file_exists($this->_normalizeDirectory(
$this->getContext()->smarty->getTemplateDir(0)).'controllers'
.DIRECTORY_SEPARATOR
.$controllerName
.DIRECTORY_SEPARATOR
.$this->getTemplateDirectory().$template))
return $this->_normalizeDirectory(
$this->getContext()->smarty->getTemplateDir(0)).'controllers'
.DIRECTORY_SEPARATOR
.$controllerName
.DIRECTORY_SEPARATOR
.$this->getTemplateDirectory().$template;
else if (file_exists($this->_normalizeDirectory(
$this->getContext()->smarty->getTemplateDir(1))
.$this->getTemplateDirectory().$template))
return $this->_normalizeDirectory(
$this->getContext()->smarty->getTemplateDir(1))
.$this->getTemplateDirectory().$template;
else if (file_exists($this->_normalizeDirectory(
$this->getContext()->smarty->getTemplateDir(0))
.$this->getTemplateDirectory().$template))
return $this->_normalizeDirectory(
$this->getContext()->smarty->getTemplateDir(0))
.$this->getTemplateDirectory().$template;
else
return $this->getTemplateDirectory().$template;
}
public function addAction($action) public function addAction($action)
{ {
if (!is_object($action)) if (!is_object($action))
@@ -135,12 +173,12 @@ class HelperTreeToolbarCore implements HelperITreeToolbarCore
{ {
foreach ($this->getActions() as $action) foreach ($this->getActions() as $action)
{ {
if (is_subclass_of($action, 'HelperTreeToolbarSearchCore')) if ($action instanceof HelperTreeToolbarSearchCore)
$action->setAttribute('data', $this->getData()); $action->setAttribute('data', $this->getData());
} }
return $this->getContext()->smarty->createTemplate( return $this->getContext()->smarty->createTemplate(
$this->getTemplateDirectory().$this->getTemplate(), $this->getTemplateFile($this->getTemplate()),
$this->getContext()->smarty $this->getContext()->smarty
)->assign('actions', $this->getActions())->fetch(); )->assign('actions', $this->getActions())->fetch();
} }
+10 -49
View File
@@ -27,9 +27,6 @@
class HelperTreeToolbarSearchCore extends HelperTreeToolbarButtonCore implements class HelperTreeToolbarSearchCore extends HelperTreeToolbarButtonCore implements
HelperITreeToolbarButtonCore HelperITreeToolbarButtonCore
{ {
private $_children_key;
private $_id_key;
private $_name_key;
protected $_template = 'tree_toolbar_search.tpl'; protected $_template = 'tree_toolbar_search.tpl';
public function __construct($label, $id, $name = null, $class = null) public function __construct($label, $id, $name = null, $class = null)
@@ -41,48 +38,6 @@ class HelperTreeToolbarSearchCore extends HelperTreeToolbarButtonCore implements
$this->setClass($class); $this->setClass($class);
} }
public function setChildrenKey($value)
{
$this->_children_key = (string)$value;
return $this;
}
public function getChildrenKey()
{
if (!isset($this->_children_key))
$this->setChildrenKey('children');
return $this->_children_key;
}
public function setIdKey($value)
{
$this->_id_key = (string)$value;
return $this;
}
public function getIdKey()
{
if (!isset($this->_id_key))
$this->setIdKey('id');
return $this->_id_key;
}
public function setNameKey($value)
{
$this->_name_key = (string)$value;
return $this;
}
public function getNameKey()
{
if (!isset($this->_name_key))
$this->setNameKey('name');
return $this->_name_key;
}
public function render() public function render()
{ {
if ($this->hasAttribute('data')) if ($this->hasAttribute('data'))
@@ -117,10 +72,16 @@ class HelperTreeToolbarSearchCore extends HelperTreeToolbarButtonCore implements
foreach ($data as $item) foreach ($data as $item)
{ {
if (array_key_exists($this->getChildrenKey(), $item) && !empty($item[$this->getChildrenKey()])) $html .= '{';
$html .= '{id : '.$item[$this->getIdKey()].', name : "'.$item[$this->getNameKey()].'"},'.$this->_renderData($item[$this->getChildrenKey()]);
else foreach ($item as $key => $value)
$html .= '{id : '.$item[$this->getIdKey()].', name : "'.$item[$this->getNameKey()].'"},'; if (!is_array($value))
$html .= $key.':'.(is_numeric($value) ? $value : '"'.$value.'"').',';
$html .= '},';
if (array_key_exists('children', $item) && !empty($item['children']))
$html .= $this->_renderData($item['children']);
} }
return $html; return $html;
+20 -10
View File
@@ -2330,8 +2330,21 @@ class AdminProductsControllerCore extends AdminController
public function renderCategoriesTree($selected_category = null) public function renderCategoriesTree($selected_category = null)
{ {
if (Tools::isSubmit('id_shop'))
$id_shop = Tools::getValue('id_shop');
else
if (Context::getContext()->shop->id)
$id_shop = Context::getContext()->shop->id;
else
if (!Shop::isFeatureActive())
$id_shop = Configuration::get('PS_SHOP_DEFAULT');
else
$id_shop = 0;
$shop = new Shop($id_shop);
$root_category = Category::getRootCategory(null, $shop);
$tree = new HelperTree('categories-tree', Category::getNestedCategories( $tree = new HelperTree('categories-tree', Category::getNestedCategories(
1, $this->context->employee->id_lang)); $root_category->id, $this->context->employee->id_lang));
$html = $tree->setTitle('Filter by category') $html = $tree->setTitle('Filter by category')
->setActions(array( ->setActions(array(
@@ -2346,7 +2359,6 @@ class AdminProductsControllerCore extends AdminController
'$(\'#categories-tree\').tree(\'expandAll\')', '$(\'#categories-tree\').tree(\'expandAll\')',
'icon-expand-alt') 'icon-expand-alt')
)) ))
->setTemplateDirectory($this->context->smarty->getTemplateDir(0).'/controllers/products/helpers/tree')
->render(); ->render();
$html .= '<script type="text/javascript"> $html .= '<script type="text/javascript">
$(document).ready(function () { $(document).ready(function () {
@@ -2389,10 +2401,7 @@ class AdminProductsControllerCore extends AdminController
$selected_categories) $selected_categories)
{ {
$tree = new HelperTree('associated-categories-tree', $tree = new HelperTree('associated-categories-tree',
Category::getNestedCategories(1, $this->context->employee->id_lang)); Category::getNestedCategories($root_category, $this->context->employee->id_lang));
$search = new HelperTreeToolbarSearch('Find a category:',
'categories-search');
$search->setIdKey('id_category');
$html = $tree->setTitle('Associated categories') $html = $tree->setTitle('Associated categories')
->setActions(array( ->setActions(array(
new HelperTreeToolbarLink( new HelperTreeToolbarLink(
@@ -2415,9 +2424,10 @@ class AdminProductsControllerCore extends AdminController
'#', '#',
'uncheckAllAssociatedCategories();', 'uncheckAllAssociatedCategories();',
'icon-check-empty'), 'icon-check-empty'),
$search new HelperTreeToolbarSearch(
'Find a category:',
'categories-search')
)) ))
->setTemplateDirectory($this->context->smarty->getTemplateDir(0).'/controllers/products/helpers/tree')
->setNodeFolderTemplate('tree_node_folder_associations.tpl') ->setNodeFolderTemplate('tree_node_folder_associations.tpl')
->setNodeItemTemplate('tree_node_item_associations.tpl') ->setNodeItemTemplate('tree_node_item_associations.tpl')
->render(); ->render();
@@ -2445,11 +2455,11 @@ class AdminProductsControllerCore extends AdminController
); );
} }
$("#categories-search").bind("typeahead:selected", function(obj, datum) { $("#categories-search").bind("typeahead:selected", function(obj, datum) {
$("#associated-categories-tree").find(":input").each( $("#associated-categories-tree").find(":input").each(
function() function()
{ {
if ($(this).val() == datum.id) if ($(this).val() == datum.id_category)
{ {
$(this).prop("checked", true); $(this).prop("checked", true);
$(this).parent().addClass("tree-selected"); $(this).parent().addClass("tree-selected");