Skip to content

Commit

Permalink
add form modifications for feature-value migrated form
Browse files Browse the repository at this point in the history
  • Loading branch information
zuk3975 committed Jun 28, 2023
1 parent 5575562 commit 5c38061
Show file tree
Hide file tree
Showing 3 changed files with 264 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/Form/FeatureValue/FormDataProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?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 Academic Free License 3.0 (AFL-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/AFL-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.
*
* @author PrestaShop SA <contact@prestashop.com>
* @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,
];
}
}
103 changes: 103 additions & 0 deletions src/Form/FeatureValue/FormModifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?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 Academic Free License 3.0 (AFL-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/AFL-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.
*
* @author PrestaShop SA <contact@prestashop.com>
* @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'],
]
);
}
}
86 changes: 86 additions & 0 deletions src/Hook/FeatureValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
*
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 5c38061

Please sign in to comment.