Browse Source

Mise en place du formulaire de recherche de tag, travaillant avec l'objet de recherche d'elements.

bastien 13 years ago
parent
commit
97d79ea6bb

+ 4 - 0
app/config/routing.yml View File

3
 #   resource: "@FrameworkBundle/Resources/config/routing/internal.xml"
3
 #   resource: "@FrameworkBundle/Resources/config/routing/internal.xml"
4
 #   prefix:   /_internal
4
 #   prefix:   /_internal
5
 
5
 
6
+MuzichCoreBundle:
7
+  resource: "@MuzichCoreBundle/Resources/config/routing.yml"
8
+  prefix: /
9
+  
6
 MuzichIndexBundle:
10
 MuzichIndexBundle:
7
   resource: "@MuzichIndexBundle/Resources/config/routing.yml"
11
   resource: "@MuzichIndexBundle/Resources/config/routing.yml"
8
   prefix: /
12
   prefix: /

+ 38 - 1
src/Muzich/CoreBundle/Controller/SearchController.php View File

6
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
6
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
7
 
7
 
8
 use Muzich\CoreBundle\Searcher\ElementSearcher;
8
 use Muzich\CoreBundle\Searcher\ElementSearcher;
9
+use Muzich\CoreBundle\Form\Search\ElementSearchForm;
9
 use Doctrine\ORM\Query;
10
 use Doctrine\ORM\Query;
10
 
11
 
11
 class SearchController extends Controller
12
 class SearchController extends Controller
12
 {
13
 {
13
   
14
   
14
   /**
15
   /**
16
+   * Procédure de recherche, qui met a jour l'objet de recherche (ainsi
17
+   * que les paramétres en session). 
15
    * 
18
    * 
16
    */
19
    */
17
-  public function searchelementsAction(ElementSearcher $search, $template = 'default')
20
+  public function searchElementsAction()
21
+  {
22
+    $request = $this->getRequest();
23
+    $search_object = $this->getElementSearcher($this->getUser()->getId());
24
+    
25
+    $search_form = $this->createForm(
26
+      new ElementSearchForm(), 
27
+      $search_object->getParams(),
28
+      array('tags' => $this->getTagsArray())
29
+    );
30
+    
31
+    if ($request->getMethod() == 'POST')
32
+    {
33
+      $search_form->bindRequest($request);
34
+      if ($search_form->isValid())
35
+      {
36
+        $search_object->update($search_form->getData());
37
+        $this->setElementSearcher($search_object);
38
+      }
39
+    }
40
+    
41
+    if ($this->getRequest()->isXmlHttpRequest())
42
+    {
43
+      // template qui apelle doSearchElementsAction 
44
+    }
45
+    else
46
+    {
47
+      return $this->redirect($this->generateUrl('home'));
48
+    }
49
+  }
50
+  
51
+  /**
52
+   *  Procédure chargé d'afficher les résultats de la recherche.
53
+   */
54
+  public function doSearchElementsAction(ElementSearcher $search, $template = 'default')
18
   {
55
   {
19
     $elements = $this->getDoctrine()
56
     $elements = $this->getDoctrine()
20
       ->getRepository('MuzichCoreBundle:Element')
57
       ->getRepository('MuzichCoreBundle:Element')

+ 1 - 0
src/Muzich/CoreBundle/Entity/Tag.php View File

10
  * 
10
  * 
11
  * @ORM\Entity
11
  * @ORM\Entity
12
  * @ORM\Table(name="tag")
12
  * @ORM\Table(name="tag")
13
+ * @ORM\Entity(repositoryClass="Muzich\CoreBundle\Repository\TagRepository")
13
  */
14
  */
14
 class Tag
15
 class Tag
15
 {
16
 {

+ 39 - 0
src/Muzich/CoreBundle/Form/Search/ElementSearchForm.php View File

1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Form\Search;
4
+
5
+use Symfony\Component\Form\AbstractType;
6
+use Symfony\Component\Form\FormBuilder;
7
+use Muzich\CoreBundle\Searcher\ElementSearcher;
8
+
9
+class ElementSearchForm extends AbstractType
10
+{
11
+  public function buildForm(FormBuilder $builder, array $options)
12
+  {
13
+    $builder->add('network', 'choice', array(
14
+      'choices' => array(
15
+        ElementSearcher::NETWORK_PUBLIC => 'tout le réseau',
16
+        ElementSearcher::NETWORK_PERSONAL => 'mon réseau'
17
+      ),
18
+      'required' => true,
19
+    ));
20
+        
21
+    $builder->add('tags', 'choice', array(
22
+      'choices'           => $options['tags'],
23
+      'expanded'          => true,
24
+      'multiple'          => true
25
+    ));
26
+  }
27
+
28
+  public function getName()
29
+  {
30
+    return 'element_search_form';
31
+  }
32
+  
33
+  public function getDefaultOptions(array $options)
34
+  {
35
+    return array(
36
+      'tags' => array(),
37
+    );
38
+  }
39
+}

+ 31 - 0
src/Muzich/CoreBundle/Repository/TagRepository.php View File

1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Repository;
4
+
5
+use Doctrine\ORM\EntityRepository;
6
+
7
+class TagRepository extends EntityRepository
8
+{
9
+  
10
+  /**
11
+   * Retourne les tags
12
+   * 
13
+   * @return array id => name
14
+   */
15
+  public function getTagsArray()
16
+  {
17
+    $tag_array = array();
18
+    foreach ($this->getEntityManager()
19
+      ->createQuery('
20
+        SELECT t.id, t.name FROM MuzichCoreBundle:Tag t
21
+        ORDER BY t.count DESC'
22
+      )
23
+      ->getArrayResult() as $tag)
24
+    {
25
+      $tag_array[$tag['id']] = $tag['name'];
26
+    }
27
+    return $tag_array;
28
+  }
29
+  
30
+}
31
+  

+ 8 - 4
src/Muzich/CoreBundle/Repository/UserRepository.php View File

8
 {
8
 {
9
   
9
   
10
   /**
10
   /**
11
-   * Retourne les tag_ids préférés de l'user.
11
+   * Retourne les tag id préférés de l'user.
12
    * 
12
    * 
13
    * @param int $user_id
13
    * @param int $user_id
14
    * @param int $limit
14
    * @param int $limit
16
    */
16
    */
17
   public function getTagIdsFavorites($user_id, $limit)
17
   public function getTagIdsFavorites($user_id, $limit)
18
   {
18
   {
19
-    return $this->getEntityManager()
19
+    $tag_ids = array();
20
+    foreach ($this->getEntityManager()
20
       ->createQuery('
21
       ->createQuery('
21
         SELECT t.id FROM MuzichCoreBundle:Tag t 
22
         SELECT t.id FROM MuzichCoreBundle:Tag t 
22
         JOIN t.users_favorites uf
23
         JOIN t.users_favorites uf
26
       )
27
       )
27
       ->setParameter('uid', $user_id)
28
       ->setParameter('uid', $user_id)
28
       ->setMaxResults($limit)
29
       ->setMaxResults($limit)
29
-      ->getArrayResult()
30
-    ;
30
+      ->getArrayResult() as $tag)
31
+    {
32
+      $tag_ids[] = $tag['id'];
33
+    }
34
+    return $tag_ids;
31
   }
35
   }
32
   
36
   
33
 }
37
 }

+ 6 - 1
src/Muzich/CoreBundle/Resources/config/routing.yml View File

1
-## Routing du Bundle Core
1
+
2
+
3
+search_elements:
4
+   pattern:  /search-elements
5
+   defaults: { _controller: MuzichCoreBundle:Search:searchElements }
6
+     

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

1
+<form action="{{ path('search_elements') }}" method="post" {{ form_enctype(search_form) }}>
2
+  {{ form_errors(search_form) }}
3
+
4
+  {{ form_row(search_form.network) }}
5
+
6
+  {{ form_row(search_form.tags) }}
7
+
8
+  {{ form_rest(search_form) }}
9
+
10
+  <input type="submit" />
11
+</form>

+ 39 - 39
src/Muzich/CoreBundle/lib/Controller.php View File

8
 class Controller extends BaseController
8
 class Controller extends BaseController
9
 {
9
 {
10
   
10
   
11
-  private $ElementSearcher = null;
11
+  //private $ElementSearcher = null;
12
     
12
     
13
   /**
13
   /**
14
-   * Met a jour l'objet ElementSearcher
14
+   * Met a jour l'objet ElementSearcher (en réallité on met a jour les
15
+   * paramètres en sessions).
15
    * 
16
    * 
16
    * @param ElementSearcher $es 
17
    * @param ElementSearcher $es 
17
    */
18
    */
18
   protected function setElementSearcher(ElementSearcher $es)
19
   protected function setElementSearcher(ElementSearcher $es)
19
   {
20
   {
20
-    $this->ElementSearcher = $es;
21
-    $session->set('user.element_search.params', $es->getParams());
21
+    $this->get("session")->set('user.element_search.params', $es->getParams());
22
   }
22
   }
23
   
23
   
24
   /**
24
   /**
29
    */
29
    */
30
   protected function getElementSearcher($user_id)
30
   protected function getElementSearcher($user_id)
31
   {
31
   {
32
-    // Premièrement, est-ce que l'objet existe
33
-    if (!$this->ElementSearcher)
32
+    $session = $this->get("session");
33
+    // Si l'objet n'existe pas encore, a t-on déjà des paramètres de recherche
34
+    if (!$session->has('user.element_search.params'))
34
     {
35
     {
35
-      $session  = $this->get("session");
36
-      // Si l'objet n'existe pas encore, a t-on déjà des paramètres de recherche
37
-      if (!$session->has('user.element_search.params'))
38
-      {
39
-        // Il nous faut instancier notre premier objet recherche
40
-        // Premièrement on récupère les tags favoris de l'utilisateur
41
-        $tags_id = array();
42
-        foreach ($this->getDoctrine()->getRepository('MuzichCoreBundle:User')
43
-          // TODO: 3: CONFIG !!
44
-          ->getTagIdsFavorites($user_id, 3)
45
-          as $tag)
46
-        {
47
-          $tags_id[] = $tag['id'];
48
-        }
49
-        
50
-        // Ensuite on fabrique l'objet ElementSearcher
51
-        $this->ElementSearcher = new ElementSearcher();
52
-        $this->ElementSearcher->init(array(
53
-          'tags' => $tags_id
54
-        ));
55
-        
56
-        // Et on met en session les paramètres
57
-        $session->set('user.element_search.params', $this->ElementSearcher->getParams());
58
-      }
59
-      else
60
-      {
61
-        // Des paramètres existes, on fabrique l'objet recherche
62
-        $this->ElementSearcher = new ElementSearcher();
63
-        $this->ElementSearcher->init($session->get('user.element_search.params'));
64
-      }
65
-      
66
-      // L'objet existe déjà, on le retourne
67
-      return $this->ElementSearcher;
36
+      // Il nous faut instancier notre premier objet recherche
37
+      // Premièrement on récupère les tags favoris de l'utilisateur
38
+      $this->ElementSearcher = new ElementSearcher();
39
+      $this->ElementSearcher->init(array(
40
+        'tags' => $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
41
+        // TODO: 3: CONFIG !!
42
+        ->getTagIdsFavorites($user_id, 3)
43
+      ));
44
+
45
+      // Et on met en session les paramètres
46
+      $session->set('user.element_search.params', $this->ElementSearcher->getParams());
47
+    }
48
+    else
49
+    {
50
+      // Des paramètres existes, on fabrique l'objet recherche
51
+      $this->ElementSearcher = new ElementSearcher();
52
+      $this->ElementSearcher->init($session->get('user.element_search.params'));
68
     }
53
     }
54
+    
55
+    // on le retourne
56
+    return $this->ElementSearcher;
69
   }
57
   }
70
   
58
   
71
   /**
59
   /**
78
     return $this->container->get('security.context')->getToken()->getUser();
66
     return $this->container->get('security.context')->getToken()->getUser();
79
   }
67
   }
80
   
68
   
69
+  /**
70
+   * Retourne un tabeau avec les tags connus.
71
+   * TODO: Voir pour que cette info soit stocké (par exemple) dans un champs
72
+   * texte en base. (json array)
73
+   * 
74
+   * @return array
75
+   */
76
+  protected function getTagsArray()
77
+  {
78
+    return $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->getTagsArray();
79
+  }
80
+  
81
 }
81
 }

+ 15 - 4
src/Muzich/HomeBundle/Controller/HomeController.php View File

2
 
2
 
3
 namespace Muzich\HomeBundle\Controller;
3
 namespace Muzich\HomeBundle\Controller;
4
 
4
 
5
-use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6
 use Muzich\CoreBundle\lib\Controller;
5
 use Muzich\CoreBundle\lib\Controller;
7
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
6
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
8
 
7
 
9
-use Muzich\CoreBundle\Searcher\ElementSearcher;
10
 use Doctrine\ORM\Query;
8
 use Doctrine\ORM\Query;
9
+use Muzich\CoreBundle\Form\Search\ElementSearchForm;
11
 
10
 
12
 class HomeController extends Controller
11
 class HomeController extends Controller
13
 {
12
 {
13
+  
14
   /**
14
   /**
15
    * @Template()
15
    * @Template()
16
    */
16
    */
17
   public function indexAction()
17
   public function indexAction()
18
-  {        
19
-    return array('search' => $this->getElementSearcher($this->getUser()->getId()));
18
+  {
19
+    $search_object = $this->getElementSearcher($this->getUser()->getId());
20
+    
21
+    $search_form = $this->createForm(
22
+      new ElementSearchForm(), 
23
+      $search_object->getParams(),
24
+      array('tags' => $this->getTagsArray())
25
+    );
26
+        
27
+    return array(
28
+      'search_object' => $search_object,
29
+      'search_form'   => $search_form->createView()
30
+    );
20
   }
31
   }
21
 }
32
 }

+ 1 - 1
src/Muzich/HomeBundle/Resources/config/routing.yml View File

1
 ## Routing du Bundle
1
 ## Routing du Bundle
2
 
2
 
3
-index:
3
+home:
4
   pattern:  /
4
   pattern:  /
5
   defaults: { _controller: MuzichHomeBundle:Home:index }
5
   defaults: { _controller: MuzichHomeBundle:Home:index }

+ 3 - 1
src/Muzich/HomeBundle/Resources/views/Home/index.html.twig View File

4
 
4
 
5
 {% block content %}
5
 {% block content %}
6
     
6
     
7
-  {% render "MuzichCoreBundle:Search:searchelements" with { 'search': search } %}
7
+  {% include "MuzichCoreBundle:SearchElement:form.html.twig" %}
8
+
9
+  {% render "MuzichCoreBundle:Search:doSearchElements" with { 'search': search_object } %}
8
     
10
     
9
 {% endblock %}
11
 {% endblock %}