AdminTwigExtension.php 4.8 KB

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