Browse Source

Evolution #166: La recherche

bastien 12 years ago
parent
commit
01a9709153

+ 8 - 3
src/Muzich/CoreBundle/Controller/SearchController.php View File

299
     $form = $this->getGlobalSearchForm($searcher = new GlobalSearcher());
299
     $form = $this->getGlobalSearchForm($searcher = new GlobalSearcher());
300
     $form->bindRequest($request);
300
     $form->bindRequest($request);
301
     $results = array(
301
     $results = array(
302
-      'users'  => null,
303
-      'groups' => null
302
+      'users'    => null,
303
+      'groups'   => null,
304
+      'elements' => null
304
     );
305
     );
305
     if ($form->isValid())
306
     if ($form->isValid())
306
     {
307
     {
307
-      $results = $searcher->getResults($this->getDoctrine());
308
+      $results = $searcher->getResults(
309
+        $this->getDoctrine(), 
310
+        $this->getUserId(),
311
+        $this->container->getParameter('search_global_elements_word_min_length')
312
+      );
308
     }
313
     }
309
     
314
     
310
     return array(
315
     return array(

+ 63 - 1
src/Muzich/CoreBundle/Repository/ElementRepository.php View File

34
    * @param ElementSearcher $searcher
34
    * @param ElementSearcher $searcher
35
    * @return Doctrine\ORM\Query
35
    * @return Doctrine\ORM\Query
36
    */
36
    */
37
-  public function findBySearch(ElementSearcher $searcher, $user_id, $exec_type = 'execute')
37
+  public function findBySearch(ElementSearcher $searcher, $user_id, $exec_type = 'execute', $params = array())
38
   {
38
   {
39
     // Tableaux des paramétres
39
     // Tableaux des paramétres
40
     $params_ids = array();
40
     $params_ids = array();
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
+    // Si c'est une recherche string, les autres paramètres ne sont pas nécéssaire
66
+    if (($string = $searcher->getString()))
67
+    {
68
+      // On prépare notre liste de mots
69
+      $words = array_unique(array_merge(
70
+        explode(' ', $string),
71
+        explode('-', $string),
72
+        explode('- ', $string),
73
+        explode(' -', $string),
74
+        explode(' - ', $string),
75
+        explode(',', $string),
76
+        explode(', ', $string),
77
+        explode(' ,', $string),
78
+        explode(' , ', $string)
79
+      ));
80
+      
81
+      // On récupère les ids des elements correspondants
82
+      $where_string = "WHERE 1 = 2";
83
+      $params_string = array();
84
+      $word_min_length = 0;
85
+      if (isset($params['word_min_length']))
86
+      {
87
+        $word_min_length = $params['word_min_length'];
88
+      }
89
+      foreach ($words as $i => $word)
90
+      {
91
+        if (strlen($word) >= $word_min_length)
92
+        {
93
+          if ($where_string === "WHERE 1 = 2")
94
+          {
95
+            $where_string = " WHERE UPPER(e.name) LIKE :str".$i;
96
+          }
97
+          else
98
+          {
99
+            $where_string .= " OR UPPER(e.name) LIKE :str".$i;
100
+          }
101
+          $params_string['str'.$i] = '%'.strtoupper($word).'%';
102
+        }
103
+      }
104
+      
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
+      ;
125
+    }
126
+    
65
     
127
     
66
     // Booléen nous permettant de savoir si un where a déjà été écrit
128
     // Booléen nous permettant de savoir si un where a déjà été écrit
67
     $is_where = false;
129
     $is_where = false;

+ 12 - 1
src/Muzich/CoreBundle/Resources/views/GlobalSearch/results.html.twig View File

8
   
8
   
9
   {% include "MuzichCoreBundle:GlobalSearch:form.html.twig" with {'form': form } %}
9
   {% include "MuzichCoreBundle:GlobalSearch:form.html.twig" with {'form': form } %}
10
   
10
   
11
-  {% if results.users|length or results.groups|length %}
11
+  {% if results.users|length or results.groups|length or results.elements|length %}
12
   
12
   
13
     {% if results.users %}
13
     {% if results.users %}
14
 
14
 
38
 
38
 
39
     {% endif %}
39
     {% endif %}
40
   
40
   
41
+    {% if results.elements %}
42
+      
43
+      {% include "MuzichCoreBundle:SearchElement:default.html.twig" with {
44
+        'noelements_filter' : true,
45
+        'elements'          : results.elements
46
+      }%}
47
+      
48
+    {% endif %}
49
+      
50
+  {% else %}
51
+      
41
   {% endif %}
52
   {% endif %}
42
 
53
 
43
 {% endblock %}
54
 {% endblock %}

+ 28 - 9
src/Muzich/CoreBundle/Searcher/ElementSearcher.php View File

101
   protected $tag_strict = false;
101
   protected $tag_strict = false;
102
   
102
   
103
   /**
103
   /**
104
+   * A renseigné pour une recherche portant sur les nom
105
+   * 
106
+   * @var string 
107
+   */
108
+  protected $string = null;
109
+  
110
+  
111
+  /**
104
    * @see SearcherInterface
112
    * @see SearcherInterface
105
    * @param array $params 
113
    * @param array $params 
106
    */
114
    */
115
     $this->setAttributes(array(
123
     $this->setAttributes(array(
116
       'network', 'tags', 'count', 'user_id', 'group_id', 
124
       'network', 'tags', 'count', 'user_id', 'group_id', 
117
       'favorite', 'id_limit', 'searchnew', 'ids', 'ids_display',
125
       'favorite', 'id_limit', 'searchnew', 'ids', 'ids_display',
118
-      'tag_strict'
126
+      'tag_strict', 'string'
119
     ), $params);
127
     ), $params);
120
     
128
     
121
   }
129
   }
130
     $this->setAttributes(array(
138
     $this->setAttributes(array(
131
       'network', 'tags', 'count', 'user_id', 'group_id', 
139
       'network', 'tags', 'count', 'user_id', 'group_id', 
132
       'favorite', 'id_limit', 'searchnew', 'ids', 'ids_display',
140
       'favorite', 'id_limit', 'searchnew', 'ids', 'ids_display',
133
-      'tag_strict'
141
+      'tag_strict', 'string'
134
     ), $params);
142
     ), $params);
135
   }
143
   }
136
   
144
   
150
       'favorite'    => $this->isFavorite(),
158
       'favorite'    => $this->isFavorite(),
151
       'ids'         => $this->getIds(),
159
       'ids'         => $this->getIds(),
152
       'ids_display' => $this->getIdsDisplay(),
160
       'ids_display' => $this->getIdsDisplay(),
153
-      'tag_strict'  => $this->getTagStrict()
161
+      'tag_strict'  => $this->getTagStrict(),
162
+      'string'      => $this->getString()
154
     );
163
     );
155
   }
164
   }
156
   
165
   
255
   {
264
   {
256
     return $this->tag_strict;
265
     return $this->tag_strict;
257
   }
266
   }
267
+  
268
+  public function setString($string)
269
+  {
270
+    $this->string = $string;
271
+  }
272
+  
273
+  public function getString()
274
+  {
275
+    return $this->string;
276
+  }
258
 
277
 
259
   /**
278
   /**
260
    * Construction de l'objet Query
279
    * Construction de l'objet Query
265
    * 
284
    * 
266
    * @return collection
285
    * @return collection
267
    */
286
    */
268
-  protected function constructQueryObject(Registry $doctrine, $user_id, $exec_type = 'execute')
287
+  protected function constructQueryObject(Registry $doctrine, $user_id, $exec_type = 'execute', $params = array())
269
   {
288
   {
270
     $this->setQuery($doctrine
289
     $this->setQuery($doctrine
271
       ->getRepository('MuzichCoreBundle:Element')
290
       ->getRepository('MuzichCoreBundle:Element')
272
-      ->findBySearch($this, $user_id, $exec_type))
291
+      ->findBySearch($this, $user_id, $exec_type, $params))
273
     ;
292
     ;
274
   }
293
   }
275
   
294
   
282
    * 
301
    * 
283
    * @return collection
302
    * @return collection
284
    */
303
    */
285
-  public function getQuery(Registry $doctrine, $user_id, $exec_type = 'execute')
304
+  public function getQuery(Registry $doctrine, $user_id, $exec_type = 'execute', $params = array())
286
   {
305
   {
287
-    $this->constructQueryObject($doctrine, $user_id, $exec_type);
306
+    $this->constructQueryObject($doctrine, $user_id, $exec_type, $params);
288
     return $this->query;
307
     return $this->query;
289
   }
308
   }
290
 
309
 
298
    * 
317
    * 
299
    * @return collection
318
    * @return collection
300
    */
319
    */
301
-  public function getElements(Registry $doctrine, $user_id, $exec_type = 'execute')
320
+  public function getElements(Registry $doctrine, $user_id, $exec_type = 'execute', $params = array())
302
   {
321
   {
303
-    $query = $this->getQuery($doctrine, $user_id, $exec_type);
322
+    $query = $this->getQuery($doctrine, $user_id, $exec_type, $params);
304
     
323
     
305
     switch ($exec_type)
324
     switch ($exec_type)
306
     {
325
     {

+ 16 - 9
src/Muzich/CoreBundle/Searcher/GlobalSearcher.php View File

5
 use Symfony\Component\Validator\Constraints as Assert;
5
 use Symfony\Component\Validator\Constraints as Assert;
6
 use Symfony\Bundle\DoctrineBundle\Registry;
6
 use Symfony\Bundle\DoctrineBundle\Registry;
7
 use Muzich\CoreBundle\Searcher\UserAndGroupSearcher;
7
 use Muzich\CoreBundle\Searcher\UserAndGroupSearcher;
8
+use Muzich\CoreBundle\Searcher\ElementSearcher;
8
 
9
 
9
 /**
10
 /**
10
  * 
11
  * 
50
    * @param Registry $doctrine
51
    * @param Registry $doctrine
51
    * @return array
52
    * @return array
52
    */
53
    */
53
-  public function getResults(Registry $doctrine)
54
+  public function getResults(Registry $doctrine, $user_id, $min_word_length = null)
54
   {
55
   {
55
-    $ug_searcher = new UserAndGroupSearcher();
56
-    $ug_searcher->setString($this->string);
57
-    
58
-    
59
-    // instancier objet SearchUser and groups;
60
-    // puis faire recherche sur elements
61
-    
62
     // 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.
63
     $string = str_replace('%', '#', $this->string);
57
     $string = str_replace('%', '#', $this->string);
58
+    // instancier objet SearchUser and groups;
59
+    $ugs = new UserAndGroupSearcher();
60
+    $ugs->setString($this->string);
64
     
61
     
65
-    return $ug_searcher->getResults($doctrine);
62
+    // puis on fait recherche sur elements
63
+    $es = new ElementSearcher();
64
+    $es->init(array('string' => $string));
65
+    $results = $ugs->getResults($doctrine);
66
+    $results['elements'] = $es->getElements(
67
+      $doctrine, 
68
+      $user_id, 
69
+      'execute',
70
+      array('word_min_length' => $min_word_length)
71
+    );
72
+    return $results;
66
   }
73
   }
67
   
74
   
68
   
75