diff --git a/config.xml b/config.xml index 07a332015..b32333baa 100644 --- a/config.xml +++ b/config.xml @@ -2,7 +2,7 @@ ps_facetedsearch - + diff --git a/ps_facetedsearch.php b/ps_facetedsearch.php index c3e618e43..f3e1d12e5 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.14.1'; + $this->version = '3.15.0'; $this->author = 'PrestaShop'; $this->need_instance = 0; $this->bootstrap = true; diff --git a/src/Adapter/index.php b/src/Adapter/index.php new file mode 100644 index 000000000..88355f610 --- /dev/null +++ b/src/Adapter/index.php @@ -0,0 +1,11 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + */ + +declare(strict_types=1); + +namespace PrestaShop\Module\FacetedSearch\Form\Attribute; + +use Db; +use PrestaShopDatabaseException; + +class FormDataProvider +{ + /** + * @var Db + */ + private $database; + + public function __construct(Db $database) + { + $this->database = $database; + } + + /** + * Fills form data + * + * @param array $params + * + * @return array + * + * @throws PrestaShopDatabaseException + */ + public function getData(array $params) + { + $defaultUrl = []; + $defaultMetaTitle = []; + + // if params contains id, gets data for edit form + if (!empty($params['id'])) { + $attributeId = (int) $params['id']; + $result = $this->database->executeS( + 'SELECT `url_name`, `meta_title`, `id_lang` ' . + 'FROM ' . _DB_PREFIX_ . 'layered_indexable_attribute_lang_value ' . + 'WHERE `id_attribute` = ' . $attributeId + ); + + 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_name' => $defaultUrl, + 'meta_title' => $defaultMetaTitle, + ]; + } +} diff --git a/src/Form/Attribute/FormModifier.php b/src/Form/Attribute/FormModifier.php new file mode 100644 index 000000000..dec8431fc --- /dev/null +++ b/src/Form/Attribute/FormModifier.php @@ -0,0 +1,95 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + */ + +declare(strict_types=1); + +namespace PrestaShop\Module\FacetedSearch\Form\Attribute; + +use PrestaShop\Module\FacetedSearch\Constraint\UrlSegment; +use PrestaShopBundle\Form\Admin\Type\TranslatableType; +use PrestaShopBundle\Translation\TranslatorComponent; +use Symfony\Component\Form\FormBuilderInterface; + +class FormModifier +{ + /** + * @var TranslatorComponent + */ + private $translator; + + public function __construct(TranslatorComponent $translator) + { + $this->translator = $translator; + } + + public function modify(FormBuilderInterface $formBuilder) + { + $invalidCharsHint = $this->translator->trans( + 'Invalid characters: <>;=#{}_', + [], + 'Modules.Facetedsearch.Admin' + ); + + $urlTip = $this->translator->trans( + 'When the Faceted Search module is enabled, you can get more detailed URLs by choosing the word that best represent this attribute. By default, PrestaShop uses the attribute\'s name, but you can change that setting using this field.', + [], + 'Modules.Facetedsearch.Admin' + ); + $metaTitleTip = $this->translator->trans( + 'When the Faceted Search module is enabled, you can get more detailed page titles by choosing the word that best represent this attribute. By default, PrestaShop uses the attribute\'s name, but you can change that setting using this field.', + [], + 'Modules.Facetedsearch.Admin' + ); + + $formBuilder + ->add( + 'url_name', + TranslatableType::class, + [ + 'required' => false, + 'label' => $this->translator->trans('URL', [], 'Modules.Facetedsearch.Admin'), + 'help' => $urlTip . ' ' . $invalidCharsHint, + 'options' => [ + 'constraints' => [ + new UrlSegment([ + 'message' => $this->translator->trans('%s is invalid.', [], 'Admin.Notifications.Error'), + ]), + ], + ], + ] + ) + ->add( + 'meta_title', + TranslatableType::class, + [ + 'required' => false, + 'label' => $this->translator->trans('Meta title', [], 'Modules.Facetedsearch.Admin'), + 'help' => $metaTitleTip, + ] + ) + ; + } +} diff --git a/src/Form/AttributeGroup/FormDataProvider.php b/src/Form/AttributeGroup/FormDataProvider.php new file mode 100644 index 000000000..38afb5a5e --- /dev/null +++ b/src/Form/AttributeGroup/FormDataProvider.php @@ -0,0 +1,93 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + */ + +declare(strict_types=1); + +namespace PrestaShop\Module\FacetedSearch\Form\AttributeGroup; + +use Db; +use PrestaShopDatabaseException; + +class FormDataProvider +{ + /** + * @var Db + */ + private $database; + + public function __construct(Db $database) + { + $this->database = $database; + } + + /** + * Fills form data + * + * @param array $params + * + * @return array + * + * @throws PrestaShopDatabaseException + */ + public function getData(array $params) + { + $defaultUrl = []; + $defaultMetaTitle = []; + $isIndexable = false; + + // if params contains id, gets data for edit form + if (!empty($params['id'])) { + $attributeGroupId = (int) $params['id']; + + // returns false if request failed. + $queryIndexable = $this->database->getValue( + 'SELECT `indexable` ' . + 'FROM ' . _DB_PREFIX_ . 'layered_indexable_attribute_group ' . + 'WHERE `id_attribute_group` = ' . $attributeGroupId + ); + + $isIndexable = (bool) $queryIndexable; + $result = $this->database->executeS( + 'SELECT `url_name`, `meta_title`, `id_lang` ' . + 'FROM ' . _DB_PREFIX_ . 'layered_indexable_attribute_group_lang_value ' . + 'WHERE `id_attribute_group` = ' . $attributeGroupId + ); + + 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_name' => $defaultUrl, + 'meta_title' => $defaultMetaTitle, + 'is_indexable' => $isIndexable, + ]; + } +} diff --git a/src/Form/AttributeGroup/FormModifier.php b/src/Form/AttributeGroup/FormModifier.php new file mode 100644 index 000000000..e7a8d6cce --- /dev/null +++ b/src/Form/AttributeGroup/FormModifier.php @@ -0,0 +1,121 @@ + + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) + */ + +declare(strict_types=1); + +namespace PrestaShop\Module\FacetedSearch\Form\AttributeGroup; + +use PrestaShop\Module\FacetedSearch\Constraint\UrlSegment; +use PrestaShop\PrestaShop\Core\Exception\CoreException; +use PrestaShopBundle\Form\Admin\Type\SwitchType; +use PrestaShopBundle\Form\Admin\Type\TranslatableType; +use PrestaShopBundle\Translation\TranslatorComponent; +use Symfony\Component\Form\FormBuilderInterface; + +class FormModifier +{ + /** + * @var TranslatorComponent + */ + private $translator; + + public function __construct(TranslatorComponent $translator) + { + $this->translator = $translator; + } + + public function modify(FormBuilderInterface $formBuilder) + { + // Dynamically check the class and instanciate it, this avoids the module from requiring PrestaShop 1.7.8 minimum, + // besides this code is not supposed to be called in older versions + if (!class_exists('\PrestaShopBundle\Form\FormBuilderModifier')) { + throw new CoreException('FormBuilderModifier class was not found, it is only available in PrestaShop 1.7.8 and more'); + } + $formBuilderModifier = new \PrestaShopBundle\Form\FormBuilderModifier(); + + $invalidCharsHint = $this->translator->trans( + 'Invalid characters: <>;=#{}_', + [], + 'Modules.Facetedsearch.Admin' + ); + + $urlTip = $this->translator->trans( + 'When the Faceted Search module is enabled, you can get more detailed URLs by choosing the word that best represent this attribute. By default, PrestaShop uses the attribute\'s name, but you can change that setting using this field.', + [], + 'Modules.Facetedsearch.Admin' + ); + $metaTitleTip = $this->translator->trans( + 'When the Faceted Search module is enabled, you can get more detailed page titles by choosing the word that best represent this attribute. By default, PrestaShop uses the attribute\'s name, but you can change that setting using this field.', + [], + 'Modules.Facetedsearch.Admin' + ); + + $formBuilderModifier->addBefore( + $formBuilder, + 'group_type', + 'url_name', + TranslatableType::class, + [ + 'required' => false, + 'label' => $this->translator->trans('URL', [], 'Modules.Facetedsearch.Admin'), + 'help' => $urlTip . ' ' . $invalidCharsHint, + 'options' => [ + 'constraints' => [ + new UrlSegment([ + 'message' => $this->translator->trans('%s is invalid.', [], 'Admin.Notifications.Error'), + ]), + ], + ], + ] + ); + $formBuilderModifier->addBefore( + $formBuilder, + 'group_type', + 'meta_title', + TranslatableType::class, + [ + 'required' => false, + 'label' => $this->translator->trans('Meta title', [], 'Modules.Facetedsearch.Admin'), + 'help' => $metaTitleTip, + ] + ); + $formBuilderModifier->addBefore( + $formBuilder, + 'group_type', + 'is_indexable', + SwitchType::class, + [ + 'required' => false, + 'label' => $this->translator->trans('Indexable', [], 'Modules.Facetedsearch.Admin'), + 'help' => $this->translator->trans( + 'Use this attribute in URL generated by the Faceted Search module.', + [], + 'Modules.Facetedsearch.Admin' + ), + ] + ); + } +} diff --git a/src/Form/Feature/index.php b/src/Form/Feature/index.php new file mode 100644 index 000000000..88355f610 --- /dev/null +++ b/src/Form/Feature/index.php @@ -0,0 +1,11 @@ +context->getTranslator()); + $formModifier->modify($params['form_builder']); + } + + /** + * Hook that provides extra data in the form. + * + * @since PrestaShop 9.0.0 + * + * @param array $params + */ + public function actionAttributeFormDataProviderData(array $params) + { + $formDataProvider = new FormDataProvider($this->database); + $attributeData = $formDataProvider->getData($params); + // Update data field in params which is passed by reference + $params['data'] = array_merge($params['data'], $attributeData); + } + + /** + * Hook after creation form is handled in migrated page. + * + * @since PrestaShop 9.0.0 + * + * @param array $params + */ + public function actionAfterCreateAttributeFormHandler(array $params): void + { + $this->save(array_merge(['id_attribute' => $params['id']], $params['form_data'])); + } + + /** + * Hook after edition form is handled in migrated page. + * + * @since PrestaShop 9.0.0 + * + * @param array $params + */ + public function actionAfterUpdateAttributeFormHandler(array $params): void + { + $this->save(array_merge(['id_attribute' => $params['id']], $params['form_data'])); + } + /** * After save attribute * @@ -43,28 +102,21 @@ public function actionAttributeSave(array $params) return; } - $this->database->execute( - 'DELETE FROM ' . _DB_PREFIX_ . 'layered_indexable_attribute_lang_value - WHERE `id_attribute` = ' . (int) $params['id_attribute'] - ); - + $formData = [ + 'id_attribute' => (int) $params['id_attribute'], + ]; foreach (Language::getLanguages(false) as $language) { - $seoUrl = Tools::getValue('url_name_' . (int) $language['id_lang']); - $metaTitle = Tools::getValue('meta_title_' . (int) $language['id_lang']); - if (empty($seoUrl) && empty($metaTitle)) { - continue; + $langId = (int) $language['id_lang']; + $seoUrl = Tools::getValue('url_name_' . $langId); + if (!empty($seoUrl)) { + $formData['url_name'][$langId] = $seoUrl; + } + $metaTitle = Tools::getValue('meta_title_' . $langId); + if (!empty($metaTitle)) { + $formData['meta_title'][$langId] = $metaTitle; } - - $this->database->execute( - 'INSERT INTO ' . _DB_PREFIX_ . 'layered_indexable_attribute_lang_value - (`id_attribute`, `id_lang`, `url_name`, `meta_title`) - VALUES ( - ' . (int) $params['id_attribute'] . ', ' . (int) $language['id_lang'] . ', - \'' . pSQL(Tools::str2url($seoUrl)) . '\', - \'' . pSQL($metaTitle, true) . '\')' - ); } - $this->module->invalidateLayeredFilterBlockCache(); + $this->save($formData); } /** @@ -122,4 +174,42 @@ public function displayAttributeForm(array $params) return $this->module->render('attribute_form.tpl'); } + + /** + * This is the common save method, the calling methods just need to format the form data appropriately + * depending on the page being migrated or not. + * + * @param array $formData + */ + private function save(array $formData): void + { + if (empty($formData['id_attribute'])) { + return; + } + + $attributeId = (int) $formData['id_attribute']; + $this->database->execute( + 'DELETE FROM ' . _DB_PREFIX_ . 'layered_indexable_attribute_lang_value + WHERE `id_attribute` = ' . $attributeId + ); + + $landIds = array_unique(array_merge(array_keys($formData['meta_title'] ?? []), array_keys($formData['url_name'] ?? []))); + foreach ($landIds as $langId) { + $seoUrl = $formData['url_name'][$langId] ?? null; + $metaTitle = $formData['meta_title'][$langId] ?? null; + if (empty($seoUrl) && empty($metaTitle)) { + continue; + } + + $this->database->execute( + 'INSERT INTO ' . _DB_PREFIX_ . 'layered_indexable_attribute_lang_value + (`id_attribute`, `id_lang`, `url_name`, `meta_title`) + VALUES ( + ' . $attributeId . ', ' . $langId . ', + \'' . pSQL(Tools::str2url($seoUrl)) . '\', + \'' . pSQL($metaTitle, true) . '\')' + ); + } + $this->module->invalidateLayeredFilterBlockCache(); + } } diff --git a/src/Hook/AttributeGroup.php b/src/Hook/AttributeGroup.php index 2d91b6e8a..ff4b539c7 100644 --- a/src/Hook/AttributeGroup.php +++ b/src/Hook/AttributeGroup.php @@ -21,6 +21,8 @@ namespace PrestaShop\Module\FacetedSearch\Hook; use Language; +use PrestaShop\Module\FacetedSearch\Form\AttributeGroup\FormDataProvider; +use PrestaShop\Module\FacetedSearch\Form\AttributeGroup\FormModifier; use Tools; class AttributeGroup extends AbstractHook @@ -30,8 +32,65 @@ class AttributeGroup extends AbstractHook 'actionAttributeGroupSave', 'displayAttributeGroupForm', 'displayAttributeGroupPostProcess', + // Hooks for migrated page + 'actionAttributeGroupFormBuilderModifier', + 'actionAttributeGroupFormDataProviderData', + 'actionAfterCreateAttributeGroupFormHandler', + 'actionAfterUpdateAttributeGroupFormHandler', ]; + /** + * Hook for modifying attribute group form formBuilder + * + * @since PrestaShop 9.0.0 + * + * @param array $params + */ + public function actionAttributeGroupFormBuilderModifier(array $params) + { + $formModifier = new FormModifier($this->context->getTranslator()); + $formModifier->modify($params['form_builder']); + } + + /** + * Hook that provides extra data in the form. + * + * @since PrestaShop 9.0.0 + * + * @param array $params + */ + public function actionAttributeGroupFormDataProviderData(array $params) + { + $formDataProvider = new FormDataProvider($this->database); + $attributeGroupData = $formDataProvider->getData($params); + // Update data field in params which is passed by reference + $params['data'] = array_merge($params['data'], $attributeGroupData); + } + + /** + * Hook after creation form is handled in migrated page. + * + * @since PrestaShop 9.0.0 + * + * @param array $params + */ + public function actionAfterCreateAttributeGroupFormHandler(array $params): void + { + $this->save(array_merge(['id_attribute_group' => $params['id']], $params['form_data'])); + } + + /** + * Hook after edition form is handled in migrated page. + * + * @since PrestaShop 9.0.0 + * + * @param array $params + */ + public function actionAfterUpdateAttributeGroupFormHandler(array $params): void + { + $this->save(array_merge(['id_attribute_group' => $params['id']], $params['form_data'])); + } + /** * After save Attributes group * @@ -43,37 +102,23 @@ public function actionAttributeGroupSave(array $params) return; } - $this->database->execute( - 'DELETE FROM ' . _DB_PREFIX_ . 'layered_indexable_attribute_group - WHERE `id_attribute_group` = ' . (int) $params['id_attribute_group'] - ); - $this->database->execute( - 'DELETE FROM ' . _DB_PREFIX_ . 'layered_indexable_attribute_group_lang_value - WHERE `id_attribute_group` = ' . (int) $params['id_attribute_group'] - ); - - $this->database->execute( - 'INSERT INTO ' . _DB_PREFIX_ . 'layered_indexable_attribute_group (`id_attribute_group`, `indexable`) -VALUES (' . (int) $params['id_attribute_group'] . ', ' . (int) Tools::getValue('layered_indexable') . ')' - ); + $formData = [ + 'id_attribute_group' => (int) $params['id_attribute_group'], + 'is_indexable' => (int) Tools::getValue('layered_indexable'), + ]; foreach (Language::getLanguages(false) as $language) { - $seoUrl = Tools::getValue('url_name_' . (int) $language['id_lang']); - $metaTitle = Tools::getValue('meta_title_' . (int) $language['id_lang']); - if (empty($seoUrl) && empty($metaTitle)) { - continue; + $langId = (int) $language['id_lang']; + $seoUrl = Tools::getValue('url_name_' . $langId); + if (!empty($seoUrl)) { + $formData['url_name'][$langId] = $seoUrl; + } + $metaTitle = Tools::getValue('meta_title_' . $langId); + if (!empty($metaTitle)) { + $formData['meta_title'][$langId] = $metaTitle; } - - $this->database->execute( - 'INSERT INTO ' . _DB_PREFIX_ . 'layered_indexable_attribute_group_lang_value - (`id_attribute_group`, `id_lang`, `url_name`, `meta_title`) - VALUES ( - ' . (int) $params['id_attribute_group'] . ', ' . (int) $language['id_lang'] . ', - \'' . pSQL(Tools::str2url($seoUrl)) . '\', - \'' . pSQL($metaTitle, true) . '\')' - ); } - $this->module->invalidateLayeredFilterBlockCache(); + $this->save($formData); } /** @@ -142,4 +187,53 @@ public function displayAttributeGroupForm(array $params) return $this->module->render('attribute_group_form.tpl'); } + + /** + * This is the common save method, the calling methods just need to format the form data appropriately + * depending on the page being migrated or not. + * + * @param array $formData + */ + private function save(array $formData): void + { + if (empty($formData['id_attribute_group'])) { + return; + } + + $attributeGroupId = $formData['id_attribute_group']; + + // First clean all existing data + $this->database->execute( + 'DELETE FROM ' . _DB_PREFIX_ . 'layered_indexable_attribute_group + WHERE `id_attribute_group` = ' . $attributeGroupId + ); + $this->database->execute( + 'DELETE FROM ' . _DB_PREFIX_ . 'layered_indexable_attribute_group_lang_value + WHERE `id_attribute_group` = ' . $attributeGroupId + ); + + $this->database->execute( + 'INSERT INTO ' . _DB_PREFIX_ . 'layered_indexable_attribute_group (`id_attribute_group`, `indexable`) +VALUES (' . $attributeGroupId . ', ' . (int) $formData['is_indexable'] . ')' + ); + + $landIds = array_unique(array_merge(array_keys($formData['meta_title'] ?? []), array_keys($formData['url_name'] ?? []))); + foreach ($landIds as $langId) { + $seoUrl = $formData['url_name'][$langId] ?? null; + $metaTitle = $formData['meta_title'][$langId] ?? null; + if (empty($seoUrl) && empty($metaTitle)) { + continue; + } + + $this->database->execute( + 'INSERT INTO ' . _DB_PREFIX_ . 'layered_indexable_attribute_group_lang_value + (`id_attribute_group`, `id_lang`, `url_name`, `meta_title`) + VALUES ( + ' . $attributeGroupId . ', ' . $langId . ', + \'' . pSQL(Tools::str2url($seoUrl)) . '\', + \'' . pSQL($metaTitle, true) . '\')' + ); + } + $this->module->invalidateLayeredFilterBlockCache(); + } } diff --git a/src/Hook/index.php b/src/Hook/index.php new file mode 100644 index 000000000..88355f610 --- /dev/null +++ b/src/Hook/index.php @@ -0,0 +1,11 @@ +assertCount(31, $this->dispatcher->getAvailableHooks()); + $this->assertCount(39, $this->dispatcher->getAvailableHooks()); $this->assertEquals( [ 'actionAttributeGroupDelete', 'actionAttributeSave', 'displayAttributeForm', 'actionAttributePostProcess', + 'actionAttributeFormBuilderModifier', + 'actionAttributeFormDataProviderData', + 'actionAfterCreateAttributeFormHandler', + 'actionAfterUpdateAttributeFormHandler', 'actionAttributeGroupDelete', 'actionAttributeGroupSave', 'displayAttributeGroupForm', 'displayAttributeGroupPostProcess', + 'actionAttributeGroupFormBuilderModifier', + 'actionAttributeGroupFormDataProviderData', + 'actionAfterCreateAttributeGroupFormHandler', + 'actionAfterUpdateAttributeGroupFormHandler', 'actionCategoryAdd', 'actionCategoryDelete', 'actionCategoryUpdate', diff --git a/upgrade/index.php b/upgrade/index.php new file mode 100644 index 000000000..88355f610 --- /dev/null +++ b/upgrade/index.php @@ -0,0 +1,11 @@ + + * @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_15_0(Ps_Facetedsearch $module) +{ + // New hooks for migrated attribute page + $newHooks = [ + 'actionAttributeFormBuilderModifier', + 'actionAttributeFormDataProviderData', + 'actionAfterCreateAttributeFormHandler', + 'actionAfterUpdateAttributeFormHandler', + 'actionAttributeGroupFormBuilderModifier', + 'actionAttributeGroupFormDataProviderData', + 'actionAfterCreateAttributeGroupFormHandler', + 'actionAfterUpdateAttributeGroupFormHandler', + ]; + + return $module->registerHook($newHooks); +} diff --git a/views/templates/admin/_functions/index.php b/views/templates/admin/_functions/index.php new file mode 100644 index 000000000..88355f610 --- /dev/null +++ b/views/templates/admin/_functions/index.php @@ -0,0 +1,11 @@ +