Преглед на файлове

Evolution #118: Elements: affiché la date de diffusion

bastien преди 13 години
родител
ревизия
4bbb6754f9

+ 6 - 1
app/Resources/translations/userui.fr.yml Целия файл

@@ -71,4 +71,9 @@ system:
71 71
     normally_js:   |
72 72
                     Vous avez ouvert cette page dans un nouvel onglet/nouvelle page
73 73
                     Sachez qu'avez le clic gauche de votre souris la modification
74
-                    de l'élément est intégré dans la page.
74
+                    de l'élément est intégré dans la page.
75
+                    
76
+date:
77
+  instant:               a l'instant
78
+  less_than_minute:      il y a moins d'une minute
79
+ 

+ 28 - 0
src/Muzich/CoreBundle/DependencyInjection/CoreExtension.php Целия файл

@@ -0,0 +1,28 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\DependencyInjection;
4
+
5
+use Symfony\Component\HttpKernel\DependencyInjection\Extension;
6
+use Symfony\Component\DependencyInjection\ContainerBuilder;
7
+use Symfony\Component\DependencyInjection\Definition;
8
+
9
+class CoreExtension extends Extension
10
+{
11
+
12
+  public function load(array $config, ContainerBuilder $container) {
13
+      $definition = new Definition('Muzich\CoreBundle\Extension\MyTwigExtension');
14
+      // this is the most important part. Later in the startup process TwigBundle
15
+      // searches through the container and registres all services taged as twig.extension.
16
+      $definition->addTag('twig.extension');
17
+      $container->setDefinition('my_twig_extension', $definition);
18
+  }
19
+
20
+  /**
21
+   * Was necessary in previous Symfony2 PR releases.
22
+   * Symfony2 calls `load` method automatically now.
23
+   *
24
+   * public function getAlias() {
25
+   *     return 'hello'; // that's how we'll call this extension in configuration files
26
+   * }
27
+   */
28
+}

+ 109 - 0
src/Muzich/CoreBundle/Extension/MyTwigExtension.php Целия файл

@@ -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
+}

+ 1 - 0
src/Muzich/CoreBundle/Resources/views/SearchElement/element.html.twig Целия файл

@@ -91,6 +91,7 @@
91 91
           }, 'elements') }}
92 92
         {% endif %}
93 93
       {% endautoescape %}
94
+      {{ element.created.date|date_or_relative_date }}
94 95
       
95 96
       <div class="loader">
96 97
         <img class="element_loader" style="display: none;" src="{{ asset('/bundles/muzichcore/img/ajax-loader.gif') }}" alt="loading"/>