AdminTwigExtension.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. 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, Page $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 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 $this->grav['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. if (empty($date)) {
  95. return $this->grav['admin']->translate('NICETIME.NO_DATE_PROVIDED', null, true);
  96. }
  97. if ($long_strings) {
  98. $periods = [
  99. 'NICETIME.SECOND',
  100. 'NICETIME.MINUTE',
  101. 'NICETIME.HOUR',
  102. 'NICETIME.DAY',
  103. 'NICETIME.WEEK',
  104. 'NICETIME.MONTH',
  105. 'NICETIME.YEAR',
  106. 'NICETIME.DECADE'
  107. ];
  108. } else {
  109. $periods = [
  110. 'NICETIME.SEC',
  111. 'NICETIME.MIN',
  112. 'NICETIME.HR',
  113. 'NICETIME.DAY',
  114. 'NICETIME.WK',
  115. 'NICETIME.MO',
  116. 'NICETIME.YR',
  117. 'NICETIME.DEC'
  118. ];
  119. }
  120. $lengths = ['60', '60', '24', '7', '4.35', '12', '10'];
  121. $now = time();
  122. // check if unix timestamp
  123. if ((string)(int)$date === (string)$date) {
  124. $unix_date = $date;
  125. } else {
  126. $unix_date = strtotime($date);
  127. }
  128. // check validity of date
  129. if (empty($unix_date)) {
  130. return $this->grav['admin']->translate('NICETIME.BAD_DATE', null, true);
  131. }
  132. // is it future date or past date
  133. if ($now > $unix_date) {
  134. $difference = $now - $unix_date;
  135. $tense = $this->grav['admin']->translate('NICETIME.AGO', null, true);
  136. } else {
  137. $difference = $unix_date - $now;
  138. $tense = $this->grav['admin']->translate('NICETIME.FROM_NOW', null, true);
  139. }
  140. $len = count($lengths) - 1;
  141. for ($j = 0; $difference >= $lengths[$j] && $j < $len; $j++) {
  142. $difference /= $lengths[$j];
  143. }
  144. $difference = round($difference);
  145. if ($difference !== 1) {
  146. $periods[$j] .= '_PLURAL';
  147. }
  148. if ($this->grav['language']->getTranslation($this->grav['user']->language,
  149. $periods[$j] . '_MORE_THAN_TWO')
  150. ) {
  151. if ($difference > 2) {
  152. $periods[$j] .= '_MORE_THAN_TWO';
  153. }
  154. }
  155. $periods[$j] = $this->grav['admin']->translate($periods[$j], null, true);
  156. return "{$difference} {$periods[$j]} {$tense}";
  157. }
  158. }