* @copyright 2007-2016 PrestaShop SA * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) * International Registered Trademark & Property of PrestaShop SA */ require_once (dirname(__FILE__). '/ITreeToolbarButton.php'); require_once (dirname(__FILE__). '/TreeToolbarButton.php'); class TreeToolbarSearch extends TreeToolbarButton implements ITreeToolbarButton { 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_search')) { $this->setAttribute('typeahead_source', $this->_renderData($this->getAttribute('data_search'))); } elseif ($this->hasAttribute('data')) { $this->setAttribute('typeahead_source', $this->_renderData($this->getAttribute('data'))); } $js_path = __PS_BASE_URI__.'modules/x13categoryassign/backward-compatibility/templates/assets/typeahead.min.js'; if ($this->getContext()->controller->ajax) { $html = ''; } else { $this->getContext()->controller->addJs($js_path); } 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 .= Tools::jsonEncode($item).','; if (array_key_exists('children', $item) && !empty($item['children'])) { $html .= $this->_renderData($item['children']); } } return $html; } }