Files
wyczarujprezent.pl/modules/x13categoryassign/backward-compatibility/TreeToolbarSearch.php
2024-10-28 22:14:22 +01:00

85 lines
2.8 KiB
PHP

<?php
/*
* 2007-2016 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-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 = '<script type="text/javascript">
$(function(){ $.getScript(\''.$js_path.'\'); });
</script>';
} 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;
}
}