2018-05-10 10:14:18 +03:00
|
|
|
<?php
|
|
|
|
|
namespace Grav\Plugin\Admin\Twig;
|
|
|
|
|
|
|
|
|
|
use Grav\Common\Grav;
|
2018-08-22 12:45:39 -06:00
|
|
|
use Grav\Common\Yaml;
|
2018-05-10 10:14:18 +03:00
|
|
|
use Grav\Common\Language\Language;
|
2018-05-09 12:24:01 +03:00
|
|
|
use Grav\Common\Page\Page;
|
2018-05-10 10:14:18 +03:00
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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']),
|
2018-10-26 12:56:07 -06:00
|
|
|
new \Twig_SimpleFilter('nested', [$this, 'nestedFilter']),
|
2018-05-10 10:14:18 +03:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getFunctions()
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
new \Twig_SimpleFunction('getPageUrl', [$this, 'getPageUrl'], ['needs_context' => true]),
|
|
|
|
|
new \Twig_SimpleFunction('clone', [$this, 'cloneFunc']),
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-26 12:56:07 -06:00
|
|
|
public function nestedFilter($current, $name)
|
|
|
|
|
{
|
|
|
|
|
$path = explode('.', trim($name, '.'));
|
|
|
|
|
|
|
|
|
|
foreach ($path as $field) {
|
|
|
|
|
if (is_object($current) && isset($current->{$field})) {
|
|
|
|
|
$current = $current->{$field};
|
|
|
|
|
} elseif (is_array($current) && isset($current[$field])) {
|
|
|
|
|
$current = $current[$field];
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $current;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-10 10:14:18 +03:00
|
|
|
public function cloneFunc($obj)
|
|
|
|
|
{
|
|
|
|
|
return clone $obj;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-09 12:24:01 +03:00
|
|
|
public function getPageUrl($context, Page $page)
|
2018-05-10 10:14:18 +03:00
|
|
|
{
|
|
|
|
|
$page_route = trim($page->rawRoute(), '/');
|
|
|
|
|
$page_lang = $page->language();
|
|
|
|
|
$base_url = $context['base_url'];
|
|
|
|
|
$base_url_simple = $context['base_url_simple'];
|
|
|
|
|
$admin_lang = Grav::instance()['session']->admin_lang ?: 'en';
|
|
|
|
|
|
2018-05-09 12:24:01 +03:00
|
|
|
if ($page_lang && $page_lang !== $admin_lang) {
|
2018-05-10 10:14:18 +03:00
|
|
|
$page_url = $base_url_simple . '/' . $page_lang . '/' . $context['admin_route'] . '/pages/' . $page_route;
|
|
|
|
|
} else {
|
|
|
|
|
$page_url = $base_url . '/pages/' . $page_route;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $page_url;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function tuFilter()
|
|
|
|
|
{
|
|
|
|
|
$args = func_get_args();
|
|
|
|
|
$numargs = count($args);
|
|
|
|
|
$lang = null;
|
|
|
|
|
|
|
|
|
|
if (($numargs === 3 && is_array($args[1])) || ($numargs === 2 && !is_array($args[1]))) {
|
|
|
|
|
$lang = array_pop($args);
|
|
|
|
|
} elseif ($numargs === 2 && is_array($args[1])) {
|
|
|
|
|
$subs = array_pop($args);
|
|
|
|
|
$args = array_merge($args, $subs);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->grav['admin']->translate($args, $lang);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-22 12:45:39 -06:00
|
|
|
public function toYamlFilter($value, $inline = null)
|
2018-05-10 10:14:18 +03:00
|
|
|
{
|
|
|
|
|
return Yaml::dump($value, $inline);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function fromYamlFilter($value)
|
|
|
|
|
{
|
2018-08-22 12:45:39 -06:00
|
|
|
return Yaml::parse($value);
|
2018-05-10 10:14:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function adminNicetimeFilter($date, $long_strings = true)
|
|
|
|
|
{
|
|
|
|
|
if (empty($date)) {
|
2018-10-24 14:46:24 -06:00
|
|
|
return $this->grav['admin']->translate('GRAV.NICETIME.NO_DATE_PROVIDED', null, true);
|
2018-05-10 10:14:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 === (string)$date) {
|
|
|
|
|
$unix_date = $date;
|
|
|
|
|
} else {
|
|
|
|
|
$unix_date = strtotime($date);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check validity of date
|
|
|
|
|
if (empty($unix_date)) {
|
2018-10-24 14:46:24 -06:00
|
|
|
return $this->grav['admin']->translate('GRAV.NICETIME.BAD_DATE', null, true);
|
2018-05-10 10:14:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// is it future date or past date
|
|
|
|
|
if ($now > $unix_date) {
|
|
|
|
|
$difference = $now - $unix_date;
|
2018-10-24 14:46:24 -06:00
|
|
|
$tense = $this->grav['admin']->translate('GRAV.NICETIME.AGO', null, true);
|
2018-05-10 10:14:18 +03:00
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
$difference = $unix_date - $now;
|
2018-10-24 14:46:24 -06:00
|
|
|
$tense = $this->grav['admin']->translate('GRAV.NICETIME.FROM_NOW', null, true);
|
2018-05-10 10:14:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$len = count($lengths) - 1;
|
|
|
|
|
for ($j = 0; $difference >= $lengths[$j] && $j < $len; $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';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 14:46:24 -06:00
|
|
|
$periods[$j] = $this->grav['admin']->translate('GRAV.'.$periods[$j], null, true);
|
2018-05-10 10:14:18 +03:00
|
|
|
|
2018-05-09 12:24:01 +03:00
|
|
|
return "{$difference} {$periods[$j]} {$tense}";
|
2018-05-10 10:14:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|