feat(extra-features): Refactor smarty helper

This commit is contained in:
2025-12-31 13:56:41 +01:00
parent 28f8794e16
commit e2c4f265c2

View File

@ -190,25 +190,28 @@ class SmartyHelperFunctions
return $content;
}
/* CUSTOM ADDITIONS BELOW */
public static function isBright($hexColor)
{
$calculator = new ColorBrightnessCalculator();
return $calculator->isBright($hexColor);
}
/* Helper to display SVG icons stored in themes/falcon/_dev/img/icons/
Registered under public_html/config/smartyfront.config.inc.php
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' color='red' width='24' height='24'}
{svg_icon file='person.svg' color='#3498db'}
{svg_icon file='person.svg' width='32'}
{svg_icon file='person.svg' fill='red' width='24' height='24'}
{svg_icon file='person.svg'}
*/
public static function displaySvgIcon($params)
{
if (empty($params['file'])) {
return '';
}
@ -226,34 +229,53 @@ class SmartyHelperFunctions
$svg_content = file_get_contents($icon_path);
// Build style attribute from parameters
$styles = [];
if (!empty($params['width'])) {
$styles[] = 'width: ' . $params['width'] . (is_numeric($params['width']) ? 'px' : '');
}
if (!empty($params['height'])) {
$styles[] = 'height: ' . $params['height'] . (is_numeric($params['height']) ? 'px' : '');
}
if (!empty($params['color'])) {
$styles[] = 'color: ' . $params['color'];
}
// Define SVG attributes that should be replaced/added
$svg_attributes = [
'width',
'height',
'fill',
'stroke',
'stroke-width',
'opacity',
'viewBox',
];
// If we have styles to apply, add them to the SVG
if (!empty($styles)) {
$style_attr = 'style="' . implode('; ', $styles) . '"';
$svg_content = preg_replace('/(<svg[^>]*?)>/', '$1 ' . $style_attr . '>', $svg_content);
}
foreach ($svg_attributes as $attr) {
if (!empty($params[$attr])) {
$value = $params[$attr];
// Check if attribute already exists in the SVG
if (preg_match('/' . preg_quote($attr) . '="[^"]*"/', $svg_content)) {
// Replace existing attribute
$svg_content = preg_replace(
'/' . preg_quote($attr) . '="[^"]*"/',
$attr . '="' . $value . '"',
$svg_content
);
} else {
// Add new attribute to the opening <svg> tag
$svg_content = preg_replace(
'/(<svg[^>]*?)>/',
'$1 ' . $attr . '="' . $value . '">',
$svg_content,
1 // Only replace the first occurrence
);
}
}
}
return $svg_content;
}
public static function displayPriceSplit($price){
/* 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}
*/
public static function displayPriceSplit($price)
{
//Convert to a string and only keep numbers, commas and dots
$price_string = implode(',', $price);
$price_string = preg_replace('/[^0-9,.]/', '', $price_string);
@ -269,5 +291,4 @@ class SmartyHelperFunctions
}
}
}
}