1<?php
2/**
3 * 2007-2020 PrestaShop SA and Contributors
4 *
5 * NOTICE OF LICENSE
6 *
7 * This source file is subject to the Academic Free License 3.0 (AFL-3.0)
8 * that is bundled with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * https://opensource.org/licenses/AFL-3.0
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@prestashop.com so we can send you a copy immediately.
14 *
15 * DISCLAIMER
16 *
17 * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
18 * versions in the future. If you wish to customize PrestaShop for your
19 * needs please refer to https://www.prestashop.com for more information.
20 *
21 * @author    PrestaShop SA <contact@prestashop.com>
22 * @copyright 2007-2020 PrestaShop SA and Contributors
23 * @license   https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0)
24 * International Registered Trademark & Property of PrestaShop SA
25 */
26if (!defined('_PS_VERSION_')) {
27    exit;
28}
29
30$autoloadPath = __DIR__ . '/vendor/autoload.php';
31if (file_exists($autoloadPath)) {
32    require_once $autoloadPath;
33}
34
35use PrestaShop\PrestaShop\Core\Module\WidgetInterface;
36
37class Ps_Searchbar extends Module implements WidgetInterface
38{
39    /**
40     * @var string Name of the module running on PS 1.6.x. Used for data migration.
41     */
42    const PS_16_EQUIVALENT_MODULE = 'blocksearch';
43
44    private $templateFile;
45
46    public function __construct()
47    {
48        $this->name = 'ps_searchbar';
49        $this->author = 'PrestaShop';
50        $this->version = '2.1.2';
51        $this->need_instance = 0;
52
53        parent::__construct();
54
55        $this->displayName = $this->trans('Search bar', [], 'Modules.Searchbar.Admin');
56        $this->description = $this->trans('Help your visitors find what they are looking for, add a quick search field to your store.', [], 'Modules.Searchbar.Admin');
57
58        $this->ps_versions_compliancy = ['min' => '1.7.8.0', 'max' => _PS_VERSION_];
59
60        $this->templateFile = 'module:ps_searchbar/ps_searchbar.tpl';
61    }
62
63    public function install()
64    {
65        // Migrate data from 1.6 equivalent module (if applicable), then uninstall
66        if (Module::isInstalled(self::PS_16_EQUIVALENT_MODULE)) {
67            $oldModule = Module::getInstanceByName(self::PS_16_EQUIVALENT_MODULE);
68            if ($oldModule) {
69                $oldModule->uninstall();
70            }
71        }
72
73        return parent::install()
74            && $this->registerHook('displayTop')
75            && $this->registerHook('displaySearch')
76            && $this->registerHook('displayHeader')
77        ;
78    }
79
80    public function hookDisplayHeader()
81    {
82        $this->context->controller->addJqueryUI('ui.autocomplete');
83        $this->context->controller->registerStylesheet('modules-searchbar', 'modules/' . $this->name . '/ps_searchbar.css');
84        $this->context->controller->registerJavascript('modules-searchbar', 'modules/' . $this->name . '/ps_searchbar.js', ['position' => 'bottom', 'priority' => 150]);
85    }
86
87    public function getWidgetVariables($hookName, array $configuration = [])
88    {
89        $widgetVariables = [
90            'search_controller_url' => $this->context->link->getPageLink('search', null, null, null, false, null, true),
91        ];
92
93        /** @var array $templateVars */
94        $templateVars = $this->context->smarty->getTemplateVars();
95        if (is_array($templateVars) && !array_key_exists('search_string', $templateVars)) {
96            $widgetVariables['search_string'] = '';
97        }
98
99        return $widgetVariables;
100    }
101
102    public function renderWidget($hookName, array $configuration = [])
103    {
104        $this->smarty->assign($this->getWidgetVariables($hookName, $configuration));
105
106        return $this->fetch($this->templateFile);
107    }
108}
109