Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add form modifications for feature-value migrated form #859

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<module>
<name>ps_facetedsearch</name>
<displayName><![CDATA[Faceted search]]></displayName>
<version><![CDATA[3.12.1]]></version>
<version><![CDATA[3.13.0]]></version>
<description><![CDATA[Displays a block allowing multiple filters.]]></description>
<author><![CDATA[PrestaShop]]></author>
<tab><![CDATA[front_office_features]]></tab>
Expand Down
2 changes: 1 addition & 1 deletion ps_facetedsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
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,
];
}
}
102 changes: 102 additions & 0 deletions src/Form/FeatureValue/FormModifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?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\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'],
]
);
}
}
95 changes: 95 additions & 0 deletions src/Hook/FeatureValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*
Expand Down Expand Up @@ -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();
}
}
5 changes: 4 additions & 1 deletion tests/php/FacetedSearch/HookDispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -74,6 +74,9 @@ public function testGetAvailableHooks()
'actionFeatureValueDelete',
'displayFeatureValueForm',
'displayFeatureValuePostProcess',
'actionFeatureValueFormBuilderModifier',
'actionAfterCreateFeatureValueFormHandler',
'actionAfterUpdateFeatureValueFormHandler',
'actionProductSave',
'productSearchProvider',
'actionObjectSpecificPriceRuleUpdateBefore',
Expand Down
33 changes: 33 additions & 0 deletions upgrade/upgrade-3.13.0.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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)
*/
if (!defined('_PS_VERSION_')) {
exit;
}

function upgrade_module_3_13_0(Ps_Facetedsearch $module)
{
$newHooks = [
'actionFeatureValueFormBuilderModifier',
'actionAfterCreateFeatureValueFormHandler',
'actionAfterUpdateFeatureValueFormHandler',
];

return $module->registerHook($newHooks);
}