Quellcode durchsuchen

Evolution #91: Favoris: Nouvelle présentation

bastien vor 12 Jahren
Ursprung
Commit
f8e33e860e

+ 1 - 0
src/Muzich/CoreBundle/Entity/UsersElementsFavorites.php Datei anzeigen

@@ -10,6 +10,7 @@ use Doctrine\ORM\Mapping as ORM;
10 10
  * 
11 11
  * @ORM\Entity
12 12
  * @ORM\Table(name="users_elements_favorites")
13
+ * @ORM\Entity(repositoryClass="Muzich\CoreBundle\Repository\UsersElementsFavoritesRepository")
13 14
  */
14 15
 class UsersElementsFavorites
15 16
 {

+ 2 - 2
src/Muzich/CoreBundle/Repository/ElementRepository.php Datei anzeigen

@@ -142,9 +142,9 @@ class ElementRepository extends EntityRepository
142 142
     $where_id_limit = '';
143 143
     if (($id_limit = $searcher->getIdLimit()))
144 144
     {
145
-      $where_favorite = ($is_where) ? ' AND' : ' WHERE';
145
+      $where_id_limit = ($is_where) ? ' AND' : ' WHERE';
146 146
       $is_where = true;
147
-      $where_favorite .= " e_.id < :id_limit";
147
+      $where_id_limit .= " e_.id < :id_limit";
148 148
       $params_ids['id_limit'] = $id_limit;
149 149
     }
150 150
     

+ 31 - 0
src/Muzich/CoreBundle/Repository/UsersElementsFavoritesRepository.php Datei anzeigen

@@ -0,0 +1,31 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Repository;
4
+
5
+use Doctrine\ORM\EntityRepository;
6
+
7
+class UsersElementsFavoritesRepository extends EntityRepository
8
+{
9
+  
10
+  /**
11
+   * Retourne tous les tags des favoris de l'utilisateur
12
+   * 
13
+   * @return doctrine_collection
14
+   */
15
+  public function getTags($user_id)
16
+  {
17
+    return $this->getEntityManager()
18
+      ->createQuery('
19
+        SELECT t FROM MuzichCoreBundle:Tag t
20
+        LEFT JOIN t.elements e
21
+        LEFT JOIN e.elements_favorites f
22
+        WHERE f.user = :uid
23
+        ORDER BY t.name ASC'
24
+      )
25
+      ->setParameter('uid', $user_id)
26
+      ->getResult()
27
+    ;
28
+  }
29
+  
30
+}
31
+  

+ 62 - 4
src/Muzich/FavoriteBundle/Controller/FavoriteController.php Datei anzeigen

@@ -134,12 +134,25 @@ class FavoriteController extends Controller
134 134
   {
135 135
     $search_object = $this->createSearchObject(array(
136 136
       'user_id'  => $this->getUserId(),
137
-      'favorite' => true
137
+      'favorite' => true,
138
+      'count'    => $this->container->getParameter('search_default_count')
138 139
     ));
139 140
     
141
+    $tags = $this->getDoctrine()->getRepository('MuzichCoreBundle:UsersElementsFavorites')
142
+      ->getTags($this->getUserId())      
143
+    ;
144
+    
145
+    $tags_id = array();
146
+    foreach ($tags as $tag)
147
+    {
148
+      $tags_id[] = $tag->getId();
149
+    }
150
+    
140 151
     return array(
141
-      'user'     => $this->getUser(),
142
-      'elements' => $search_object->getElements($this->getDoctrine(), $this->getUserId())
152
+      'tags'          => $tags,
153
+      'tags_id_json'  => json_encode($tags_id),
154
+      'user'          => $this->getUser(),
155
+      'elements'      => $search_object->getElements($this->getDoctrine(), $this->getUserId())
143 156
     );
144 157
   }
145 158
   
@@ -155,7 +168,8 @@ class FavoriteController extends Controller
155 168
     
156 169
     $search_object = $this->createSearchObject(array(
157 170
       'user_id'  => $viewed_user->getId(),
158
-      'favorite' => true
171
+      'favorite' => true,
172
+      'count'    => $this->container->getParameter('search_default_count')
159 173
     ));
160 174
     
161 175
     return array(
@@ -165,4 +179,48 @@ class FavoriteController extends Controller
165 179
     );
166 180
   }
167 181
   
182
+  public function getElementsAction($user_id, $tags_ids_json, $id_limit = null, $invert = false)
183
+  {
184
+    $tag_ids = json_decode($tags_ids_json);
185
+    $search_object = new ElementSearcher();
186
+    
187
+    $tags = array();
188
+    foreach ($tag_ids as $id)
189
+    {
190
+      $tags[$id] = $id;
191
+    }
192
+    
193
+    $search_object->init(array(
194
+      'tags'     => $tags,
195
+      'user_id'  => $user_id,
196
+      'favorite' => true,
197
+      'count'    => $this->container->getParameter('search_default_count'),
198
+      'id_limit' => $id_limit
199
+    ));
200
+    
201
+    $message = $this->trans(
202
+      'elements.ajax.more.noelements', 
203
+      array(), 
204
+      'elements'
205
+    );
206
+    
207
+    $elements = $search_object->getElements($this->getDoctrine(), $user_id);
208
+    $count = count($elements);
209
+    $html = '';
210
+    if ($count)
211
+    {
212
+      $html = $this->render('MuzichCoreBundle:SearchElement:default.html.twig', array(
213
+        'user'        => $this->getUser(),
214
+        'elements'    => $elements,
215
+        'invertcolor' => $invert
216
+      ))->getContent();
217
+    }
218
+    
219
+    return $this->jsonResponse(array(
220
+      'count'   => $count,
221
+      'message' => $message,
222
+      'html'    => $html
223
+    ));
224
+  }
225
+  
168 226
 }

+ 6 - 1
src/Muzich/FavoriteBundle/Resources/config/routing.yml Datei anzeigen

@@ -13,4 +13,9 @@ favorites_my_list:
13 13
 
14 14
 favorite_user_list:
15 15
   pattern:   /user/{slug}/favorites
16
-  defaults: { _controller: MuzichFavoriteBundle:Favorite:userList }
16
+  defaults: { _controller: MuzichFavoriteBundle:Favorite:userList }
17
+
18
+# post
19
+favorite_get:
20
+  pattern:   /favorites/{user_id}/getElements/{tags_ids_json}/{id_limit}/{invert}
21
+  defaults: { _controller: MuzichFavoriteBundle:Favorite:getElements, id_limit: null, invert: false }

+ 27 - 0
src/Muzich/FavoriteBundle/Resources/views/Favorite/myList.html.twig Datei anzeigen

@@ -12,6 +12,33 @@
12 12
 
13 13
   <h2>{{ 'favorites.your_favorites'|trans({}, 'network') }}</h2>
14 14
 
15
+  {% if tags|length %}
16
+    <input type="hidden" id="get_elements_url" value="{{ path('favorite_get', {'user_id': app.user.id, 'tags_ids_json' : ''}) }}" />
17
+    <ul id="favorite_tags">
18
+      {% for tag in tags %}
19
+        <li>
20
+          <a href="#{{ tag.id }}" class="button tag">
21
+            {{ tag.name }}
22
+          </a>
23
+        </li>
24
+      {% endfor %}
25
+    </ul>
26
+    <div class="clearboth" ></div>
27
+  {% endif %}
28
+
15 29
   {% include "MuzichCoreBundle:SearchElement:default.html.twig" %}
16 30
     
31
+  <div class="elements_loader_div">
32
+    <img class="elements_more_loader" style="display: none;" src="{{ asset('/bundles/muzichcore/img/ajax-loader.gif') }}" alt="loading" />
33
+  </div>
34
+  
35
+  {% if elements|length %}
36
+     <span class="elements_more">
37
+       <input type="hidden" id="more_elements_url" value="{{ path('favorite_get', {'user_id': app.user.id, 'tags_ids_json' : ''}) }}" />
38
+       <a href="{{ path('favorite_get', {'user_id': app.user.id, 'tags_ids_json' : tags_id_json}) }}" class="elements_more button">
39
+         {{ 'more'|trans({}, 'userui') }}
40
+       </a>
41
+     </span>
42
+  {% endif %}
43
+    
17 44
 {% endblock %}

+ 3 - 0
src/Muzich/FavoriteBundle/Resources/views/layout.html.twig Datei anzeigen

@@ -3,6 +3,9 @@
3 3
 {% block css %}
4 4
 	<link href="{{ asset('bundles/muzichfavorite/css/favorite.css') }}" rel="stylesheet" media="screen" type="text/css" />
5 5
 {% endblock %}
6
+{% block js %}
7
+	<script src="{{ asset('bundles/muzichfavorite/js/favorite.js') }}" type="text/javascript"></script>
8
+{% endblock %}
6 9
 
7 10
 {% block main_content %}
8 11
   

+ 10 - 0
web/bundles/muzichcore/css/main.css Datei anzeigen

@@ -45,6 +45,11 @@
45 45
   
46 46
 }
47 47
 
48
+div.clearboth
49
+{
50
+  clear: both;
51
+}
52
+
48 53
 .button {
49 54
 	display: inline-block;
50 55
 	line-height: 1;
@@ -67,6 +72,11 @@ input.button, button.button {
67 72
   background-color: #39c9cc;
68 73
 }
69 74
 
75
+.button.active
76
+{
77
+  background-color: #39C9CC;
78
+}
79
+
70 80
 #container a
71 81
 {
72 82
   text-decoration: none;

+ 1 - 1
web/bundles/muzichcore/js/muzich.js Datei anzeigen

@@ -598,7 +598,7 @@ $(document).ready(function(){
598 598
                 for (n in string_exploded)
599 599
                 {
600 600
                   r_string = string_exploded[n];
601
-                  var re = new RegExp(r_string, "i") ;
601
+                  var re = new RegExp(r_string, "i");
602 602
                   t_string = t_string.replace(re,"<strong>" + r_string + "</strong>");
603 603
                 }
604 604
                                 

+ 14 - 0
web/bundles/muzichfavorite/css/favorite.css Datei anzeigen

@@ -0,0 +1,14 @@
1
+
2
+ul#favorite_tags
3
+{
4
+  padding:0;
5
+  margin:0;
6
+  list-style-type:none;
7
+}
8
+
9
+ul#favorite_tags li
10
+{
11
+  display: inline;
12
+  margin-left:2px;
13
+  float:left; /*pour IE*/
14
+}

+ 92 - 0
web/bundles/muzichfavorite/js/favorite.js Datei anzeigen

@@ -0,0 +1,92 @@
1
+$(document).ready(function(){
2
+   
3
+  function refresh_elements_with_tags_selected(link)
4
+  {
5
+    
6
+    
7
+    // Puis on fait notre rekékéte ajax.
8
+    $('ul.elements').html('');
9
+    $('div.no_elements').hide();
10
+    $('img.elements_more_loader').show();
11
+    $.getJSON($('input#get_elements_url').val()+'/'+array2json(tags_ids), function(response){
12
+      
13
+      $('ul.elements').html(response.html);
14
+      
15
+      if (response.count)
16
+       {
17
+         $('img.elements_more_loader').hide();
18
+         $('span.elements_more').show();
19
+         $('a.elements_more').show();
20
+       }
21
+    });
22
+    
23
+    return false;
24
+  }
25
+   
26
+  $('ul#favorite_tags a.tag').click(function(){
27
+    // Ensuite on l'active ou le désactive'
28
+    if ($(this).hasClass('active'))
29
+    {
30
+      $(this).removeClass('active');
31
+    }
32
+    else
33
+    {
34
+      $(this).addClass('active');
35
+    }
36
+    
37
+    // On construit notre liste de tags
38
+    tags_ids = new Array();
39
+    $('ul#favorite_tags a.tag.active').each(function(index){
40
+      id = str_replace('#', '', $(this).attr('href'));
41
+      tags_ids[id] = id;
42
+    });
43
+    
44
+    // On adapte le lien afficher plus de résultats
45
+    a_more = $('a.elements_more');
46
+    a_more.attr('href', $('input#more_elements_url').val()+'/'+array2json(tags_ids));
47
+    
48
+    return check_timelaps_and_find_with_tags($(this), new Date().getTime(), false);
49
+  });
50
+  
51
+  last_keypress = 0;
52
+  function check_timelaps_and_find_with_tags(link, time_id, timed)
53
+  {
54
+    if (!timed)
55
+    {
56
+      // C'est une nouvelle touche (pas redirigé) on lui donne un id
57
+      // et on met a jour l'id de la dernière pressé
58
+      last_keypress = new Date().getTime(); 
59
+      var this_time_id = last_keypress;
60
+    }
61
+    else
62
+    {
63
+      // Si elle a été redirigé, on met son id dans cette variable
64
+      var this_time_id = time_id;
65
+    }
66
+    
67
+    // C'est une touche redirigé dans le temps qui a été suivit d'une autre touche
68
+    if (time_id != last_keypress && timed)
69
+    {
70
+      // elle disparait
71
+    }
72
+    else
73
+    {
74
+      //
75
+      if ((new Date().getTime() - last_keypress) < 800 || timed == false)
76
+      {
77
+        // Si elle vient d'être tapé (timed == false) elle doit attendre (au cas ou une autre touche soit tapé)
78
+        // Si c'est une redirigé qui n'a pas été remplacé par une nouvelle lettre
79
+        // elle doit attendre au cas ou soit pressé.
80
+        setTimeout(function(){check_timelaps_and_find_with_tags(link, this_time_id, true)}, 900);
81
+      }
82
+      else
83
+      {
84
+        // il n'y a plus a attendre, on envoie la demande de tag.
85
+        return refresh_elements_with_tags_selected(link);
86
+      }
87
+    }
88
+    
89
+    return null;
90
+  }
91
+   
92
+});