Compare commits
3 Commits
main
...
feature/ex
| Author | SHA1 | Date | |
|---|---|---|---|
| ea9b67ffc5 | |||
| e2c4f265c2 | |||
| 28f8794e16 |
@ -190,9 +190,113 @@ class SmartyHelperFunctions
|
|||||||
|
|
||||||
return $content;
|
return $content;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* CUSTOM ADDITIONS BELOW */
|
||||||
|
|
||||||
|
|
||||||
public static function isBright($hexColor)
|
public static function isBright($hexColor)
|
||||||
{
|
{
|
||||||
$calculator = new ColorBrightnessCalculator();
|
$calculator = new ColorBrightnessCalculator();
|
||||||
return $calculator->isBright($hexColor);
|
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){
|
||||||
|
/* 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
|
||||||
|
$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="euros" aria-label="euros">' . $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>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -56,5 +56,15 @@ public function hookActionDispatcherBefore(): void
|
|||||||
} else {
|
} else {
|
||||||
return;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user