Browse Source

Evolution #166: La recherche

bastien 13 years ago
parent
commit
535759a3f2

+ 4 - 1
app/Resources/translations/userui.fr.yml View File

141
       no:                Non
141
       no:                Non
142
       
142
       
143
 search_tag:
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 View File

184
     
184
     
185
     throw new \Exception('XmlHttpRequest only for this action');
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
    * Procédure (ajax) de recherche de tags. Essentielement utilisé dans 
222
    * Procédure (ajax) de recherche de tags. Essentielement utilisé dans 
311
         $results = $searcher->getResults(
344
         $results = $searcher->getResults(
312
           $this->getDoctrine(), 
345
           $this->getDoctrine(), 
313
           $this->getUserId(),
346
           $this->getUserId(),
347
+          $this->container->getParameter('search_default_count'),
314
           $this->container->getParameter('search_global_elements_word_min_length')
348
           $this->container->getParameter('search_global_elements_word_min_length')
315
         );
349
         );
316
       }
350
       }
317
     }
351
     }
318
     
352
     
319
     return array(
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 View File

62
       return $this->getSelectElementForSearchQuery($params_select, $user_id, $searcher->getIds(), null, null, $searcher->getIdsDisplay());
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
     // Si c'est une recherche string, les autres paramètres ne sont pas nécéssaire
68
     // Si c'est une recherche string, les autres paramètres ne sont pas nécéssaire
69
+    $where_string = '';
66
     if (($string = $searcher->getString()))
70
     if (($string = $searcher->getString()))
67
     {
71
     {
68
       // On prépare notre liste de mots
72
       // On prépare notre liste de mots
79
       ));
83
       ));
80
       
84
       
81
       // On récupère les ids des elements correspondants
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
       $word_min_length = 0;
88
       $word_min_length = 0;
85
       if (isset($params['word_min_length']))
89
       if (isset($params['word_min_length']))
86
       {
90
       {
90
       {
94
       {
91
         if (strlen($word) >= $word_min_length)
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
           else
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
     // Construction des conditions pour la selection d'ids
144
     // Construction des conditions pour la selection d'ids
132
     $where_tags = '';
145
     $where_tags = '';
133
     $join_tags  = '';
146
     $join_tags  = '';
312
         $where_user
325
         $where_user
313
         $where_group
326
         $where_group
314
         $where_favorite
327
         $where_favorite
315
-        $where_id_limit
316
         $where_tag_strict
328
         $where_tag_strict
329
+        $where_string
330
+        $where_id_limit
317
         GROUP BY e_.id
331
         GROUP BY e_.id
318
         $order_by")
332
         $order_by")
319
      ->setParameters($params_ids)
333
      ->setParameters($params_ids)
353
   }
367
   }
354
   
368
   
355
   protected function getSelectElementForSearchQuery($params_select, $user_id, $ids, $id_limit = null, $count_limit = null, $ids_display = null)
369
   protected function getSelectElementForSearchQuery($params_select, $user_id, $ids, $id_limit = null, $count_limit = null, $ids_display = null)
356
-  {    
370
+  {
357
     $where = "";
371
     $where = "";
358
     if ($id_limit)
372
     if ($id_limit)
359
     {
373
     {

+ 8 - 0
src/Muzich/CoreBundle/Resources/config/routing.yml View File

16
   pattern:  /search-elements/show/{type}/{object_id}/{id_limit}/{invertcolors}
16
   pattern:  /search-elements/show/{type}/{object_id}/{id_limit}/{invertcolors}
17
   defaults: { _controller: MuzichCoreBundle:Search:searchElementsShow }
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
 follow:
27
 follow:
20
   pattern:  /follow/{type}/{id}/{token}
28
   pattern:  /follow/{type}/{id}/{token}
21
   defaults: { _controller: MuzichCoreBundle:Core:follow }
29
   defaults: { _controller: MuzichCoreBundle:Core:follow }

+ 13 - 3
src/Muzich/CoreBundle/Resources/views/GlobalSearch/results.html.twig View File

6
 
6
 
7
   {% include "MuzichCoreBundle:Menu:containerMenu.html.twig" with {'active': null} %}
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
   {% if results.users|length or results.groups|length or results.elements|length %}
13
   {% if results.users|length or results.groups|length or results.elements|length %}
12
   
14
   
45
         'elements'          : results.elements
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
   {% endif %}
62
   {% endif %}
53
 
63
 
54
 {% endblock %}
64
 {% endblock %}

+ 11 - 0
src/Muzich/CoreBundle/Resources/views/SearchElement/more_button.html.twig View File

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 View File

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

+ 5 - 2
src/Muzich/CoreBundle/Searcher/GlobalSearcher.php View File

51
    * @param Registry $doctrine
51
    * @param Registry $doctrine
52
    * @return array
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
     // On remplace le caratcère '%' au cas ou un malin l'insére.
56
     // On remplace le caratcère '%' au cas ou un malin l'insére.
57
     $string = str_replace('%', '#', $this->string);
57
     $string = str_replace('%', '#', $this->string);
61
     
61
     
62
     // puis on fait recherche sur elements
62
     // puis on fait recherche sur elements
63
     $es = new ElementSearcher();
63
     $es = new ElementSearcher();
64
-    $es->init(array('string' => $string));
64
+    $es->init(array(
65
+      'string' => $string,
66
+      'count'  => $search_elements_count
67
+    ));
65
     $results = $ugs->getResults($doctrine);
68
     $results = $ugs->getResults($doctrine);
66
     $results['elements'] = $es->getElements(
69
     $results['elements'] = $es->getElements(
67
       $doctrine, 
70
       $doctrine, 

+ 5 - 2
src/Muzich/HomeBundle/Controller/HomeController.php View File

28
     $search_form = $this->getSearchForm($search_object);
28
     $search_form = $this->getSearchForm($search_object);
29
     $add_form = $this->getAddForm();
29
     $add_form = $this->getAddForm();
30
     
30
     
31
+    $elements = $search_object->getElements($this->getDoctrine(), $this->getUserId());
32
+    $count_elements = count($elements);
33
+    
31
     return array(
34
     return array(
32
       'search_tags_id'   => $search_object->getTags(),
35
       'search_tags_id'   => $search_object->getTags(),
33
       'ids_display'      => $search_object->getIdsDisplay(),
36
       'ids_display'      => $search_object->getIdsDisplay(),
37
       'search_form'      => $search_form->createView(),
40
       'search_form'      => $search_form->createView(),
38
       'search_form_name' => 'search',
41
       'search_form_name' => 'search',
39
       'network_public'   => $search_object->isNetworkPublic(),
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 View File

58
     <img class="elements_new_loader" style="display: none;" src="{{ asset('/bundles/muzichcore/img/ajax-loader.gif') }}" alt="loading" />
58
     <img class="elements_new_loader" style="display: none;" src="{{ asset('/bundles/muzichcore/img/ajax-loader.gif') }}" alt="loading" />
59
   </div>
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
 {% endblock %}
71
 {% endblock %}

+ 10 - 1
web/bundles/muzichcore/js/muzich.js View File

461
        invertcolor = 1;
461
        invertcolor = 1;
462
      }
462
      }
463
      $('img.elements_more_loader').show();
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
        if (response.status == 'mustbeconnected')
473
        if (response.status == 'mustbeconnected')
466
         {
474
         {
467
           $(location).attr('href', url_index);
475
           $(location).attr('href', url_index);
471
        {
479
        {
472
          $('ul.elements').append(response.html);
480
          $('ul.elements').append(response.html);
473
          $('img.elements_more_loader').hide();
481
          $('img.elements_more_loader').hide();
482
+         recolorize_element_list();
474
        }
483
        }
475
        
484
        
476
        if (response.end || response.count < 1)
485
        if (response.end || response.count < 1)