2018-05-10 10:14:18 +03:00
|
|
|
<?php
|
2019-03-20 12:52:16 +02:00
|
|
|
|
2018-05-10 10:14:18 +03:00
|
|
|
namespace Grav\Plugin\Admin\Twig;
|
|
|
|
|
|
|
|
|
|
use Grav\Common\Grav;
|
2019-03-05 15:46:55 +02:00
|
|
|
use Grav\Common\Page\Interfaces\PageInterface;
|
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;
|
2019-06-02 15:21:24 +03:00
|
|
|
use Twig\Extension\AbstractExtension;
|
|
|
|
|
use Twig\TwigFilter;
|
|
|
|
|
use Twig\TwigFunction;
|
2018-05-10 10:14:18 +03:00
|
|
|
|
2019-06-02 15:21:24 +03:00
|
|
|
class AdminTwigExtension extends AbstractExtension
|
2018-05-10 10:14:18 +03:00
|
|
|
{
|
2019-06-02 15:21:24 +03:00
|
|
|
/** @var Grav */
|
2018-05-10 10:14:18 +03:00
|
|
|
protected $grav;
|
|
|
|
|
|
2019-06-02 15:21:24 +03:00
|
|
|
/** @var Language $lang */
|
2018-05-10 10:14:18 +03:00
|
|
|
protected $lang;
|
|
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->grav = Grav::instance();
|
|
|
|
|
$this->lang = $this->grav['user']->language;
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-02 15:21:24 +03:00
|
|
|
public function getFilters(): array
|
2018-05-10 10:14:18 +03:00
|
|
|
{
|
|
|
|
|
return [
|
2019-06-02 15:21:24 +03:00
|
|
|
new TwigFilter('tu', [$this, 'tuFilter']),
|
|
|
|
|
new TwigFilter('toYaml', [$this, 'toYamlFilter']),
|
|
|
|
|
new TwigFilter('fromYaml', [$this, 'fromYamlFilter']),
|
|
|
|
|
new TwigFilter('adminNicetime', [$this, 'adminNicetimeFilter']),
|
|
|
|
|
new TwigFilter('nested', [$this, 'nestedFilter']),
|
2018-05-10 10:14:18 +03:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2019-06-02 15:21:24 +03:00
|
|
|
public function getFunctions(): array
|
2018-05-10 10:14:18 +03:00
|
|
|
{
|
|
|
|
|
return [
|
2019-06-02 15:21:24 +03:00
|
|
|
new TwigFunction('getPageUrl', [$this, 'getPageUrl'], ['needs_context' => true]),
|
|
|
|
|
new TwigFunction('clone', [$this, 'cloneFunc']),
|
2018-05-10 10:14:18 +03:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-05 15:46:55 +02:00
|
|
|
public function getPageUrl($context, PageInterface $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;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 13:01:33 -07:00
|
|
|
public static function tuFilter()
|
2018-05-10 10:14:18 +03:00
|
|
|
{
|
|
|
|
|
$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);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-04 13:01:33 -07:00
|
|
|
return Grav::instance()['admin']->translate($args, $lang);
|
2018-05-10 10:14:18 +03:00
|
|
|
}
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2019-02-08 17:17:05 -07:00
|
|
|
return Grav::instance()['admin']->adminNiceTime($date, $long_strings);
|
2018-05-10 10:14:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|