AdminTwigExtension.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Grav\Plugin\Admin;
  3. use Grav\Common\Grav;
  4. use Grav\Common\Language\Language;
  5. use Symfony\Component\Yaml\Yaml;
  6. use Symfony\Component\Yaml\Parser;
  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. /**
  23. * Returns extension name.
  24. *
  25. * @return string
  26. */
  27. public function getName()
  28. {
  29. return 'AdminTwigExtension';
  30. }
  31. public function getFilters()
  32. {
  33. return [
  34. new \Twig_SimpleFilter('tu', [$this, 'tuFilter']),
  35. new \Twig_SimpleFilter('toYaml', [$this, 'toYamlFilter']),
  36. new \Twig_SimpleFilter('fromYaml', [$this, 'fromYamlFilter']),
  37. new \Twig_SimpleFilter('adminNicetime', [$this, 'adminNicetimeFilter']),
  38. ];
  39. }
  40. public function getFunctions()
  41. {
  42. return [
  43. new \Twig_SimpleFunction('getPageUrl', [$this, 'getPageUrl'], ['needs_context' => true]),
  44. new \Twig_SimpleFunction('clone', [$this, 'cloneFunc']),
  45. ];
  46. }
  47. public function cloneFunc($obj)
  48. {
  49. return clone $obj;
  50. }
  51. public function getPageUrl($context, $page)
  52. {
  53. $page_route = trim($page->rawRoute(), '/');
  54. $page_lang = $page->language();
  55. $base_url = $context['base_url'];
  56. $base_url_simple = $context['base_url_simple'];
  57. $admin_lang = Grav::instance()['session']->admin_lang ?: 'en';
  58. if ($page_lang && $page_lang != $admin_lang) {
  59. $page_url = $base_url_simple . '/' . $page_lang . '/' . $context['admin_route'] . '/pages/' . $page_route;
  60. } else {
  61. $page_url = $base_url . '/pages/' . $page_route;
  62. }
  63. return $page_url;
  64. }
  65. public function tuFilter()
  66. {
  67. $args = func_get_args();
  68. $numargs = count($args);
  69. $lang = null;
  70. if (($numargs == 3 && is_array($args[1])) || ($numargs == 2 && !is_array($args[1]))) {
  71. $lang = array_pop($args);
  72. } elseif ($numargs == 2 && is_array($args[1])) {
  73. $subs = array_pop($args);
  74. $args = array_merge($args, $subs);
  75. }
  76. return $this->grav['admin']->translate($args, $lang);
  77. }
  78. public function toYamlFilter($value, $inline = true)
  79. {
  80. return Yaml::dump($value, $inline);
  81. }
  82. public function fromYamlFilter($value)
  83. {
  84. $yaml = new Parser();
  85. return $yaml->parse($value);
  86. }
  87. public function adminNicetimeFilter($date, $long_strings = true)
  88. {
  89. if (empty($date)) {
  90. return $this->grav['admin']->translate('NICETIME.NO_DATE_PROVIDED', null, true);
  91. }
  92. if ($long_strings) {
  93. $periods = [
  94. "NICETIME.SECOND",
  95. "NICETIME.MINUTE",
  96. "NICETIME.HOUR",
  97. "NICETIME.DAY",
  98. "NICETIME.WEEK",
  99. "NICETIME.MONTH",
  100. "NICETIME.YEAR",
  101. "NICETIME.DECADE"
  102. ];
  103. } else {
  104. $periods = [
  105. "NICETIME.SEC",
  106. "NICETIME.MIN",
  107. "NICETIME.HR",
  108. "NICETIME.DAY",
  109. "NICETIME.WK",
  110. "NICETIME.MO",
  111. "NICETIME.YR",
  112. "NICETIME.DEC"
  113. ];
  114. }
  115. $lengths = ["60", "60", "24", "7", "4.35", "12", "10"];
  116. $now = time();
  117. // check if unix timestamp
  118. if ((string)(int)$date == $date) {
  119. $unix_date = $date;
  120. } else {
  121. $unix_date = strtotime($date);
  122. }
  123. // check validity of date
  124. if (empty($unix_date)) {
  125. return $this->grav['admin']->translate('NICETIME.BAD_DATE', null, true);
  126. }
  127. // is it future date or past date
  128. if ($now > $unix_date) {
  129. $difference = $now - $unix_date;
  130. $tense = $this->grav['admin']->translate('NICETIME.AGO', null, true);
  131. } else {
  132. $difference = $unix_date - $now;
  133. $tense = $this->grav['admin']->translate('NICETIME.FROM_NOW', null, true);
  134. }
  135. for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths) - 1; $j++) {
  136. $difference /= $lengths[$j];
  137. }
  138. $difference = round($difference);
  139. if ($difference != 1) {
  140. $periods[$j] .= '_PLURAL';
  141. }
  142. if ($this->grav['language']->getTranslation($this->grav['user']->language,
  143. $periods[$j] . '_MORE_THAN_TWO')
  144. ) {
  145. if ($difference > 2) {
  146. $periods[$j] .= '_MORE_THAN_TWO';
  147. }
  148. }
  149. $periods[$j] = $this->grav['admin']->translate($periods[$j], null, true);
  150. return "$difference $periods[$j] {$tense}";
  151. }
  152. }