瀏覽代碼

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

bastien 13 年之前
父節點
當前提交
4ed1e38a45

+ 4 - 1
app/Resources/translations/elements.fr.yml 查看文件

@@ -4,4 +4,7 @@ noelements:
4 4
   
5 5
 element:
6 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 查看文件

@@ -139,8 +139,8 @@ class CoreController extends Controller
139 139
   /**
140 140
    *  Procédure d'ajout d'un element
141 141
    */
142
-  public function elementAddAction()
143
-  {
142
+  public function elementAddAction($group_slug)
143
+  {    
144 144
     $user = $this->getUser();
145 145
     $em = $this->getDoctrine()->getEntityManager();
146 146
     
@@ -149,6 +149,7 @@ class CoreController extends Controller
149 149
       array(),
150 150
       array(
151 151
        'tags'   => $this->getTagsArray(),
152
+        // Ligne non obligatoire (cf. verif du contenu du form -> ticket)
152 153
        'groups' => $this->getGroupsArray()
153 154
       )
154 155
     );
@@ -165,16 +166,35 @@ class CoreController extends Controller
165 166
         $factory = new ElementManager($element, $em, $this->container);
166 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 188
         $em->persist($element);
169 189
         $em->flush();
170 190
         
171 191
         if ($this->getRequest()->isXmlHttpRequest())
172 192
         {
173
-
193
+          
174 194
         }
175 195
         else
176 196
         {
177
-          return $this->redirect($this->generateUrl('home'));
197
+          return $this->redirect($redirect_url);
178 198
         }
179 199
         
180 200
       }
@@ -187,7 +207,7 @@ class CoreController extends Controller
187 207
         else
188 208
         {
189 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 查看文件

@@ -83,9 +83,9 @@ class ElementManager
83 83
   
84 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 89
       $this->element->setGroup($group);
90 90
     }
91 91
   }

+ 9 - 0
src/Muzich/CoreBundle/Entity/Element.php 查看文件

@@ -210,6 +210,10 @@ class Element
210 210
     return $this->tags;
211 211
   }
212 212
   
213
+  public function setTags($tags)
214
+  {
215
+    $this->tags = $tags;
216
+  }
213 217
 
214 218
   /**
215 219
    * Set owner
@@ -352,4 +356,9 @@ class Element
352 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 查看文件

@@ -321,4 +321,24 @@ class Group
321 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 查看文件

@@ -22,14 +22,15 @@ class ElementAddForm extends AbstractType
22 22
       'expanded'          => true,
23 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 28
       'choices'           => $options['groups'],
30 29
       'expanded'          => false,
31
-      'multiple'          => false
30
+      'multiple'          => false,
31
+      'required'          => false
32 32
     ));
33
+    
33 34
   }
34 35
 
35 36
   public function getName()
@@ -43,7 +44,7 @@ class ElementAddForm extends AbstractType
43 44
       'name' => '',
44 45
       'url' => '',
45 46
       'tags' => array(),
46
-      'groups' => '',
47
+      'groups' => array(),
47 48
       'data_class' => 'Muzich\CoreBundle\Entity\Element'
48 49
     );
49 50
   }

+ 2 - 2
src/Muzich/CoreBundle/Resources/config/routing.yml 查看文件

@@ -9,5 +9,5 @@ follow:
9 9
   defaults: { _controller: MuzichCoreBundle:Core:follow }
10 10
    
11 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 查看文件

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

+ 16 - 1
src/Muzich/HomeBundle/Controller/ShowController.php 查看文件

@@ -4,6 +4,8 @@ namespace Muzich\HomeBundle\Controller;
4 4
 
5 5
 use Muzich\CoreBundle\lib\Controller;
6 6
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
7
+use Muzich\CoreBundle\Form\Element\ElementAddForm;
8
+use Muzich\CoreBundle\Entity\Element;
7 9
 
8 10
 class ShowController extends Controller
9 11
 {
@@ -44,12 +46,25 @@ class ShowController extends Controller
44 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 61
     return array(
48 62
       'group'     => $group,
49 63
       'his_group' => ($group->getOwner()->getId() == $this->getUserId()) ? true : false,
50 64
       'elements'  => $search_object->getElements($this->getDoctrine(), $this->getUserId()),
51 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 查看文件

@@ -24,6 +24,18 @@
24 24
     
25 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 39
   {% include "MuzichCoreBundle:SearchElement:default.html.twig" %}
28 40
     
29 41
 {% endblock %}