Skip to content

Commit

Permalink
Handle Attribute hooks to integrate the module into the migrate Attri…
Browse files Browse the repository at this point in the history
…bute form
  • Loading branch information
jolelievre committed Feb 6, 2024
1 parent 9d3a9df commit 915f7ac
Show file tree
Hide file tree
Showing 3 changed files with 286 additions and 19 deletions.
82 changes: 82 additions & 0 deletions src/Form/Attribute/FormDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @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,
];
}
}
95 changes: 95 additions & 0 deletions src/Form/Attribute/FormModifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/OSL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to https://devdocs.prestashop.com/ for more information.
*
* @author PrestaShop SA and Contributors <contact@prestashop.com>
* @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,
]
)
;
}
}
128 changes: 109 additions & 19 deletions src/Hook/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
namespace PrestaShop\Module\FacetedSearch\Hook;

use Language;
use PrestaShop\Module\FacetedSearch\Form\Attribute\FormDataProvider;
use PrestaShop\Module\FacetedSearch\Form\Attribute\FormModifier;
use Tools;

class Attribute extends AbstractHook
Expand All @@ -30,8 +32,65 @@ class Attribute extends AbstractHook
'actionAttributeSave',
'displayAttributeForm',
'actionAttributePostProcess',
// Hooks for migrated page
'actionAttributeFormBuilderModifier',
'actionAttributeFormDataProviderData',
'actionAfterCreateAttributeFormHandler',
'actionAfterUpdateAttributeFormHandler',
];

/**
* Hook for modifying attribute form formBuilder
*
* @since PrestaShop 9.0.0
*
* @param array $params
*/
public function actionAttributeFormBuilderModifier(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 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
*
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 915f7ac

Please sign in to comment.