Browse Source

Amélioration du système de trie des tags.

bastien 13 years ago
parent
commit
37e7d15733

+ 103 - 23
src/Muzich/CoreBundle/Controller/SearchController.php View File

171
     throw new \Exception('XmlHttpRequest only for this action');
171
     throw new \Exception('XmlHttpRequest only for this action');
172
   }
172
   }
173
   
173
   
174
-  protected function sort_search_tags($tags, $search)
174
+  /**
175
+   * Ajoute le tag au début du tableau passé en paramètre si celui-ci
176
+   * n'est pas a l'intérieur.
177
+   * 
178
+   * @param array $array
179
+   * @param Tag $tag
180
+   * @return array 
181
+   */
182
+  private function sort_addtop_if_isnt_in($array, $tag)
183
+  {
184
+    $in = false;
185
+    for ($x=0;$x<=sizeof($array)-1;$x++)
186
+    {
187
+      if ($array[$x]['id'] == $tag['id'])
188
+      {
189
+        $in = true;
190
+        break;
191
+      }
192
+    }
193
+    
194
+    if (!$in)
195
+    {
196
+      array_unshift($array, $tag);
197
+      return $array;
198
+    }
199
+    return $array;
200
+  }
201
+  
202
+  /**
203
+   * Ajoute le tag a al fin du tableau passé en paramètre si celui-ci
204
+   * n'est pas a l'intérieur.
205
+   * 
206
+   * @param array $array
207
+   * @param Tag $tag
208
+   * @return array 
209
+   */
210
+  private function sort_addbottom_if_isnt_in($array, $tag)
211
+  {
212
+    $in = false;
213
+    for ($x=0;$x<=sizeof($array)-1;$x++)
214
+    {
215
+      if ($array[$x]['id'] == $tag['id'])
216
+      {
217
+        $in = true;
218
+        break;
219
+      }
220
+    }
221
+    
222
+    if (!$in)
223
+    {
224
+      $array[] = $tag;
225
+      return $array;
226
+    }
227
+    return $array;
228
+  }
229
+  
230
+  /**
231
+   * Organise le trie des tags de manière plus friendly user.
232
+   *
233
+   * @param array $tags
234
+   * @param string $search
235
+   * @return array 
236
+   */
237
+  private function sort_search_tags($tags, $search)
175
   {
238
   {
176
     $canonicalizer = new StrictCanonicalizer();
239
     $canonicalizer = new StrictCanonicalizer();
177
-    $tag_sorted = $tags;
240
+    $tag_sorted = array();
178
     
241
     
179
     foreach ($tags as $i => $tag)
242
     foreach ($tags as $i => $tag)
180
     {
243
     {
181
       // Pas plus de trois caractères en plus de la recherche
244
       // Pas plus de trois caractères en plus de la recherche
182
-      foreach (explode(' ', $search) as $word)
245
+      $terms = array_merge(array($search), explode(' ', $search));
246
+      foreach ($terms as $word)
183
       {
247
       {
184
-        if (strlen(str_replace(strtoupper($canonicalizer->canonicalize($word)), '', strtoupper($tag['slug']))) < 4)
248
+        if (strlen($word) > 1)
185
         {
249
         {
186
-          unset($tag_sorted[$i]);
187
-          $tag_sorted = array_merge(array($tag), $tag_sorted);
250
+          if (strlen(str_replace(strtoupper($canonicalizer->canonicalize($word)), '', strtoupper($tag['slug']))) < 4)
251
+          {
252
+            $tag_sorted = $this->sort_addtop_if_isnt_in($tag_sorted, $tag);
253
+          }
188
         }
254
         }
189
       }
255
       }
190
       
256
       
191
     }
257
     }
192
     
258
     
193
-    $tags = $tag_sorted;
194
-    
195
     foreach ($tags as $i => $tag)
259
     foreach ($tags as $i => $tag)
196
     {
260
     {
197
       // Chaine de caractère identique
261
       // Chaine de caractère identique
198
-      foreach (explode(' ', $search) as $word)
262
+      $terms = array_merge(
263
+        array($search), 
264
+        explode(' ', $search), 
265
+        explode('-', $search),
266
+        array(str_replace(' ', '-', $search)),
267
+        array(str_replace('-', ' ', $search))
268
+      );
269
+      
270
+      foreach ($terms as $word)
199
       {
271
       {
200
-        if (strtoupper($canonicalizer->canonicalize($word)) == strtoupper($tag['slug']))
272
+        if (strlen($word) > 1)
201
         {
273
         {
202
-          unset($tag_sorted[$i]);
203
-          $tag_sorted = array_merge(array($tag), $tag_sorted);
274
+          if (strtoupper($canonicalizer->canonicalize($word)) == strtoupper($tag['slug']))
275
+          {
276
+            $tag_sorted = $this->sort_addtop_if_isnt_in($tag_sorted, $tag);
277
+          }
204
         }
278
         }
205
       }
279
       }
206
-      
207
-      
280
+    }
281
+    
282
+    foreach ($tags as $i => $tag)
283
+    {
284
+      $tag_sorted = $this->sort_addbottom_if_isnt_in($tag_sorted, $tag);
208
     }
285
     }
209
     
286
     
210
     return $tag_sorted;
287
     return $tag_sorted;
242
         $params = array();
319
         $params = array();
243
         foreach ($words as $i => $word)
320
         foreach ($words as $i => $word)
244
         {
321
         {
245
-          $word = $canonicalizer->canonicalize($word);
246
-          if ($where == '')
247
-          {
248
-            $where .= 'WHERE UPPER(t.slug) LIKE :str'.$i;
249
-          }
250
-          else
322
+          if (strlen($word) > 1)
251
           {
323
           {
252
-            $where .= ' OR UPPER(t.slug) LIKE :str'.$i;
253
-          }
324
+            $word = $canonicalizer->canonicalize($word);
325
+            if ($where == '')
326
+            {
327
+              $where .= 'WHERE UPPER(t.slug) LIKE :str'.$i;
328
+            }
329
+            else
330
+            {
331
+              $where .= ' OR UPPER(t.slug) LIKE :str'.$i;
332
+            }
254
 
333
 
255
-          $params['str'.$i] = '%'.strtoupper($word).'%';
334
+            $params['str'.$i] = '%'.strtoupper($word).'%';
335
+          }
256
         }
336
         }
257
 
337
 
258
         $tags = $this->getDoctrine()->getEntityManager()->createQuery("
338
         $tags = $this->getDoctrine()->getEntityManager()->createQuery("

+ 2 - 2
src/Muzich/CoreBundle/Util/StrictCanonicalizer.php View File

24
             'ô', 'ö', 'ò', 'ó', 'õ', 'ø', 
24
             'ô', 'ö', 'ò', 'ó', 'õ', 'ø', 
25
             'ù', 'û', 'ü', 'ú', 
25
             'ù', 'û', 'ü', 'ú', 
26
             'é', 'è', 'ê', 'ë', 
26
             'é', 'è', 'ê', 'ë', 
27
-            'ç', 'ÿ', 'ñ', 
27
+            'ç', 'ÿ', 'ñ', '\'', '"'
28
         ),
28
         ),
29
         array(
29
         array(
30
             'a', 'a', 'a', 'a', 'a', 'a', 
30
             'a', 'a', 'a', 'a', 'a', 'a', 
32
             'o', 'o', 'o', 'o', 'o', 'o', 
32
             'o', 'o', 'o', 'o', 'o', 'o', 
33
             'u', 'u', 'u', 'u', 
33
             'u', 'u', 'u', 'u', 
34
             'e', 'e', 'e', 'e', 
34
             'e', 'e', 'e', 'e', 
35
-            'c', 'y', 'n', 
35
+            'c', 'y', 'n', '', ''
36
         ),
36
         ),
37
         $texte
37
         $texte
38
     );
38
     );

+ 5 - 8
web/bundles/muzichcore/js/muzich.js View File

647
   function autocomplete_tag(input, form_name)
647
   function autocomplete_tag(input, form_name)
648
   {
648
   {
649
     // Il doit y avoir au moin un caractère
649
     // Il doit y avoir au moin un caractère
650
-    if (input.val().length > 0) 
650
+    if ((input.val().length > 0) && (input.val() != string_tag_prompt_input_help)) 
651
     {
651
     {
652
 
652
 
653
       // on met en variable l'input
653
       // on met en variable l'input
718
                 var t_string = tag_name
718
                 var t_string = tag_name
719
                 // On construit un li
719
                 // On construit un li
720
                 
720
                 
721
-                string_exploded = explode(' ', $.trim(input.val()));
722
-                for (n in string_exploded)
723
-                {
724
-                  r_string = string_exploded[n];
725
-                  var re = new RegExp(r_string, "i");
726
-                  t_string = t_string.replace(re,"<strong>" + r_string + "</strong>");
727
-                }
721
+                r_string = $.trim(input.val());
722
+                var re = new RegExp(r_string, "i");
723
+                t_string = t_string.replace(re,"<strong>" + r_string + "</strong>");
724
+                
728
                                 
725
                                 
729
                 li_tag = 
726
                 li_tag = 
730
                   $('<li>').append(
727
                   $('<li>').append(