Bladeren bron

Evolution #166: La recherche

bastien 12 jaren geleden
bovenliggende
commit
535759a3f2

+ 4 - 1
app/Resources/translations/userui.fr.yml Bestand weergeven

@@ -141,4 +141,7 @@ comment:
141 141
       no:                Non
142 142
       
143 143
 search_tag:
144
-  title:                 Recherche de tag correspondant à \"%string_search%\" ...
144
+  title:                 Recherche de tag correspondant à \"%string_search%\" ...
145
+  
146
+global_search:
147
+  no_results:            Il n'y a aucun résultat a votre recherche.

+ 37 - 2
src/Muzich/CoreBundle/Controller/SearchController.php Bestand weergeven

@@ -184,6 +184,39 @@ class SearchController extends Controller
184 184
     
185 185
     throw new \Exception('XmlHttpRequest only for this action');
186 186
   }
187
+  
188
+  /**
189
+   * Action permettant d'afficher plus de résultats (éléments) dans
190
+   * une global search.
191
+   * 
192
+   * @param Request $request
193
+   * @param int $last_id
194
+   * @param string $string
195
+   * @return Response 
196
+   */
197
+  public function globalSearchMoreAction(Request $request, $last_id, $string)
198
+  {
199
+    if (($response = $this->mustBeConnected(true)))
200
+    {
201
+      return $response;
202
+    }
203
+    
204
+    $search = $this->createSearchObject(array(
205
+      'count'    => $this->container->getParameter('search_ajax_more'),
206
+      'id_limit' => $last_id,
207
+      'string'   => $string
208
+    ));
209
+    
210
+    $elements = $search->getElements($this->getDoctrine(), $this->getUserId());
211
+      
212
+    return $this->searchElementsMore($elements, false,
213
+      $this->trans(
214
+        'elements.ajax.more.noelements', 
215
+        array(), 
216
+        'elements'
217
+      )
218
+    );
219
+  }
187 220
     
188 221
   /**
189 222
    * Procédure (ajax) de recherche de tags. Essentielement utilisé dans 
@@ -311,14 +344,16 @@ class SearchController extends Controller
311 344
         $results = $searcher->getResults(
312 345
           $this->getDoctrine(), 
313 346
           $this->getUserId(),
347
+          $this->container->getParameter('search_default_count'),
314 348
           $this->container->getParameter('search_global_elements_word_min_length')
315 349
         );
316 350
       }
317 351
     }
318 352
     
319 353
     return array(
320
-      'form' => $form->createView(),
321
-      'results'     => $results
354
+      'form'        => $form->createView(),
355
+      'results'     => $results,
356
+      'display_more_button' => (count($results['elements']))? (count($results['elements']) >= $this->container->getParameter('search_default_count'))? true : false : false
322 357
     );
323 358
   }
324 359
   

+ 45 - 31
src/Muzich/CoreBundle/Repository/ElementRepository.php Bestand weergeven

@@ -62,7 +62,11 @@ class ElementRepository extends EntityRepository
62 62
       return $this->getSelectElementForSearchQuery($params_select, $user_id, $searcher->getIds(), null, null, $searcher->getIdsDisplay());
63 63
     }
64 64
     
65
+    // Booléen nous permettant de savoir si un where a déjà été écrit
66
+    $is_where = false;
67
+    
65 68
     // Si c'est une recherche string, les autres paramètres ne sont pas nécéssaire
69
+    $where_string = '';
66 70
     if (($string = $searcher->getString()))
67 71
     {
68 72
       // On prépare notre liste de mots
@@ -79,8 +83,8 @@ class ElementRepository extends EntityRepository
79 83
       ));
80 84
       
81 85
       // On récupère les ids des elements correspondants
82
-      $where_string = "WHERE 1 = 2";
83
-      $params_string = array();
86
+      //$where_string = "WHERE 1 = 2";
87
+      //$params_string = array();
84 88
       $word_min_length = 0;
85 89
       if (isset($params['word_min_length']))
86 90
       {
@@ -90,44 +94,53 @@ class ElementRepository extends EntityRepository
90 94
       {
91 95
         if (strlen($word) >= $word_min_length)
92 96
         {
93
-          if ($where_string === "WHERE 1 = 2")
97
+          $where_string = ($is_where) ? ' AND (' : ' WHERE (';
98
+          $is_where = true;
99
+          if ($where_string === ' AND (' || $where_string === ' WHERE (')
94 100
           {
95
-            $where_string = " WHERE UPPER(e.name) LIKE :str".$i;
101
+            $where_string .= "UPPER(e_.name) LIKE :str".$i;
96 102
           }
97 103
           else
98 104
           {
99
-            $where_string .= " OR UPPER(e.name) LIKE :str".$i;
105
+            $where_string .= " OR UPPER(e_.name) LIKE :str".$i;
100 106
           }
101
-          $params_string['str'.$i] = '%'.strtoupper($word).'%';
107
+          //$params_string['str'.$i] = '%'.strtoupper($word).'%';
108
+          $params_ids['str'.$i] = '%'.strtoupper($word).'%';
102 109
         }
103 110
       }
111
+      $where_string .= ')';
104 112
       
105
-      $ids_result = $this->getEntityManager()
106
-        ->createQuery("SELECT e.id FROM MuzichCoreBundle:Element e
107
-           $where_string
108
-           GROUP BY e.id
109
-           ORDER BY e.created DESC, e.id DESC
110
-        ")->setParameters($params_string)->getScalarResult()
111
-      ;
112
-      $ids = array();
113
-      foreach ($ids_result as $id_record)
114
-      {
115
-        $ids[] = $id_record['id'];
116
-      }
117
-      
118
-      if (count($ids))
119
-      {
120
-        return $this->getSelectElementForSearchQuery($params_select, $user_id, $ids);
121
-      }
122
-      return $query = $this->getEntityManager()
123
-        ->createQuery("SELECT e FROM MuzichCoreBundle:Element e WHERE 1 = 2")
124
-      ;
113
+//      $ids_query = $this->getEntityManager()
114
+//        ->createQuery("SELECT e.id FROM MuzichCoreBundle:Element e
115
+//           $where_string
116
+//           GROUP BY e.id
117
+//           ORDER BY e.created DESC, e.id DESC
118
+//        ")->setParameters($params_string)
119
+//      ;
120
+//      
121
+//      $ids_query->setMaxResults($searcher->getCount());
122
+//      $ids_result = $ids_query->getScalarResult();
123
+//      
124
+//      $ids = array();
125
+//      foreach ($ids_result as $id_record)
126
+//      {
127
+//        $ids[] = $id_record['id'];
128
+//      }
129
+//      
130
+//      if (count($ids))
131
+//      {
132
+//        if (($id_limit = $searcher->getIdLimit()))
133
+//        {
134
+//          $this->getSelectElementForSearchQuery($params_select, $user_id, $ids, $id_limit);
135
+//        }
136
+//        return $this->getSelectElementForSearchQuery($params_select, $user_id, $ids);
137
+//      }
138
+//      return $query = $this->getEntityManager()
139
+//        ->createQuery("SELECT e FROM MuzichCoreBundle:Element e WHERE 1 = 2")
140
+//      ;
125 141
     }
126 142
     
127 143
     
128
-    // Booléen nous permettant de savoir si un where a déjà été écrit
129
-    $is_where = false;
130
-    
131 144
     // Construction des conditions pour la selection d'ids
132 145
     $where_tags = '';
133 146
     $join_tags  = '';
@@ -312,8 +325,9 @@ class ElementRepository extends EntityRepository
312 325
         $where_user
313 326
         $where_group
314 327
         $where_favorite
315
-        $where_id_limit
316 328
         $where_tag_strict
329
+        $where_string
330
+        $where_id_limit
317 331
         GROUP BY e_.id
318 332
         $order_by")
319 333
      ->setParameters($params_ids)
@@ -353,7 +367,7 @@ class ElementRepository extends EntityRepository
353 367
   }
354 368
   
355 369
   protected function getSelectElementForSearchQuery($params_select, $user_id, $ids, $id_limit = null, $count_limit = null, $ids_display = null)
356
-  {    
370
+  {
357 371
     $where = "";
358 372
     if ($id_limit)
359 373
     {

+ 8 - 0
src/Muzich/CoreBundle/Resources/config/routing.yml Bestand weergeven

@@ -16,6 +16,14 @@ search_elements_show_more:
16 16
   pattern:  /search-elements/show/{type}/{object_id}/{id_limit}/{invertcolors}
17 17
   defaults: { _controller: MuzichCoreBundle:Search:searchElementsShow }
18 18
    
19
+global_search_elements_empty:
20
+  pattern:  /global-search-elements/
21
+  defaults: { _controller: MuzichCoreBundle:Search:globalSearchMore }
22
+  
23
+global_search_elements:
24
+  pattern:  /global-search-elements/{last_id}/{string}
25
+  defaults: { _controller: MuzichCoreBundle:Search:globalSearchMore }
26
+   
19 27
 follow:
20 28
   pattern:  /follow/{type}/{id}/{token}
21 29
   defaults: { _controller: MuzichCoreBundle:Core:follow }

+ 13 - 3
src/Muzich/CoreBundle/Resources/views/GlobalSearch/results.html.twig Bestand weergeven

@@ -6,7 +6,9 @@
6 6
 
7 7
   {% include "MuzichCoreBundle:Menu:containerMenu.html.twig" with {'active': null} %}
8 8
   
9
-  {% include "MuzichCoreBundle:GlobalSearch:form.html.twig" with {'form': form } %}
9
+  <div id="results_search_form">
10
+    {% include "MuzichCoreBundle:GlobalSearch:form.html.twig" with {'form': form } %}
11
+  </div>
10 12
   
11 13
   {% if results.users|length or results.groups|length or results.elements|length %}
12 14
   
@@ -45,10 +47,18 @@
45 47
         'elements'          : results.elements
46 48
       }%}
47 49
       
48
-    {% endif %}
50
+      {% include "MuzichCoreBundle:SearchElement:more_button.html.twig" with {
51
+        'display_more_button' : display_more_button,
52
+        'elements_length'     : results.elements|length,
53
+        'more_path'           : 'global_search_elements_empty'
54
+      }%}
49 55
       
50
-  {% else %}
56
+    {% endif %}
51 57
       
58
+  {% elseif app.request.getMethod == 'POST' %}
59
+      <p>
60
+        {{ 'global_search.no_results'|trans({}, 'userui') }}
61
+      </p>
52 62
   {% endif %}
53 63
 
54 64
 {% endblock %}

+ 11 - 0
src/Muzich/CoreBundle/Resources/views/SearchElement/more_button.html.twig Bestand weergeven

@@ -0,0 +1,11 @@
1
+<div class="elements_loader_div">
2
+  <img class="elements_more_loader" style="display: none;" src="{{ asset('/bundles/muzichcore/img/ajax-loader.gif') }}" alt="loading" />
3
+</div>
4
+
5
+{% if display_more_button and elements_length %} 
6
+   <span class="elements_more">
7
+     <a href="{{ path(more_path) }}" class="elements_more button">
8
+       {{ 'more'|trans({}, 'userui') }}
9
+     </a>
10
+   </span>
11
+{% endif %}

+ 1 - 0
src/Muzich/CoreBundle/Resources/views/layout.html.twig Bestand weergeven

@@ -59,6 +59,7 @@
59 59
     url_add_tag = "{{ path('ajax_add_tag') }}";
60 60
     url_element_new_count = "{{ path('element_new_count') }}";
61 61
     url_element_new_get = "{{ path('element_new_get') }}";
62
+    url_global_search = "{{ path('global_search') }}";
62 63
     
63 64
     url_img_ajax_loader = "{{ asset('/bundles/muzichcore/img/ajax-loader.gif') }}";
64 65
   </script>

+ 5 - 2
src/Muzich/CoreBundle/Searcher/GlobalSearcher.php Bestand weergeven

@@ -51,7 +51,7 @@ class GlobalSearcher extends Searcher implements SearcherInterface
51 51
    * @param Registry $doctrine
52 52
    * @return array
53 53
    */
54
-  public function getResults(Registry $doctrine, $user_id, $min_word_length = null)
54
+  public function getResults(Registry $doctrine, $user_id, $search_elements_count, $min_word_length = null)
55 55
   {
56 56
     // On remplace le caratcère '%' au cas ou un malin l'insére.
57 57
     $string = str_replace('%', '#', $this->string);
@@ -61,7 +61,10 @@ class GlobalSearcher extends Searcher implements SearcherInterface
61 61
     
62 62
     // puis on fait recherche sur elements
63 63
     $es = new ElementSearcher();
64
-    $es->init(array('string' => $string));
64
+    $es->init(array(
65
+      'string' => $string,
66
+      'count'  => $search_elements_count
67
+    ));
65 68
     $results = $ugs->getResults($doctrine);
66 69
     $results['elements'] = $es->getElements(
67 70
       $doctrine, 

+ 5 - 2
src/Muzich/HomeBundle/Controller/HomeController.php Bestand weergeven

@@ -28,6 +28,9 @@ class HomeController extends Controller
28 28
     $search_form = $this->getSearchForm($search_object);
29 29
     $add_form = $this->getAddForm();
30 30
     
31
+    $elements = $search_object->getElements($this->getDoctrine(), $this->getUserId());
32
+    $count_elements = count($elements);
33
+    
31 34
     return array(
32 35
       'search_tags_id'   => $search_object->getTags(),
33 36
       'ids_display'      => $search_object->getIdsDisplay(),
@@ -37,8 +40,8 @@ class HomeController extends Controller
37 40
       'search_form'      => $search_form->createView(),
38 41
       'search_form_name' => 'search',
39 42
       'network_public'   => $search_object->isNetworkPublic(),
40
-      'elements'         => $search_object->getElements($this->getDoctrine(), $this->getUserId()),
41
-      'more_count'       => ($count)?$count+$this->container->getParameter('search_default_count'):$this->container->getParameter('search_default_count')*2
43
+      'elements'         => $elements,
44
+      'display_more_button' => ($count_elements >= $this->container->getParameter('search_default_count'))?true:false
42 45
     );
43 46
   }
44 47
 }

+ 8 - 14
src/Muzich/HomeBundle/Resources/views/Home/index.html.twig Bestand weergeven

@@ -58,20 +58,14 @@
58 58
     <img class="elements_new_loader" style="display: none;" src="{{ asset('/bundles/muzichcore/img/ajax-loader.gif') }}" alt="loading" />
59 59
   </div>
60 60
 
61
-  {% include "MuzichCoreBundle:SearchElement:default.html.twig" with {'noelements_filter' : true }%}
61
+  {% include "MuzichCoreBundle:SearchElement:default.html.twig" with {
62
+    'noelements_filter' : true 
63
+  }%}
62 64
     
63
-  <div class="elements_loader_div">
64
-    <img class="elements_more_loader" style="display: none;" src="{{ asset('/bundles/muzichcore/img/ajax-loader.gif') }}" alt="loading" />
65
-  </div>
66
-  
67
-  {% if more_count is defined %} 
68
-  {% if elements|length %}
69
-     <span class="elements_more">
70
-       <a href="{{ path('search_elements') }}" class="elements_more button">
71
-         {{ 'more'|trans({}, 'userui') }}
72
-       </a>
73
-     </span>
74
-  {% endif %}
75
-  {% endif %}
65
+  {% include "MuzichCoreBundle:SearchElement:more_button.html.twig" with {
66
+    'display_more_button' : display_more_button,
67
+    'elements_length'     : elements|length,
68
+    'more_path'           : 'search_elements'
69
+  }%}
76 70
   
77 71
 {% endblock %}

+ 10 - 1
web/bundles/muzichcore/js/muzich.js Bestand weergeven

@@ -461,7 +461,15 @@ $(document).ready(function(){
461 461
        invertcolor = 1;
462 462
      }
463 463
      $('img.elements_more_loader').show();
464
-     $.getJSON(link.attr('href')+'/'+id_last+'/'+invertcolor, function(response) {
464
+     
465
+     var url = link.attr('href')+'/'+id_last+'/'+invertcolor;
466
+     // Cas exeptionel si on se trouve sur la global_search
467
+     if ($('div#results_search_form').length)
468
+     {
469
+       url = link.attr('href')+id_last+'/'+$('div#results_search_form form input[type="text"]').val();
470
+     }
471
+     
472
+     $.getJSON(url, function(response) {
465 473
        if (response.status == 'mustbeconnected')
466 474
         {
467 475
           $(location).attr('href', url_index);
@@ -471,6 +479,7 @@ $(document).ready(function(){
471 479
        {
472 480
          $('ul.elements').append(response.html);
473 481
          $('img.elements_more_loader').hide();
482
+         recolorize_element_list();
474 483
        }
475 484
        
476 485
        if (response.end || response.count < 1)