// Toolbar

This commit is contained in:
Jerome Nadaud
2013-09-10 14:08:29 +02:00
parent 03d1697e19
commit 85dc552667
15 changed files with 368 additions and 96 deletions
@@ -31,10 +31,12 @@ interface HelperITreeToolbarCore
public function getActions();
public function setContext($value);
public function getContext();
public function setData($value);
public function getData();
public function setTemplate($value);
public function getTemplate();
public function setTemplateDirectory($value);
public function getTemplateDirectory();
public function getTemplateDirectory();
public function addAction($action);
public function render();
}
+7 -3
View File
@@ -27,20 +27,24 @@
interface HelperITreeToolbarButtonCore
{
public function __toString();
public function setAction($value);
public function getAction();
public function setAttribute($name, $value);
public function getAttribute($name);
public function setAttributes($value);
public function getAttributes();
public function setClass($value);
public function getClass();
public function setContext($value);
public function getContext();
public function setId($value);
public function getId();
public function setLabel($value);
public function getLabel();
public function setName($value);
public function getName();
public function setTemplate($value);
public function getTemplate();
public function setTemplateDirectory($value);
public function getTemplateDirectory();
public function addAttribute($name, $value);
public function hasAttribute($name);
public function render();
}
+53 -4
View File
@@ -32,10 +32,13 @@ class HelperTreeCore
const DEFAULT_NODE_FOLDER_TEMPLATE = 'tree_node_folder.tpl';
const DEFAULT_NODE_ITEM_TEMPLATE = 'tree_node_item.tpl';
private $_children_key;
private $_context;
private $_data;
private $_headerTemplate;
private $_id;
private $_id_key;
private $_name_key;
private $_node_folder_template;
private $_node_item_template;
private $_template_directory;
@@ -80,6 +83,20 @@ class HelperTreeCore
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)
{
$this->_context = $value;
@@ -136,6 +153,34 @@ class HelperTreeCore
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)
{
$this->_node_folder_template = $value;
@@ -223,6 +268,9 @@ class HelperTreeCore
public function getToolbar()
{
if (isset($this->_toolbar))
$this->_toolbar->setData($this->getData());
return $this->_toolbar;
}
@@ -296,13 +344,14 @@ class HelperTreeCore
foreach ($data as $item)
{
if (array_key_exists('children', $item) && !empty($item['children']))
if (array_key_exists($this->getChildrenKey(), $item)
&& !empty($item[$this->getChildrenKey()]))
$html .= $this->getContext()->smarty->createTemplate(
$this->getTemplateDirectory().$this->getNodeFolderTemplate(),
$this->getContext()->smarty
)->assign(array(
'name' => $item['name'],
'children' => $this->renderNodes($item['children']),
'name' => $item[$this->getNameKey()],
'children' => $this->renderNodes($item[$this->getChildrenKey()]),
'node' => $item
))->fetch();
else
@@ -310,7 +359,7 @@ class HelperTreeCore
$this->getTemplateDirectory().$this->getNodeItemTemplate(),
$this->getContext()->smarty
)->assign(array(
'name' => $item['name'],
'name' => $item[$this->getNameKey()],
'node' => $item
))->fetch();
}
+21
View File
@@ -31,6 +31,7 @@ class HelperTreeToolbarCore implements HelperITreeToolbarCore
private $_actions;
private $_context;
private $_data;
private $_template;
private $_template_directory;
@@ -69,6 +70,20 @@ class HelperTreeToolbarCore implements HelperITreeToolbarCore
return $this->_context;
}
public function setData($value)
{
if (!is_array($value) && !$value instanceof Traversable)
throw new PrestaShopException('Data value must be an traversable array');
$this->_data = $value;
return $this;
}
public function getData()
{
return $this->_data;
}
public function setTemplate($value)
{
$this->_template = $value;
@@ -118,6 +133,12 @@ class HelperTreeToolbarCore implements HelperITreeToolbarCore
public function render()
{
foreach ($this->getActions() as $action)
{
if (is_subclass_of($action, 'HelperTreeToolbarSearchCore'))
$action->setAttribute('data', $this->getData());
}
return $this->getContext()->smarty->createTemplate(
$this->getTemplateDirectory().$this->getTemplate(),
$this->getContext()->smarty
+41 -21
View File
@@ -28,18 +28,20 @@ abstract class HelperTreeToolbarButtonCore
{
const DEFAULT_TEMPLATE_DIRECTORY = 'helpers/tree';
private $_action;
private $_attributes;
protected $_attributes;
private $_class;
private $_context;
private $_id;
private $_label;
private $_name;
protected $_template;
protected $_template_directory;
public function __construct($label, $action = null, $class = null)
public function __construct($label, $id = null, $name = null, $class = null)
{
$this->setLabel($label);
$this->setAction($action);
$this->setId($id);
$this->setName($name);
$this->setClass($class);
}
@@ -48,15 +50,18 @@ abstract class HelperTreeToolbarButtonCore
return $this->render();
}
public function setAction($value)
public function setAttribute($name, $value)
{
$this->_action = $value;
if (!isset($this->_attributes))
$this->_attributes = array();
$this->_attributes[$name] = $value;
return $this;
}
public function getAction()
public function getAttribute($name)
{
return $this->_action;
return $this->hasAttribute($name) ? $this->_attributes[$name] : null;
}
public function setAttributes($value)
@@ -78,13 +83,12 @@ abstract class HelperTreeToolbarButtonCore
public function setClass($value)
{
$this->_class = $value;
return $this;
return $this->setAttribute('class', $value);
}
public function getClass()
{
return $this->_class;
return $this->getAttribute('class');
}
public function setContext($value)
@@ -101,15 +105,34 @@ abstract class HelperTreeToolbarButtonCore
return $this->_context;
}
public function setId($value)
{
return $this->setAttribute('id', $value);
}
public function getId()
{
return $this->getAttribute('id');
}
public function setLabel($value)
{
$this->_label = $value;
return $this;
return $this->setAttribute('label', $value);
}
public function getLabel()
{
return $this->_label;
return $this->getAttribute('label');
}
public function setName($value)
{
return $this->setAttribute('name', $value);
}
public function getName()
{
return $this->getAttribute('name');
}
public function setTemplate($value)
@@ -139,13 +162,10 @@ abstract class HelperTreeToolbarButtonCore
return $this->_template_directory;
}
public function addAttribute($name, $value)
public function hasAttribute($name)
{
if (!isset($this->_attributes))
$this->_attributes = array();
$this->_attributes[$name] = $value;
return $this;
return (isset($this->_attributes)
&& array_key_exists($name, $this->_attributes));
}
public function render()
@@ -153,7 +173,7 @@ abstract class HelperTreeToolbarButtonCore
return $this->getContext()->smarty->createTemplate(
$this->getTemplateDirectory().$this->getTemplate(),
$this->getContext()->smarty
)->assign($this->getAttributes());
)->assign($this->getAttributes())->fetch();
}
private function _normalizeDirectory($directory)
+19 -24
View File
@@ -27,52 +27,47 @@
class HelperTreeToolbarLinkCore extends HelperTreeToolbarButtonCore implements
HelperITreeToolbarButtonCore
{
private $_action;
private $_icon_class;
private $_link;
protected $_template = 'tree_toolbar_link.tpl';
public function __construct($label, $action = null, $link, $iconClass)
public function __construct($label, $link, $action = null, $iconClass = null)
{
parent::__construct($label, $action);
parent::__construct($label);
$this->setLink($link);
$this->setAction($action);
$this->setIconClass($iconClass);
}
public function setAction($value)
{
return $this->setAttribute('action', $value);
}
public function getAction()
{
return $this->getAttribute('action');
}
public function setIconClass($value)
{
$this->_icon_class = $value;
return $this;
return $this->setAttribute('icon_class', $value);
}
public function getIconClass()
{
return $this->_icon_class;
return $this->getAttribute('icon_class');
}
public function setLink($value)
{
$this->_link = $value;
return $this;
return $this->setAttribute('link', $value);
}
public function getLink()
{
if (!isset($this->_link))
$this->_link = '';
return $this->_link;
}
public function render()
{
$template = parent::render();
$template->assign(array(
'link' => $this->getLink(),
'action' => $this->getAction(),
'label' => $this->getLabel(),
'class' => $this->getClass(),
'icon_class' => $this->getIconClass()
));
return $template->fetch();
return $this->getAttribute('link');
}
}
+77 -7
View File
@@ -27,10 +27,68 @@
class HelperTreeToolbarSearchCore extends HelperTreeToolbarButtonCore implements
HelperITreeToolbarButtonCore
{
private $_children_key;
private $_id_key;
private $_name_key;
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 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()
{
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)
@@ -47,12 +105,24 @@ class HelperTreeToolbarSearchCore extends HelperTreeToolbarButtonCore implements
$this->getContext()->controller->addJs(__PS_BASE_URI__.$admin_webpath
.'/themes/'.$bo_theme.'/js/vendor/typeahead.min.js');
$template = parent::render();
$template->assign(array(
'action' => $this->getAction(),
'label' => $this->getLabel(),
'class' => $this->getClass()
));
return (isset($html)?$html:'').$template->fetch();
return (isset($html)?$html:'').parent::render();
}
private function _renderData($data)
{
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($this->getChildrenKey(), $item) && !empty($item[$this->getChildrenKey()]))
$html .= '{id : '.$item[$this->getIdKey()].', name : "'.$item[$this->getNameKey()].'"},'.$this->_renderData($item[$this->getChildrenKey()]);
else
$html .= '{id : '.$item[$this->getIdKey()].', name : "'.$item[$this->getNameKey()].'"},';
}
return $html;
}
}