Browse Source

Meilleure gestion des accents dans la recherche de tags.

bastien 13 years ago
parent
commit
15a1e4c409
1 changed files with 14 additions and 6 deletions
  1. 14 6
      src/Muzich/CoreBundle/Controller/SearchController.php

+ 14 - 6
src/Muzich/CoreBundle/Controller/SearchController.php View File

@@ -8,6 +8,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
8 8
 use Muzich\CoreBundle\Searcher\ElementSearcher;
9 9
 use Muzich\CoreBundle\Form\Search\ElementSearchForm;
10 10
 use Symfony\Component\HttpFoundation\Response;
11
+use Muzich\CoreBundle\Util\StrictCanonicalizer;
11 12
 
12 13
 class SearchController extends Controller
13 14
 {
@@ -172,6 +173,7 @@ class SearchController extends Controller
172 173
   
173 174
   protected function sort_search_tags($tags, $search)
174 175
   {
176
+    $canonicalizer = new StrictCanonicalizer();
175 177
     $tag_sorted = $tags;
176 178
     
177 179
     foreach ($tags as $i => $tag)
@@ -179,7 +181,7 @@ class SearchController extends Controller
179 181
       // Pas plus de trois caractères en plus de la recherche
180 182
       foreach (explode(' ', $search) as $word)
181 183
       {
182
-        if (strlen(str_replace(strtoupper($word), '', strtoupper($tag['name']))) < 4)
184
+        if (strlen(str_replace(strtoupper($canonicalizer->canonicalize($word)), '', strtoupper($tag['slug']))) < 4)
183 185
         {
184 186
           unset($tag_sorted[$i]);
185 187
           $tag_sorted = array_merge(array($tag), $tag_sorted);
@@ -195,7 +197,7 @@ class SearchController extends Controller
195 197
       // Chaine de caractère identique
196 198
       foreach (explode(' ', $search) as $word)
197 199
       {
198
-        if (strtoupper($word) == strtoupper($tag['name']))
200
+        if (strtoupper($canonicalizer->canonicalize($word)) == strtoupper($tag['slug']))
199 201
         {
200 202
           unset($tag_sorted[$i]);
201 203
           $tag_sorted = array_merge(array($tag), $tag_sorted);
@@ -229,6 +231,7 @@ class SearchController extends Controller
229 231
     }
230 232
    
231 233
     $string_search = trim($string_search);
234
+    $canonicalizer = new StrictCanonicalizer();
232 235
     
233 236
     if ($this->getRequest()->isXmlHttpRequest())
234 237
     {
@@ -239,20 +242,21 @@ class SearchController extends Controller
239 242
         $params = array();
240 243
         foreach ($words as $i => $word)
241 244
         {
245
+          $word = $canonicalizer->canonicalize($word);
242 246
           if ($where == '')
243 247
           {
244
-            $where .= 'WHERE UPPER(t.name) LIKE :str'.$i;
248
+            $where .= 'WHERE UPPER(t.slug) LIKE :str'.$i;
245 249
           }
246 250
           else
247 251
           {
248
-            $where .= ' OR UPPER(t.name) LIKE :str'.$i;
252
+            $where .= ' OR UPPER(t.slug) LIKE :str'.$i;
249 253
           }
250 254
 
251 255
           $params['str'.$i] = '%'.strtoupper($word).'%';
252 256
         }
253 257
 
254 258
         $tags = $this->getDoctrine()->getEntityManager()->createQuery("
255
-          SELECT t.name, t.id FROM MuzichCoreBundle:Tag t
259
+          SELECT t.name, t.slug, t.id FROM MuzichCoreBundle:Tag t
256 260
           $where
257 261
           ORDER BY t.name ASC"
258 262
         )->setParameters($params)
@@ -262,7 +266,11 @@ class SearchController extends Controller
262 266
         $tags_response = array();
263 267
         foreach ($tags as $tag)
264 268
         {
265
-          $tags_response[] = array('name' => $tag['name'], 'id' => $tag['id']);
269
+          $tags_response[] = array(
270
+            'name' => $tag['name'], 
271
+            'id'   => $tag['id'],
272
+            'slug' => $tag['slug']
273
+          );
266 274
         }
267 275
         
268 276
         $tags_response = $this->sort_search_tags($tags_response, $string_search);