AdminTwigExtension.php 4.7 KB

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