feat(extra-features): Refactor smarty helper

This commit is contained in:
2025-12-31 13:56:41 +01:00
parent 797bedf573
commit 97a3a05938

View File

@ -192,32 +192,26 @@ class SmartyHelperFunctions
}
/* 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/
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 '';
}
@ -235,36 +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'];
}
// 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);
}
// Define SVG attributes that should be replaced/added
$svg_attributes = [
'width',
'height',
'fill',
'stroke',
'stroke-width',
'opacity',
'viewBox',
];
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;
}
/* Helper to split the price into euros and cents to make it more stylish
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);
@ -280,64 +291,4 @@ class SmartyHelperFunctions
}
}
}
/* Helper to add a read more function to text
registered under public_html/modules/is_themecore/src/Hook/Smarty.php
Use this JS in public_html/themes/falcon/_dev/js/theme/custom.js to handle the read more link:
// Read more buttons
$(".read-more-text").on("click", function (event) {
event.preventDefault();
const $link = $(this);
const $container = $link.closest(".read-more");
$container.find(".truncated-after").toggleClass("d-none");
$container.find(".ellipsis").toggleClass("d-none");
$link.find(".read-more").toggleClass("d-none");
$link.find(".read-less").toggleClass("d-none");
});
Usage example in a TPL file:
{read_more text=$product.description_short maxLength=10 readMoreText={l s='Read more' d='Shop.Theme.Catalog'} readLessText={l s='Read less' d='Shop.Theme.Catalog'}}
*/
public static function readMore($params){
if (empty($params['text'])) {
return '';
}
$text = $params['text'];
$maxLength = isset($params['maxLength']) ? (int)$params['maxLength'] : 200;
$readMoreText = isset($params['readMoreText']) ? $params['readMoreText'] : 'Read more';
$readLessText = isset($params['readLessText']) ? $params['readLessText'] : 'Read less';
// Strip and count text length
$text_stripped = strip_tags($text);
if (strlen($text_stripped) <= $maxLength) {
return $text;
}
// Split text and add read more link
$truncatedBefore = substr($text_stripped, 0, $maxLength);
$lastSpace = strrpos($truncatedBefore, ' ');
if ($lastSpace !== false) {
$truncatedBefore = substr($truncatedBefore, 0, $lastSpace);
$truncatedAfter = substr($text_stripped, $lastSpace);
} else {
$truncatedAfter = substr($text_stripped, $maxLength);
}
return
'<div class="read-more">'.
'<span class="truncated-before">' . $truncatedBefore . '</span>' .
'<span class="ellipsis">...</span>' .
'<span class="truncated-after d-none">' . $truncatedAfter . '</span>' .
'<a class="read-more-text d-flex" href="#">' .
'<span class="read-more">' . $readMoreText . '</span>' .
'<span class="read-less d-none">' . $readLessText . '</span>' .
'</a>'.
'</div>';
}
}
}