|
@@ -0,0 +1,109 @@
|
|
1
|
+<?php
|
|
2
|
+
|
|
3
|
+namespace Muzich\CoreBundle\Extension;
|
|
4
|
+
|
|
5
|
+use Symfony\Bundle\FrameworkBundle\Translation\Translator;
|
|
6
|
+
|
|
7
|
+class MyTwigExtension extends \Twig_Extension {
|
|
8
|
+
|
|
9
|
+ private $translator;
|
|
10
|
+
|
|
11
|
+ public function __construct(Translator $translator)
|
|
12
|
+ {
|
|
13
|
+ $this->translator = $translator;
|
|
14
|
+ }
|
|
15
|
+
|
|
16
|
+ public function getFilters()
|
|
17
|
+ {
|
|
18
|
+ return array(
|
|
19
|
+ 'var_dump' => new \Twig_Filter_Function('var_dump'),
|
|
20
|
+ 'date_or_relative_date' => new \Twig_Filter_Method($this, 'date_or_relative_date')
|
|
21
|
+ );
|
|
22
|
+ }
|
|
23
|
+
|
|
24
|
+ protected function datetime2timestamp($string)
|
|
25
|
+ {
|
|
26
|
+ list($date, $time) = explode(' ', $string);
|
|
27
|
+ list($year, $month, $day) = explode('-', $date);
|
|
28
|
+ list($hour, $minute, $second) = explode(':', $time);
|
|
29
|
+
|
|
30
|
+ $timestamp = mktime($hour, $minute, $second, $month, $day, $year);
|
|
31
|
+
|
|
32
|
+ return $timestamp;
|
|
33
|
+ }
|
|
34
|
+
|
|
35
|
+ public function date_or_relative_date($sentence, $expr = null)
|
|
36
|
+ {
|
|
37
|
+ $iTimeDifference = time() - $this->datetime2timestamp($sentence);
|
|
38
|
+ if( $iTimeDifference<0 )
|
|
39
|
+ {
|
|
40
|
+ return $this->translator->trans('date.instant', array(), 'userui');;
|
|
41
|
+ }
|
|
42
|
+ $iSeconds = $iTimeDifference ;
|
|
43
|
+ $iMinutes = round( $iTimeDifference/60 );
|
|
44
|
+ $iHours = round( $iTimeDifference/3600 );
|
|
45
|
+ $iDays = round( $iTimeDifference/86400 );
|
|
46
|
+ $iWeeks = round( $iTimeDifference/604800 );
|
|
47
|
+ $iMonths = round( $iTimeDifference/2419200 );
|
|
48
|
+ $iYears = round( $iTimeDifference/29030400 );
|
|
49
|
+
|
|
50
|
+ if( $iSeconds<60 )
|
|
51
|
+ {
|
|
52
|
+ return $this->translator->trans('date.less_than_minute', array(), 'userui');
|
|
53
|
+ }
|
|
54
|
+ elseif( $iMinutes<60 )
|
|
55
|
+ {
|
|
56
|
+ return $this->translator->transChoice(
|
|
57
|
+ 'il y a une minute|Il y a %count% minutes',
|
|
58
|
+ 1,
|
|
59
|
+ array('%count%' => $iMinutes)
|
|
60
|
+ );
|
|
61
|
+ }
|
|
62
|
+ elseif( $iHours<24 )
|
|
63
|
+ {
|
|
64
|
+ return $this->translator->transChoice(
|
|
65
|
+ 'il y a une heure|Il y a %count% heures',
|
|
66
|
+ 1,
|
|
67
|
+ array('%count%' => $iHours)
|
|
68
|
+ );
|
|
69
|
+ }
|
|
70
|
+ elseif( $iDays<7 )
|
|
71
|
+ {
|
|
72
|
+ return $this->translator->transChoice(
|
|
73
|
+ 'il y a un jour|Il y a %count% jours',
|
|
74
|
+ 1,
|
|
75
|
+ array('%count%' => $iDays)
|
|
76
|
+ );
|
|
77
|
+ }
|
|
78
|
+ elseif( $iWeeks <4 )
|
|
79
|
+ {
|
|
80
|
+ return $this->translator->transChoice(
|
|
81
|
+ 'il y a une semaine|Il y a %count% semaines',
|
|
82
|
+ 1,
|
|
83
|
+ array('%count%' => $iWeeks)
|
|
84
|
+ );
|
|
85
|
+ }
|
|
86
|
+ elseif( $iMonths<12 )
|
|
87
|
+ {
|
|
88
|
+ return $this->translator->transChoice(
|
|
89
|
+ 'il y a un mois|Il y a %count% mois',
|
|
90
|
+ 1,
|
|
91
|
+ array('%count%' => $iMonths)
|
|
92
|
+ );
|
|
93
|
+ }
|
|
94
|
+ else
|
|
95
|
+ {
|
|
96
|
+ return $this->translator->transChoice(
|
|
97
|
+ 'il y a un an|Il y a %count% ans',
|
|
98
|
+ 1,
|
|
99
|
+ array('%count%' => $iYears)
|
|
100
|
+ );
|
|
101
|
+ }
|
|
102
|
+ }
|
|
103
|
+
|
|
104
|
+ public function getName()
|
|
105
|
+ {
|
|
106
|
+ return 'my_twig_extension';
|
|
107
|
+ }
|
|
108
|
+
|
|
109
|
+}
|