Browse Source

Ajout du formulaire d'ajout d'élément sur une page de groupe.

bastien 13 years ago
parent
commit
4ed1e38a45

+ 4 - 1
app/Resources/translations/elements.fr.yml View File

4
   
4
   
5
 element:
5
 element:
6
   favorite:
6
   favorite:
7
-    add:                Ajouter a mes favoris
7
+    add:                Ajouter a mes favoris
8
+  add:
9
+    group:
10
+      will_be_in:       L'élément sera ajouté dans le groupe

+ 25 - 5
src/Muzich/CoreBundle/Controller/CoreController.php View File

139
   /**
139
   /**
140
    *  Procédure d'ajout d'un element
140
    *  Procédure d'ajout d'un element
141
    */
141
    */
142
-  public function elementAddAction()
143
-  {
142
+  public function elementAddAction($group_slug)
143
+  {    
144
     $user = $this->getUser();
144
     $user = $this->getUser();
145
     $em = $this->getDoctrine()->getEntityManager();
145
     $em = $this->getDoctrine()->getEntityManager();
146
     
146
     
149
       array(),
149
       array(),
150
       array(
150
       array(
151
        'tags'   => $this->getTagsArray(),
151
        'tags'   => $this->getTagsArray(),
152
+        // Ligne non obligatoire (cf. verif du contenu du form -> ticket)
152
        'groups' => $this->getGroupsArray()
153
        'groups' => $this->getGroupsArray()
153
       )
154
       )
154
     );
155
     );
165
         $factory = new ElementManager($element, $em, $this->container);
166
         $factory = new ElementManager($element, $em, $this->container);
166
         $factory->proceedFill($data, $user);
167
         $factory->proceedFill($data, $user);
167
         
168
         
169
+        // Si on a précisé un groupe
170
+        if ($group_slug)
171
+        {
172
+          $group = $this->findGroupWithSlug($group_slug);
173
+          if ($group->userCanAddElement($this->getUserId()))
174
+          {
175
+            $element->setGroup($group);
176
+          }
177
+          else
178
+          {
179
+            throw $this->createNotFoundException('Vous ne pouvez ajouter d\'element a ce groupe.');
180
+          }
181
+          $redirect_url = $this->generateUrl('show_group', array('slug' => $group->getSlug()));
182
+        }
183
+        else
184
+        {
185
+          $redirect_url = $this->generateUrl('home');
186
+        }
187
+        
168
         $em->persist($element);
188
         $em->persist($element);
169
         $em->flush();
189
         $em->flush();
170
         
190
         
171
         if ($this->getRequest()->isXmlHttpRequest())
191
         if ($this->getRequest()->isXmlHttpRequest())
172
         {
192
         {
173
-
193
+          
174
         }
194
         }
175
         else
195
         else
176
         {
196
         {
177
-          return $this->redirect($this->generateUrl('home'));
197
+          return $this->redirect($redirect_url);
178
         }
198
         }
179
         
199
         
180
       }
200
       }
187
         else
207
         else
188
         {
208
         {
189
           $this->setFlash('error', 'element.add.error');
209
           $this->setFlash('error', 'element.add.error');
190
-          return $this->redirect($this->generateUrl('home'));
210
+          return $this->redirect($redirect_url);
191
         }
211
         }
192
         
212
         
193
       }
213
       }

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

83
   
83
   
84
   protected function setGroup($params)
84
   protected function setGroup($params)
85
   {
85
   {
86
-    if ($params['group_id'])
86
+    if ($params['group'])
87
     {
87
     {
88
-      $group = $this->em->getRepository('MuzichCoreBundle:Group')->findOneById($params['group_id']);
88
+      $group = $this->em->getRepository('MuzichCoreBundle:Group')->findOneById($params['group']);
89
       $this->element->setGroup($group);
89
       $this->element->setGroup($group);
90
     }
90
     }
91
   }
91
   }

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

210
     return $this->tags;
210
     return $this->tags;
211
   }
211
   }
212
   
212
   
213
+  public function setTags($tags)
214
+  {
215
+    $this->tags = $tags;
216
+  }
213
 
217
 
214
   /**
218
   /**
215
    * Set owner
219
    * Set owner
352
     return count($this->elements_favorites);
356
     return count($this->elements_favorites);
353
   }
357
   }
354
   
358
   
359
+  public function setGroupToId()
360
+  {
361
+    $this->group = $this->group->getId();
362
+  }
363
+  
355
 }
364
 }

+ 20 - 0
src/Muzich/CoreBundle/Entity/Group.php View File

321
     $this->tags = $tags_id;
321
     $this->tags = $tags_id;
322
   }
322
   }
323
   
323
   
324
+  /**
325
+   * Retourne vrai si l'user_id peut poster dans ce groupe.
326
+   * 
327
+   * @param int $user_id
328
+   * @return boolean 
329
+   */
330
+  public function userCanAddElement($user_id)
331
+  {
332
+    if ($this->open)
333
+    {
334
+      return true;
335
+    }
336
+    
337
+    if ($this->getOwner()->getId() == $user_id)
338
+    {
339
+      return true;
340
+    }
341
+    
342
+    return false;
343
+  }
324
 }
344
 }

+ 6 - 5
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
     
25
     
28
-    $builder->add('group_id', 'choice', array(
26
+    $options['groups'][''] = '';
27
+    $builder->add('group', 'choice', array(
29
       'choices'           => $options['groups'],
28
       'choices'           => $options['groups'],
30
       'expanded'          => false,
29
       'expanded'          => false,
31
-      'multiple'          => false
30
+      'multiple'          => false,
31
+      'required'          => false
32
     ));
32
     ));
33
+    
33
   }
34
   }
34
 
35
 
35
   public function getName()
36
   public function getName()
43
       'name' => '',
44
       'name' => '',
44
       'url' => '',
45
       'url' => '',
45
       'tags' => array(),
46
       'tags' => array(),
46
-      'groups' => '',
47
+      'groups' => array(),
47
       'data_class' => 'Muzich\CoreBundle\Entity\Element'
48
       'data_class' => 'Muzich\CoreBundle\Entity\Element'
48
     );
49
     );
49
   }
50
   }

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

9
   defaults: { _controller: MuzichCoreBundle:Core:follow }
9
   defaults: { _controller: MuzichCoreBundle:Core:follow }
10
    
10
    
11
 element_add:
11
 element_add:
12
-  pattern:  /element/add
13
-  defaults: { _controller: MuzichCoreBundle:Core:elementAdd }
12
+  pattern:  /element/add/{group_slug}
13
+  defaults: { _controller: MuzichCoreBundle:Core:elementAdd, group_slug: null }

+ 1 - 1
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) }}
9
+{{ form_row(form.group) }}
10
 
10
 
11
 {{ form_rest(form) }}
11
 {{ form_rest(form) }}

+ 16 - 1
src/Muzich/HomeBundle/Controller/ShowController.php View File

4
 
4
 
5
 use Muzich\CoreBundle\lib\Controller;
5
 use Muzich\CoreBundle\lib\Controller;
6
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
6
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
7
+use Muzich\CoreBundle\Form\Element\ElementAddForm;
8
+use Muzich\CoreBundle\Entity\Element;
7
 
9
 
8
 class ShowController extends Controller
10
 class ShowController extends Controller
9
 {
11
 {
44
       'group_id'  => $group->getId()
46
       'group_id'  => $group->getId()
45
     ));
47
     ));
46
     
48
     
49
+    ($group->getOwner()->getId() == $this->getUserId()) ? $his = true : $his = false;
50
+    if ($his || $group->getOpen())
51
+    {      
52
+      $add_form = $this->createForm(
53
+        new ElementAddForm(),
54
+        array(),
55
+        array(
56
+          'tags' => $this->getTagsArray()
57
+        )
58
+      );
59
+    }
60
+    
47
     return array(
61
     return array(
48
       'group'     => $group,
62
       'group'     => $group,
49
       'his_group' => ($group->getOwner()->getId() == $this->getUserId()) ? true : false,
63
       'his_group' => ($group->getOwner()->getId() == $this->getUserId()) ? true : false,
50
       'elements'  => $search_object->getElements($this->getDoctrine(), $this->getUserId()),
64
       'elements'  => $search_object->getElements($this->getDoctrine(), $this->getUserId()),
51
       'following' => $this->getUser()->isFollowingGroupByQuery($this->getDoctrine(), $group->getId()),
65
       'following' => $this->getUser()->isFollowingGroupByQuery($this->getDoctrine(), $group->getId()),
52
-      'user'      => $this->getUser()
66
+      'user'      => $this->getUser(),
67
+      'add_form'  => (isset($add_form)) ? $add_form->createView() : null
53
     );
68
     );
54
   }
69
   }
55
   
70
   

+ 12 - 0
src/Muzich/HomeBundle/Resources/views/Show/showGroup.html.twig View File

24
     
24
     
25
   <h2>{{ group.name }}</h2>
25
   <h2>{{ group.name }}</h2>
26
   
26
   
27
+  <form action="{{ path('element_add', {'group_slug' : group.slug}) }}" method="post" {{ form_enctype(add_form) }}>
28
+    
29
+    {% include "MuzichCoreBundle:Element:form.add.html.twig" with { 'form': add_form } %}
30
+
31
+    <p>
32
+      <i>
33
+        {{ 'element.add.group.will_be_in'|trans({}, 'elements') }}
34
+      </i>
35
+    </p>
36
+    <input type="submit" />
37
+  </form>
38
+  
27
   {% include "MuzichCoreBundle:SearchElement:default.html.twig" %}
39
   {% include "MuzichCoreBundle:SearchElement:default.html.twig" %}
28
     
40
     
29
 {% endblock %}
41
 {% endblock %}