4 Commits
Author SHA1 Message Date
isabelle 92fd21d137 feat(extra-features): Update price_split to be more dynamic
Added show_currency bool as a parameter; made the span class and aria-label displaying the currency dynamic for accessibility
2026-02-17 10:01:06 +01:00
isabelle ea9b67ffc5 fix(extra-features): Add attributes & styles to displaySvgIcon method 2025-12-31 15:22:32 +01:00
isabelle e2c4f265c2 feat(extra-features): Refactor smarty helper 2025-12-31 13:57:28 +01:00
isabelle 28f8794e16 feat(extra-features): Add svg icon and price splitter smarty functions
These are just helpers and useful for many prestashop designs
2025-11-20 16:45:32 +01:00
7 changed files with 160 additions and 44 deletions
+3 -5
View File
@@ -70,12 +70,12 @@ class Is_themecore extends Module
$this->displayName = $this->trans(
'Theme core module',
[],
'Modules.Isthemecore.Admin'
'Modules.isthemecore.Admin'
);
$this->description = $this->trans(
'Required for theme to work.',
[],
'Modules.Isthemecore.Admin'
'Modules.isthemecore.Admin'
);
$this->tabs = [
@@ -85,9 +85,8 @@ class Is_themecore extends Module
'route_name' => 'is_themecore_module_settings',
'parent_class_name' => 'CONFIGURE',
'visible' => false,
'active' => true,
'wording' => 'Theme core module settings',
'wording_domain' => 'Modules.Isthemecore.Admin',
'wording_domain' => 'Modules.isthemecore.Admin',
],
];
@@ -184,7 +183,6 @@ class Is_themecore extends Module
$router = $container->get('router');
Tools::redirectAdmin($router->generate('is_themecore_module_settings'));
exit;
}
}
@@ -7,7 +7,7 @@ namespace Oksydan\Module\IsThemeCore\Controller\Admin;
use PrestaShop\PrestaShop\Core\Form\FormHandlerInterface;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use PrestaShopBundle\Security\Annotation\AdminSecurity;
use PrestaShopBundle\Security\Attribute\DemoRestricted;
use PrestaShopBundle\Security\Annotation\DemoRestricted;
use PrestaShopBundle\Security\Annotation\ModuleActivated;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
@@ -23,7 +23,7 @@ class SettingsController extends FrameworkBundleAdminController
{
/**
* @AdminSecurity(
* "is_granted('read', request.get('_legacy_controller'))",
* "is_granted(['read'], request.get('_legacy_controller'))",
* message="You do not have permission to access this."
* )
*
@@ -53,13 +53,14 @@ class SettingsController extends FrameworkBundleAdminController
* redirectRoute="is_themecore_module_settings"
* )
*
* @DemoRestricted(redirectRoute="is_themecore_module_settings")
*
* @param Request $request
*
* @return RedirectResponse
*
* @throws \LogicException
*/
#[DemoRestricted(redirectRoute: 'is_themecore_module_settings')]
public function processGeneralFormAction(Request $request)
{
return $this->processForm(
@@ -76,13 +77,14 @@ class SettingsController extends FrameworkBundleAdminController
* redirectRoute="is_themecore_module_settings"
* )
*
* @DemoRestricted(redirectRoute="is_themecore_module_settings")
*
* @param Request $request
*
* @return RedirectResponse
*
* @throws \LogicException
*/
#[DemoRestricted(redirectRoute: 'is_themecore_module_settings')]
public function processWebpFormAction(Request $request)
{
return $this->processForm(
@@ -93,13 +95,14 @@ class SettingsController extends FrameworkBundleAdminController
}
/**
* @DemoRestricted(redirectRoute="is_themecore_module_settings")
*
* @param Request $request
*
* @return RedirectResponse
*
* @throws \LogicException
*/
#[DemoRestricted(redirectRoute: 'is_themecore_module_settings')]
public function processWebpEraseImages(Request $request)
{
$time_start = microtime(true);
@@ -107,22 +110,22 @@ class SettingsController extends FrameworkBundleAdminController
switch ($request->get('type')) {
case 'all':
$eraser->setQuery(\_PS_ROOT_DIR_);
$eraser->setQuery(_PS_ROOT_DIR_);
break;
case 'product':
$eraser->setQuery(\_PS_IMG_DIR_ . 'p/');
$eraser->setQuery(_PS_PROD_IMG_DIR_);
break;
case 'module':
$eraser->setQuery(\_PS_MODULE_DIR_);
$eraser->setQuery(_PS_MODULE_DIR_);
break;
case 'cms':
$eraser->setQuery(\_PS_IMG_DIR_ . 'cms/');
$eraser->setQuery(_PS_IMG_DIR_ . 'cms/');
break;
case 'themes':
$eraser->setQuery(\_PS_ROOT_DIR_ . '/themes/');
$eraser->setQuery(_PS_ROOT_DIR_ . '/themes/');
break;
default:
$eraser->setQuery(\_PS_ROOT_DIR_);
$eraser->setQuery(_PS_ROOT_DIR_);
break;
}
@@ -131,7 +134,7 @@ class SettingsController extends FrameworkBundleAdminController
$time_end = microtime(true);
$execution_time = round($time_end - $time_start, 2);
$this->addFlash('success', $this->trans('%1$s - webp images has been erased successfully in %2$ss', 'Modules.Isthemecore.Admin', [$eraser->getFilesCount(), $execution_time]));
$this->addFlash('success', $this->trans('%1$s - webp images has been erased successfully in %2$ss', 'Modules.isthemecore.Admin', [$eraser->getFilesCount(), $execution_time]));
return $this->redirectToRoute('is_themecore_module_settings');
}
@@ -190,9 +190,114 @@ class SmartyHelperFunctions
return $content;
}
/* CUSTOM ADDITIONS BELOW */
public static function isBright($hexColor)
{
$calculator = new ColorBrightnessCalculator();
return $calculator->isBright($hexColor);
}
public static function displaySvgIcon($params){
/*
Helper to display SVG icons stored in themes/falcon/_dev/img/icons/
registered under public_html/modules/is_themecore/src/Hook/Smarty.php
Usage examples in a TPL file:
{svg_icon file='person.svg' fill='red' width='24' height='24'}
{svg_icon file='person.svg'}
*/
// Grab svg under falcon/_dev/img and display an error if not found
if (empty($params['file'])) {
return '';
}
$icon_path = _PS_THEME_DIR_ . '/_dev/img/' . basename($params['file']);
if (!is_file($icon_path)) {
if (defined('_PS_MODE_DEV_') && _PS_MODE_DEV_) {
return 'Invalid SVG path: ' . htmlspecialchars($icon_path);
}
return '';
}
$svg_content = file_get_contents($icon_path);
/* Define an associative array of SVG properties
If the property is a style, addd it in a style tag.
If the property is an attribute, replace it in the svg code.
This reduces the risk of interfering with other attributes. */
$svg_props = array(
'width' => 'style',
'height' => 'style',
'opacity' => 'style',
'fill' => 'attribute',
'stroke' => 'attribute',
);
// Styles
$styles = [];
foreach ($svg_props as $prop => $type) {
if ($type === 'style' && !empty($params[$prop])) {
$value = $params[$prop];
if (in_array($prop, ['width', 'height']) && is_numeric($value)) {
$value .= 'px';
}
$styles[] = $prop . ': ' . $value;
}
}
if (!empty($styles)) {
$style_attr = 'style="' . implode('; ', $styles) . '"';
$svg_content = preg_replace('/(<svg[^>]*?)>/', '$1 ' . $style_attr . '>', $svg_content, 1);
}
// Attributes
foreach ($svg_props as $prop => $type) {
if ($type === 'attribute' && !empty($params[$prop])) {
$value = $params[$prop];
// Use word boundaries to avoid matching partial names like fill-opacity or stroke-width
// Replace ALL occurrences of the attribute
$svg_content = preg_replace(
'/\b' . preg_quote($prop) . '\b\s*=\s*"[^"]*"/',
$prop . '="' . $value . '"',
$svg_content
);
}
}
return $svg_content;
}
public static function displayPriceSplit($price, $show_currency = false) {
/* Helper to split the price into euros and cents to make it more stylish
registered under public_html/modules/is_themecore/src/Hook/Smarty.php
Usage examples in a TPL file:
{price_split price=$product.price}
*/
//Convert to a string and only keep numbers, commas and dots
$currency_name = \Context::getContext()->currency->name;
$currency_symbol = \Context::getContext()->currency->symbol;
$price_string = implode(',', $price);
$price_string = preg_replace('/[^0-9,.]/', '', $price_string);
// Split by comma and wrap each part in a span
$price_parts = explode(',', $price_string);
if (count($price_parts) >= 2) {
echo '<span class="currency" aria-label="' . htmlspecialchars($currency_name) . '">' . ($show_currency ? $currency_symbol : '') . $price_parts[0] . '</span>';
echo '<sup class="cents" aria-label="cents">' . $price_parts[1] . '</sup>';
} else {
foreach ($price_parts as $part) {
echo '<span>' . $part . '</span>';
}
}
}
}
@@ -9,7 +9,7 @@ use PrestaShopBundle\Form\Admin\Type\SwitchType;
use PrestaShopBundle\Form\Admin\Type\TranslatorAwareType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface;
class GeneralType extends TranslatorAwareType
{
@@ -52,7 +52,7 @@ class GeneralType extends TranslatorAwareType
ChoiceType::class,
[
'choices' => $this->displayListChoices,
'label' => $this->trans('Default list display', 'Modules.Isthemecore.Admin'),
'label' => $this->trans('Default list display', 'Modules.isthemecore.Admin'),
'multistore_configuration_key' => GeneralConfiguration::THEMECORE_DISPLAY_LIST,
]
)
@@ -60,8 +60,8 @@ class GeneralType extends TranslatorAwareType
SwitchType::class,
[
'required' => false,
'label' => $this->trans('Early hints (HTTP 103) enabled', 'Modules.Isthemecore.Admin'),
'help' => $this->trans('Cloudflare CDN, Early hints option have to enabled. <a href="https://developers.cloudflare.com/cache/about/early-hints/">More information</a>', 'Modules.Isthemecore.Admin'),
'label' => $this->trans('Early hints (HTTP 103) enabled', 'Modules.isthemecore.Admin'),
'help' => $this->trans('Cloudflare CDN, Early hints option have to enabled. <a href="https://developers.cloudflare.com/cache/about/early-hints/">More information</a>', 'Modules.isthemecore.Admin'),
'multistore_configuration_key' => GeneralConfiguration::THEMECORE_EARLY_HINTS,
]
)
@@ -69,7 +69,7 @@ class GeneralType extends TranslatorAwareType
SwitchType::class,
[
'required' => false,
'label' => $this->trans('Preload css enabled, only working with CCC for css option enabled', 'Modules.Isthemecore.Admin'),
'label' => $this->trans('Preload css enabled, only working with CCC for css option enabled', 'Modules.isthemecore.Admin'),
'multistore_configuration_key' => GeneralConfiguration::THEMECORE_PRELOAD_CSS,
]
)
@@ -77,8 +77,8 @@ class GeneralType extends TranslatorAwareType
SwitchType::class,
[
'required' => false,
'label' => $this->trans('Load partytown script', 'Modules.Isthemecore.Admin'),
'help' => $this->trans('Be aware that partytown is still beta. Make sure that everything is working as expected before pushing it to your production store.', 'Modules.Isthemecore.Admin'),
'label' => $this->trans('Load partytown script', 'Modules.isthemecore.Admin'),
'help' => $this->trans('Be aware that partytown is still beta. Make sure that everything is working as expected before pushing it to your production store.', 'Modules.isthemecore.Admin'),
'multistore_configuration_key' => GeneralConfiguration::THEMECORE_LOAD_PARTY_TOWN,
]
)
@@ -86,7 +86,7 @@ class GeneralType extends TranslatorAwareType
SwitchType::class,
[
'required' => false,
'label' => $this->trans('Enable debug mode for partytown', 'Modules.Isthemecore.Admin'),
'label' => $this->trans('Enable debug mode for partytown', 'Modules.isthemecore.Admin'),
'multistore_configuration_key' => GeneralConfiguration::THEMECORE_DEBUG_PARTY_TOWN,
]
);
+15 -15
View File
@@ -12,7 +12,7 @@ use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Range;
@@ -79,7 +79,7 @@ class WebpType extends TranslatorAwareType
if ($webpDisabled) {
$extraAttributes = [
'alert_message' => $this->trans('Webp converters not available contact your admin or hosting provider.', 'Modules.Isthemecore.Admin'),
'alert_message' => $this->trans('Webp converters not available contact your admin or hosting provider.', 'Modules.isthemecore.Admin'),
'alert_type' => 'danger',
'alert_position' => 'append',
];
@@ -91,7 +91,7 @@ class WebpType extends TranslatorAwareType
array_merge(
[
'required' => false,
'label' => $this->trans('Enable WEBP', 'Modules.Isthemecore.Admin'),
'label' => $this->trans('Enable WEBP', 'Modules.isthemecore.Admin'),
'disabled' => $webpDisabled,
],
$extraAttributes
@@ -101,7 +101,7 @@ class WebpType extends TranslatorAwareType
SwitchType::class,
[
'required' => false,
'label' => $this->trans('Enable better RGB->YUV color conversion', 'Modules.Isthemecore.Admin'),
'label' => $this->trans('Enable better RGB->YUV color conversion', 'Modules.isthemecore.Admin'),
'disabled' => $webpDisabled,
]
)
@@ -109,8 +109,8 @@ class WebpType extends TranslatorAwareType
TextType::class,
[
'required' => false,
'label' => $this->trans('Webp quality', 'Modules.Isthemecore.Admin'),
'help' => $this->trans('Range 1-100', 'Modules.Isthemecore.Admin'),
'label' => $this->trans('Webp quality', 'Modules.isthemecore.Admin'),
'help' => $this->trans('Range 1-100', 'Modules.isthemecore.Admin'),
'disabled' => $webpDisabled,
'constraints' => [
$this->getRangeConstraint(1, 100),
@@ -122,7 +122,7 @@ class WebpType extends TranslatorAwareType
ChoiceType::class,
[
'choices' => $this->convertersList,
'label' => $this->trans('Webp converter options', 'Modules.Isthemecore.Admin'),
'label' => $this->trans('Webp converter options', 'Modules.isthemecore.Admin'),
'disabled' => $webpDisabled,
'expanded' => true,
'multiple' => false,
@@ -130,12 +130,12 @@ class WebpType extends TranslatorAwareType
return ['disabled' => $this->convertersListFull[$choice]['disabled']];
},
'choice_label' => function ($choice) {
return $this->convertersListFull[$choice]['label'] . ($this->convertersListFull[$choice]['disabled'] ? '<span class="ml-1 badge badge-danger">' . $this->trans('not available', 'Modules.Isthemecore.Admin') . '</span>' : '');
return $this->convertersListFull[$choice]['label'] . ($this->convertersListFull[$choice]['disabled'] ? '<span class="ml-1 badge badge-danger">' . $this->trans('not available', 'Modules.isthemecore.Admin') . '</span>' : '');
},
]
)
->add('erase_all_webp', IconButtonType::class, [
'label' => $this->trans('Erase all webp images', 'Modules.Isthemecore.Admin'),
'label' => $this->trans('Erase all webp images', 'Modules.isthemecore.Admin'),
'type' => 'link',
'icon' => 'delete',
'attr' => [
@@ -149,7 +149,7 @@ class WebpType extends TranslatorAwareType
],
])
->add('erase_product_webp', IconButtonType::class, [
'label' => $this->trans('Erase all product webp images', 'Modules.Isthemecore.Admin'),
'label' => $this->trans('Erase all product webp images', 'Modules.isthemecore.Admin'),
'type' => 'link',
'icon' => 'delete',
'attr' => [
@@ -163,7 +163,7 @@ class WebpType extends TranslatorAwareType
],
])
->add('erase_modules_webp', IconButtonType::class, [
'label' => $this->trans('Erase all modules webp images', 'Modules.Isthemecore.Admin'),
'label' => $this->trans('Erase all modules webp images', 'Modules.isthemecore.Admin'),
'type' => 'link',
'icon' => 'delete',
'attr' => [
@@ -177,7 +177,7 @@ class WebpType extends TranslatorAwareType
],
])
->add('erase_cms_webp', IconButtonType::class, [
'label' => $this->trans('Erase all CMS webp images', 'Modules.Isthemecore.Admin'),
'label' => $this->trans('Erase all CMS webp images', 'Modules.isthemecore.Admin'),
'type' => 'link',
'icon' => 'delete',
'attr' => [
@@ -191,7 +191,7 @@ class WebpType extends TranslatorAwareType
],
])
->add('erase_themes_webp', IconButtonType::class, [
'label' => $this->trans('Erase all themes webp images', 'Modules.Isthemecore.Admin'),
'label' => $this->trans('Erase all themes webp images', 'Modules.isthemecore.Admin'),
'type' => 'link',
'icon' => 'delete',
'attr' => [
@@ -222,7 +222,7 @@ class WebpType extends TranslatorAwareType
private function getNotBlankConstraint()
{
return new NotBlank([
'message' => $this->trans('This field cannot be empty.', 'Modules.Isthemecore.Admin'),
'message' => $this->trans('This field cannot be empty.', 'Modules.isthemecore.Admin'),
]);
}
@@ -236,7 +236,7 @@ class WebpType extends TranslatorAwareType
'max' => $max,
'invalidMessage' => $this->trans(
'This field value have to be between %min% and %max%.',
'Modules.Isthemecore.Admin',
'Modules.isthemecore.Admin',
[
'%min%' => $min,
'%max%' => $max,
+10
View File
@@ -56,5 +56,15 @@ public function hookActionDispatcherBefore(): void
} else {
return;
}
if (!isset($this->context->smarty->registered_plugins['function']['svg_icon'])) {
$this->context->smarty->registerPlugin('function', 'svg_icon', ['Oksydan\Module\IsThemeCore\Core\Smarty\SmartyHelperFunctions', 'displaySvgIcon']);
} else {
return;
}
if (!isset($this->context->smarty->registered_plugins['function']['price_split'])) {
$this->context->smarty->registerPlugin('function', 'price_split', ['Oksydan\Module\IsThemeCore\Core\Smarty\SmartyHelperFunctions', 'displayPriceSplit']);
} else {
return;
}
}
}
@@ -8,7 +8,7 @@
'form': general_form,
'form_id': 'settings_general_form',
'form_action': path('is_themecore_module_settings_general_save'),
'card_header': 'General' | trans({}, 'Modules.Isthemecore.Admin'),
'card_header': 'General' | trans({}, 'Modules.isthemecore.Admin'),
'cancel_path': 'is_themecore_module_settings'
} %}
</div>
@@ -18,7 +18,7 @@
'form': webp_form,
'form_id': 'settings_webp_form',
'form_action': path('is_themecore_module_settings_webp_save'),
'card_header': 'Webp' | trans({}, 'Modules.Isthemecore.Admin'),
'card_header': 'Webp' | trans({}, 'Modules.isthemecore.Admin'),
'icon_header': 'image',
'cancel_path': 'is_themecore_module_settings'
} %}