MyTwigExtension.php 6.7KB

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