MyTwigExtension.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace Muzich\CoreBundle\Extension;
  3. use Symfony\Bundle\FrameworkBundle\Translation\Translator;
  4. use Muzich\CoreBundle\Entity\Event;
  5. class MyTwigExtension extends \Twig_Extension {
  6. private $translator;
  7. public function __construct(Translator $translator)
  8. {
  9. $this->translator = $translator;
  10. }
  11. public function getFilters()
  12. {
  13. return array(
  14. 'var_dump' => new \Twig_Filter_Function('var_dump'),
  15. 'date_or_relative_date' => new \Twig_Filter_Method($this, 'date_or_relative_date'),
  16. 'date_epurate' => new \Twig_Filter_Method($this, 'date_epurate')
  17. );
  18. }
  19. public function getFunctions() {
  20. return array(
  21. 'date_or_relative_date' => new \Twig_Filter_Method($this, 'date_or_relative_date'),
  22. 'event_const' => new \Twig_Filter_Method($this, 'event_const')
  23. );
  24. }
  25. protected function datetime2timestamp($string)
  26. {
  27. list($date, $time) = explode(' ', $string);
  28. list($year, $month, $day) = explode('-', $date);
  29. list($hour, $minute, $second) = explode(':', $time);
  30. $timestamp = mktime($hour, $minute, $second, $month, $day, $year);
  31. return $timestamp;
  32. }
  33. protected function translate_date_relative($tr, $type, $x)
  34. {
  35. if ($x != 1)
  36. {
  37. return $this->translator->trans(
  38. $tr.'x_'.$type,
  39. array('%x%' => $x),
  40. 'messages'
  41. );
  42. }
  43. return $this->translator->trans(
  44. $tr.'one_'.$type,
  45. array(),
  46. 'messages'
  47. );
  48. }
  49. public function date_or_relative_date($sentence, $context = "default")
  50. {
  51. $iTimeDifference = time() - $this->datetime2timestamp($sentence);
  52. if( $iTimeDifference<0 )
  53. {
  54. return $this->translator->trans('date.instant', array(), 'userui');;
  55. }
  56. $iSeconds = $iTimeDifference ;
  57. $iMinutes = round( $iTimeDifference/60 );
  58. $iHours = round( $iTimeDifference/3600 );
  59. $iDays = round( $iTimeDifference/86400 );
  60. $iWeeks = round( $iTimeDifference/604800 );
  61. $iMonths = round( $iTimeDifference/2419200 );
  62. $iYears = round( $iTimeDifference/29030400 );
  63. $tr = 'date_since.'.$context.'.';
  64. if( $iSeconds<60 )
  65. {
  66. return $this->translator->trans('date_since.'.$context.'.less_min', array(), 'messages');
  67. }
  68. elseif( $iMinutes<60 )
  69. {
  70. return $this->translate_date_relative($tr, 'min', $iMinutes);
  71. }
  72. elseif( $iHours<24 )
  73. {
  74. return $this->translate_date_relative($tr, 'hour', $iHours);
  75. }
  76. elseif( $iDays<7 )
  77. {
  78. return $this->translate_date_relative($tr, 'day', $iDays);
  79. }
  80. elseif( $iWeeks <4 )
  81. {
  82. return $this->translate_date_relative($tr, 'week', $iWeeks);
  83. }
  84. elseif( $iMonths<12 )
  85. {
  86. return $this->translate_date_relative($tr, 'month', $iMonths);
  87. }
  88. else
  89. {
  90. return $this->translate_date_relative($tr, 'year', $iYears);
  91. }
  92. }
  93. public function getName()
  94. {
  95. return 'my_twig_extension';
  96. }
  97. public function date_epurate($date)
  98. {
  99. $date = str_replace(' ', '', $date);
  100. $date = str_replace('-', '', $date);
  101. $date = str_replace(':', '', $date);
  102. $date = str_replace('.', '', $date);
  103. return $date;
  104. }
  105. public function event_const($const_name)
  106. {
  107. switch ($const_name)
  108. {
  109. case 'TYPE_COMMENT_ADDED_ELEMENT':
  110. return Event::TYPE_COMMENT_ADDED_ELEMENT;
  111. break;
  112. case 'TYPE_FAV_ADDED_ELEMENT':
  113. return Event::TYPE_FAV_ADDED_ELEMENT;
  114. break;
  115. case 'TYPE_USER_FOLLOW':
  116. return Event::TYPE_USER_FOLLOW;
  117. break;
  118. case 'TYPE_TAGS_PROPOSED':
  119. return Event::TYPE_TAGS_PROPOSED;
  120. break;
  121. default:
  122. throw new \Exception('Constante non géré dans MyTwigExtension::event_const');
  123. break;
  124. }
  125. return null;
  126. }
  127. }