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,4 +15,8 @@ group:
15 15
     failure:      Un erreur est survenue lors de la création du groupe
16 16
   update:
17 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,7 +132,10 @@ class CoreController extends Controller
132 132
     $form = $this->createForm(
133 133
       new ElementAddForm(),
134 134
       array(),
135
-      array('tags' => $this->getTagsArray())
135
+      array(
136
+       'tags'   => $this->getTagsArray(),
137
+       'groups' => $this->getGroupsArray()
138
+      )
136 139
     );
137 140
     
138 141
     if ($this->getRequest()->getMethod() == 'POST')
@@ -148,19 +151,33 @@ class CoreController extends Controller
148 151
         
149 152
         $em->persist($element);
150 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 183
 //  protected function proceedElement(Element $element)

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

@@ -70,10 +70,20 @@ class ElementManager
70 70
     $this->element->setUrl($params['url']);
71 71
     $this->element->setOwner($owner);
72 72
     $this->element->setTagsWithIds($this->em, $params['tags']);
73
+    $this->setGroup($params);
73 74
     $this->determineType();
74 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 88
    * Determine le type d'objet auquel on a affaire.
79 89
    */

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

@@ -22,6 +22,14 @@ class ElementAddForm extends AbstractType
22 22
       'expanded'          => true,
23 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 35
   public function getName()
@@ -35,6 +43,7 @@ class ElementAddForm extends AbstractType
35 43
       'name' => '',
36 44
       'url' => '',
37 45
       'tags' => array(),
46
+      'groups' => '',
38 47
       'data_class' => 'Muzich\CoreBundle\Entity\Element'
39 48
     );
40 49
   }

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

@@ -23,6 +23,12 @@ class ElementSearchForm extends AbstractType
23 23
       'expanded'          => true,
24 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 34
   public function getName()
@@ -34,6 +40,7 @@ class ElementSearchForm extends AbstractType
34 40
   {
35 41
     return array(
36 42
       'tags' => array(),
43
+//      'groups' => array(),
37 44
     );
38 45
   }
39 46
 }

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

@@ -49,5 +49,27 @@ class GroupRepository extends EntityRepository
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,4 +6,6 @@
6 6
 
7 7
 {{ form_row(form.tags) }}
8 8
 
9
+{{ form_row(form.group_id) }}
10
+
9 11
 {{ form_rest(form) }}

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

@@ -9,6 +9,9 @@ use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
9 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 16
    * Authenticate a user with Symfony Security
14 17
    *
@@ -73,25 +76,45 @@ class Controller extends BaseController
73 76
    * @param array $params
74 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 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 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 113
    * Retourne un tabeau avec les tags connus.
93 114
    * TODO: Voir pour que cette info soit stocké (par exemple) dans un champs
94 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 119
    * @return array
97 120
    */
@@ -100,6 +123,17 @@ class Controller extends BaseController
100 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 137
   protected function setFlash($type, $value)
104 138
   {
105 139
     $this->container->get('session')->setFlash($type, $value);

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

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