Skip to content

Instantly share code, notes, and snippets.

@Hotfirenet
Last active April 19, 2017 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hotfirenet/756addd51e0fba49606404497c2aefd8 to your computer and use it in GitHub Desktop.
Save Hotfirenet/756addd51e0fba49606404497c2aefd8 to your computer and use it in GitHub Desktop.
<?php
/*
* Sources:
* http://blog.dev-net.fr/ajoutez-vos-propres-fonctions-a-smarty-depuis-un-module-prestashop/
* http://www.smarty.net/forums/viewtopic.php?p=23628#23628
*/
if (!defined('_PS_VERSION_'))
exit;
class extend_prestashop_smarty extends Module
{
public function __construct()
{
$this->name = 'extend_prestashop_smarty';
$this->tab = 'front_office_features';
$this->version = '0.0.1';
$this->author = 'Johan VIVIEN';
$this->need_instance = 0;
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Extend Smarty');
$this->description = $this->l('Description of my module.');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
if (!Configuration::get('extend_prestashop_smarty'))
$this->warning = $this->l('No name provided');
}
public function install()
{
if (!parent::install())
return false;
if (!$this->registerHook('header'))
return false;
return true;
}
public function uninstall()
{
if ( !parent::uninstall())
return false;
return true;
}
public function hookHeader($params)
{
global $smarty;
smartyRegisterFunction($smarty, 'function', 'sortby', array(&$this,'smarty_modifier_sortby'));
}
public function array_sort_by_fields(&$data, $sortby){
static $sort_funcs = array();
if (empty($sort_funcs[$sortby]))
{
$code = "\$c=0;";
foreach (split(',', $sortby) as $key)
{
$d = '1';
if (substr($key, 0, 1) == '-')
{
$d = '-1';
$key = substr($key, 1);
}
if (substr($key, 0, 1) == '#')
{
$key = substr($key, 1);
$code .= "if ( ( \$c = (\$a['$key'] - \$b['$key'])) != 0 ) return $d * \$c;\n";
}
else
{
$code .= "if ( (\$c = strcasecmp(\$a['$key'],\$b['$key'])) != 0 ) return $d * \$c;\n";
}
}
$code .= 'return $c;';
$sort_func = $sort_funcs[$sortby] = create_function('$a, $b', $code);
}
else
{
$sort_func = $sort_funcs[$sortby];
}
uasort($data, $sort_func);
}
#
# Modifier: sortby - allows arrays of named arrays to be sorted by a given field
#
public function smarty_modifier_sortby($arrData,$sortfields) {
$this->array_sort_by_fields($arrData,$sortfields);
return $arrData;
}
}
@Broceliande
Copy link

Salut,
Je n'ai plus l'accès à l'admin du blog de devnet pour valider ton post.
Mais comme c'est une réponse à mon post initial j'ai eu ton commentaire par mail.
Voici ton problème :
Tu utilises une globale pour smarty : $smarty.
Depuis la 1.5 , la classe module possède sa propre propriété smarty.
Ta fonction devient donc simplement :
public function hookHeader($params) { smartyRegisterFunction($this->smarty, 'function', 'sortby', array(&$this,'smarty_modifier_sortby')); }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment