MyTwigExtension.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. namespace Muzich\CoreBundle\Extension;
  3. use Symfony\Bundle\FrameworkBundle\Translation\Translator;
  4. use Muzich\CoreBundle\Entity\Event;
  5. use Symfony\Component\Form\FormView;
  6. class MyTwigExtension extends \Twig_Extension {
  7. private $translator;
  8. public function __construct(Translator $translator)
  9. {
  10. $this->translator = $translator;
  11. }
  12. public function getFilters()
  13. {
  14. return array(
  15. 'var_dump' => new \Twig_Filter_Function('var_dump'),
  16. 'date_or_relative_date' => new \Twig_Filter_Method($this, 'date_or_relative_date'),
  17. 'date_epurate' => new \Twig_Filter_Method($this, 'date_epurate'),
  18. 'form_has_errors' => new \Twig_Filter_Method($this, 'form_has_errors'),
  19. 'format_score' => new \Twig_Filter_Method($this, 'format_score')
  20. );
  21. }
  22. public function getFunctions() {
  23. return array(
  24. 'date_or_relative_date' => new \Twig_Filter_Method($this, 'date_or_relative_date'),
  25. 'event_const' => new \Twig_Filter_Method($this, 'event_const'),
  26. 'css_list_length_class' => new \Twig_Filter_Method($this, 'getCssLengthClassForList')
  27. );
  28. }
  29. public function format_score($score)
  30. {
  31. return number_format($score, 0, '.', ' ');
  32. }
  33. protected function datetime2timestamp($string)
  34. {
  35. list($date, $time) = explode(' ', $string);
  36. list($year, $month, $day) = explode('-', $date);
  37. list($hour, $minute, $second) = explode(':', $time);
  38. $timestamp = mktime($hour, $minute, $second, $month, $day, $year);
  39. return $timestamp;
  40. }
  41. protected function translate_date_relative($tr, $type, $x)
  42. {
  43. if ($x != 1)
  44. {
  45. return $this->translator->trans(
  46. $tr.'x_'.$type,
  47. array('%x%' => $x),
  48. 'messages'
  49. );
  50. }
  51. return $this->translator->trans(
  52. $tr.'one_'.$type,
  53. array(),
  54. 'messages'
  55. );
  56. }
  57. public function date_or_relative_date($sentence, $context = "default")
  58. {
  59. $iTimeDifference = time() - $this->datetime2timestamp($sentence);
  60. if( $iTimeDifference<0 )
  61. {
  62. return $this->translator->trans('date.instant', array(), 'userui');;
  63. }
  64. $iSeconds = $iTimeDifference ;
  65. $iMinutes = round( $iTimeDifference/60 );
  66. $iHours = round( $iTimeDifference/3600 );
  67. $iDays = round( $iTimeDifference/86400 );
  68. $iWeeks = round( $iTimeDifference/604800 );
  69. $iMonths = round( $iTimeDifference/2419200 );
  70. $iYears = round( $iTimeDifference/29030400 );
  71. $tr = 'date_since.'.$context.'.';
  72. if( $iSeconds<60 )
  73. {
  74. return $this->translator->trans('date_since.'.$context.'.less_min', array(), 'messages');
  75. }
  76. elseif( $iMinutes<60 )
  77. {
  78. return $this->translate_date_relative($tr, 'min', $iMinutes);
  79. }
  80. elseif( $iHours<24 )
  81. {
  82. return $this->translate_date_relative($tr, 'hour', $iHours);
  83. }
  84. elseif( $iDays<7 )
  85. {
  86. return $this->translate_date_relative($tr, 'day', $iDays);
  87. }
  88. elseif( $iWeeks <4 )
  89. {
  90. return $this->translate_date_relative($tr, 'week', $iWeeks);
  91. }
  92. elseif( $iMonths<12 )
  93. {
  94. return $this->translate_date_relative($tr, 'month', $iMonths);
  95. }
  96. else
  97. {
  98. return $this->translate_date_relative($tr, 'year', $iYears);
  99. }
  100. }
  101. public function getName()
  102. {
  103. return 'my_twig_extension';
  104. }
  105. public function date_epurate($date)
  106. {
  107. $date = str_replace(' ', '', $date);
  108. $date = str_replace('-', '', $date);
  109. $date = str_replace(':', '', $date);
  110. $date = str_replace('.', '', $date);
  111. return $date;
  112. }
  113. public function event_const($const_name)
  114. {
  115. switch ($const_name)
  116. {
  117. case 'TYPE_COMMENT_ADDED_ELEMENT':
  118. return Event::TYPE_COMMENT_ADDED_ELEMENT;
  119. break;
  120. case 'TYPE_FAV_ADDED_ELEMENT':
  121. return Event::TYPE_FAV_ADDED_ELEMENT;
  122. break;
  123. case 'TYPE_USER_FOLLOW':
  124. return Event::TYPE_USER_FOLLOW;
  125. break;
  126. case 'TYPE_TAGS_PROPOSED':
  127. return Event::TYPE_TAGS_PROPOSED;
  128. break;
  129. default:
  130. throw new \Exception('Constante non géré dans MyTwigExtension::event_const');
  131. break;
  132. }
  133. return null;
  134. }
  135. /**
  136. * Cette fonction retourne une classe CSS (string) permettant de donner
  137. * de l'importance a des élements de liste en foncyion de leur position dans
  138. * la liste.
  139. *
  140. * @param int $position
  141. * @param int $count
  142. * @return string
  143. */
  144. public function getCssLengthClassForList($position, $count)
  145. {
  146. // On établie 3 type de taille
  147. // grand, moyen, standart
  148. // chacun correspondant a 1/3
  149. if ($position <= $count/3)
  150. {
  151. return 'list_length_big';
  152. }
  153. elseif ($position <= ($count/3)*2)
  154. {
  155. return 'list_length_medium';
  156. }
  157. else
  158. {
  159. return 'list_length_default';
  160. }
  161. }
  162. public function form_has_errors(FormView $form)
  163. {
  164. $form_vars = $form->getVars();
  165. $count_error = count($form_vars['errors']);
  166. foreach ($form->getChildren() as $form_children)
  167. {
  168. $form_children_vars = $form_children->getVars();
  169. $count_error += count($form_children_vars['errors']);
  170. }
  171. if ($count_error)
  172. {
  173. return true;
  174. }
  175. return false;
  176. }
  177. }