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

Evolution #163: Evenements: Prise en comptes des autres facteurs

bastien преди 12 години
родител
ревизия
58ce2dcd63

+ 4 - 0
app/Resources/translations/users.fr.yml Целия файл

@@ -17,6 +17,10 @@ events:
17 17
     no:                         Pas de nouveaux commentaires sur vos partages
18 18
     one:                        %count% nouveaux commentaire sur un de vos partages
19 19
     yes:                        %count% nouveaux commentaires sur vos partages
20
+  new_favoriteds:
21
+    no:                         Pas de nouveaux partages mis en favoris
22
+    one:                        %count% de vos partage a été mis en favoris
23
+    yes:                        %count% de vos partages ont été mis en favoris
20 24
     
21 25
 reputation:
22 26
   bar:

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

@@ -65,7 +65,8 @@ filter:
65 65
   mytags:  Tous mes tags
66 66
   network_all:   Réseau global
67 67
   network_my:    Mon réseau
68
-  new_comments:   Nvx commentaires
68
+  new_comments:     Nvx commentaires
69
+  new_favoriteds:   Nvx favoris
69 70
   
70 71
 element_add:
71 72
   url:

+ 66 - 6
src/Muzich/CoreBundle/Actions/User/Event.php Целия файл

@@ -4,6 +4,7 @@ namespace Muzich\CoreBundle\Actions\User;
4 4
 
5 5
 use Muzich\CoreBundle\Entity\User;
6 6
 use Muzich\CoreBundle\Entity\Event as EventEntity;
7
+use Symfony\Component\DependencyInjection\Container;
7 8
 
8 9
 /**
9 10
  * Refactorisation d'actions lié aux événement de l'utilisateur
@@ -19,17 +20,76 @@ class Event
19 20
    */
20 21
   protected $user;
21 22
   protected $event;
23
+  protected $new = false;
24
+  protected $container;
22 25
   
23
-  public function __construct(User $user, EventEntity $event)
26
+  /**
27
+   *
28
+   * @param User $user L'utilisateur concerné par l'événement
29
+   * @param Container $container 
30
+   */
31
+  public function __construct(User $user, Container $container)
24 32
   {
25 33
     $this->user = $user;
26
-    $this->event = $event;
34
+    $this->container = $container;
35
+  }
36
+  
37
+  /**
38
+   * Cette méthode récupére (si elle existe) en base l'objet Event
39
+   * correspondant a cet événement.
40
+   * 
41
+   * @param string $type 
42
+   */
43
+  protected function initialize($type)
44
+  {
45
+    $em = $this->container->get('doctrine')->getEntityManager();
46
+    try
47
+    {
48
+      $this->event = $em->createQuery(
49
+        'SELECT e FROM MuzichCoreBundle:Event e
50
+        WHERE e.user = :uid AND e.type = :type'
51
+      )->setParameters(array(
52
+        'uid'  => $this->user->getId(),
53
+        'type' => $type
54
+      ))->getSingleResult()
55
+      ;
56
+      $this->new = false;
57
+    } 
58
+    catch (\Doctrine\ORM\NoResultException $e)
59
+    {
60
+      $this->event = new EventEntity();
61
+      $this->new = true;
62
+    }
63
+  }
64
+  
65
+  /**
66
+   * Procéde a l'ajout de l'événement a la liste de l'utilisateur.
67
+   * 
68
+   * @param string $type
69
+   * @param int $element_id
70
+   * @return Event 
71
+   */
72
+  public function proceed($type, $element_id)
73
+  {
74
+    $this->initialize($type);
75
+    if ($this->new)
76
+    {
77
+      $this->initEvent($type, $element_id);
78
+    }
79
+    else
80
+    {
81
+      $this->updateEvent($element_id);
82
+    }
83
+    
84
+    return $this->event;
27 85
   }
28 86
   
29
-  /*
30
-   * Mise a jour de l'objet Eevnt
87
+  /**
88
+   * 
89
+   * 
90
+   * @param int $element_id 
31 91
    */
32
-  public function updateEvent($element_id)
92
+  protected function updateEvent($element_id)
33 93
   {
34 94
     $this->event->addId($element_id);
35 95
   }
@@ -40,7 +100,7 @@ class Event
40 100
    * @param string $type
41 101
    * @param int $element_id 
42 102
    */
43
-  public function createEvent($type, $element_id)
103
+  protected function initEvent($type, $element_id)
44 104
   {
45 105
     $this->event->addId($element_id);
46 106
     $this->event->setType($type);

+ 2 - 2
src/Muzich/CoreBundle/Controller/CoreController.php Целия файл

@@ -309,7 +309,7 @@ class CoreController extends Controller
309 309
             'network_public'   => $search_object->isNetworkPublic(),
310 310
             'elements'         => $search_object->getElements($this->getDoctrine(), $this->getUserId()),
311 311
             'more_count'       => $this->container->getParameter('search_default_count')*2,
312
-            'display_comments' => false
312
+            'ids_display'      => $search_object->getIdsDisplay()
313 313
           ));
314 314
         }
315 315
         else
@@ -336,7 +336,7 @@ class CoreController extends Controller
336 336
             'add_form_name' => (isset($add_form)) ? 'add' : null,
337 337
             'more_count'    => null,
338 338
             'more_route'    => 'show_group_more',
339
-            'display_comments' => false
339
+            'ids_display'   => $search_object->getIdsDisplay()
340 340
           ));
341 341
         }
342 342
 

+ 1 - 0
src/Muzich/CoreBundle/Entity/Event.php Целия файл

@@ -19,6 +19,7 @@ class Event
19 19
 {
20 20
   
21 21
   const TYPE_COMMENT_ADDED_ELEMENT = "com.adde.ele";
22
+  const TYPE_FAV_ADDED_ELEMENT     = "fav.adde.ele";
22 23
   
23 24
   /**
24 25
    * @ORM\Id

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

@@ -124,6 +124,12 @@ class MyTwigExtension extends \Twig_Extension {
124 124
       case 'TYPE_COMMENT_ADDED_ELEMENT':
125 125
         return \Muzich\CoreBundle\Entity\Event::TYPE_COMMENT_ADDED_ELEMENT;
126 126
       break;
127
+      case 'TYPE_FAV_ADDED_ELEMENT':
128
+        return \Muzich\CoreBundle\Entity\Event::TYPE_FAV_ADDED_ELEMENT;
129
+      break;
130
+      default:
131
+        throw new \Exception('Constante non géré dans MyTwigExtension::event_const');
132
+      break;
127 133
     }
128 134
     return null;
129 135
   }

+ 7 - 34
src/Muzich/CoreBundle/Propagator/EventElement.php Целия файл

@@ -26,40 +26,9 @@ class EventElement extends EventPropagator
26 26
    */
27 27
   public function commentAdded(Element $element)
28 28
   {
29
-    $em = $this->container->get('doctrine')->getEntityManager();
30
-    
31
-    try
32
-    {
33
-      $event = $em->createQuery(
34
-        'SELECT e FROM MuzichCoreBundle:Event e
35
-        WHERE e.user = :uid AND e.type = :type'
36
-      )->setParameters(array(
37
-        'uid' => $element->getOwner()->getId(),
38
-        'type' => Event::TYPE_COMMENT_ADDED_ELEMENT
39
-      ))->getSingleResult()
40
-      ;
41
-      $new = false;
42
-    } 
43
-    catch (\Doctrine\ORM\NoResultException $e)
44
-    {
45
-      $event = new Event();
46
-      $new = true;
47
-    }
48
-    
49
-    $uea = new UserEventAction($element->getOwner(), $event);
50
-    if ($new)
51
-    {
52
-      $uea->createEvent(
53
-        Event::TYPE_COMMENT_ADDED_ELEMENT,
54
-        $element->getId()
55
-      );
56
-    }
57
-    else
58
-    {
59
-      $uea->updateEvent($element->getId());
60
-    }
61
-    
62
-    $em->persist($event);
29
+    $uea = new UserEventAction($element->getOwner(), $this->container);
30
+    $event = $uea->proceed(Event::TYPE_COMMENT_ADDED_ELEMENT, $element->getId());
31
+    $this->container->get('doctrine')->getEntityManager()->persist($event);
63 32
   }
64 33
   
65 34
   /**
@@ -103,6 +72,10 @@ class EventElement extends EventPropagator
103 72
     $ur->addPoints(
104 73
       $this->container->getParameter('reputation_element_favorite_value')
105 74
     );
75
+    
76
+    $uea = new UserEventAction($element->getOwner(), $this->container);
77
+    $event = $uea->proceed(Event::TYPE_FAV_ADDED_ELEMENT, $element->getId());
78
+    $this->container->get('doctrine')->getEntityManager()->persist($event);
106 79
   }
107 80
   
108 81
   /**

+ 11 - 3
src/Muzich/CoreBundle/Resources/views/SearchElement/form.html.twig Целия файл

@@ -7,20 +7,28 @@
7 7
       {{ form_widget(search_form.network, { 'attr': {'style': 'display: none;'} }) }}
8 8
     </div>
9 9
     
10
-    <a href="#" class="button all_network {% if network_public and not display_comments %}active{% endif %}">
10
+    <a href="#" class="button all_network {% if network_public and not ids_display %}active{% endif %}">
11 11
       {{ 'filter.network_all'|trans({}, 'userui') }}
12 12
     </a>
13
-    <a href="#" class="button my_network {% if not network_public and not display_comments %}active{% endif %}">
13
+    <a href="#" class="button my_network {% if not network_public and not ids_display %}active{% endif %}">
14 14
       {{ 'filter.network_my'|trans({}, 'userui') }}
15 15
     </a>
16 16
       
17 17
   </div>
18 18
 
19 19
   <div class="more_filters">
20
-    <a href="{{ path('ajax_filter_remove_ids') }}" class="button active new_comments" style="{% if not display_comments %}display: none;{% endif %}">
20
+    <a href="{{ path('ajax_filter_remove_ids') }}" class="button active new_comments" 
21
+       style="{% if ids_display != event_const('TYPE_COMMENT_ADDED_ELEMENT') %}display: none;{% endif %}"
22
+    >
21 23
       {{ 'filter.new_comments'|trans({}, 'userui') }}
22 24
       <img src="{{ asset('bundles/muzichcore/img/1328276855_list-remove.png') }}" alt="remove" />
23 25
     </a>
26
+    <a href="{{ path('ajax_filter_remove_ids') }}" class="button active new_favorites" 
27
+       style="{% if ids_display != event_const('TYPE_FAV_ADDED_ELEMENT') %}display: none;{% endif %}"
28
+    >
29
+      {{ 'filter.new_favoriteds'|trans({}, 'userui') }}
30
+      <img src="{{ asset('bundles/muzichcore/img/1328276855_list-remove.png') }}" alt="remove" />
31
+    </a>
24 32
   </div>
25 33
   
26 34
   {% include "MuzichCoreBundle:Tag:tagsPrompt.html.twig" with { 

+ 29 - 9
src/Muzich/CoreBundle/Searcher/ElementSearcher.php Целия файл

@@ -83,6 +83,15 @@ class ElementSearcher extends Searcher implements SearcherInterface
83 83
   protected $ids;
84 84
   
85 85
   /**
86
+   * On stocke la dedans le bouton a afficher dans le gestionnaire de filtres
87
+   * correspondant aux ids filtrés. La valeur doit correspondre a une constante
88
+   * de l'Entité metier Event.
89
+   * 
90
+   * @var string 
91
+   */
92
+  protected $ids_display;
93
+  
94
+  /**
86 95
    * @see SearcherInterface
87 96
    * @param array $params 
88 97
    */
@@ -95,7 +104,7 @@ class ElementSearcher extends Searcher implements SearcherInterface
95 104
     
96 105
     // Mise a jour des attributs
97 106
     $this->setAttributes(array(
98
-      'network', 'tags', 'count', 'user_id', 'group_id', 'favorite', 'id_limit', 'searchnew', 'ids'
107
+      'network', 'tags', 'count', 'user_id', 'group_id', 'favorite', 'id_limit', 'searchnew', 'ids', 'ids_display'
99 108
     ), $params);
100 109
     
101 110
   }
@@ -108,7 +117,7 @@ class ElementSearcher extends Searcher implements SearcherInterface
108 117
   {
109 118
     // Mise a jour des attributs
110 119
     $this->setAttributes(array(
111
-      'network', 'tags', 'count', 'user_id', 'group_id', 'favorite', 'id_limit', 'searchnew', 'ids'
120
+      'network', 'tags', 'count', 'user_id', 'group_id', 'favorite', 'id_limit', 'searchnew', 'ids', 'ids_display'
112 121
     ), $params);
113 122
   }
114 123
   
@@ -120,13 +129,14 @@ class ElementSearcher extends Searcher implements SearcherInterface
120 129
   public function getParams($tags_string = false)
121 130
   {
122 131
     return array(
123
-      'network'  => $this->getNetwork(),
124
-      'tags'     => $this->getTags($tags_string),
125
-      'count'    => $this->getCount(),
126
-      'user_id'  => $this->getUserId(),
127
-      'group_id' => $this->getGroupId(),
128
-      'favorite' => $this->isFavorite(),
129
-      'ids'      => $this->getIds()
132
+      'network'     => $this->getNetwork(),
133
+      'tags'        => $this->getTags($tags_string),
134
+      'count'       => $this->getCount(),
135
+      'user_id'     => $this->getUserId(),
136
+      'group_id'    => $this->getGroupId(),
137
+      'favorite'    => $this->isFavorite(),
138
+      'ids'         => $this->getIds(),
139
+      'ids_display' => $this->getIdsDisplay()
130 140
     );
131 141
   }
132 142
   
@@ -211,6 +221,16 @@ class ElementSearcher extends Searcher implements SearcherInterface
211 221
     }
212 222
     return false;
213 223
   }
224
+  
225
+  public function setIdsDisplay($display)
226
+  {
227
+    $this->ids_display = $display;
228
+  }
229
+  
230
+  public function getIdsDisplay()
231
+  {
232
+    return $this->ids_display;
233
+  }
214 234
 
215 235
   /**
216 236
    * Construction de l'objet Query

+ 97 - 0
src/Muzich/CoreBundle/Tests/Controller/EventTest.php Целия файл

@@ -202,4 +202,101 @@ class EventTest extends FunctionalTest
202 202
     
203 203
   }
204 204
   
205
+  public function testFavoriteAdded()
206
+  {
207
+    $this->client = self::createClient();
208
+    $this->connectUser('paul', 'toor');
209
+    
210
+    $paul = $this->getUser();
211
+    $bux = $this->getUser('bux');
212
+    
213
+    // Actuellement il n'y a aucun event d'ouvert pour bux (fixtures)
214
+    $result = $this->getDoctrine()->getEntityManager()
215
+      ->createQuery('SELECT e FROM MuzichCoreBundle:Event e
216
+        WHERE e.user = :uid'
217
+      )
218
+      ->setParameter('uid', $bux->getId())
219
+      ->getArrayResult()
220
+    ;
221
+    $this->assertEquals(count($result), 0);
222
+    
223
+    $element = $this->getDoctrine()->getRepository('MuzichCoreBundle:Element')
224
+      ->findOneByName('Ed Cox - La fanfare des teuffeurs (Hardcordian)')
225
+    ;
226
+        
227
+    // Ajout d'un élément en favoris
228
+    // Il ajoute cet élément en favoris
229
+    $url = $this->generateUrl('favorite_add', array(
230
+      'id'    => $element->getId(),
231
+      'token' => $paul->getPersonalHash()
232
+    ));
233
+    
234
+    $crawler = $this->client->request('GET', $url, array(), array(), array(
235
+        'HTTP_X-Requested-With' => 'XMLHttpRequest',
236
+    ));
237
+    
238
+    $this->isResponseSuccess();
239
+    
240
+    // On contrôle la présence du favoris
241
+    $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')
242
+      ->findOneBy(array(
243
+        'user'    => $paul->getId(),
244
+        'element' => $element->getId()
245
+      ));
246
+    
247
+    $this->assertTrue(!is_null($fav));
248
+    
249
+    // bux a maintenant un event en base
250
+    $result = $this->getDoctrine()->getEntityManager()
251
+      ->createQuery('
252
+        SELECT e FROM MuzichCoreBundle:Event e
253
+        WHERE e.user = :uid'
254
+      )
255
+      ->setParameter('uid', $bux->getId())
256
+      ->getArrayResult()
257
+    ;
258
+    $this->assertEquals(count($result), 1);
259
+    $this->assertEquals($result[0]['type'], Event::TYPE_FAV_ADDED_ELEMENT);
260
+    $this->assertEquals($result[0]['count'], 1);
261
+    $this->assertEquals($result[0]['ids'], json_encode(array((string)$element->getId())));
262
+    
263
+    // On enlève des favoris
264
+    $url = $this->generateUrl('favorite_remove', array(
265
+      'id'    => $element->getId(),
266
+      'token' => $paul->getPersonalHash()
267
+    ));
268
+    
269
+    $crawler = $this->client->request('GET', $url, array(), array(), array(
270
+        'HTTP_X-Requested-With' => 'XMLHttpRequest',
271
+    ));
272
+    
273
+    // On contrôle l'absence du favoris
274
+    $fav = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')
275
+      ->findOneBy(array(
276
+        'user'    => $paul->getId(),
277
+        'element' => $element->getId()
278
+      ));
279
+    
280
+    $this->assertTrue(is_null($fav));
281
+    
282
+    // bux a toujours qu'un event avec un seul element signalé.
283
+    $result = $this->getDoctrine()->getEntityManager()
284
+      ->createQuery('
285
+        SELECT e FROM MuzichCoreBundle:Event e
286
+        WHERE e.user = :uid'
287
+      )
288
+      ->setParameter('uid', $bux->getId())
289
+      ->getArrayResult()
290
+    ;
291
+    $this->assertEquals(count($result), 1);
292
+    $this->assertEquals($result[0]['type'], Event::TYPE_FAV_ADDED_ELEMENT);
293
+    $this->assertEquals($result[0]['count'], 1);
294
+    $this->assertEquals($result[0]['ids'], json_encode(array((string)$element->getId())));
295
+    
296
+    // Pour le moment pas de tests supplémentaire comme mettre de nouveaux favoris 
297
+    // ou consulter la liste des éléments concernés. Il faudrait coder ces test certe.
298
+    // Mais la refactorisation du code fait qu'il n'y a que le type (Event) de diféfrent.
299
+    // donc a coder (tests) mais pas urgent a l'isntant.
300
+  }
301
+  
205 302
 }

+ 4 - 2
src/Muzich/CoreBundle/Tests/Searcher/ElementSearcherTest.php Целия файл

@@ -17,7 +17,8 @@ class ElementSearcherTest extends UnitTest
17 17
         'user_id'   => 185, 
18 18
         'group_id'  => null, 
19 19
         'favorite'  => false,
20
-        'ids'       => null
20
+        'ids'       => null,
21
+        'ids_display' => null
21 22
     ));
22 23
 
23 24
     $this->assertEquals($ia, $es->getParams());
@@ -41,7 +42,8 @@ class ElementSearcherTest extends UnitTest
41 42
         'user_id'   => 115, 
42 43
         'group_id'  => null, 
43 44
         'favorite'  => false,
44
-        'ids'       => null
45
+        'ids'       => null,
46
+        'ids_display' => null
45 47
     ));
46 48
 
47 49
     $this->assertEquals($ua, $es->getParams());

+ 1 - 1
src/Muzich/HomeBundle/Controller/HomeController.php Целия файл

@@ -30,7 +30,7 @@ class HomeController extends Controller
30 30
     
31 31
     return array(
32 32
       'search_tags_id'   => $search_object->getTags(),
33
-      'display_comments' => $search_object->hasIds(),
33
+      'ids_display'      => $search_object->getIdsDisplay(),
34 34
       'user'             => $this->getUser(),
35 35
       'add_form'         => $add_form->createView(),
36 36
       'add_form_name'    => 'add',

+ 1 - 0
src/Muzich/UserBundle/Controller/EventController.php Целия файл

@@ -55,6 +55,7 @@ class EventController extends Controller
55 55
     
56 56
     $es = $this->getElementSearcher();
57 57
     $es->setIds($event->getIds());
58
+    $es->setIdsDisplay($event->getType());
58 59
     $this->setElementSearcherParams($es->getParams());
59 60
     $this->getDoctrine()->getEntityManager()->remove($event);
60 61
     $this->getDoctrine()->getEntityManager()->flush();

+ 19 - 0
src/Muzich/UserBundle/Resources/views/Info/bar.html.twig Целия файл

@@ -21,6 +21,25 @@
21 21
           <img title="{{ 'events.new_comments.no'|trans({}, 'users') }}" src="{{ asset('bundles/muzichcore/img/1332286007_comments_reply.png') }}" alt="comments" />
22 22
           <span class="new_comments">0</span>
23 23
         {% endif %}
24
+          
25
+    </div>
26
+    
27
+    <div class="favorites">
28
+      
29
+        {% if events[event_const('TYPE_FAV_ADDED_ELEMENT')] is defined %}
30
+          <a 
31
+            href="{{ path('event_view_elements', {'event_id':events[event_const('TYPE_FAV_ADDED_ELEMENT')].id}) }}" 
32
+            title="{%if events[event_const('TYPE_FAV_ADDED_ELEMENT')].count == 1 %}{{ 'events.new_favoriteds.one'|trans({'%count%':1}, 'users') }}{% else %}{{ 'events.new_favoriteds.yes'|trans({'%count%':events[event_const('TYPE_FAV_ADDED_ELEMENT')].count}, 'users') }}{% endif %}"
33
+            class="view_elements_event"
34
+          >
35
+            <img src="{{ asset('bundles/muzichcore/img/favorite.png') }}" alt="favoriteds" />
36
+            <span class="new_comments">{{ events[event_const('TYPE_FAV_ADDED_ELEMENT')].count }}</span>
37
+          </a>
38
+        {% else %}
39
+          <img title="{{ 'events.new_favoriteds.no'|trans({}, 'users') }}" src="{{ asset('bundles/muzichcore/img/favorite_bw.png') }}" alt="favoriteds" />
40
+          <span class="new_comments">0</span>
41
+        {% endif %}
42
+          
24 43
     </div>
25 44
   
26 45
   </div>

+ 12 - 0
web/bundles/muzichcore/css/main.css Целия файл

@@ -1142,4 +1142,16 @@ div#events div.comments span
1142 1142
 {
1143 1143
   font-weight: bold;
1144 1144
   margin-left: 5px;
1145
+}
1146
+
1147
+div#events div.favorites img
1148
+{
1149
+  margin-left: 3px;
1150
+  margin-bottom: -2px;
1151
+}
1152
+
1153
+div#events div.favorites span
1154
+{
1155
+  font-weight: bold;
1156
+  margin-left: 5px;
1145 1157
 }

+ 2 - 1
web/bundles/muzichcore/js/muzich.js Целия файл

@@ -1685,7 +1685,7 @@ $(document).ready(function(){
1685 1685
   
1686 1686
   
1687 1687
   // Enlever les ids du ElementSearch
1688
-  $('div.more_filters a.new_comments').live('click', function(){
1688
+  $('div.more_filters a.new_comments, div.more_filters a.new_favorites').live('click', function(){
1689 1689
     
1690 1690
     $('img.elements_more_loader').show();
1691 1691
     $('ul.elements').html('');
@@ -1702,6 +1702,7 @@ $(document).ready(function(){
1702 1702
       {
1703 1703
         $('form[name="search"]').submit();
1704 1704
         $('div.more_filters a.new_comments').hide();
1705
+        $('div.more_filters a.new_favorites').hide();
1705 1706
       }
1706 1707
       
1707 1708
     });

+ 10 - 1
web/bundles/muzichhome/css/home.css Целия файл

@@ -85,7 +85,16 @@ div.more_filters a.new_comments
85 85
   background-position: 2px 3px;
86 86
 }
87 87
 
88
-div.more_filters a.new_comments img
88
+div.more_filters a.new_favorites
89
+{
90
+  margin-top: 5px;
91
+  padding-left: 20px;
92
+  background-image: url(/bundles/muzichcore/img/favorite.png);
93
+  background-repeat: no-repeat;
94
+  background-position: 2px 3px;
95
+}
96
+
97
+div.more_filters a.new_comments img, div.more_filters a.new_favorites img
89 98
 {
90 99
   margin-bottom: -3px;
91 100
 }