Skip to content

Commit

Permalink
Merge pull request #976 from Quetzacoalt91/update-options-page
Browse files Browse the repository at this point in the history
[NEW UI] Update options page
  • Loading branch information
Quetzacoalt91 authored Nov 18, 2024
2 parents 5628665 + b8b1351 commit 8932acf
Show file tree
Hide file tree
Showing 21 changed files with 248 additions and 77 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ Here is an example of the different fields that can be found in it:
"archive_num": "8.0.0", // Release number, specific to the archive channel
"PS_AUTOUP_CUSTOM_MOD_DESACT": 1, // Disable non-native modules
"PS_AUTOUP_CHANGE_DEFAULT_THEME": 0, // Keep the current theme
"PS_AUTOUP_KEEP_MAILS": 0, // Retain customized email templates
"PS_AUTOUP_REGEN_EMAIL": 1, // Retain customized email templates
"PS_AUTOUP_BACKUP": 0, // Do not create a store backup
"PS_AUTOUP_KEEP_IMAGES": 1, // Retain images
"PS_DISABLE_OVERRIDES": 1 // Disable all overrides
Expand Down
17 changes: 9 additions & 8 deletions _dev/jest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
beforeAll(() => {
window.AutoUpgradeVariables = {
token: 'test-token',
admin_url: 'http://localhost',
admin_dir: '/admin_directory',
stepper_parent_id: 'stepper_content'
};
});
// We don't wait for the call to beforeAll to define window properties.
window.AutoUpgradeVariables = {
token: 'test-token',
admin_url: 'http://localhost',
admin_dir: '/admin_directory',
stepper_parent_id: 'stepper_content'
};

beforeAll(() => {});
46 changes: 42 additions & 4 deletions _dev/src/ts/pages/UpdatePageUpdateOptions.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,51 @@
import UpdatePage from './UpdatePage';
import api from '../api/RequestHandler';

export default class UpdatePageUpdateOptions extends UpdatePage {
protected stepCode = 'update-options';

constructor() {
super();
}

public mount() {
this.initStepper();
this.form.addEventListener('submit', this.onSubmit);
this.form.addEventListener('change', this.onChange);
}

public beforeDestroy() {
try {
this.form.removeEventListener('submit', this.onSubmit);
this.form.removeEventListener('change', this.onChange);
} catch {
// Do Nothing, page is likely removed from the DOM already
}
}

private get form(): HTMLFormElement {
const form = document.forms.namedItem('update-options-page-form');
if (!form) {
throw new Error('Form not found');
}

['routeToSave', 'routeToSubmit'].forEach((data) => {
if (!form.dataset[data]) {
throw new Error(`Missing data ${data} from form dataset.`);
}
});

return form;
}

private readonly onChange = async (ev: Event) => {
const optionInput = ev.target as HTMLInputElement;

const data = new FormData(this.form);
optionInput.setAttribute('disabled', 'true');
await api.post(this.form.dataset.routeToSave!, data);
optionInput.removeAttribute('disabled');
};

private readonly onSubmit = async (event: Event) => {
event.preventDefault();

await api.post(this.form.dataset.routeToSubmit!, new FormData(this.form));
};
}
2 changes: 1 addition & 1 deletion classes/Analytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function getProperties($type): array
'upgrade_channel' => $this->upgradeConfiguration->getChannel(),
'disable_non_native_modules' => $this->upgradeConfiguration->shouldDeactivateCustomModules(),
'switch_to_default_theme' => $this->upgradeConfiguration->shouldSwitchToDefaultTheme(),
'keep_customized_email_templates' => $this->upgradeConfiguration->shouldKeepMails(),
'regenerate_customized_email_templates' => $this->upgradeConfiguration->shouldRegenerateMailTemplates(),
];
$upgradeProperties = $this->properties[self::WITH_UPDATE_PROPERTIES] ?? [];
$additionalProperties = array_merge($upgradeProperties, $additionalProperties);
Expand Down
9 changes: 6 additions & 3 deletions classes/Parameters/ConfigurationValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function validate(array $array = []): array
$error = $this->validateArchiveXml($value, $isLocal);
break;
case UpgradeConfiguration::PS_AUTOUP_CUSTOM_MOD_DESACT:
case UpgradeConfiguration::PS_AUTOUP_KEEP_MAILS:
case UpgradeConfiguration::PS_AUTOUP_REGEN_EMAIL:
case UpgradeConfiguration::PS_AUTOUP_KEEP_IMAGES:
case UpgradeConfiguration::PS_DISABLE_OVERRIDES:
$error = $this->validateBool($value, $key);
Expand Down Expand Up @@ -106,9 +106,12 @@ private function validateArchiveXml(string $xml, bool $isLocal): ?string
return null;
}

private function validateBool(string $boolValue, string $key): ?string
/**
* @param string|bool $boolValue
*/
private function validateBool($boolValue, string $key): ?string
{
if ($boolValue === '' || filter_var($boolValue, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) === null) {
if (!is_bool($boolValue) && ($boolValue === '' || filter_var($boolValue, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) === null)) {
return $this->translator->trans('Value must be a boolean for %s', [$key]);
}

Expand Down
14 changes: 7 additions & 7 deletions classes/Parameters/UpgradeConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class UpgradeConfiguration extends ArrayCollection
{
const PS_AUTOUP_CUSTOM_MOD_DESACT = 'PS_AUTOUP_CUSTOM_MOD_DESACT';
const PS_AUTOUP_CHANGE_DEFAULT_THEME = 'PS_AUTOUP_CHANGE_DEFAULT_THEME';
const PS_AUTOUP_KEEP_MAILS = 'PS_AUTOUP_KEEP_MAILS';
const PS_AUTOUP_REGEN_EMAIL = 'PS_AUTOUP_REGEN_EMAIL';
const PS_AUTOUP_BACKUP = 'PS_AUTOUP_BACKUP';
const PS_AUTOUP_KEEP_IMAGES = 'PS_AUTOUP_KEEP_IMAGES';
const PS_DISABLE_OVERRIDES = 'PS_DISABLE_OVERRIDES';
Expand All @@ -56,7 +56,7 @@ class UpgradeConfiguration extends ArrayCollection
const UPGRADE_CONST_KEYS = [
self::PS_AUTOUP_CUSTOM_MOD_DESACT,
self::PS_AUTOUP_CHANGE_DEFAULT_THEME,
self::PS_AUTOUP_KEEP_MAILS,
self::PS_AUTOUP_REGEN_EMAIL,
self::PS_AUTOUP_BACKUP,
self::PS_AUTOUP_KEEP_IMAGES,
self::PS_DISABLE_OVERRIDES,
Expand All @@ -69,7 +69,7 @@ class UpgradeConfiguration extends ArrayCollection
const PS_CONST_DEFAULT_VALUE = [
self::PS_AUTOUP_CUSTOM_MOD_DESACT => true,
self::PS_AUTOUP_CHANGE_DEFAULT_THEME => false,
self::PS_AUTOUP_KEEP_MAILS => false,
self::PS_AUTOUP_REGEN_EMAIL => true,
self::PS_AUTOUP_BACKUP => true,
self::PS_AUTOUP_KEEP_IMAGES => true,
];
Expand Down Expand Up @@ -201,11 +201,11 @@ public function shouldDeactivateCustomModules(): bool
}

/**
* @return bool true if we should keep the merchant emails untouched
* @return bool true if we should regenerate the merchant emails
*/
public function shouldKeepMails(): bool
public function shouldRegenerateMailTemplates(): bool
{
return $this->computeBooleanConfiguration(self::PS_AUTOUP_KEEP_MAILS);
return $this->computeBooleanConfiguration(self::PS_AUTOUP_REGEN_EMAIL);
}

/**
Expand All @@ -232,7 +232,7 @@ private function computeBooleanConfiguration(string $const): bool

public static function isOverrideAllowed(): bool
{
return (bool) Configuration::get(self::PS_DISABLE_OVERRIDES);
return !Configuration::get(self::PS_DISABLE_OVERRIDES);
}

public static function updateDisabledOverride(bool $value, ?int $shopId = null): void
Expand Down
8 changes: 8 additions & 0 deletions classes/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ public function __construct(UpgradeContainer $upgradeContainer)
'controller' => UpdatePageUpdateOptionsController::class,
'method' => 'step',
],
Routes::UPDATE_STEP_UPDATE_OPTIONS_SAVE_OPTION => [
'controller' => UpdatePageUpdateOptionsController::class,
'method' => 'saveOption',
],
Routes::UPDATE_STEP_UPDATE_OPTIONS_SUBMIT_FORM => [
'controller' => UpdatePageUpdateOptionsController::class,
'method' => 'submit',
],
Routes::UPDATE_PAGE_BACKUP => [
'controller' => UpdatePageBackupController::class,
'method' => 'index',
Expand Down
2 changes: 2 additions & 0 deletions classes/Router/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Routes
/* step: update options */
const UPDATE_PAGE_UPDATE_OPTIONS = 'update-page-update-options';
const UPDATE_STEP_UPDATE_OPTIONS = 'update-step-update-options';
const UPDATE_STEP_UPDATE_OPTIONS_SAVE_OPTION = 'update-step-update-options-save-option';
const UPDATE_STEP_UPDATE_OPTIONS_SUBMIT_FORM = 'update-step-update-options-submit-form';

/* step: backup */
const UPDATE_PAGE_BACKUP = 'update-page-backup';
Expand Down
6 changes: 3 additions & 3 deletions classes/Twig/Form/UpgradeOptionsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,13 @@ public function __construct(Translator $translator, FormRenderer $formRenderer)
'desc' => $translator->trans('This will change your theme: your shop will then use the default theme of the version of PrestaShop you are upgrading to.'),
],

UpgradeConfiguration::PS_AUTOUP_KEEP_MAILS => [
'title' => $translator->trans('Keep the customized email templates'),
UpgradeConfiguration::PS_AUTOUP_REGEN_EMAIL => [
'title' => $translator->trans('Regenerate the customized email templates'),
'cast' => 'intval',
'validation' => 'isBool',
'type' => 'bool',
'desc' => $translator->trans('This will not upgrade the default PrestaShop e-mails.') . '<br />'
. $translator->trans('If you customized the default PrestaShop e-mail templates, enabling this option will keep your modifications.'),
. $translator->trans('If you customized the default PrestaShop e-mail templates, switching off this option will keep your modifications.'),
],
];
}
Expand Down
25 changes: 25 additions & 0 deletions classes/Twig/ValidatorToFormFormater.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace PrestaShop\Module\AutoUpgrade\Twig;

abstract class ValidatorToFormFormater
{
/**
* @param array<array{message:string, target?:string}> $errors
*
* @return array<'global'|string, string>
*/
public static function format($errors): array
{
return array_column(
array_map(function ($error) {
return [
'key' => $error['target'] ?? 'global',
'value' => $error['message'],
];
}, $errors),
'value',
'key'
);
}
}
4 changes: 2 additions & 2 deletions classes/UpgradeTools/CoreUpgrader/CoreUpgrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ public function finalizeCoreUpdate(): void
$this->cleanXmlFiles();

if (UpgradeConfiguration::isOverrideAllowed()) {
$this->logger->info($this->container->getTranslator()->trans('Keeping overrides in place'));
} else {
$this->logger->info($this->container->getTranslator()->trans('Disabling overrides'));
$this->disableOverrides();
} else {
$this->logger->info($this->container->getTranslator()->trans('Keeping overrides in place'));
}

$this->updateTheme();
Expand Down
2 changes: 1 addition & 1 deletion classes/UpgradeTools/CoreUpgrader/CoreUpgrader17.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected function upgradeLanguage($lang): void
$lang_pack = \Language::getLangDetails($isoCode);
\Language::installSfLanguagePack($lang_pack['locale'], $errorsLanguage);

if (!$this->container->getUpgradeConfiguration()->shouldKeepMails()) {
if ($this->container->getUpgradeConfiguration()->shouldRegenerateMailTemplates()) {
\Language::installEmailsLanguagePack($lang_pack, $errorsLanguage);
}

Expand Down
2 changes: 1 addition & 1 deletion classes/UpgradeTools/CoreUpgrader/CoreUpgrader80.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ protected function upgradeLanguage($lang): void
$lang_pack = \Language::getLangDetails($isoCode);
\Language::installSfLanguagePack($lang_pack['locale'], $errorsLanguage);

if (!$this->container->getUpgradeConfiguration()->shouldKeepMails()) {
if ($this->container->getUpgradeConfiguration()->shouldRegenerateMailTemplates()) {
$this->logger->debug($this->container->getTranslator()->trans('Generating mail templates for %lang%', ['%lang%' => $isoCode]));
$mailTheme = \Configuration::get('PS_MAIL_THEME', null, null, null, 'modern');

Expand Down
6 changes: 3 additions & 3 deletions controllers/admin/AdminSelfUpgradeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,13 +238,13 @@ private function _setFields()
'type' => 'bool',
'desc' => $this->trans('This will change your theme: your shop will then use the default theme of the version of PrestaShop you are upgrading to.'),
],
UpgradeConfiguration::PS_AUTOUP_KEEP_MAILS => [
'title' => $this->trans('Keep the customized email templates'),
UpgradeConfiguration::PS_AUTOUP_REGEN_EMAIL => [
'title' => $this->trans('Regenerate the customized email templates'),
'cast' => 'intval',
'validation' => 'isBool',
'type' => 'bool',
'desc' => $this->trans('This will not upgrade the default PrestaShop e-mails.') . '<br />'
. $this->trans('If you customized the default PrestaShop e-mail templates, enabling this option will keep your modifications.'),
. $this->trans('If you customized the default PrestaShop e-mail templates, switching off this option will keep your modifications.'),
],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,18 @@ public function step()
return new Response('Unexpected call to a step route outside an ajax call.', 404);
}

// It may be tempting to move this line inside the parameters of the method
// `getTwig()->render()`. Please refrain to do so as this makes Twig
// called BEFORE the call to the function sent as parameters. Initiating it too early
// can be misleading when rendering the templates as more autoloaders can be loaded
// in the meantime (i.e the core).
$params = $this->getParams();

return AjaxResponseBuilder::hydrationResponse(
PageSelectors::STEP_PARENT_ID,
$this->getTwig()->render(
'@ModuleAutoUpgrade/steps/' . $this->getStepTemplate() . '.html.twig',
$this->getParams()
$params
),
$this->displayRouteInUrl()
);
Expand Down
Loading

0 comments on commit 8932acf

Please sign in to comment.