privilegedemarque/modules/customsmartyfunctions/customsmartyfunctions.php
Serveur preprod f0c0c48223 first push
2016-04-14 16:14:31 +02:00

148 lines
4.1 KiB
PHP
Executable File

<?php
if (!defined('_PS_VERSION_'))
exit;
class CustomSmartyFunctions extends Module
{
public function __construct(){
$this->name = 'customsmartyfunctions';
$this->tab = 'front_office_features';
$this->version = '1.0';
$this->author = 'Antadis';
parent::__construct();
$this->displayName = $this->l('Custom smarty functions');
$this->description = $this->l('Allow the use of custom smarty functions.');
}
public function install()
{
if ( !parent::install() ||
!$this->registerHook('header') ||
!$this->registerHook('displayTop') ||
!$this->registerHook('displayPriceFormat')
)
return false;
return true;
}
public function uninstall(){
return parent::uninstall();
}
public function hookDisplayTop($params){
}
/**
* Smarty function declarations
*/
public function hookHeader($params)
{
global $smarty;
smartyRegisterFunction($smarty, 'function', 'convertPriceFormat', array(&$this,'convertPriceFormat'));
smartyRegisterFunction($smarty, 'function', 'vd', array(&$this,'formatVarDump'));
}
public function hookDisplayPriceFormat($params){
return $this->convertPriceFormat($params);
}
/**
* ===========================
* CUSTOM FUNTIONS
* ===========================
**/
/**
* Return price converted to specified format
*
* @param float $price Product price
* @param object $currency Current currency object
* @param boolean $to_currency convert to currency or from currency to default currency
* @param string $format format to display (%p for rounded price (without cents), %c for cents, %$ for currecny sign)
* @param int $decimals number of decimals for cents
*/
public function convertPriceFormat($params)
{
$price = $params['price'];
if ( isset($params['decimals']) )
$decimals = $params['decimals'];
else
$decimals = 2;
if ( isset($params['format']) )
$format = $params['format'];
else
$format = '%p,%c<sup>%$</sup>';
if ( isset($params['currency']) )
$currency = $params['currency'];
else
$currency = null;
if ( isset($params['to_currency']) )
$to_currency = $params['to_currency'];
else
$to_currency = true;
if( isset($params['debug']) )
$debug = $params['debug'];
else
$debug = false;
$default_currency = (int)Configuration::get('PS_CURRENCY_DEFAULT');
$context = Context::getContext();
if ($currency === null)
$currency = $context->currency;
elseif (is_numeric($currency))
$currency = Currency::getCurrencyInstance($currency);
$c_id = (is_array($currency) ? $currency['id_currency'] : $currency->id);
$c_sign = (is_array($currency) ? $currency['sign'] : $currency->sign);
$c_rate = (is_array($currency) ? $currency['conversion_rate'] : $currency->conversion_rate);
if ($c_id != $default_currency)
{
if ($to_currency)
$price *= $c_rate;
else
$price /= $c_rate;
}
$round_price = floor($price);
$price_cents = str_pad(round($price - $round_price,2) * pow(10, $decimals), $decimals, "0", STR_PAD_LEFT);
if ($price_cents >= pow(10, $decimals) ){
$round_price++;
$price_cents = str_pad($price_cents - pow(10, $decimals), $decimals, "0", STR_PAD_LEFT);
}
$displayPrice = str_replace('%$', $c_sign, str_replace('%c', $price_cents, str_replace('%p', $round_price, $format)));
if($debug){
$tab = '&nbsp;&nbsp;';
$debug_display = '<p>Debug {
<br/>'.$tab.'<b>decimals: </b>'.$decimals.'
<br/>'.$tab.'<b>format: </b>'.$format.'
<br/>'.$tab.'<b>currency sign: </b>'.$c_sign.'
<br/>'.$tab.'<b>currency rate: </b>'.$c_rate.'
<br/>'.$tab.'<b>price: </b>'.$price.'
<br/>'.$tab.'<b>round_price: </b>'.$round_price.'
<br/>'.$tab.'<b>price_cents: </b>'.$price_cents.'
<br/>'.$tab.'<b>display_price: </b>'.$displayPrice.'
<br/>}</p>';
}
else
$debug_display = false;
return $displayPrice.($debug_display?' '.$debug_display:'');
}
public function formatVarDump($params){
echo '<div style="background: none repeat scroll 0 0 rgba(0, 0, 0, 0.7); color: #fff; padding: 1px; position: relative; z-index: 99999;">
<span onclick="$(this).parent().children(\'pre\').toggle(150);">[Debug]</span>
<pre style="display:none">';var_dump($params['s']);echo'</pre>
</div>';
}
}