From 5c380612164a2a0974c6fc76599f9fb258e3ec1a Mon Sep 17 00:00:00 2001 From: Julius Zukauskas Date: Wed, 28 Jun 2023 16:52:47 +0300 Subject: [PATCH] add form modifications for feature-value migrated form --- src/Form/FeatureValue/FormDataProvider.php | 75 +++++++++++++++ src/Form/FeatureValue/FormModifier.php | 103 +++++++++++++++++++++ src/Hook/FeatureValue.php | 86 +++++++++++++++++ 3 files changed, 264 insertions(+) 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..23b9b317a --- /dev/null +++ b/src/Form/FeatureValue/FormModifier.php @@ -0,0 +1,103 @@ + + * @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\SwitchType; +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..eb75b7024 100644 --- a/src/Hook/FeatureValue.php +++ b/src/Hook/FeatureValue.php @@ -21,7 +21,10 @@ namespace PrestaShop\Module\FacetedSearch\Hook; use Language; +use PrestaShop\Module\FacetedSearch\Form\FeatureValue\FormDataProvider; +use PrestaShop\Module\FacetedSearch\Form\FeatureValue\FormModifier; use Tools; +use Ps_Facetedsearch; class FeatureValue extends AbstractHook { @@ -30,8 +33,56 @@ class FeatureValue extends AbstractHook 'actionFeatureValueDelete', 'displayFeatureValueForm', 'displayFeatureValuePostProcess', + 'actionFeatureValueFormBuilderModifier', + 'actionAfterCreateFeatureValueFormHandler', + 'actionAfterUpdateFeatureValueFormHandler', ]; + 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->isMigratedPage = true; + $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 +177,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(); + } }