Explorar el Código

Mise en place de la recherche d'Users et de Groups.

bastien hace 13 años
padre
commit
7c25591fd7

+ 1 - 0
src/Muzich/CoreBundle/Entity/Group.php Ver fichero

@@ -13,6 +13,7 @@ use \Doctrine\Common\Collections\ArrayCollection;
13 13
  * 
14 14
  * @ORM\Entity
15 15
  * @ORM\Table(name="m_group")
16
+ * @ORM\Entity(repositoryClass="Muzich\CoreBundle\Repository\GroupRepository")
16 17
  */
17 18
 class Group
18 19
 {

+ 32 - 0
src/Muzich/CoreBundle/Repository/GroupRepository.php Ver fichero

@@ -0,0 +1,32 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Repository;
4
+
5
+use Doctrine\ORM\EntityRepository;
6
+
7
+class GroupRepository extends EntityRepository
8
+{
9
+  
10
+  /**
11
+   * Retourne un tableau d'user correspondant a un chaine de caractère
12
+   * La recherche est effectué sur le username.
13
+   * 
14
+   * @param type $string
15
+   * @return Doctrine\ORM\Query
16
+   */
17
+  public function findByString($string)
18
+  {
19
+    return $this->getEntityManager()
20
+      ->createQuery("
21
+        SELECT g FROM MuzichCoreBundle:Group g
22
+        WHERE UPPER(g.name) LIKE :str
23
+        ORDER BY g.name ASC"
24
+      )
25
+      ->setParameters(array(
26
+        'str' => '%'.strtoupper(trim($string)).'%'
27
+      ))
28
+    ;
29
+  }
30
+  
31
+}
32
+  

+ 22 - 0
src/Muzich/CoreBundle/Repository/UserRepository.php Ver fichero

@@ -34,5 +34,27 @@ class UserRepository extends EntityRepository
34 34
     return $tag_ids;
35 35
   }
36 36
   
37
+  /**
38
+   * Retourne un tableau d'user correspondant a un chaine de caractère
39
+   * La recherche est effectué sur le username.
40
+   * 
41
+   * @param type $string
42
+   * @return Doctrine\ORM\Query
43
+   */
44
+  public function findByString($string)
45
+  {
46
+    return $this->getEntityManager()
47
+      ->createQuery("
48
+        SELECT u FROM MuzichCoreBundle:User u
49
+        WHERE UPPER(u.username) LIKE :str
50
+        OR UPPER(u.usernameCanonical) LIKE :str
51
+        ORDER BY u.username ASC"
52
+      )
53
+      ->setParameters(array(
54
+        'str' => '%'.strtoupper(trim($string)).'%'
55
+      ))
56
+    ;
57
+  }
58
+  
37 59
 }
38 60
   

+ 1 - 1
src/Muzich/CoreBundle/Resources/views/layout.html.twig Ver fichero

@@ -15,7 +15,7 @@
15 15
 	<link href="{{ asset('bundles/muzichcore/css/base.css') }}" rel="stylesheet" media="screen" type="text/css" />
16 16
 	<link href="{{ asset('bundles/muzichcore/css/main.css') }}" rel="stylesheet" media="screen" type="text/css" />
17 17
   {% block css %}{% endblock %}
18
-	{#<script src="{{ asset('js/main.js') }}" type="text/javascript"></script>#}
18
+	<script src="{{ asset('bundles/muzichcore/js/jquery-1.6.4.min.js') }}" type="text/javascript"></script>
19 19
   {% block js %}{% endblock %}
20 20
 </head>
21 21
 <body>

+ 1 - 1
src/Muzich/CoreBundle/Searcher/ElementSearcher.php Ver fichero

@@ -65,7 +65,7 @@ class ElementSearcher extends Searcher implements SearcherInterface
65 65
   }
66 66
   
67 67
   /**
68
-   * Récupération des paramètres de la recherche.
68
+   * @see SearcherInterface
69 69
    * 
70 70
    * @return array 
71 71
    */

+ 5 - 0
src/Muzich/CoreBundle/Searcher/SearcherInterface.php Ver fichero

@@ -19,6 +19,11 @@ interface SearcherInterface
19 19
    */
20 20
   public function update($params);
21 21
   
22
+  /**
23
+   * Récupération des paramètres
24
+   */
25
+  public function getParams();
26
+  
22 27
 //  /**
23 28
 //   * Procédure qui construit la requete.
24 29
 //   */

+ 70 - 0
src/Muzich/CoreBundle/Searcher/UserAndGroupSearcher.php Ver fichero

@@ -0,0 +1,70 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Searcher;
4
+
5
+use Symfony\Component\Validator\Constraints as Assert;
6
+
7
+class UserAndGroupSearcher extends Searcher implements SearcherInterface
8
+{
9
+  
10
+  /**
11
+   * Chaine de caractère représentant la recherche.
12
+   * 
13
+   * @var string
14
+   * @Assert\NotBlank()
15
+   * @Assert\Type("string")
16
+   * @Assert\MinLength(3)
17
+   */
18
+  protected $string;
19
+  
20
+  /**
21
+   * @see SearcherInterface
22
+   * @param array $params 
23
+   */
24
+  public function init($params)
25
+  {
26
+    // Control des parametres transmis.
27
+    $this->checkParams($params, array(
28
+      'string' => "Muzich\CoreBundle\Searcher\UserAndGroupSearch::init(): \$params: Un string est nécéssaire"
29
+    ));
30
+    
31
+    // Mise a jour des attributs
32
+    $this->setAttributes(array('string', 'min_lenght'), $params);
33
+  }
34
+  
35
+  /**
36
+   * @see SearcherInterface
37
+   * @param array $params 
38
+   */
39
+  public function update($params)
40
+  {
41
+    // Mise a jour des attributs
42
+    $this->setAttributes(array(
43
+      'string', 'min_length'
44
+    ), $params);
45
+  }
46
+  
47
+  /**
48
+   * @see SearcherInterface
49
+   * 
50
+   * @return array 
51
+   */
52
+  public function getParams()
53
+  {
54
+    return array(
55
+      'string' => $this->string,
56
+      'min_length' => $this->min_length
57
+    );
58
+  }
59
+  
60
+  public function getString()
61
+  {
62
+    return $this->string;
63
+  }
64
+  
65
+  public function setString($string)
66
+  {
67
+    $this->string = $string;
68
+  }
69
+  
70
+}

+ 54 - 2
src/Muzich/MynetworkBundle/Controller/MynetworkController.php Ver fichero

@@ -4,7 +4,7 @@ namespace Muzich\MynetworkBundle\Controller;
4 4
 
5 5
 use Muzich\CoreBundle\lib\Controller;
6 6
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
7
-
7
+use Muzich\CoreBundle\Searcher\UserAndGroupSearcher;
8 8
 
9 9
 class MynetworkController extends Controller
10 10
 {
@@ -29,13 +29,65 @@ class MynetworkController extends Controller
29 29
   }
30 30
   
31 31
   /**
32
+   * Action qui affiche la page de recherche, et efectue la recherche
33
+   * d'utilisateurs et de groupes.
32 34
    * 
33 35
    * @Template()
34 36
    */
35 37
   public function searchAction()
36 38
   {
39
+    $request = $this->getRequest();
40
+    $search = new UserAndGroupSearcher();
41
+    $results = array('users' => null, 'groups' => null);
42
+    
43
+    $search_form = $this->createFormBuilder($search)
44
+      ->add('string', 'text')
45
+    ->getForm();
46
+    
47
+    // Si l'utilisateur effectue la recherche
48
+    if ($request->getMethod() == 'POST')
49
+    {
50
+      $search_form->bindRequest($request);
51
+      if ($search_form->isValid())
52
+      {
53
+        $results = $this->doSearchAction($search);
54
+      }
55
+    }
37 56
     
38
-    return array();
57
+    if ($this->getRequest()->isXmlHttpRequest())
58
+    {
59
+      
60
+    }
61
+    else
62
+    {
63
+      return array(
64
+        'search_form' => $search_form->createView(),
65
+        'results'     => $results,
66
+        'search_done' => $search->getString() ? true : false
67
+      );
68
+    }
69
+  }
70
+  
71
+  protected function doSearchAction($search)
72
+  {
73
+    $string = str_replace('%', '#', $search->getString());
74
+    
75
+    $users = $this->getDoctrine()
76
+      ->getRepository('MuzichCoreBundle:User')
77
+      ->findByString($string)
78
+      ->execute()
79
+    ;
80
+    
81
+    $groups = $this->getDoctrine()
82
+      ->getRepository('MuzichCoreBundle:Group')
83
+      ->findByString($string)
84
+      ->execute()
85
+    ;
86
+    
87
+    return array(
88
+      'users'  => $users,
89
+      'groups' => $groups
90
+    );
39 91
   }
40 92
   
41 93
 }

+ 4 - 0
src/Muzich/MynetworkBundle/Resources/config/routing.yml Ver fichero

@@ -6,4 +6,8 @@ mynetwork_index:
6 6
 mynetwork_search:
7 7
   pattern:  /my-network/search
8 8
   defaults: { _controller: MuzichMynetworkBundle:Mynetwork:search }
9
+  
10
+#mynetwork_search:
11
+#  pattern:  /my-network/search/do
12
+#  defaults: { _controller: MuzichMynetworkBundle:Mynetwork:search }
9 13
   

+ 47 - 0
src/Muzich/MynetworkBundle/Resources/views/Mynetwork/search.html.twig Ver fichero

@@ -10,6 +10,53 @@
10 10
     </a>
11 11
   </p>
12 12
   
13
+  <b>Rechercher des utilisateurs et des groupes</b>
13 14
   
15
+  <form action="{{ path('mynetwork_search') }}" method="post" {{ form_enctype(search_form) }}>
16
+    
17
+    {{ form_errors(search_form) }}
18
+
19
+    {{ form_row(search_form.string) }}
20
+
21
+    {{ form_rest(search_form) }}
22
+
23
+    <input type="submit" />
24
+  </form>
25
+  
26
+  {% if results.users|length or results.groups|length %}
27
+  
28
+    {% if results.users %}
29
+
30
+      <b>Utilisateurs</b>
31
+      
32
+      <ul>
33
+      {% for user in results.users %} 
34
+        <li>
35
+          {{ user.username }}
36
+        </li>
37
+      {% endfor %}
38
+      </ul>
39
+
40
+    {% endif %}
41
+  
42
+    {% if results.groups %}
43
+
44
+      <b>Groupes</b>
45
+      
46
+      <ul>
47
+      {% for group in results.groups %} 
48
+        <li>
49
+          {{ group.name }}
50
+        </li>
51
+      {% endfor %}
52
+      </ul>
53
+
54
+    {% endif %}
55
+  
56
+  {% elseif search_done %}
57
+      
58
+      <p>Il n'y a aucun résultat a votre recherche</p>
59
+      
60
+  {% endif %}
14 61
 
15 62
 {% endblock %}

La diferencia del archivo ha sido suprimido porque es demasiado grande
+ 4 - 0
web/bundles/muzichcore/js/jquery-1.6.4.min.js