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
 use Muzich\CoreBundle\Searcher\ElementSearcher;
8
 use Muzich\CoreBundle\Searcher\ElementSearcher;
9
 use Muzich\CoreBundle\Form\Search\ElementSearchForm;
9
 use Muzich\CoreBundle\Form\Search\ElementSearchForm;
10
 use Symfony\Component\HttpFoundation\Response;
10
 use Symfony\Component\HttpFoundation\Response;
11
+use Muzich\CoreBundle\Util\StrictCanonicalizer;
11
 
12
 
12
 class SearchController extends Controller
13
 class SearchController extends Controller
13
 {
14
 {
172
   
173
   
173
   protected function sort_search_tags($tags, $search)
174
   protected function sort_search_tags($tags, $search)
174
   {
175
   {
176
+    $canonicalizer = new StrictCanonicalizer();
175
     $tag_sorted = $tags;
177
     $tag_sorted = $tags;
176
     
178
     
177
     foreach ($tags as $i => $tag)
179
     foreach ($tags as $i => $tag)
179
       // Pas plus de trois caractères en plus de la recherche
181
       // Pas plus de trois caractères en plus de la recherche
180
       foreach (explode(' ', $search) as $word)
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
           unset($tag_sorted[$i]);
186
           unset($tag_sorted[$i]);
185
           $tag_sorted = array_merge(array($tag), $tag_sorted);
187
           $tag_sorted = array_merge(array($tag), $tag_sorted);
195
       // Chaine de caractère identique
197
       // Chaine de caractère identique
196
       foreach (explode(' ', $search) as $word)
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
           unset($tag_sorted[$i]);
202
           unset($tag_sorted[$i]);
201
           $tag_sorted = array_merge(array($tag), $tag_sorted);
203
           $tag_sorted = array_merge(array($tag), $tag_sorted);
229
     }
231
     }
230
    
232
    
231
     $string_search = trim($string_search);
233
     $string_search = trim($string_search);
234
+    $canonicalizer = new StrictCanonicalizer();
232
     
235
     
233
     if ($this->getRequest()->isXmlHttpRequest())
236
     if ($this->getRequest()->isXmlHttpRequest())
234
     {
237
     {
239
         $params = array();
242
         $params = array();
240
         foreach ($words as $i => $word)
243
         foreach ($words as $i => $word)
241
         {
244
         {
245
+          $word = $canonicalizer->canonicalize($word);
242
           if ($where == '')
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
           else
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
           $params['str'.$i] = '%'.strtoupper($word).'%';
255
           $params['str'.$i] = '%'.strtoupper($word).'%';
252
         }
256
         }
253
 
257
 
254
         $tags = $this->getDoctrine()->getEntityManager()->createQuery("
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
           $where
260
           $where
257
           ORDER BY t.name ASC"
261
           ORDER BY t.name ASC"
258
         )->setParameters($params)
262
         )->setParameters($params)
262
         $tags_response = array();
266
         $tags_response = array();
263
         foreach ($tags as $tag)
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
         $tags_response = $this->sort_search_tags($tags_response, $string_search);
276
         $tags_response = $this->sort_search_tags($tags_response, $string_search);