AdminTwigExtension.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Grav\Plugin\Admin\Twig;
  3. use Grav\Common\Data\Data;
  4. use Grav\Common\Grav;
  5. use Grav\Common\Page\Interfaces\PageInterface;
  6. use Grav\Common\Utils;
  7. use Grav\Common\Yaml;
  8. use Grav\Common\Language\Language;
  9. use Twig\Extension\AbstractExtension;
  10. use Twig\TwigFilter;
  11. use Twig\TwigFunction;
  12. use Grav\Plugin\Admin\Admin;
  13. class AdminTwigExtension extends AbstractExtension
  14. {
  15. /** @var Grav */
  16. protected $grav;
  17. /** @var Language $lang */
  18. protected $lang;
  19. public function __construct()
  20. {
  21. $this->grav = Grav::instance();
  22. $this->lang = $this->grav['user']->language;
  23. }
  24. public function getFilters(): array
  25. {
  26. return [
  27. new TwigFilter('tu', [$this, 'tuFilter']),
  28. new TwigFilter('toYaml', [$this, 'toYamlFilter']),
  29. new TwigFilter('fromYaml', [$this, 'fromYamlFilter']),
  30. new TwigFilter('adminNicetime', [$this, 'adminNicetimeFilter']),
  31. new TwigFilter('nested', [$this, 'nestedFilter']),
  32. new TwigFilter('flatten', [$this, 'flattenFilter']),
  33. ];
  34. }
  35. public function getFunctions(): array
  36. {
  37. return [
  38. new TwigFunction('admin_route', [$this, 'adminRouteFunc']),
  39. new TwigFunction('getPageUrl', [$this, 'getPageUrl']),
  40. new TwigFunction('clone', [$this, 'cloneFunc']),
  41. new TwigFunction('data', [$this, 'dataFunc']),
  42. ];
  43. }
  44. public function nestedFilter($current, $name)
  45. {
  46. $path = explode('.', trim($name, '.'));
  47. foreach ($path as $field) {
  48. if (is_object($current) && isset($current->{$field})) {
  49. $current = $current->{$field};
  50. } elseif (is_array($current) && isset($current[$field])) {
  51. $current = $current[$field];
  52. } else {
  53. return null;
  54. }
  55. }
  56. return $current;
  57. }
  58. public function flattenFilter($array)
  59. {
  60. return Utils::arrayFlattenDotNotation($array);
  61. }
  62. public function cloneFunc($obj)
  63. {
  64. return clone $obj;
  65. }
  66. public function adminRouteFunc(string $route = '', string $languageCode = null)
  67. {
  68. /** @var Admin $admin */
  69. $admin = Grav::instance()['admin'];
  70. return $admin->getAdminRoute($route, $languageCode)->toString(true);
  71. }
  72. public function getPageUrl(PageInterface $page)
  73. {
  74. /** @var Admin $admin */
  75. $admin = Grav::instance()['admin'];
  76. return $admin->getAdminRoute('/pages' . $page->rawRoute(), $page->language())->toString(true);
  77. }
  78. public static function tuFilter()
  79. {
  80. $args = func_get_args();
  81. $numargs = count($args);
  82. $lang = null;
  83. if (($numargs === 3 && is_array($args[1])) || ($numargs === 2 && !is_array($args[1]))) {
  84. $lang = array_pop($args);
  85. } elseif ($numargs === 2 && is_array($args[1])) {
  86. $subs = array_pop($args);
  87. $args = array_merge($args, $subs);
  88. }
  89. return Grav::instance()['admin']->translate($args, $lang);
  90. }
  91. public function toYamlFilter($value, $inline = null)
  92. {
  93. return Yaml::dump($value, $inline);
  94. }
  95. public function fromYamlFilter($value)
  96. {
  97. return Yaml::parse($value);
  98. }
  99. public function adminNicetimeFilter($date, $long_strings = true)
  100. {
  101. return Grav::instance()['admin']->adminNiceTime($date, $long_strings);
  102. }
  103. public function dataFunc(array $data, $blueprints = null)
  104. {
  105. return new Data($data, $blueprints);
  106. }
  107. }