AdminTwigExtension.php 3.1 KB

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