Browse Source

Mise en place d'une surcharge pour le Controlleur. Permettant la manipulation (objet, session) de l'objet recherche d'elements.

bastien 13 years ago
parent
commit
881b01985a

+ 8 - 0
src/Muzich/CoreBundle/Entity/User.php View File

@@ -11,6 +11,7 @@ use \Doctrine\Common\Collections\ArrayCollection;
11 11
  * 
12 12
  * @ORM\Entity
13 13
  * @ORM\Table(name="m_user")
14
+ * @ORM\Entity(repositoryClass="Muzich\CoreBundle\Repository\UserRepository")
14 15
  */
15 16
 class User extends BaseUser
16 17
 {
@@ -228,4 +229,11 @@ class User extends BaseUser
228 229
   {
229 230
     return $this->groups;
230 231
   }
232
+  
233
+  /*
234
+   * 
235
+   * 
236
+   */
237
+  
238
+  
231 239
 }

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

@@ -46,14 +46,14 @@ class ElementRepository extends EntityRepository
46 46
     }
47 47
     
48 48
     $query_with = "WITH ";
49
-    foreach ($searcher->getTags() as $tag)
49
+    foreach ($searcher->getTags() as $tag_id)
50 50
     {
51 51
       if ($query_with != "WITH ")
52 52
       {
53 53
         $query_with .= "OR ";
54 54
       }
55
-      $query_with .= "t.id = :tagid".$tag->getId()." ";
56
-      $params['tagid'.$tag->getId()] = $tag->getId();
55
+      $query_with .= "t.id = :tagid".$tag_id." ";
56
+      $params['tagid'.$tag_id] = $tag_id;
57 57
     }
58 58
     
59 59
     $query_join2 = ' JOIN e.owner';

+ 34 - 0
src/Muzich/CoreBundle/Repository/UserRepository.php View File

@@ -0,0 +1,34 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Repository;
4
+
5
+use Doctrine\ORM\EntityRepository;
6
+
7
+class UserRepository extends EntityRepository
8
+{
9
+  
10
+  /**
11
+   * Retourne les tag_ids préférés de l'user.
12
+   * 
13
+   * @param int $user_id
14
+   * @param int $limit
15
+   * @return array 
16
+   */
17
+  public function getTagIdsFavorites($user_id, $limit)
18
+  {
19
+    return $this->getEntityManager()
20
+      ->createQuery('
21
+        SELECT t.id FROM MuzichCoreBundle:Tag t 
22
+        JOIN t.users_favorites uf
23
+        JOIN uf.user u
24
+        WHERE u.id = :uid
25
+        ORDER BY uf.position ASC'
26
+      )
27
+      ->setParameter('uid', $user_id)
28
+      ->setMaxResults($limit)
29
+      ->getArrayResult()
30
+    ;
31
+  }
32
+  
33
+}
34
+  

+ 14 - 46
src/Muzich/CoreBundle/Searcher/ElementSearcher.php View File

@@ -20,7 +20,7 @@ class ElementSearcher extends Searcher implements SearcherInterface
20 20
   protected $network = self::NETWORK_PUBLIC;
21 21
   
22 22
   /**
23
-   * Liste des tags utilisés lors de la recherche.
23
+   * Liste des tag_ids utilisés lors de la recherche.
24 24
    * 
25 25
    * @var array
26 26
    */
@@ -34,20 +34,6 @@ class ElementSearcher extends Searcher implements SearcherInterface
34 34
    */
35 35
   protected $count = 20;
36 36
   
37
-//  /**
38
-//   * Objet requete
39
-//   * 
40
-//   * @var  
41
-//   */
42
-//  protected $query = null;
43
-//  
44
-//  /**
45
-//   * Liste des Element Résultats
46
-//   * 
47
-//   * @var array
48
-//   */
49
-//  protected $results = array();
50
-  
51 37
   /**
52 38
    * @see SearcherInterface
53 39
    * @param array $params 
@@ -78,37 +64,19 @@ class ElementSearcher extends Searcher implements SearcherInterface
78 64
     ), $params);
79 65
   }
80 66
   
81
-//  /**
82
-//   * @see SearcherInterface
83
-//   */
84
-//  public function constructQueryObject()
85
-//  {
86
-//    
87
-//  }
88
-//  
89
-//  /**
90
-//   * @see SearcherInterface
91
-//   * @return 
92
-//   */
93
-//  public function getQueryObject()
94
-//  {
95
-//    if (!$this->query)
96
-//    {
97
-//      $this->constructQueryObject();
98
-//    }
99
-//    
100
-//    return $this->query;
101
-//  }
102
-//  
103
-//  public function getResults()
104
-//  {
105
-//    if (!$this->query)
106
-//    {
107
-//      $this->constructQueryObject();
108
-//    }
109
-//    
110
-//    //...
111
-//  }
67
+  /**
68
+   * Récupération des paramètres de la recherche.
69
+   * 
70
+   * @return array 
71
+   */
72
+  public function getParams()
73
+  {
74
+    return array(
75
+      'network' => $this->getNetwork(),
76
+      'tags' => $this->getTags(),
77
+      'count' => $this->getCount()
78
+    );
79
+  }
112 80
   
113 81
   public function getNetwork()
114 82
   {

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

@@ -0,0 +1,71 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\lib;
4
+
5
+use Symfony\Bundle\FrameworkBundle\Controller\Controller as BaseController;
6
+use Muzich\CoreBundle\Searcher\ElementSearcher;
7
+
8
+class Controller extends BaseController
9
+{
10
+  
11
+  private $ElementSearcher = null;
12
+    
13
+  /**
14
+   * Met a jour l'objet ElementSearcher
15
+   * 
16
+   * @param ElementSearcher $es 
17
+   */
18
+  protected function setElementSearcher(ElementSearcher $es)
19
+  {
20
+    $this->ElementSearcher = $es;
21
+    $session->set('user.element_search.params', $es->getParams());
22
+  }
23
+  
24
+  /**
25
+   * Retourn l'objet ElementSearcher en cours.
26
+   * 
27
+   * @param int $user_id
28
+   * @return  ElementSearcher
29
+   */
30
+  protected function getElementSearcher($user_id)
31
+  {
32
+    // Premièrement, est-ce que l'objet existe
33
+    if (!$this->ElementSearcher)
34
+    {
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;
68
+    }
69
+  }
70
+  
71
+}

+ 3 - 17
src/Muzich/HomeBundle/Controller/HomeController.php View File

@@ -3,7 +3,7 @@
3 3
 namespace Muzich\HomeBundle\Controller;
4 4
 
5 5
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
6
-use Symfony\Bundle\FrameworkBundle\Controller\Controller;
6
+use Muzich\CoreBundle\lib\Controller;
7 7
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
8 8
 
9 9
 use Muzich\CoreBundle\Searcher\ElementSearcher;
@@ -18,26 +18,12 @@ class HomeController extends Controller
18 18
   public function indexAction()
19 19
   {        
20 20
     $user = $this->container->get('security.context')->getToken()->getUser();
21
-    
22
-    $search = new ElementSearcher();
23
-    
24
-    $search->init(array(
25
-      'network' => ElementSearcher::NETWORK_PUBLIC,
26
-      'tags' => array(
27
-        $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Hardtek'),
28
-        $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->findOneByName('Tribe')
29
-      ),
30
-      'count' => 30
31
-    ));
32
-    
21
+       
33 22
     $query = $this->getDoctrine()
34 23
       ->getRepository('MuzichCoreBundle:Element')
35
-      ->findBySearch($search)
24
+      ->findBySearch($this->getElementSearcher($user->getId()))
36 25
     ;
37 26
     
38
-    //$product = new Query();
39
-//    $product->execute();
40
-    
41 27
     return array('elements' => $query->execute());
42 28
   }
43 29
 }