Browse Source

Ajout du choix de groupe lors d'un ajout d'élément.

bastien 13 years ago
parent
commit
39188142e3

+ 5 - 1
app/Resources/translations/flash.fr.yml View File

15
     failure:      Un erreur est survenue lors de la création du groupe
15
     failure:      Un erreur est survenue lors de la création du groupe
16
   update:
16
   update:
17
     success:      La mise a jour a été un succés
17
     success:      La mise a jour a été un succés
18
-    failure:      Une erreur est survenue lors de la mise a jour du groupe
18
+    failure:      Une erreur est survenue lors de la mise a jour du groupe
19
+    
20
+element:
21
+  add:
22
+    error:        Une erreur est survenus lors de l'ajout

+ 27 - 10
src/Muzich/CoreBundle/Controller/CoreController.php View File

132
     $form = $this->createForm(
132
     $form = $this->createForm(
133
       new ElementAddForm(),
133
       new ElementAddForm(),
134
       array(),
134
       array(),
135
-      array('tags' => $this->getTagsArray())
135
+      array(
136
+       'tags'   => $this->getTagsArray(),
137
+       'groups' => $this->getGroupsArray()
138
+      )
136
     );
139
     );
137
     
140
     
138
     if ($this->getRequest()->getMethod() == 'POST')
141
     if ($this->getRequest()->getMethod() == 'POST')
148
         
151
         
149
         $em->persist($element);
152
         $em->persist($element);
150
         $em->flush();
153
         $em->flush();
154
+        
155
+        if ($this->getRequest()->isXmlHttpRequest())
156
+        {
157
+
158
+        }
159
+        else
160
+        {
161
+          return $this->redirect($this->generateUrl('home'));
162
+        }
163
+        
164
+      }
165
+      else
166
+      {
167
+        if ($this->getRequest()->isXmlHttpRequest())
168
+        {
169
+
170
+        }
171
+        else
172
+        {
173
+          $this->setFlash('error', 'element.add.error');
174
+          return $this->redirect($this->generateUrl('home'));
175
+        }
176
+        
151
       }
177
       }
152
       
178
       
153
     }
179
     }
154
     
180
     
155
-    if ($this->getRequest()->isXmlHttpRequest())
156
-    {
157
-      
158
-    }
159
-    else
160
-    {
161
-      return $this->redirect($this->generateUrl('home'));
162
-    }
163
-    
164
   }
181
   }
165
   
182
   
166
 //  protected function proceedElement(Element $element)
183
 //  protected function proceedElement(Element $element)

+ 10 - 0
src/Muzich/CoreBundle/ElementFactory/ElementManager.php View File

70
     $this->element->setUrl($params['url']);
70
     $this->element->setUrl($params['url']);
71
     $this->element->setOwner($owner);
71
     $this->element->setOwner($owner);
72
     $this->element->setTagsWithIds($this->em, $params['tags']);
72
     $this->element->setTagsWithIds($this->em, $params['tags']);
73
+    $this->setGroup($params);
73
     $this->determineType();
74
     $this->determineType();
74
     $this->proceedExtraFields();
75
     $this->proceedExtraFields();
75
   }
76
   }
76
   
77
   
78
+  protected function setGroup($params)
79
+  {
80
+    if ($params['group_id'])
81
+    {
82
+      $group = $this->em->getRepository('MuzichCoreBundle:Group')->findOneById($params['group_id']);
83
+      $this->element->setGroup($group);
84
+    }
85
+  }
86
+  
77
   /**
87
   /**
78
    * Determine le type d'objet auquel on a affaire.
88
    * Determine le type d'objet auquel on a affaire.
79
    */
89
    */

+ 9 - 0
src/Muzich/CoreBundle/Form/Element/ElementAddForm.php View File

22
       'expanded'          => true,
22
       'expanded'          => true,
23
       'multiple'          => true
23
       'multiple'          => true
24
     ));
24
     ));
25
+        
26
+    $options['groups'][''] = '';
27
+    
28
+    $builder->add('group_id', 'choice', array(
29
+      'choices'           => $options['groups'],
30
+      'expanded'          => false,
31
+      'multiple'          => false
32
+    ));
25
   }
33
   }
26
 
34
 
27
   public function getName()
35
   public function getName()
35
       'name' => '',
43
       'name' => '',
36
       'url' => '',
44
       'url' => '',
37
       'tags' => array(),
45
       'tags' => array(),
46
+      'groups' => '',
38
       'data_class' => 'Muzich\CoreBundle\Entity\Element'
47
       'data_class' => 'Muzich\CoreBundle\Entity\Element'
39
     );
48
     );
40
   }
49
   }

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

23
       'expanded'          => true,
23
       'expanded'          => true,
24
       'multiple'          => true
24
       'multiple'          => true
25
     ));
25
     ));
26
+        
27
+//    $builder->add('groups', 'choice', array(
28
+//      'choices'           => $options['groups'],
29
+//      'expanded'          => true,
30
+//      'multiple'          => true
31
+//    ));
26
   }
32
   }
27
 
33
 
28
   public function getName()
34
   public function getName()
34
   {
40
   {
35
     return array(
41
     return array(
36
       'tags' => array(),
42
       'tags' => array(),
43
+//      'groups' => array(),
37
     );
44
     );
38
   }
45
   }
39
 }
46
 }

+ 22 - 0
src/Muzich/CoreBundle/Repository/GroupRepository.php View File

49
     ;
49
     ;
50
   }
50
   }
51
   
51
   
52
+  /**
53
+   * Retourne un tableau de groupes étant publics, ou possédé par l'user
54
+   * 
55
+   * @param int $user_id
56
+   * @return array id => name
57
+   */
58
+  public function getPublicAndOwnedArray($user_id)
59
+  {
60
+    $group_array = array();
61
+    foreach ($this->getEntityManager()
62
+      ->createQuery('
63
+        SELECT g.id, g.name FROM MuzichCoreBundle:Group g
64
+        LEFT JOIN g.owner o 
65
+        WHERE g.open = \'1\' OR o.id = :uid'
66
+      )->setParameter('uid', $user_id)
67
+      ->getArrayResult() as $group)
68
+    {
69
+      $group_array[$group['id']] = $group['name'];
70
+    }
71
+    return $group_array;
72
+  }
73
+  
52
 }
74
 }
53
   
75
   

+ 2 - 0
src/Muzich/CoreBundle/Resources/views/Element/form.add.html.twig View File

6
 
6
 
7
 {{ form_row(form.tags) }}
7
 {{ form_row(form.tags) }}
8
 
8
 
9
+{{ form_row(form.group_id) }}
10
+
9
 {{ form_rest(form) }}
11
 {{ form_rest(form) }}

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

9
 class Controller extends BaseController
9
 class Controller extends BaseController
10
 {
10
 {
11
   
11
   
12
+  protected static $user = null;
13
+  protected static $user_personal_query = null;
14
+  
12
   /**
15
   /**
13
    * Authenticate a user with Symfony Security
16
    * Authenticate a user with Symfony Security
14
    *
17
    *
73
    * @param array $params
76
    * @param array $params
74
    * @return User
77
    * @return User
75
    */
78
    */
76
-  protected function getUser($personal_query = false, $params = array())
79
+  protected function getUser($personal_query = false, $params = array(), $force_refresh = false)
77
   {
80
   {
78
     if (!$personal_query)
81
     if (!$personal_query)
79
     {
82
     {
80
-      return $this->container->get('security.context')->getToken()->getUser();
83
+      if ($force_refresh || !self::$user)
84
+      {
85
+        self::$user = $this->container->get('security.context')->getToken()->getUser();
86
+        return self::$user;
87
+      }
88
+      return self::$user;
81
     }
89
     }
82
     else
90
     else
83
     {
91
     {
84
-      return $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
85
-        $this->container->get('security.context')->getToken()->getUser()->getId(),
86
-        array_key_exists('join', $params) ? $params['join'] : array()
87
-      )->getSingleResult();
92
+      if ($force_refresh || !self::$user_personal_query)
93
+      {
94
+        self::$user_personal_query = $this->getDoctrine()->getRepository('MuzichCoreBundle:User')->findOneById(
95
+          $this->container->get('security.context')->getToken()->getUser()->getId(),
96
+          array_key_exists('join', $params) ? $params['join'] : array()
97
+        )->getSingleResult();
98
+        return self::$user_personal_query;
99
+      }
100
+      return self::$user_personal_query;
88
     }
101
     }
89
   }
102
   }
90
   
103
   
91
   /**
104
   /**
105
+   * @desc Retourne l'id de l'utilisateur en cours
106
+   */
107
+  protected function getUserId()
108
+  {
109
+    $this->getUser()->getId();
110
+  }
111
+  
112
+  /**
92
    * Retourne un tabeau avec les tags connus.
113
    * Retourne un tabeau avec les tags connus.
93
    * TODO: Voir pour que cette info soit stocké (par exemple) dans un champs
114
    * TODO: Voir pour que cette info soit stocké (par exemple) dans un champs
94
    * texte en base. (json array)
115
    * texte en base. (json array)
116
+   * TODO2: Voir si la question d'opt. "Formulaire d'ajout d'un élément" ne résoue pas
117
+   * le problème du TODO ci-dessus.
95
    * 
118
    * 
96
    * @return array
119
    * @return array
97
    */
120
    */
100
     return $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->getTagsArray();
123
     return $this->getDoctrine()->getRepository('MuzichCoreBundle:Tag')->getTagsArray();
101
   }
124
   }
102
   
125
   
126
+  /**
127
+   * Retourne un tabeau avec les groupes accessible pour un ajout d'element.
128
+   * 
129
+   * @return array
130
+   */
131
+  protected function getGroupsArray()
132
+  {
133
+    return $this->getDoctrine()->getRepository('MuzichCoreBundle:Group')
134
+      ->getPublicAndOwnedArray($this->getUserId());
135
+  }
136
+  
103
   protected function setFlash($type, $value)
137
   protected function setFlash($type, $value)
104
   {
138
   {
105
     $this->container->get('session')->setFlash($type, $value);
139
     $this->container->get('session')->setFlash($type, $value);

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

17
    */
17
    */
18
   public function indexAction()
18
   public function indexAction()
19
   {
19
   {
20
-    $search_object = $this->getElementSearcher($this->getUser()->getId());
20
+    $search_object = $this->getElementSearcher($this->getUserId());
21
     
21
     
22
     $search_form = $this->createForm(
22
     $search_form = $this->createForm(
23
       new ElementSearchForm(), 
23
       new ElementSearchForm(), 
24
       $search_object->getParams(),
24
       $search_object->getParams(),
25
-      array('tags' => $tags = $this->getTagsArray())
25
+      array(
26
+        'tags' => $tags = $this->getTagsArray()
27
+      )
26
     );
28
     );
27
     
29
     
28
     $add_form = $this->createForm(
30
     $add_form = $this->createForm(
29
       new ElementAddForm(),
31
       new ElementAddForm(),
30
       array(),
32
       array(),
31
-      array('tags' => $tags)
33
+      array(
34
+        'tags' => $tags,
35
+        'groups' => $this->getGroupsArray(),
36
+      )
32
     );
37
     );
33
         
38
         
34
     return array(
39
     return array(