diff --git a/is_themecore/src/Core/Smarty/SmartyHelperFunctions.php b/is_themecore/src/Core/Smarty/SmartyHelperFunctions.php index a28ea5f..af12eb4 100644 --- a/is_themecore/src/Core/Smarty/SmartyHelperFunctions.php +++ b/is_themecore/src/Core/Smarty/SmartyHelperFunctions.php @@ -190,9 +190,114 @@ class SmartyHelperFunctions return $content; } - public static function isBright($hexColor) + + + +/* 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('/(]*?)>/', '$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 '' . ($show_currency ? $currency_symbol : '') . $price_parts[0] . ''; + echo '' . $price_parts[1] . ''; + } else { + foreach ($price_parts as $part) { + echo '' . $part . ''; + } + } + } +} \ No newline at end of file diff --git a/is_themecore/src/Hook/Smarty.php b/is_themecore/src/Hook/Smarty.php index 7a9fc00..a83f7ec 100644 --- a/is_themecore/src/Hook/Smarty.php +++ b/is_themecore/src/Hook/Smarty.php @@ -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; + } } }