mirror of
https://github.com/getgrav/grav-plugin-admin.git
synced 2025-11-04 20:36:03 +01:00
144 lines
3.6 KiB
PHP
144 lines
3.6 KiB
PHP
<?php
|
|
namespace Grav\Plugin;
|
|
|
|
use Grav\Common\Grav;
|
|
use Grav\Common\Language\Language;
|
|
use Symfony\Component\Yaml\Yaml;
|
|
use Symfony\Component\Yaml\Parser;
|
|
|
|
class AdminTwigExtension extends \Twig_Extension
|
|
{
|
|
/**
|
|
* @var Grav
|
|
*/
|
|
protected $grav;
|
|
|
|
/**
|
|
* @var Language $lang
|
|
*/
|
|
protected $lang;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->grav = Grav::instance();
|
|
$this->lang = $this->grav['user']->language;
|
|
}
|
|
|
|
/**
|
|
* Returns extension name.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getName()
|
|
{
|
|
return 'AdminTwigExtension';
|
|
}
|
|
|
|
public function getFilters()
|
|
{
|
|
return [
|
|
new \Twig_SimpleFilter('tu', [$this, 'tuFilter']),
|
|
new \Twig_SimpleFilter('toYaml', [$this, 'toYamlFilter']),
|
|
new \Twig_SimpleFilter('fromYaml', [$this, 'fromYamlFilter']),
|
|
new \Twig_SimpleFilter('adminNicetime', [$this, 'adminNicetimeFilter']),
|
|
|
|
];
|
|
}
|
|
|
|
public function tuFilter($args, $lang = null)
|
|
{
|
|
return $this->grav['admin']->translate($args, $lang);
|
|
}
|
|
|
|
public function toYamlFilter($value, $inline = true)
|
|
{
|
|
return Yaml::dump($value, $inline);
|
|
|
|
}
|
|
|
|
public function fromYamlFilter($value)
|
|
{
|
|
$yaml = new Parser();
|
|
return $yaml->parse($value);
|
|
}
|
|
|
|
public function adminNicetimeFilter($date, $long_strings = true)
|
|
{
|
|
if (empty($date)) {
|
|
return $this->grav['admin']->translate('NICETIME.NO_DATE_PROVIDED', null, true);
|
|
}
|
|
|
|
if ($long_strings) {
|
|
$periods = [
|
|
"NICETIME.SECOND",
|
|
"NICETIME.MINUTE",
|
|
"NICETIME.HOUR",
|
|
"NICETIME.DAY",
|
|
"NICETIME.WEEK",
|
|
"NICETIME.MONTH",
|
|
"NICETIME.YEAR",
|
|
"NICETIME.DECADE"
|
|
];
|
|
} else {
|
|
$periods = [
|
|
"NICETIME.SEC",
|
|
"NICETIME.MIN",
|
|
"NICETIME.HR",
|
|
"NICETIME.DAY",
|
|
"NICETIME.WK",
|
|
"NICETIME.MO",
|
|
"NICETIME.YR",
|
|
"NICETIME.DEC"
|
|
];
|
|
}
|
|
|
|
$lengths = ["60", "60", "24", "7", "4.35", "12", "10"];
|
|
|
|
$now = time();
|
|
|
|
// check if unix timestamp
|
|
if ((string)(int)$date == $date) {
|
|
$unix_date = $date;
|
|
} else {
|
|
$unix_date = strtotime($date);
|
|
}
|
|
|
|
// check validity of date
|
|
if (empty($unix_date)) {
|
|
return $this->grav['admin']->translate('NICETIME.BAD_DATE', null, true);
|
|
}
|
|
|
|
// is it future date or past date
|
|
if ($now > $unix_date) {
|
|
$difference = $now - $unix_date;
|
|
$tense = $this->grav['admin']->translate('NICETIME.AGO', null, true);
|
|
|
|
} else {
|
|
$difference = $unix_date - $now;
|
|
$tense = $this->grav['admin']->translate('NICETIME.FROM_NOW', null, true);
|
|
}
|
|
|
|
for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths) - 1; $j++) {
|
|
$difference /= $lengths[$j];
|
|
}
|
|
|
|
$difference = round($difference);
|
|
|
|
if ($difference != 1) {
|
|
$periods[$j] .= '_PLURAL';
|
|
}
|
|
|
|
if ($this->grav['language']->getTranslation($this->grav['user']->language,
|
|
$periods[$j] . '_MORE_THAN_TWO')
|
|
) {
|
|
if ($difference > 2) {
|
|
$periods[$j] .= '_MORE_THAN_TWO';
|
|
}
|
|
}
|
|
|
|
$periods[$j] = $this->grav['admin']->translate($periods[$j], null, true);
|
|
|
|
return "$difference $periods[$j] {$tense}";
|
|
}
|
|
}
|