From df193fc4af7691607f9c82e6aaf3975048fa5243 Mon Sep 17 00:00:00 2001 From: Julius Zukauskas Date: Wed, 28 Jun 2023 16:52:47 +0300 Subject: [PATCH 1/3] add form modifications for feature-value migrated form --- src/Form/FeatureValue/FormDataProvider.php | 75 +++++++++++++ src/Form/FeatureValue/FormModifier.php | 102 ++++++++++++++++++ src/Hook/FeatureValue.php | 95 ++++++++++++++++ .../php/FacetedSearch/HookDispatcherTest.php | 5 +- 4 files changed, 276 insertions(+), 1 deletion(-) create mode 100644 src/Form/FeatureValue/FormDataProvider.php create mode 100644 src/Form/FeatureValue/FormModifier.php diff --git a/src/Form/FeatureValue/FormDataProvider.php b/src/Form/FeatureValue/FormDataProvider.php new file mode 100644 index 000000000..6a3607b1d --- /dev/null +++ b/src/Form/FeatureValue/FormDataProvider.php @@ -0,0 +1,75 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) + */ + +namespace PrestaShop\Module\FacetedSearch\Form\FeatureValue; + +use Db; + +/** + * Provides form data + */ +class FormDataProvider +{ + /** + * @var Db + */ + private $database; + + public function __construct(Db $database) + { + $this->database = $database; + } + + /** + * Fills form data + * + * @param array $params + * + * @return array + */ + public function getData(array $params) + { + $defaultUrl = []; + $defaultMetaTitle = []; + + // if params contains id, gets data for edit form + if (!empty($params['id'])) { + $featureValueId = (int) $params['id']; + + $result = $this->database->executeS( + 'SELECT `url_name`, `meta_title`, `id_lang` ' . + 'FROM ' . _DB_PREFIX_ . 'layered_indexable_feature_value_lang_value ' . + 'WHERE `id_feature_value` = ' . $featureValueId + ); + + if (!empty($result) && is_array($result)) { + foreach ($result as $data) { + $defaultUrl[$data['id_lang']] = $data['url_name']; + $defaultMetaTitle[$data['id_lang']] = $data['meta_title']; + } + } + } + + return [ + 'url' => $defaultUrl, + 'meta_title' => $defaultMetaTitle, + ]; + } +} diff --git a/src/Form/FeatureValue/FormModifier.php b/src/Form/FeatureValue/FormModifier.php new file mode 100644 index 000000000..271f443e2 --- /dev/null +++ b/src/Form/FeatureValue/FormModifier.php @@ -0,0 +1,102 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) + */ + +namespace PrestaShop\Module\FacetedSearch\Form\FeatureValue; + +use Context; +use PrestaShop\Module\FacetedSearch\Constraint\UrlSegment; +use PrestaShopBundle\Form\Admin\Type\TranslatableType; +use PrestaShopBundle\Translation\TranslatorComponent; +use Symfony\Component\Form\FormBuilderInterface; + +/** + * Adds module specific fields to BO form + */ +class FormModifier +{ + /** + * @var Context + */ + private $context; + + public function __construct(Context $context) + { + $this->context = $context; + } + + public function modify( + FormBuilderInterface $formBuilder, + array $data + ) { + /** + * @var TranslatorComponent + */ + $translator = $this->context->getTranslator(); + $invalidCharsHint = $translator->trans( + 'Invalid characters: <>;=#{}_', + [], + 'Modules.Facetedsearch.Admin' + ); + + $urlTip = $translator->trans( + 'When the Faceted Search module is enabled, you can get more detailed URLs by choosing ' . + 'the word that best represents this feature. By default, PrestaShop uses the ' . + 'feature\'s value, but you can change that setting using this field.', + [], + 'Modules.Facetedsearch.Admin' + ); + $metaTitleTip = $translator->trans( + 'When the Faceted Search module is enabled, you can get more detailed page titles by ' . + 'choosing the word that best represents this feature. By default, PrestaShop uses the ' . + 'feature\'s value, but you can change that setting using this field.', + [], + 'Modules.Facetedsearch.Admin' + ); + + $formBuilder + ->add( + 'url_name', + TranslatableType::class, + [ + 'required' => false, + 'label' => $translator->trans('URL', [], 'Modules.Facetedsearch.Admin'), + 'help' => $urlTip . ' ' . $invalidCharsHint, + 'options' => [ + 'constraints' => [ + new UrlSegment([ + 'message' => $translator->trans('%s is invalid.', [], 'Admin.Notifications.Error'), + ]), + ], + ], + 'data' => $data['url'], + ] + ) + ->add( + 'meta_title', + TranslatableType::class, + [ + 'required' => false, + 'label' => $translator->trans('Meta title', [], 'Modules.Facetedsearch.Admin'), + 'help' => $metaTitleTip, + 'data' => $data['meta_title'], + ] + ); + } +} diff --git a/src/Hook/FeatureValue.php b/src/Hook/FeatureValue.php index c676ef406..775b59479 100644 --- a/src/Hook/FeatureValue.php +++ b/src/Hook/FeatureValue.php @@ -21,6 +21,9 @@ namespace PrestaShop\Module\FacetedSearch\Hook; use Language; +use PrestaShop\Module\FacetedSearch\Form\FeatureValue\FormDataProvider; +use PrestaShop\Module\FacetedSearch\Form\FeatureValue\FormModifier; +use Ps_Facetedsearch; use Tools; class FeatureValue extends AbstractHook @@ -30,8 +33,65 @@ class FeatureValue extends AbstractHook 'actionFeatureValueDelete', 'displayFeatureValueForm', 'displayFeatureValuePostProcess', + 'actionFeatureValueFormBuilderModifier', + 'actionAfterCreateFeatureValueFormHandler', + 'actionAfterUpdateFeatureValueFormHandler', ]; + /** + * @var FormModifier + */ + private $formModifier; + + /** + * @var FormDataProvider + */ + private $dataProvider; + + public function __construct(Ps_Facetedsearch $module) + { + parent::__construct($module); + + $this->formModifier = new FormModifier($module->getContext()); + $this->dataProvider = new FormDataProvider($module->getDatabase()); + } + + /** + * Hook for modifying feature form formBuilder + * + * @since PrestaShop 9.0 + * + * @param array $params + */ + public function actionFeatureValueFormBuilderModifier(array $params) + { + $this->formModifier->modify($params['form_builder'], $this->dataProvider->getData($params)); + } + + /** + * Hook after create feature. + * + * @since PrestaShop 9.0 + * + * @param array $params + */ + public function actionAfterCreateFeatureValueFormHandler(array $params) + { + $this->save($params['id'], $params['form_data']); + } + + /** + * Hook after update feature. + * + * @since PrestaShop 9.0 + * + * @param array $params + */ + public function actionAfterUpdateFeatureValueFormHandler(array $params) + { + $this->save($params['id'], $params['form_data']); + } + /** * After save feature value * @@ -126,4 +186,39 @@ public function displayFeatureValueForm(array $params) return $this->module->render('feature_value_form.tpl'); } + + private function save($featureValueId, array $formData) + { + $featureValueId = (int) $featureValueId; + $this->database->execute( + 'DELETE FROM ' . _DB_PREFIX_ . 'layered_indexable_feature_value_lang_value + WHERE `id_feature_value` = ' . $featureValueId + ); + + $query = 'INSERT INTO ' . _DB_PREFIX_ . 'layered_indexable_feature_value_lang_value ' . + '(`id_feature_value`, `id_lang`, `url_name`, `meta_title`) ' . + 'VALUES (%d, %d, \'%s\', \'%s\')'; + + foreach (Language::getLanguages(false) as $language) { + $langId = (int) $language['id_lang']; + $metaTitle = pSQL($formData['meta_title'][$langId]); + $seoUrl = $formData['url_name'][$langId]; + + if (!empty($seoUrl)) { + $seoUrl = pSQL(Tools::str2url($seoUrl)); + } + + $this->database->execute( + sprintf( + $query, + $featureValueId, + $langId, + $seoUrl, + $metaTitle + ) + ); + } + + $this->module->invalidateLayeredFilterBlockCache(); + } } diff --git a/tests/php/FacetedSearch/HookDispatcherTest.php b/tests/php/FacetedSearch/HookDispatcherTest.php index 4246c88ad..03ab968de 100644 --- a/tests/php/FacetedSearch/HookDispatcherTest.php +++ b/tests/php/FacetedSearch/HookDispatcherTest.php @@ -47,7 +47,7 @@ protected function setUp() public function testGetAvailableHooks() { - $this->assertCount(28, $this->dispatcher->getAvailableHooks()); + $this->assertCount(31, $this->dispatcher->getAvailableHooks()); $this->assertEquals( [ 'actionAttributeGroupDelete', @@ -70,6 +70,9 @@ public function testGetAvailableHooks() 'actionFeatureFormBuilderModifier', 'actionAfterCreateFeatureFormHandler', 'actionAfterUpdateFeatureFormHandler', + 'actionFeatureValueFormBuilderModifier', + 'actionAfterCreateFeatureValueFormHandler', + 'actionAfterUpdateFeatureValueFormHandler', 'actionFeatureValueSave', 'actionFeatureValueDelete', 'displayFeatureValueForm', From 57922811aae56ebc3f727653236dc0d6e0ca6060 Mon Sep 17 00:00:00 2001 From: Julius Zukauskas Date: Thu, 29 Jun 2023 18:16:40 +0300 Subject: [PATCH 2/3] fix unit test --- tests/php/FacetedSearch/HookDispatcherTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/php/FacetedSearch/HookDispatcherTest.php b/tests/php/FacetedSearch/HookDispatcherTest.php index 03ab968de..ccf5e3c32 100644 --- a/tests/php/FacetedSearch/HookDispatcherTest.php +++ b/tests/php/FacetedSearch/HookDispatcherTest.php @@ -70,13 +70,13 @@ public function testGetAvailableHooks() 'actionFeatureFormBuilderModifier', 'actionAfterCreateFeatureFormHandler', 'actionAfterUpdateFeatureFormHandler', - 'actionFeatureValueFormBuilderModifier', - 'actionAfterCreateFeatureValueFormHandler', - 'actionAfterUpdateFeatureValueFormHandler', 'actionFeatureValueSave', 'actionFeatureValueDelete', 'displayFeatureValueForm', 'displayFeatureValuePostProcess', + 'actionFeatureValueFormBuilderModifier', + 'actionAfterCreateFeatureValueFormHandler', + 'actionAfterUpdateFeatureValueFormHandler', 'actionProductSave', 'productSearchProvider', 'actionObjectSpecificPriceRuleUpdateBefore', From 8b27ae6c7073fd067170529ccc8e38b2f6fddd54 Mon Sep 17 00:00:00 2001 From: Julius Zukauskas Date: Fri, 30 Jun 2023 13:13:54 +0300 Subject: [PATCH 3/3] upgrade to 3.13.0 --- config.xml | 2 +- ps_facetedsearch.php | 2 +- upgrade/upgrade-3.13.0.php | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 upgrade/upgrade-3.13.0.php diff --git a/config.xml b/config.xml index b770a6676..765c257b5 100644 --- a/config.xml +++ b/config.xml @@ -2,7 +2,7 @@ ps_facetedsearch - + diff --git a/ps_facetedsearch.php b/ps_facetedsearch.php index 4cd9310ea..b21d3aacb 100644 --- a/ps_facetedsearch.php +++ b/ps_facetedsearch.php @@ -96,7 +96,7 @@ public function __construct() { $this->name = 'ps_facetedsearch'; $this->tab = 'front_office_features'; - $this->version = '3.12.1'; + $this->version = '3.13.0'; $this->author = 'PrestaShop'; $this->need_instance = 0; $this->bootstrap = true; diff --git a/upgrade/upgrade-3.13.0.php b/upgrade/upgrade-3.13.0.php new file mode 100644 index 000000000..5a8bc80ca --- /dev/null +++ b/upgrade/upgrade-3.13.0.php @@ -0,0 +1,33 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License 3.0 (AFL-3.0) + */ +if (!defined('_PS_VERSION_')) { + exit; +} + +function upgrade_module_3_13_0(Ps_Facetedsearch $module) +{ + $newHooks = [ + 'actionFeatureValueFormBuilderModifier', + 'actionAfterCreateFeatureValueFormHandler', + 'actionAfterUpdateFeatureValueFormHandler', + ]; + + return $module->registerHook($newHooks); +}