MyTwigExtension.php 6.4KB

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