Browse Source

Merge branch 'feature/group' into unstable

bastien 13 years ago
parent
commit
fa8bc2fc33

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

@@ -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 View File

@@ -116,8 +116,8 @@ class CoreController extends Controller
116 116
   /**
117 117
    *  Procédure d'ajout d'un element
118 118
    */
119
-  public function elementAddAction()
120
-  {
119
+  public function elementAddAction($group_slug)
120
+  {    
121 121
     $user = $this->getUser();
122 122
     $em = $this->getDoctrine()->getEntityManager();
123 123
     
@@ -126,6 +126,7 @@ class CoreController extends Controller
126 126
       array(),
127 127
       array(
128 128
        'tags'   => $this->getTagsArray(),
129
+        // Ligne non obligatoire (cf. verif du contenu du form -> ticket)
129 130
        'groups' => $this->getGroupsArray()
130 131
       )
131 132
     );
@@ -142,16 +143,35 @@ class CoreController extends Controller
142 143
         $factory = new ElementManager($element, $em, $this->container);
143 144
         $factory->proceedFill($data, $user);
144 145
         
146
+        // Si on a précisé un groupe
147
+        if ($group_slug)
148
+        {
149
+          $group = $this->findGroupWithSlug($group_slug);
150
+          if ($group->userCanAddElement($this->getUserId()))
151
+          {
152
+            $element->setGroup($group);
153
+          }
154
+          else
155
+          {
156
+            throw $this->createNotFoundException('Vous ne pouvez ajouter d\'element a ce groupe.');
157
+          }
158
+          $redirect_url = $this->generateUrl('show_group', array('slug' => $group->getSlug()));
159
+        }
160
+        else
161
+        {
162
+          $redirect_url = $this->generateUrl('home');
163
+        }
164
+        
145 165
         $em->persist($element);
146 166
         $em->flush();
147 167
         
148 168
         if ($this->getRequest()->isXmlHttpRequest())
149 169
         {
150
-
170
+          
151 171
         }
152 172
         else
153 173
         {
154
-          return $this->redirect($this->generateUrl('home'));
174
+          return $this->redirect($redirect_url);
155 175
         }
156 176
         
157 177
       }
@@ -164,7 +184,7 @@ class CoreController extends Controller
164 184
         else
165 185
         {
166 186
           $this->setFlash('error', 'element.add.error');
167
-          return $this->redirect($this->generateUrl('home'));
187
+          return $this->redirect($redirect_url);
168 188
         }
169 189
         
170 190
       }

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

@@ -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 View File

@@ -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 View File

@@ -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
 }

+ 15 - 0
src/Muzich/CoreBundle/Entity/User.php View File

@@ -260,6 +260,21 @@ class User extends BaseUser
260 260
   {
261 261
     return $this->groups_owned;
262 262
   }
263
+
264
+  /**
265
+   * Get groups in array (id => name)
266
+   *
267
+   * @return Doctrine\Common\Collections\Collection 
268
+   */
269
+  public function getGroupsOwnedArray()
270
+  {
271
+    $groups = array();
272
+    foreach ($this->groups_owned as $group)
273
+    {
274
+      $groups[$group->getId()] = $group->getName();
275
+    }
276
+    return $groups;
277
+  }
263 278
   
264 279
   public function getSlug()
265 280
   {

+ 6 - 5
src/Muzich/CoreBundle/Form/Element/ElementAddForm.php View File

@@ -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 View File

@@ -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 View File

@@ -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) }}

+ 4 - 1
src/Muzich/HomeBundle/Controller/HomeController.php View File

@@ -21,6 +21,9 @@ class HomeController extends Controller
21 21
   public function indexAction()
22 22
   {
23 23
     $search_object = $this->getElementSearcher();
24
+    $user = $this->getUser(true, array('join' => array(
25
+      'groups_owned'
26
+    )), true);
24 27
     
25 28
     $search_form = $this->createForm(
26 29
       new ElementSearchForm(), 
@@ -35,7 +38,7 @@ class HomeController extends Controller
35 38
       array(),
36 39
       array(
37 40
         'tags' => $tags,
38
-        'groups' => $this->getGroupsArray(),
41
+        'groups' => $user->getGroupsOwnedArray(),
39 42
       )
40 43
     );
41 44
         

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

@@ -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 View File

@@ -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 %}