MyTwigExtension.php 5.4KB

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