From 40fc98801b32f3dcb186465622148ef826ca518b Mon Sep 17 00:00:00 2001 From: davidmolinacano Date: Tue, 8 Oct 2024 13:24:20 +0200 Subject: [PATCH] feat: Complete refactor --- config.php | 5 +- doofinder.php | 645 ++------------------------------ feeds/product.php | 11 +- lib/DoofinderAdminPanelView.php | 490 +++++++++++++++++++++++- lib/DoofinderConfig.php | 67 ++++ lib/FormManager.php | 105 ++++++ 6 files changed, 699 insertions(+), 624 deletions(-) create mode 100644 lib/FormManager.php diff --git a/config.php b/config.php index db10eda..fb0a15c 100644 --- a/config.php +++ b/config.php @@ -15,6 +15,7 @@ use PrestaShop\Module\Doofinder\Lib\DfTools; use PrestaShop\Module\Doofinder\Lib\DoofinderConfig; +use PrestaShop\Module\Doofinder\Lib\DoofinderConstants; $root_path = dirname(dirname(dirname($_SERVER['SCRIPT_FILENAME']))); $config_file_path = $root_path . '/config/config.inc.php'; @@ -26,6 +27,8 @@ require_once dirname(__FILE__) . '/../../init.php'; } +require_once 'autoloader.php'; + if (!defined('_PS_VERSION_')) { exit; } @@ -87,7 +90,7 @@ 'version' => _PS_VERSION_, ], 'module' => [ - 'version' => Doofinder::VERSION, + 'version' => DoofinderConstants::VERSION, 'feed' => $base . $shop->getBaseURI() . 'modules/doofinder/feed.php', 'options' => [ 'language' => $languages, diff --git a/doofinder.php b/doofinder.php index 20ac651..580a65f 100644 --- a/doofinder.php +++ b/doofinder.php @@ -18,17 +18,11 @@ require_once 'autoloader.php'; -use PrestaShop\Module\Doofinder\Lib\DfTools; use PrestaShop\Module\Doofinder\Lib\DoofinderAdminPanelView; -use PrestaShop\Module\Doofinder\Lib\DoofinderApi; -use PrestaShop\Module\Doofinder\Lib\DoofinderConstants; use PrestaShop\Module\Doofinder\Lib\DoofinderInstallation; use PrestaShop\Module\Doofinder\Lib\DoofinderScript; -use PrestaShop\Module\Doofinder\Lib\EasyREST; use PrestaShop\Module\Doofinder\Lib\HookManager; use PrestaShop\Module\Doofinder\Lib\SearchEngine; -use PrestaShop\Module\Doofinder\Lib\UpdateOnSave; -use PrestaShop\Module\Doofinder\Lib\UrlManager; class Doofinder extends Module { @@ -40,14 +34,6 @@ class Doofinder extends Module public $admin_template_dir = ''; public $hookManager; - // TODO (davidmolinacano): To be deleted after complete refactor. - const GS_SHORT_DESCRIPTION = DoofinderConstants::GS_SHORT_DESCRIPTION; - const GS_LONG_DESCRIPTION = DoofinderConstants::GS_LONG_DESCRIPTION; - const VERSION = DoofinderConstants::VERSION; - const NAME = DoofinderConstants::NAME; - const YES = DoofinderConstants::YES; - const NO = DoofinderConstants::NO; - public function __construct() { $this->name = 'doofinder'; @@ -157,81 +143,38 @@ public function setSearchEnginesByConfig() public function getContent() { $adminPanelView = new DoofinderAdminPanelView($this); - $stop = $adminPanelView->getWarningMultishopHtml(); - if ($stop) { - return $stop; - } - $adv = Tools::getValue('adv', 0); - - $this->context->smarty->assign('adv', $adv); - - $msg = $this->postProcess(); - - $output = $msg; - $oldPS = false; - $this->context->controller->addJS($this->_path . 'views/js/admin-panel.js'); - if (_PS_VERSION_ < 1.6) { - $oldPS = true; - $this->context->controller->addJS($this->_path . 'views/js/plugins/bootstrap.min.js'); - $this->context->controller->addCSS($this->_path . 'views/css/admin-theme_15.css'); - } - $configured = $this->isConfigured(); - $is_new_shop = $this->showNewShopForm(Context::getContext()->shop); - $shop_id = null; - if ($is_new_shop) { - $shop_id = Context::getContext()->shop->id; - } + return $adminPanelView->getContent(); + } - $skip_url_params = [ - 'skip' => 1, - 'configure' => $this->name, - 'tab_module' => $this->tab, - 'module_name' => $this->name, - ]; - $skipurl = $this->context->link->getAdminLink('AdminModules', true); - $separator = strpos($skipurl, '?') === false ? '?' : '&'; - $skipurl .= $separator . http_build_query($skip_url_params); - - $redirect = $this->context->shop->getBaseURL(true, false) . $this->_path . 'config.php'; - $token = Tools::encrypt($redirect); - $paramsPopup = 'email=' . $this->context->employee->email . '&token=' . $token; - $dfEnabledV9 = Configuration::get('DF_ENABLED_V9'); - - $this->context->smarty->assign('oldPS', $oldPS); - $this->context->smarty->assign('module_dir', $this->_path); - $this->context->smarty->assign('configured', $configured); - $this->context->smarty->assign('is_new_shop', $is_new_shop); - $this->context->smarty->assign('shop_id', $shop_id); - $this->context->smarty->assign('checkConnection', $this->checkOutsideConnection()); - $this->context->smarty->assign('tokenAjax', Tools::encrypt('doofinder-ajax')); - $this->context->smarty->assign('skipurl', $skipurl); - $this->context->smarty->assign('paramsPopup', $paramsPopup); - $this->context->smarty->assign('dfEnabledV9', $dfEnabledV9); - - $output .= $this->context->smarty->fetch($this->local_path . 'views/templates/admin/configure.tpl'); - if ($configured) { - $feed_indexed = Configuration::get('DF_FEED_INDEXED', false); - if (empty($feed_indexed)) { - $controller_url = $this->context->link->getAdminLink('DoofinderAdmin', true) . '&ajax=1'; - $this->context->smarty->assign('update_feed_url', $controller_url . '&action=UpdateConfigurationField'); - $this->context->smarty->assign('check_feed_url', $controller_url . '&action=CheckConfigurationField'); - $output .= $this->context->smarty->fetch($this->local_path . 'views/templates/admin/indexation_status.tpl'); - } - - $output .= $this->context->smarty->fetch($this->local_path . 'views/templates/admin/configure_administration_panel.tpl'); - $output .= $this->renderFormDataFeed($adv); - if ($adv) { - $output .= $this->renderFormAdvanced(); - } - $adv_url = $this->context->link->getAdminLink('AdminModules', true) . '&adv=1' - . '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name; - $this->context->smarty->assign('adv_url', $adv_url); - } + /** + * Returns the module path + * + * @return string + */ + public function getPath() + { + return $this->_path; + } - $output .= $this->context->smarty->fetch($this->local_path . 'views/templates/admin/configure_footer.tpl'); + /** + * Returns the name of the main table used for modules installed + * + * @return string + */ + public function getTable() + { + return $this->table; + } - return $output; + /** + * Returns the identifier of the main table + * + * @return string + */ + public function getIdentifier() + { + return $this->identifier; } /** @@ -331,520 +274,6 @@ public function hookActionObjectCategoryDeleteAfter($params) HookManager::proccessHookUpdateOnSave('category', $params['object']->id, $this->context->shop->id, 'delete'); } - /** - * Checks the connection with DooManager - * - * @return bool - */ - public function checkOutsideConnection() - { - $client = new EasyREST(true, 3); - $doomanangerRegionlessUrl = sprintf(DoofinderConstants::DOOMANAGER_REGION_URL, ''); - $result = $client->get(sprintf('%s/auth/login', $doomanangerRegionlessUrl)); - - return $result && $result->originalResponse && isset($result->headers['code']) - && (strpos($result->originalResponse, 'HTTP/2 200') || $result->headers['code'] == 200); - } - - /** - * Check if the module has already been configured - * - * @return bool - */ - protected function isConfigured() - { - $skip = Tools::getValue('skip'); - if ($skip) { - Configuration::updateValue('DF_ENABLE_HASH', 0); - Configuration::updateValue('DF_ENABLED_V9', true); - } - $sql = 'SELECT id_configuration FROM ' . _DB_PREFIX_ . 'configuration WHERE name = \'DF_ENABLE_HASH\''; - - return Db::getInstance()->getValue($sql); - } - - /** - * Check if the form to create a store installation has to be displayed - * - * @param Shop $shop - * - * @return bool - */ - protected function showNewShopForm($shop) - { - $installationId = Configuration::get('DF_INSTALLATION_ID', null, (int) $shop->id_shop_group, (int) $shop->id); - $multishopEnable = Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE'); - $apiKey = Configuration::get('DF_AI_APIKEY'); - - return !$installationId && $multishopEnable && $apiKey; - } - - /** - * Get the fields of the search layer configuration form - * - * @return array - */ - protected function getConfigFormSearchLayer() - { - $inputs = [ - [ - 'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'), - 'label' => $this->l('Doofinder search layer'), - 'name' => 'DF_SHOW_LAYER', - 'is_bool' => true, - 'values' => $this->getBooleanFormValue(), - ], - [ - 'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'), - 'label' => $this->l('Doofinder search layer in mobile version'), - 'name' => 'DF_SHOW_LAYER_MOBILE', - 'is_bool' => true, - 'values' => $this->getBooleanFormValue(), - ], - [ - 'type' => 'text', - 'label' => $this->l('Doofinder Store ID'), - 'name' => 'DF_INSTALLATION_ID', - 'desc' => $this->l('INSTALLATION_ID_EXPLANATION'), - 'lang' => false, - ], - ]; - - return [ - 'form' => [ - 'legend' => [ - 'title' => $this->l('Search Layer'), - ], - 'input' => $inputs, - 'submit' => [ - 'title' => $this->l('Save Layer Widget Options'), - 'name' => 'submitDoofinderModuleSearchLayer', - ], - ], - ]; - } - - /** - * Get the values for the search layer configuration form - * - * @return array - */ - protected function getConfigFormValuesSearchLayer() - { - $fields = []; - $fields['DF_INSTALLATION_ID'] = Configuration::get('DF_INSTALLATION_ID'); - $fields['DF_SHOW_LAYER'] = Configuration::get('DF_SHOW_LAYER', null, null, null, true); - $fields['DF_SHOW_LAYER_MOBILE'] = Configuration::get('DF_SHOW_LAYER_MOBILE', null, null, null, true); - - return $fields; - } - - /** - * Render the data feed configuration form - * - * @param bool $adv - * - * @return string - */ - protected function renderFormDataFeed($adv = false) - { - $helper = new HelperForm(); - - $helper->show_toolbar = false; - $helper->table = $this->table; - $helper->module = $this; - $helper->default_form_language = $this->context->language->id; - $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0); - - $helper->identifier = $this->identifier; - // $helper->submit_action = 'submitDoofinderModuleDataFeed'; - $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) - . (($adv) ? '&adv=1' : '') - . '&configure=' . $this->name . '&tab_module=' . $this->tab . '&module_name=' . $this->name; - $helper->token = Tools::getAdminTokenLite('AdminModules'); - - $this->context->smarty->assign('id_tab', 'data_feed_tab'); - $html = $this->context->smarty->fetch($this->local_path . 'views/templates/admin/dummy/pre_tab.tpl'); - // Data feed form - $helper->tpl_vars = [ - 'fields_value' => $this->getConfigFormValuesDataFeed(), - 'languages' => $this->context->controller->getLanguages(), - 'id_language' => $this->context->language->id, - ]; - - if (!$this->showNewShopForm(Context::getContext()->shop)) { - $valid_update_on_save = UpdateOnSave::isValid(); - $html .= $helper->generateForm([$this->getConfigFormDataFeed($valid_update_on_save)]); - // Search layer form - $helper->tpl_vars['fields_value'] = $this->getConfigFormValuesSearchLayer(); - $html .= $helper->generateForm([$this->getConfigFormSearchLayer()]); - } else { - $this->context->controller->warnings[] = $this->l("This shop is new and it hasn't been synchronized with Doofinder yet."); - } - $html .= $this->context->smarty->fetch($this->local_path . 'views/templates/admin/dummy/after_tab.tpl'); - - return $html; - } - - /** - * Get the fields of the data feed configuration form - * - * @return array - */ - protected function getConfigFormDataFeed($valid_update_on_save = false) - { - if ($valid_update_on_save) { - $disabled = false; - $query = [ - 5 => ['id' => 5, 'name' => sprintf($this->l('Each %s minutes'), '5')], - 15 => ['id' => 15, 'name' => sprintf($this->l('Each %s minutes'), '15')], - 30 => ['id' => 30, 'name' => sprintf($this->l('Each %s minutes'), '30')], - 60 => ['id' => 60, 'name' => $this->l('Each hour')], - 120 => ['id' => 120, 'name' => sprintf($this->l('Each %s hours'), '2')], - 360 => ['id' => 360, 'name' => sprintf($this->l('Each %s hours'), '6')], - 720 => ['id' => 720, 'name' => sprintf($this->l('Each %s hours'), '12')], - 1440 => ['id' => 1440, 'name' => $this->l('Once a day')], - 0 => ['id' => 0, 'name' => $this->l('Disabled')], - ]; - } else { - $disabled = true; - $query = [ - 0 => ['id' => 0, 'name' => $this->l('Disabled')], - ]; - } - - return [ - 'form' => [ - 'legend' => [ - 'title' => $this->l('Data Feed'), - ], - 'input' => [ - [ - 'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'), - 'label' => $this->l('Index product prices'), - 'desc' => $this->l('If you activate this option you will be able to show the prices of each product in the search results.'), - 'name' => 'DF_GS_DISPLAY_PRICES', - 'is_bool' => true, - 'values' => $this->getBooleanFormValue(), - ], - [ - 'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'), - 'label' => $this->l('Show product prices including taxes'), - 'desc' => $this->l('If you activate this option, the price of the products that will be displayed will be inclusive of taxes.'), - 'name' => 'DF_GS_PRICES_USE_TAX', - 'is_bool' => true, - 'values' => $this->getBooleanFormValue(), - ], - [ - 'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'), - 'label' => $this->l('Index the full path of the product category'), - 'name' => 'DF_FEED_FULL_PATH', - 'is_bool' => true, - 'values' => $this->getBooleanFormValue(), - ], - [ - 'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'), - 'label' => $this->l('Index product attribute combinations'), - 'name' => 'DF_SHOW_PRODUCT_VARIATIONS', - 'is_bool' => true, - 'values' => $this->getBooleanFormValue(), - ], - [ - 'type' => 'select', - 'label' => $this->l('Define which combinations of product attributes you want to index for'), - 'name' => 'DF_GROUP_ATTRIBUTES_SHOWN', - 'multiple' => true, - 'options' => [ - 'query' => AttributeGroup::getAttributesGroups(Context::getContext()->language->id), - 'id' => 'id_attribute_group', - 'name' => 'name', - ], - ], - [ - 'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'), - 'label' => $this->l('Index customized product features'), - 'name' => 'DF_SHOW_PRODUCT_FEATURES', - 'is_bool' => true, - 'values' => $this->getBooleanFormValue(), - ], - [ - 'type' => 'select', - 'label' => $this->l('Select features will be shown in feed'), - 'name' => 'DF_FEATURES_SHOWN', - 'multiple' => true, - 'options' => [ - 'query' => Feature::getFeatures( - Context::getContext()->language->id, - $this->context->shop->id - ), - 'id' => 'id_feature', - 'name' => 'name', - ], - ], - [ - 'type' => 'select', - 'label' => $this->l('Product Image Size'), - 'name' => 'DF_GS_IMAGE_SIZE', - 'options' => [ - 'query' => DfTools::getAvailableImageSizes(), - 'id' => 'DF_GS_IMAGE_SIZE', - 'name' => 'name', - ], - ], - [ - 'type' => 'select', - 'label' => $this->l('Automatically process modified products'), - 'desc' => $this->l('This action will only be executed if there are changes. If you see the field disabled, it is because you are making a usage in the indexes that is not supported by the automatic processing of modified products.'), - 'name' => 'DF_UPDATE_ON_SAVE_DELAY', - 'disabled' => $disabled, - 'options' => [ - 'query' => $query, - 'id' => 'id', - 'name' => 'name', - ], - ], - ], - 'submit' => [ - 'title' => $this->l('Save Data Feed Options'), - 'name' => 'submitDoofinderModuleDataFeed', - ], - ], - ]; - } - - /** - * Get the values for the data feed configuration form - * - * @return array - */ - protected function getConfigFormValuesDataFeed() - { - return [ - 'DF_GS_DISPLAY_PRICES' => Configuration::get('DF_GS_DISPLAY_PRICES'), - 'DF_GS_PRICES_USE_TAX' => Configuration::get('DF_GS_PRICES_USE_TAX'), - 'DF_FEED_FULL_PATH' => Configuration::get('DF_FEED_FULL_PATH'), - 'DF_SHOW_PRODUCT_VARIATIONS' => Configuration::get('DF_SHOW_PRODUCT_VARIATIONS'), - 'DF_GROUP_ATTRIBUTES_SHOWN[]' => explode(',', Configuration::get('DF_GROUP_ATTRIBUTES_SHOWN')), - 'DF_SHOW_PRODUCT_FEATURES' => Configuration::get('DF_SHOW_PRODUCT_FEATURES'), - 'DF_FEATURES_SHOWN[]' => explode(',', Configuration::get('DF_FEATURES_SHOWN')), - 'DF_GS_IMAGE_SIZE' => Configuration::get('DF_GS_IMAGE_SIZE'), - 'DF_UPDATE_ON_SAVE_DELAY' => Configuration::get('DF_UPDATE_ON_SAVE_DELAY'), - ]; - } - - /** - * Render the advanced configuration form - * - * @return string - */ - protected function renderFormAdvanced() - { - $helper = new HelperForm(); - - $helper->show_toolbar = false; - $helper->table = $this->table; - $helper->module = $this; - $helper->default_form_language = $this->context->language->id; - $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0); - - $helper->identifier = $this->identifier; - // helper->submit_action = 'submitDoofinderModuleAdvanced'; - $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false) - . '&configure=' . $this->name . '&adv=1&tab_module=' . $this->tab . '&module_name=' . $this->name; - $helper->token = Tools::getAdminTokenLite('AdminModules'); - - $helper->tpl_vars = [ - 'fields_value' => $this->getConfigFormValuesAdvanced(), - 'languages' => $this->context->controller->getLanguages(), - 'id_language' => $this->context->language->id, - ]; - $this->context->smarty->assign('id_tab', 'advanced_tab'); - $html = $this->context->smarty->fetch($this->local_path . 'views/templates/admin/dummy/pre_tab.tpl'); - $html .= $this->renderFeedURLs(); - $html .= $helper->generateForm([$this->getConfigFormAdvanced()]); - $html .= $this->context->smarty->fetch($this->local_path . 'views/templates/admin/dummy/after_tab.tpl'); - - return $html; - } - - /** - * Render the feed url block - * - * @return string - */ - protected function renderFeedURLs() - { - $urls = []; - foreach (Language::getLanguages(true, $this->context->shop->id) as $lang) { - foreach (Currency::getCurrencies() as $cur) { - $currencyIso = Tools::strtoupper($cur['iso_code']); - $langIso = Tools::strtoupper($lang['iso_code']); - $urls[] = [ - 'url' => UrlManager::getFeedUrl($this->context->shop->id, $langIso, $currencyIso), - 'lang' => $langIso, - 'currency' => $currencyIso, - ]; - } - } - $this->context->smarty->assign('df_feed_urls', $urls); - - return $this->context->smarty->fetch($this->local_path . 'views/templates/admin/feed_url_partial_tab.tpl'); - } - - /** - * Get the fields of the advanced configuration form - * - * @return array - */ - protected function getConfigFormAdvanced() - { - return [ - 'form' => [ - 'legend' => [ - 'title' => $this->l('Advanced Options'), - 'icon' => 'icon-cogs', - ], - 'input' => [ - [ - 'type' => 'text', - 'label' => $this->l('Doofinder Api Key'), - 'name' => 'DF_API_KEY', - ], - [ - 'type' => 'text', - 'label' => $this->l('Region'), - 'name' => 'DF_REGION', - ], - [ - 'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'), - 'label' => $this->l('Enable v9 layer (Livelayer)'), - 'name' => 'DF_ENABLED_V9', - 'is_bool' => true, - 'values' => $this->getBooleanFormValue(), - ], - [ - 'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'), - 'label' => $this->l('Debug Mode. Write info logs in doofinder.log file'), - 'name' => 'DF_DEBUG', - 'is_bool' => true, - 'values' => $this->getBooleanFormValue(), - ], - [ - 'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'), - 'label' => $this->l('CURL disable HTTPS check'), - 'name' => 'DF_DSBL_HTTPS_CURL', - 'desc' => $this->l('CURL_DISABLE_HTTPS_EXPLANATION'), - 'is_bool' => true, - 'values' => $this->getBooleanFormValue(), - ], - [ - 'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'), - 'label' => $this->l('Debug CURL error response'), - 'name' => 'DF_DEBUG_CURL', - 'desc' => $this->l('To debug if your server has symptoms of connection problems'), - 'is_bool' => true, - 'values' => $this->getBooleanFormValue(), - ], - ], - 'submit' => [ - 'title' => $this->l('Save Internal Search Options'), - 'name' => 'submitDoofinderModuleAdvanced', - ], - ], - ]; - } - - /** - * Get the values for the advanced configuration form - * - * @return array - */ - protected function getConfigFormValuesAdvanced() - { - return [ - 'DF_API_KEY' => Configuration::get('DF_API_KEY'), - 'DF_REGION' => Configuration::get('DF_REGION'), - 'DF_ENABLED_V9' => Configuration::get('DF_ENABLED_V9'), - 'DF_DEBUG' => Configuration::get('DF_DEBUG'), - 'DF_DSBL_HTTPS_CURL' => Configuration::get('DF_DSBL_HTTPS_CURL'), - 'DF_DEBUG_CURL' => Configuration::get('DF_DEBUG_CURL'), - ]; - } - - /** - * Process the backoffice configuration form - * - * @return string - */ - protected function postProcess() - { - $form_values = []; - $formUpdated = ''; - $messages = ''; - - if ((bool) Tools::isSubmit('submitDoofinderModuleLaunchReindexing')) { - UpdateOnSave::indexApiInvokeReindexing(); - } - if (((bool) Tools::isSubmit('submitDoofinderModuleDataFeed')) == true) { - $form_values = array_merge($form_values, $this->getConfigFormValuesDataFeed()); - $formUpdated = 'data_feed_tab'; - } - if (((bool) Tools::isSubmit('submitDoofinderModuleSearchLayer')) == true) { - $form_values = array_merge($form_values, $this->getConfigFormValuesSearchLayer()); - $formUpdated = 'search_layer_tab'; - } - - if (((bool) Tools::isSubmit('submitDoofinderModuleAdvanced')) == true) { - $form_values = array_merge($form_values, $this->getConfigFormValuesAdvanced()); - $formUpdated = 'advanced_tab'; - $hashid = SearchEngine::getHashId(Context::getContext()->language->id, Context::getContext()->currency->id); - $apiKey = Configuration::get('DF_API_KEY'); - $dfApi = new DoofinderApi($hashid, $apiKey, false, ['apiVersion' => '5']); - $messages .= $dfApi->test([$this, 'l']); - $this->context->smarty->assign('adv', 1); - } - - $adminPanelView = new DoofinderAdminPanelView($this); - - foreach (array_keys($form_values) as $key) { - $postKey = str_replace(['[', ']'], '', $key); - $value = Tools::getValue($postKey); - - if (isset($form_values[$key]['real_config'])) { - $postKey = $form_values[$key]['real_config']; - } - if (is_array($value)) { - $value = implode(',', $value); - } - if ($postKey === 'DF_FEED_FULL_PATH') { - Configuration::updateValue('DF_FEED_MAINCATEGORY_PATH', 0); - } - $value = trim($value); - Configuration::updateValue($postKey, $value); - } - - if ($formUpdated == 'data_feed_tab') { - if ((bool) Configuration::get('DF_UPDATE_ON_SAVE_DELAY')) { - SearchEngine::setSearchEnginesByConfig(); - } - if (Tools::getValue('DF_UPDATE_ON_SAVE_DELAY') && (int) Tools::getValue('DF_UPDATE_ON_SAVE_DELAY') < 5) { - Configuration::updateValue('DF_UPDATE_ON_SAVE_DELAY', 5); - } - - $this->context->smarty->assign('text_data_changed', $this->l('You\'ve just changed a data feed option. It may be necessary to reprocess the index to apply these changes effectively.')); - $this->context->smarty->assign('text_reindex', $this->l('Launch reindexing')); - $msg = $this->context->smarty->fetch($this->local_path . 'views/templates/admin/reindex.tpl'); - $messages .= $adminPanelView->displayWarningCtm($msg, false, true); - } - - if (!empty($formUpdated)) { - $messages .= $adminPanelView->displayConfirmationCtm($this->l('Settings updated!')); - $this->context->smarty->assign('formUpdatedToClick', $formUpdated); - } - - return $messages; - } - /** * Sets the variables to assign to the template * @@ -863,22 +292,4 @@ private function configureHookCommon($params = false) ) ); } - - private function getBooleanFormValue() - { - $option = [ - [ - 'id' => 'active_on', - 'value' => true, - 'label' => $this->l('Enabled'), - ], - [ - 'id' => 'active_off', - 'value' => false, - 'label' => $this->l('Disabled'), - ], - ]; - - return $option; - } } diff --git a/feeds/product.php b/feeds/product.php index 2f3b407..59bff6b 100644 --- a/feeds/product.php +++ b/feeds/product.php @@ -14,6 +14,7 @@ */ use PrestaShop\Module\Doofinder\Lib\DfTools; +use PrestaShop\Module\Doofinder\Lib\DoofinderConstants; if (!defined('_PS_VERSION_')) { exit; @@ -123,19 +124,19 @@ function arrayMergeByIdProduct($array1 = [], $array2 = []) $cfg_short_description = (DfTools::cfg( $shop->id, 'DF_GS_DESCRIPTION_TYPE', - Doofinder::GS_SHORT_DESCRIPTION -) == Doofinder::GS_SHORT_DESCRIPTION); + DoofinderConstants::GS_SHORT_DESCRIPTION +) == DoofinderConstants::GS_SHORT_DESCRIPTION); $cfg_display_prices = DfTools::getBooleanFromRequest( 'prices', - (bool) DfTools::cfg($shop->id, 'DF_GS_DISPLAY_PRICES', Doofinder::YES) + (bool) DfTools::cfg($shop->id, 'DF_GS_DISPLAY_PRICES', DoofinderConstants::YES) ); $cfg_prices_w_taxes = DfTools::getBooleanFromRequest( 'taxes', - (bool) DfTools::cfg($shop->id, 'DF_GS_PRICES_USE_TAX', Doofinder::YES) + (bool) DfTools::cfg($shop->id, 'DF_GS_PRICES_USE_TAX', DoofinderConstants::YES) ); $cfg_image_size = DfTools::cfg($shop->id, 'DF_GS_IMAGE_SIZE'); -$cfg_mod_rewrite = DfTools::cfg($shop->id, 'PS_REWRITING_SETTINGS', Doofinder::YES); +$cfg_mod_rewrite = DfTools::cfg($shop->id, 'PS_REWRITING_SETTINGS', DoofinderConstants::YES); $cfg_product_variations = (int) DfTools::cfg($shop->id, 'DF_SHOW_PRODUCT_VARIATIONS'); $cfg_product_features = DfTools::cfg($shop->id, 'DF_SHOW_PRODUCT_FEATURES'); $cfg_debug = DfTools::cfg($shop->id, 'DF_DEBUG'); diff --git a/lib/DoofinderAdminPanelView.php b/lib/DoofinderAdminPanelView.php index d0c3fbb..103768b 100644 --- a/lib/DoofinderAdminPanelView.php +++ b/lib/DoofinderAdminPanelView.php @@ -21,13 +21,107 @@ class DoofinderAdminPanelView { + /* + Name an tab are required because they are using internally + by Helper class (/classes/helper/Helper.php) + */ + public $name; + public $tab; private $module; public function __construct($module) { + $this->name = $module->name; + $this->tab = $module->tab; $this->module = $module; } + /** + * Handles the module's configuration page + * + * @return string The page's HTML content + */ + public function getContent() + { + $stop = $this->getWarningMultishopHtml(); + $context = \Context::getContext(); + if ($stop) { + return $stop; + } + $adv = \Tools::getValue('adv', 0); + + $context->smarty->assign('adv', $adv); + + $formManager = new FormManager($this->module); + $msg = $formManager->postProcess(); + + $output = $msg; + $oldPS = false; + $context->controller->addJS($this->module->getPath() . 'views/js/admin-panel.js'); + + if (_PS_VERSION_ < 1.6) { + $oldPS = true; + $context->controller->addJS($this->module->getPath() . 'views/js/plugins/bootstrap.min.js'); + $context->controller->addCSS($this->module->getPath() . 'views/css/admin-theme_15.css'); + } + $configured = $this->isConfigured(); + $isNewShop = $this->showNewShopForm($context->shop); + $shopId = null; + if ($isNewShop) { + $shopId = $context->shop->id; + } + + $skipUrlParams = [ + 'skip' => 1, + 'configure' => $this->module->name, + 'tab_module' => $this->module->tab, + 'module_name' => $this->module->name, + ]; + $skipUrl = $context->link->getAdminLink('AdminModules', true); + $separator = strpos($skipUrl, '?') === false ? '?' : '&'; + $skipUrl .= $separator . http_build_query($skipUrlParams); + + $redirect = $context->shop->getBaseURL(true, false) . $this->module->getPath() . 'config.php'; + $token = \Tools::encrypt($redirect); + $paramsPopup = 'email=' . $context->employee->email . '&token=' . $token; + $dfEnabledV9 = \Configuration::get('DF_ENABLED_V9'); + + $context->smarty->assign('oldPS', $oldPS); + $context->smarty->assign('module_dir', $this->module->getPath()); + $context->smarty->assign('configured', $configured); + $context->smarty->assign('is_new_shop', $isNewShop); + $context->smarty->assign('shop_id', $shopId); + $context->smarty->assign('checkConnection', DoofinderConfig::checkOutsideConnection()); + $context->smarty->assign('tokenAjax', \Tools::encrypt('doofinder-ajax')); + $context->smarty->assign('skipurl', $skipUrl); + $context->smarty->assign('paramsPopup', $paramsPopup); + $context->smarty->assign('dfEnabledV9', $dfEnabledV9); + + $output .= $context->smarty->fetch(self::getLocalPath() . 'views/templates/admin/configure.tpl'); + if ($configured) { + $feedIndexed = \Configuration::get('DF_FEED_INDEXED', false); + if (empty($feedIndexed)) { + $controllerUrl = $context->link->getAdminLink('DoofinderAdmin', true) . '&ajax=1'; + $context->smarty->assign('update_feed_url', $controllerUrl . '&action=UpdateConfigurationField'); + $context->smarty->assign('check_feed_url', $controllerUrl . '&action=CheckConfigurationField'); + $output .= $context->smarty->fetch(self::getLocalPath() . 'views/templates/admin/indexation_status.tpl'); + } + + $output .= $context->smarty->fetch(self::getLocalPath() . 'views/templates/admin/configure_administration_panel.tpl'); + $output .= $this->renderFormDataFeed($adv); + if ($adv) { + $output .= $this->renderFormAdvanced(); + } + $advUrl = $context->link->getAdminLink('AdminModules', true) . '&adv=1' + . '&configure=' . $this->module->name . '&tab_module=' . $this->module->tab . '&module_name=' . $this->module->name; + $context->smarty->assign('adv_url', $advUrl); + } + + $output .= $context->smarty->fetch(self::getLocalPath() . 'views/templates/admin/configure_footer.tpl'); + + return $output; + } + public function getWarningMultishopHtml() { $stop = false; @@ -71,8 +165,402 @@ public static function displayGeneralMsg($string, $type, $alert, $link = false, return $context->smarty->fetch(self::getLocalPath() . 'views/templates/admin/display_msg.tpl'); } - private static function getLocalPath() + public static function getLocalPath() { return _PS_MODULE_DIR_ . DoofinderConstants::NAME . DIRECTORY_SEPARATOR; } + + /** + * Check if the form to create a store installation has to be displayed + * + * @param Shop $shop + * + * @return bool + */ + protected function showNewShopForm($shop) + { + $installationId = \Configuration::get('DF_INSTALLATION_ID', null, (int) $shop->id_shop_group, (int) $shop->id); + $multishopEnable = \Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE'); + $apiKey = \Configuration::get('DF_AI_APIKEY'); + + return !$installationId && $multishopEnable && $apiKey; + } + + /** + * Check if the module has already been configured + * + * @return bool + */ + protected function isConfigured() + { + $skip = \Tools::getValue('skip'); + if ($skip) { + \Configuration::updateValue('DF_ENABLE_HASH', 0); + \Configuration::updateValue('DF_ENABLED_V9', true); + } + $sql = 'SELECT id_configuration FROM ' . _DB_PREFIX_ . 'configuration WHERE name = \'DF_ENABLE_HASH\''; + + return \Db::getInstance()->getValue($sql); + } + + /** + * Render the data feed configuration form + * + * @param bool $adv + * + * @return string + */ + protected function renderFormDataFeed($adv = false) + { + $helper = new \HelperForm(); + $context = \Context::getContext(); + $helper->show_toolbar = false; + $helper->table = $this->module->getTable(); + $helper->module = $this->module; + $helper->default_form_language = $context->language->id; + $helper->allow_employee_form_lang = \Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0); + + $helper->identifier = $this->module->getIdentifier(); + // $helper->submit_action = 'submitDoofinderModuleDataFeed'; + $helper->currentIndex = $context->link->getAdminLink('AdminModules', false) + . (($adv) ? '&adv=1' : '') + . '&configure=' . $this->module->name . '&tab_module=' . $this->module->tab . '&module_name=' . $this->module->name; + $helper->token = \Tools::getAdminTokenLite('AdminModules'); + + $context->smarty->assign('id_tab', 'data_feed_tab'); + $html = $context->smarty->fetch(self::getLocalPath() . 'views/templates/admin/dummy/pre_tab.tpl'); + // Data feed form + $helper->tpl_vars = [ + 'fields_value' => DoofinderConfig::getConfigFormValuesDataFeed(), + 'languages' => $context->controller->getLanguages(), + 'id_language' => $context->language->id, + ]; + + if (!$this->showNewShopForm(\Context::getContext()->shop)) { + $validUpdateOnSave = UpdateOnSave::isValid(); + $html .= $helper->generateForm([$this->getConfigFormDataFeed($validUpdateOnSave)]); + // Search layer form + $helper->tpl_vars['fields_value'] = DoofinderConfig::getConfigFormValuesSearchLayer(); + $html .= $helper->generateForm([$this->getConfigFormSearchLayer()]); + } else { + $context->controller->warnings[] = $this->module->l("This shop is new and it hasn't been synchronized with Doofinder yet."); + } + $html .= $context->smarty->fetch(self::getLocalPath() . 'views/templates/admin/dummy/after_tab.tpl'); + + return $html; + } + + /** + * Render the advanced configuration form + * + * @return string + */ + protected function renderFormAdvanced() + { + $helper = new \HelperForm(); + $context = \Context::getContext(); + $helper->show_toolbar = false; + $helper->table = $this->module->getTable(); + $helper->module = $this; + $helper->default_form_language = $context->language->id; + $helper->allow_employee_form_lang = \Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0); + + $helper->identifier = $this->module->getIdentifier(); + // helper->submit_action = 'submitDoofinderModuleAdvanced'; + $helper->currentIndex = $context->link->getAdminLink('AdminModules', false) + . '&configure=' . $this->module->name . '&adv=1&tab_module=' . $this->module->tab . '&module_name=' . $this->module->name; + $helper->token = \Tools::getAdminTokenLite('AdminModules'); + + $helper->tpl_vars = [ + 'fields_value' => DoofinderConfig::getConfigFormValuesAdvanced(), + 'languages' => $context->controller->getLanguages(), + 'id_language' => $context->language->id, + ]; + $context->smarty->assign('id_tab', 'advanced_tab'); + $html = $context->smarty->fetch(self::getLocalPath() . 'views/templates/admin/dummy/pre_tab.tpl'); + $html .= $this->renderFeedURLs(); + $html .= $helper->generateForm([$this->getConfigFormAdvanced()]); + $html .= $context->smarty->fetch(self::getLocalPath() . 'views/templates/admin/dummy/after_tab.tpl'); + + return $html; + } + + /** + * Render the feed url block + * + * @return string + */ + protected function renderFeedURLs() + { + $urls = []; + $context = \Context::getContext(); + $languages = \Language::getLanguages(true, $context->shop->id); + foreach ($languages as $lang) { + foreach (\Currency::getCurrencies() as $cur) { + $currencyIso = \Tools::strtoupper($cur['iso_code']); + $langIso = \Tools::strtoupper($lang['iso_code']); + $urls[] = [ + 'url' => UrlManager::getFeedUrl($context->shop->id, $langIso, $currencyIso), + 'lang' => $langIso, + 'currency' => $currencyIso, + ]; + } + } + $context->smarty->assign('df_feed_urls', $urls); + + return $context->smarty->fetch(self::getLocalPath() . 'views/templates/admin/feed_url_partial_tab.tpl'); + } + + /** + * Get the fields of the data feed configuration form + * + * @return array + */ + protected function getConfigFormDataFeed($validUpdateOnSave = false) + { + $context = \Context::getContext(); + if ($validUpdateOnSave) { + $disabled = false; + $query = [ + 5 => ['id' => 5, 'name' => sprintf($this->module->l('Each %s minutes'), '5')], + 15 => ['id' => 15, 'name' => sprintf($this->module->l('Each %s minutes'), '15')], + 30 => ['id' => 30, 'name' => sprintf($this->module->l('Each %s minutes'), '30')], + 60 => ['id' => 60, 'name' => $this->module->l('Each hour')], + 120 => ['id' => 120, 'name' => sprintf($this->module->l('Each %s hours'), '2')], + 360 => ['id' => 360, 'name' => sprintf($this->module->l('Each %s hours'), '6')], + 720 => ['id' => 720, 'name' => sprintf($this->module->l('Each %s hours'), '12')], + 1440 => ['id' => 1440, 'name' => $this->module->l('Once a day')], + 0 => ['id' => 0, 'name' => $this->module->l('Disabled')], + ]; + } else { + $disabled = true; + $query = [ + 0 => ['id' => 0, 'name' => $this->module->l('Disabled')], + ]; + } + + return [ + 'form' => [ + 'legend' => [ + 'title' => $this->module->l('Data Feed'), + ], + 'input' => [ + [ + 'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'), + 'label' => $this->module->l('Index product prices'), + 'desc' => $this->module->l('If you activate this option you will be able to show the prices of each product in the search results.'), + 'name' => 'DF_GS_DISPLAY_PRICES', + 'is_bool' => true, + 'values' => $this->getBooleanFormValue(), + ], + [ + 'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'), + 'label' => $this->module->l('Show product prices including taxes'), + 'desc' => $this->module->l('If you activate this option, the price of the products that will be displayed will be inclusive of taxes.'), + 'name' => 'DF_GS_PRICES_USE_TAX', + 'is_bool' => true, + 'values' => $this->getBooleanFormValue(), + ], + [ + 'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'), + 'label' => $this->module->l('Index the full path of the product category'), + 'name' => 'DF_FEED_FULL_PATH', + 'is_bool' => true, + 'values' => $this->getBooleanFormValue(), + ], + [ + 'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'), + 'label' => $this->module->l('Index product attribute combinations'), + 'name' => 'DF_SHOW_PRODUCT_VARIATIONS', + 'is_bool' => true, + 'values' => $this->getBooleanFormValue(), + ], + [ + 'type' => 'select', + 'label' => $this->module->l('Define which combinations of product attributes you want to index for'), + 'name' => 'DF_GROUP_ATTRIBUTES_SHOWN', + 'multiple' => true, + 'options' => [ + 'query' => \AttributeGroup::getAttributesGroups(\Context::getContext()->language->id), + 'id' => 'id_attribute_group', + 'name' => 'name', + ], + ], + [ + 'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'), + 'label' => $this->module->l('Index customized product features'), + 'name' => 'DF_SHOW_PRODUCT_FEATURES', + 'is_bool' => true, + 'values' => $this->getBooleanFormValue(), + ], + [ + 'type' => 'select', + 'label' => $this->module->l('Select features will be shown in feed'), + 'name' => 'DF_FEATURES_SHOWN', + 'multiple' => true, + 'options' => [ + 'query' => \Feature::getFeatures( + $context->language->id, + $context->shop->id + ), + 'id' => 'id_feature', + 'name' => 'name', + ], + ], + [ + 'type' => 'select', + 'label' => $this->module->l('Product Image Size'), + 'name' => 'DF_GS_IMAGE_SIZE', + 'options' => [ + 'query' => DfTools::getAvailableImageSizes(), + 'id' => 'DF_GS_IMAGE_SIZE', + 'name' => 'name', + ], + ], + [ + 'type' => 'select', + 'label' => $this->module->l('Automatically process modified products'), + 'desc' => $this->module->l('This action will only be executed if there are changes. If you see the field disabled, it is because you are making a usage in the indexes that is not supported by the automatic processing of modified products.'), + 'name' => 'DF_UPDATE_ON_SAVE_DELAY', + 'disabled' => $disabled, + 'options' => [ + 'query' => $query, + 'id' => 'id', + 'name' => 'name', + ], + ], + ], + 'submit' => [ + 'title' => $this->module->l('Save Data Feed Options'), + 'name' => 'submitDoofinderModuleDataFeed', + ], + ], + ]; + } + + /** + * Get the fields of the search layer configuration form + * + * @return array + */ + protected function getConfigFormSearchLayer() + { + $inputs = [ + [ + 'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'), + 'label' => $this->module->l('Doofinder search layer'), + 'name' => 'DF_SHOW_LAYER', + 'is_bool' => true, + 'values' => $this->getBooleanFormValue(), + ], + [ + 'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'), + 'label' => $this->module->l('Doofinder search layer in mobile version'), + 'name' => 'DF_SHOW_LAYER_MOBILE', + 'is_bool' => true, + 'values' => $this->getBooleanFormValue(), + ], + [ + 'type' => 'text', + 'label' => $this->module->l('Doofinder Store ID'), + 'name' => 'DF_INSTALLATION_ID', + 'desc' => $this->module->l('INSTALLATION_ID_EXPLANATION'), + 'lang' => false, + ], + ]; + + return [ + 'form' => [ + 'legend' => [ + 'title' => $this->module->l('Search Layer'), + ], + 'input' => $inputs, + 'submit' => [ + 'title' => $this->module->l('Save Layer Widget Options'), + 'name' => 'submitDoofinderModuleSearchLayer', + ], + ], + ]; + } + + /** + * Get the fields of the advanced configuration form + * + * @return array + */ + protected function getConfigFormAdvanced() + { + return [ + 'form' => [ + 'legend' => [ + 'title' => $this->module->l('Advanced Options'), + 'icon' => 'icon-cogs', + ], + 'input' => [ + [ + 'type' => 'text', + 'label' => $this->module->l('Doofinder Api Key'), + 'name' => 'DF_API_KEY', + ], + [ + 'type' => 'text', + 'label' => $this->module->l('Region'), + 'name' => 'DF_REGION', + ], + [ + 'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'), + 'label' => $this->module->l('Enable v9 layer (Livelayer)'), + 'name' => 'DF_ENABLED_V9', + 'is_bool' => true, + 'values' => $this->getBooleanFormValue(), + ], + [ + 'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'), + 'label' => $this->module->l('Debug Mode. Write info logs in doofinder.log file'), + 'name' => 'DF_DEBUG', + 'is_bool' => true, + 'values' => $this->getBooleanFormValue(), + ], + [ + 'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'), + 'label' => $this->module->l('CURL disable HTTPS check'), + 'name' => 'DF_DSBL_HTTPS_CURL', + 'desc' => $this->module->l('CURL_DISABLE_HTTPS_EXPLANATION'), + 'is_bool' => true, + 'values' => $this->getBooleanFormValue(), + ], + [ + 'type' => (version_compare(_PS_VERSION_, '1.6.0', '>=') ? 'switch' : 'radio'), + 'label' => $this->module->l('Debug CURL error response'), + 'name' => 'DF_DEBUG_CURL', + 'desc' => $this->module->l('To debug if your server has symptoms of connection problems'), + 'is_bool' => true, + 'values' => $this->getBooleanFormValue(), + ], + ], + 'submit' => [ + 'title' => $this->module->l('Save Internal Search Options'), + 'name' => 'submitDoofinderModuleAdvanced', + ], + ], + ]; + } + + private function getBooleanFormValue() + { + $option = [ + [ + 'id' => 'active_on', + 'value' => true, + 'label' => $this->module->l('Enabled'), + ], + [ + 'id' => 'active_off', + 'value' => false, + 'label' => $this->module->l('Disabled'), + ], + ]; + + return $option; + } } diff --git a/lib/DoofinderConfig.php b/lib/DoofinderConfig.php index 8cd9882..134ebcb 100644 --- a/lib/DoofinderConfig.php +++ b/lib/DoofinderConfig.php @@ -104,4 +104,71 @@ public static function saveApiConfig($apiKey, $apiEndpoint, $adminEndpoint) \Configuration::updateValue('DF_API_KEY', $region . '-' . $apiKey, false, $sgid, $sid); } } + + /** + * Get the values for the data feed configuration form + * + * @return array + */ + public static function getConfigFormValuesDataFeed() + { + return [ + 'DF_GS_DISPLAY_PRICES' => \Configuration::get('DF_GS_DISPLAY_PRICES'), + 'DF_GS_PRICES_USE_TAX' => \Configuration::get('DF_GS_PRICES_USE_TAX'), + 'DF_FEED_FULL_PATH' => \Configuration::get('DF_FEED_FULL_PATH'), + 'DF_SHOW_PRODUCT_VARIATIONS' => \Configuration::get('DF_SHOW_PRODUCT_VARIATIONS'), + 'DF_GROUP_ATTRIBUTES_SHOWN[]' => explode(',', \Configuration::get('DF_GROUP_ATTRIBUTES_SHOWN')), + 'DF_SHOW_PRODUCT_FEATURES' => \Configuration::get('DF_SHOW_PRODUCT_FEATURES'), + 'DF_FEATURES_SHOWN[]' => explode(',', \Configuration::get('DF_FEATURES_SHOWN')), + 'DF_GS_IMAGE_SIZE' => \Configuration::get('DF_GS_IMAGE_SIZE'), + 'DF_UPDATE_ON_SAVE_DELAY' => \Configuration::get('DF_UPDATE_ON_SAVE_DELAY'), + ]; + } + + /** + * Get the values for the search layer configuration form + * + * @return array + */ + public static function getConfigFormValuesSearchLayer() + { + $fields = []; + $fields['DF_INSTALLATION_ID'] = \Configuration::get('DF_INSTALLATION_ID'); + $fields['DF_SHOW_LAYER'] = \Configuration::get('DF_SHOW_LAYER', null, null, null, true); + $fields['DF_SHOW_LAYER_MOBILE'] = \Configuration::get('DF_SHOW_LAYER_MOBILE', null, null, null, true); + + return $fields; + } + + /** + * Get the values for the advanced configuration form + * + * @return array + */ + public static function getConfigFormValuesAdvanced() + { + return [ + 'DF_API_KEY' => \Configuration::get('DF_API_KEY'), + 'DF_REGION' => \Configuration::get('DF_REGION'), + 'DF_ENABLED_V9' => \Configuration::get('DF_ENABLED_V9'), + 'DF_DEBUG' => \Configuration::get('DF_DEBUG'), + 'DF_DSBL_HTTPS_CURL' => \Configuration::get('DF_DSBL_HTTPS_CURL'), + 'DF_DEBUG_CURL' => \Configuration::get('DF_DEBUG_CURL'), + ]; + } + + /** + * Checks the connection with DooManager + * + * @return bool + */ + public static function checkOutsideConnection() + { + $client = new EasyREST(true, 3); + $doomanangerRegionlessUrl = sprintf(DoofinderConstants::DOOMANAGER_REGION_URL, ''); + $result = $client->get(sprintf('%s/auth/login', $doomanangerRegionlessUrl)); + + return $result && $result->originalResponse && isset($result->headers['code']) + && (strpos($result->originalResponse, 'HTTP/2 200') || $result->headers['code'] == 200); + } } diff --git a/lib/FormManager.php b/lib/FormManager.php new file mode 100644 index 0000000..0d890ce --- /dev/null +++ b/lib/FormManager.php @@ -0,0 +1,105 @@ +module = $module; + } + + /** + * Process the backoffice configuration form + * + * @return string + */ + public function postProcess() + { + $formValues = []; + $formUpdated = ''; + $messages = ''; + $context = \Context::getContext(); + + if ((bool) \Tools::isSubmit('submitDoofinderModuleLaunchReindexing')) { + UpdateOnSave::indexApiInvokeReindexing(); + } + if (((bool) \Tools::isSubmit('submitDoofinderModuleDataFeed')) == true) { + $formValues = array_merge($formValues, DoofinderConfig::getConfigFormValuesDataFeed()); + $formUpdated = 'data_feed_tab'; + } + if (((bool) \Tools::isSubmit('submitDoofinderModuleSearchLayer')) == true) { + $formValues = array_merge($formValues, DoofinderConfig::getConfigFormValuesSearchLayer()); + $formUpdated = 'search_layer_tab'; + } + + if (((bool) \Tools::isSubmit('submitDoofinderModuleAdvanced')) == true) { + $formValues = array_merge($formValues, DoofinderConfig::getConfigFormValuesAdvanced()); + $formUpdated = 'advanced_tab'; + $hashid = SearchEngine::getHashId($context->language->id, $context->currency->id); + $apiKey = \Configuration::get('DF_API_KEY'); + $dfApi = new DoofinderApi($hashid, $apiKey, false, ['apiVersion' => '5']); + $messages .= $dfApi->test([$this->module, 'l']); + $context->smarty->assign('adv', 1); + } + + $adminPanelView = new DoofinderAdminPanelView($this->module); + + foreach (array_keys($formValues) as $key) { + $postKey = str_replace(['[', ']'], '', $key); + $value = \Tools::getValue($postKey); + + if (isset($formValues[$key]['real_config'])) { + $postKey = $formValues[$key]['real_config']; + } + if (is_array($value)) { + $value = implode(',', $value); + } + if ($postKey === 'DF_FEED_FULL_PATH') { + \Configuration::updateValue('DF_FEED_MAINCATEGORY_PATH', 0); + } + $value = trim($value); + \Configuration::updateValue($postKey, $value); + } + + if ($formUpdated == 'data_feed_tab') { + if ((bool) \Configuration::get('DF_UPDATE_ON_SAVE_DELAY')) { + SearchEngine::setSearchEnginesByConfig(); + } + if (\Tools::getValue('DF_UPDATE_ON_SAVE_DELAY') && (int) \Tools::getValue('DF_UPDATE_ON_SAVE_DELAY') < 5) { + \Configuration::updateValue('DF_UPDATE_ON_SAVE_DELAY', 5); + } + + $context->smarty->assign('text_data_changed', $this->module->l('You\'ve just changed a data feed option. It may be necessary to reprocess the index to apply these changes effectively.')); + $context->smarty->assign('text_reindex', $this->module->l('Launch reindexing')); + $msg = $context->smarty->fetch(DoofinderAdminPanelView::getLocalPath() . 'views/templates/admin/reindex.tpl'); + $messages .= $adminPanelView->displayWarningCtm($msg, false, true); + } + + if (!empty($formUpdated)) { + $messages .= $adminPanelView->displayConfirmationCtm($this->module->l('Settings updated!')); + $context->smarty->assign('formUpdatedToClick', $formUpdated); + } + + return $messages; + } +}