112 lines
2.6 KiB
PHP
112 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace App;
|
|
|
|
use Twig_Extension;
|
|
use Twig_SimpleFunction;
|
|
use Twig_SimpleFilter;
|
|
|
|
use function Stringy\create as s;
|
|
|
|
use App\Repositories\Languages;
|
|
use App\Repositories\Users;
|
|
|
|
class Twiggy extends Twig_Extension
|
|
{
|
|
public function getName()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Functions
|
|
* @return array
|
|
*/
|
|
public function getFunctions()
|
|
{
|
|
return [
|
|
new Twig_SimpleFunction('translate', [$this, 'translate']),
|
|
new Twig_SimpleFunction('hasRole', [$this, 'hasRole']),
|
|
new Twig_SimpleFunction('hasPermission', [$this, 'hasPermission']),
|
|
new Twig_SimpleFunction('showMenu', [$this, 'showMenu']),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Filters
|
|
* @return array
|
|
*/
|
|
public function getFilters()
|
|
{
|
|
return [
|
|
new Twig_SimpleFilter('pretty_date', [$this, 'pretty_date']),
|
|
];
|
|
}
|
|
|
|
public function translate($string)
|
|
{
|
|
return Languages::translate($string);
|
|
}
|
|
|
|
public function hasRole($string)
|
|
{
|
|
return Users::hasRole($string);
|
|
}
|
|
|
|
public function hasPermission($string)
|
|
{
|
|
return Users::hasPermission($string);
|
|
}
|
|
|
|
public function showMenu($menu)
|
|
{
|
|
return $menu->get('menu_admin')->asUl();
|
|
}
|
|
|
|
public function pretty_date($date)
|
|
{
|
|
$time = strtotime($date);
|
|
$now = time();
|
|
$ago = $now - $time;
|
|
$futur = 0;
|
|
|
|
if ($ago < 0) {
|
|
$ago = (-1 * $ago);
|
|
$futur = 1;
|
|
}
|
|
|
|
if ($ago < 60) {
|
|
$when = round($ago);
|
|
$s = ($when == 1) ? "seconde" : "secondes";
|
|
$txt = "$when $s";
|
|
} elseif ($ago < 3600) {
|
|
$when = round($ago / 60);
|
|
$m = ($when == 1) ? "minute" : "minutes";
|
|
$txt = "$when $m";
|
|
} elseif ($ago >= 3600 && $ago < 86400) {
|
|
$when = round($ago / 60 / 60);
|
|
$h = ($when == 1) ? "heure" : "heures";
|
|
$txt = "$when $h";
|
|
} elseif ($ago >= 86400 && $ago < 2629743.83) {
|
|
$when = round($ago / 60 / 60 / 24);
|
|
$d = ($when == 1) ? "jour" : "jours";
|
|
$txt = "$when $d";
|
|
} elseif ($ago >= 2629743.83 && $ago < 31556926) {
|
|
$when = round($ago / 60 / 60 / 24 / 30.4375);
|
|
$m = "mois";
|
|
$txt = "$when $m";
|
|
} else {
|
|
$when = round($ago / 60 / 60 / 24 / 365);
|
|
$y = ($when == 1) ? "an" : "ans";
|
|
$txt = "$when $y";
|
|
}
|
|
|
|
if ($futur) {
|
|
$txt = "dans " . $txt;
|
|
} else {
|
|
$txt = "il y a " . $txt;
|
|
}
|
|
return $txt;
|
|
}
|
|
}
|