Browse Source

Refactorisation (récupération User, Group, instanciation ElementSearch)

bastien 13 years ago
parent
commit
7392c250bd

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

134
       ->getPublicAndOwnedArray($this->getUserId());
134
       ->getPublicAndOwnedArray($this->getUserId());
135
   }
135
   }
136
   
136
   
137
+  /**
138
+   * Met en place un message de type flash.
139
+   * 
140
+   * @param string $type
141
+   * @param string $value 
142
+   */
137
   protected function setFlash($type, $value)
143
   protected function setFlash($type, $value)
138
   {
144
   {
139
     $this->container->get('session')->setFlash($type, $value);
145
     $this->container->get('session')->setFlash($type, $value);
140
   }
146
   }
141
   
147
   
148
+  /**
149
+   * Instancie et retourne un objet ElementSearch
150
+   * 
151
+   * @param array $params
152
+   * @return ElementSearcher 
153
+   */
154
+  protected function createSearchObject($params)
155
+  {
156
+    $search_object = new ElementSearcher();
157
+    $search_object->init($params);
158
+    return $search_object;
159
+  }
160
+  
161
+  /**
162
+   * Retourne un User en fonction du slug passé
163
+   * 
164
+   * @param string $slug
165
+   * @return User 
166
+   */
167
+  protected function findUserWithSlug($slug)
168
+  {
169
+    try {
170
+      return $this->getDoctrine()
171
+        ->getRepository('MuzichCoreBundle:User')
172
+        ->findOneBySlug($slug)
173
+        ->getSingleResult()
174
+      ;      
175
+    } catch (\Doctrine\ORM\NoResultException $e) {
176
+        throw $this->createNotFoundException('Utilisateur introuvable.');
177
+    }
178
+  }
179
+  
180
+  /**
181
+   * Retourne un Group en fonction du slug passé
182
+   * 
183
+   * @param string $slug
184
+   * @return Group 
185
+   */
186
+  protected function findGroupWithSlug($slug)
187
+  {
188
+    try {
189
+      return $this->getDoctrine()
190
+        ->getRepository('MuzichCoreBundle:Group')
191
+        ->findOneBySlug($slug)
192
+        ->getSingleResult()
193
+      ;      
194
+    } catch (\Doctrine\ORM\NoResultException $e) {
195
+        throw $this->createNotFoundException('Groupe introuvable.');
196
+    }
197
+  }
198
+  
142
 }
199
 }

+ 3 - 15
src/Muzich/FavoriteBundle/Controller/FavoriteController.php View File

62
    */
62
    */
63
   public function myListAction()
63
   public function myListAction()
64
   {
64
   {
65
-    $search_object = new ElementSearcher();
66
-    $search_object->init(array(
65
+    $search_object = $this->createSearchObject(array(
67
       'user_id'  => $this->getUserId(),
66
       'user_id'  => $this->getUserId(),
68
       'favorite' => true
67
       'favorite' => true
69
     ));
68
     ));
81
    */
80
    */
82
   public function userListAction($slug)
81
   public function userListAction($slug)
83
   {
82
   {
84
-    try {
85
-      
86
-      $viewed_user = $this->getDoctrine()
87
-        ->getRepository('MuzichCoreBundle:User')
88
-        ->findOneBySlug($slug)
89
-        ->getSingleResult()
90
-      ;
91
-      
92
-    } catch (\Doctrine\ORM\NoResultException $e) {
93
-        throw $this->createNotFoundException('Utilisateur introuvable.');
94
-    }
83
+    $viewed_user = $this->findUserWithSlug($slug);
95
     
84
     
96
-    $search_object = new ElementSearcher();
97
-    $search_object->init(array(
85
+    $search_object = $this->createSearchObject(array(
98
       'user_id'  => $viewed_user->getId(),
86
       'user_id'  => $viewed_user->getId(),
99
       'favorite' => true
87
       'favorite' => true
100
     ));
88
     ));

+ 6 - 0
src/Muzich/FavoriteBundle/Resources/views/Favorite/myList.html.twig View File

4
 
4
 
5
 {% block content %}
5
 {% block content %}
6
 
6
 
7
+  {% include "MuzichCoreBundle:Menu:containerMenu.html.twig" with { 
8
+    'active': 'myfavorites', 
9
+    'mygroups': false, 
10
+    'myfavorites': true 
11
+  } %}
12
+
7
   <h2>{{ 'favorites.your_favorites'|trans({}, 'network') }}</h2>
13
   <h2>{{ 'favorites.your_favorites'|trans({}, 'network') }}</h2>
8
 
14
 
9
   {% render "MuzichCoreBundle:Search:doSearchElements" with { 'search': search_object } %}
15
   {% render "MuzichCoreBundle:Search:doSearchElements" with { 'search': search_object } %}

+ 6 - 0
src/Muzich/FavoriteBundle/Resources/views/Favorite/userList.html.twig View File

4
 
4
 
5
 {% block content %}
5
 {% block content %}
6
 
6
 
7
+  {% include "MuzichCoreBundle:Menu:containerMenu.html.twig" with { 
8
+    'active': '', 
9
+    'mygroups': false, 
10
+    'myfavorites': true 
11
+  } %}
12
+
7
   <h2>{{ 'favorites.user_favorites'|trans({'%name%' : viewed_user.name}, 'network') }}</h2>
13
   <h2>{{ 'favorites.user_favorites'|trans({'%name%' : viewed_user.name}, 'network') }}</h2>
8
 
14
 
9
   {% render "MuzichCoreBundle:Search:doSearchElements" with { 'search': search_object } %}
15
   {% render "MuzichCoreBundle:Search:doSearchElements" with { 'search': search_object } %}

+ 0 - 6
src/Muzich/FavoriteBundle/Resources/views/layout.html.twig View File

6
 
6
 
7
 {% block main_content %}
7
 {% block main_content %}
8
   
8
   
9
-  {% include "MuzichCoreBundle:Menu:containerMenu.html.twig" with { 
10
-    'active': 'myfavorites', 
11
-    'mygroups': false, 
12
-    'myfavorites': true 
13
-  } %}
14
-  
15
   {% block content %}{% endblock %}
9
   {% block content %}{% endblock %}
16
   
10
   
17
 {% endblock %}
11
 {% endblock %}

+ 10 - 28
src/Muzich/HomeBundle/Controller/ShowController.php View File

9
 {
9
 {
10
   
10
   
11
   /**
11
   /**
12
-   * 
12
+   * Page public de l'utilisateur demandé.
13
    * 
13
    * 
14
    * @Template()
14
    * @Template()
15
    */
15
    */
16
   public function showUserAction($slug)
16
   public function showUserAction($slug)
17
   {
17
   {
18
-    try {
19
-      
20
-      $viewed_user = $this->getDoctrine()
21
-        ->getRepository('MuzichCoreBundle:User')
22
-        ->findOneBySlug($slug)
23
-        ->getSingleResult()
24
-      ;
25
-      $user = $this->getUser();
26
-      
27
-    } catch (\Doctrine\ORM\NoResultException $e) {
28
-        throw $this->createNotFoundException('Utilisateur introuvable.');
29
-    }
30
-    
18
+    $viewed_user = $this->findUserWithSlug($slug);
19
+    $user = $this->getUser();
20
+        
31
     return array(
21
     return array(
32
       'viewed_user' => $viewed_user,
22
       'viewed_user' => $viewed_user,
33
       'elements'    => $this->getShowedEntityElements($viewed_user->getId(), 'User'),
23
       'elements'    => $this->getShowedEntityElements($viewed_user->getId(), 'User'),
37
   }
27
   }
38
   
28
   
39
   /**
29
   /**
40
-   * 
30
+   * Page publique du groupe demandé.
41
    * 
31
    * 
42
    * @Template()
32
    * @Template()
43
    */
33
    */
44
   public function showGroupAction($slug)
34
   public function showGroupAction($slug)
45
   {
35
   {
46
-    try {
47
-      
48
-      $group = $this->getDoctrine()
49
-        ->getRepository('MuzichCoreBundle:Group')
50
-        ->findOneBySlug($slug)
51
-        ->getSingleResult()
52
-      ;
53
-      $user = $this->getUser();
54
-      
55
-    } catch (\Doctrine\ORM\NoResultException $e) {
56
-        throw $this->createNotFoundException('Groupe introuvable.');
57
-    }
58
-    
36
+    $group = $this->findGroupWithSlug($slug);
37
+    $user = $this->getUser();
38
+        
59
     return array(
39
     return array(
60
       'group'       => $group,
40
       'group'       => $group,
61
       'his_group'   => ($group->getOwner()->getId() == $user->getId()) ? true : false,
41
       'his_group'   => ($group->getOwner()->getId() == $user->getId()) ? true : false,
66
   }
46
   }
67
   
47
   
68
   /**
48
   /**
49
+   * Refactorisation pour showUserAction et showGroupAction. Récupére les 
50
+   * elements de l'entité demandé.
69
    *
51
    *
70
    * @param Entity $entity
52
    * @param Entity $entity
71
    * @param string $type
53
    * @param string $type