浏览代码

Création d'un groupe.

bastien 13 年前
父节点
当前提交
363689e32d

+ 6 - 0
app/Resources/translations/flash.fr.yml 查看文件

@@ -7,3 +7,9 @@ profile.flash.updated: Le profil a été mis à jour
7 7
 change_password.flash.success: Le mot de passe a été modifié
8 8
 registration.flash.user_created: L'utilisateur a été créé avec succès
9 9
 resetting.flash.success: Le mot de passe a été réinitialisé avec succès
10
+
11
+# muzich
12
+group:
13
+  create:
14
+    success:      Le groupe a été créé avec succés
15
+    failure:      Un erreur est survenue lors de la création du groupe

+ 6 - 0
app/Resources/translations/groupform.fr.yml 查看文件

@@ -0,0 +1,6 @@
1
+
2
+name:              Nom
3
+description:       Description
4
+open:              Ouvert
5
+openhelp:          Un groupe ouvert permet a n'importe qui de publier dans votre groupe
6
+tags:              Tags

+ 0 - 2
src/Muzich/CoreBundle/ElementFactory/ElementManager.php 查看文件

@@ -61,10 +61,8 @@ class ElementManager
61 61
    * Procédure chargé de construire le contenu de l'élément.
62 62
    *  nom, Code d'embed, [...]
63 63
    * 
64
-   * @param Element $element
65 64
    * @param array $params
66 65
    * @param User $owner
67
-   * @return Element 
68 66
    */
69 67
   public function proceedFill($params, User $owner)
70 68
   {

+ 38 - 0
src/Muzich/CoreBundle/Entity/Group.php 查看文件

@@ -5,6 +5,8 @@ namespace Muzich\CoreBundle\Entity;
5 5
 use Doctrine\ORM\Mapping as ORM;
6 6
 use \Doctrine\Common\Collections\ArrayCollection;
7 7
 use Gedmo\Mapping\Annotation as Gedmo;
8
+use Doctrine\ORM\EntityManager;
9
+use Muzich\CoreBundle\Entity\GroupsTagsFavorites;
8 10
 
9 11
 /**
10 12
  * Le groupe est une sorte de liste de diffusion, a laquelle les
@@ -252,6 +254,25 @@ class Group
252 254
   {
253 255
     return $this->tags;
254 256
   }
257
+  
258
+  /**
259
+   * Add tag
260
+   *
261
+   * @param Tag $tag
262
+   */
263
+  public function addTag(Tag $tag, EntityManager $em)
264
+  {
265
+    $GroupsTagsFavorites = new GroupsTagsFavorites();
266
+    $GroupsTagsFavorites->setTag($tag);
267
+    $GroupsTagsFavorites->setPosition(0);
268
+    $em->persist($GroupsTagsFavorites);
269
+    $this->tags[] = $GroupsTagsFavorites;
270
+  }
271
+  
272
+  public function setTags($tags)
273
+  {
274
+    $this->tags = $tags;
275
+  }
255 276
 
256 277
   /**
257 278
    * Add elements
@@ -272,4 +293,21 @@ class Group
272 293
   {
273 294
       return $this->elements;
274 295
   }
296
+  
297
+  /**
298
+   * Definis les relation vers des tags.
299
+   * 
300
+   * @param array $ids 
301
+   */
302
+  public function setTagsWithIds(EntityManager $em, $ids)
303
+  {
304
+    $tags = $em->getRepository('MuzichCoreBundle:Tag')->findByIds($ids)->execute();
305
+
306
+    // Pour les nouveaux ids restants
307
+    foreach ($tags as $tag)
308
+    {
309
+      $this->addTag($tag, $em);
310
+    }
311
+  }
312
+  
275 313
 }

+ 1 - 1
src/Muzich/CoreBundle/Entity/GroupsTagsFavorites.php 查看文件

@@ -26,7 +26,7 @@ class GroupsTagsFavorites
26 26
    * Cet attribut contient l'objet Group lié
27 27
    * 
28 28
    * @ORM\ManyToOne(targetEntity="Group", inversedBy="tags")
29
-   * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
29
+   * @ORM\JoinColumn(name="group_id", referencedColumnName="id")
30 30
    */
31 31
   protected $group;
32 32
   

+ 47 - 0
src/Muzich/CoreBundle/Form/Group/GroupForm.php 查看文件

@@ -0,0 +1,47 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Form\Group;
4
+
5
+use Symfony\Component\Form\AbstractType;
6
+use Symfony\Component\Form\FormBuilder;
7
+
8
+class GroupForm extends AbstractType
9
+{
10
+  public function buildForm(FormBuilder $builder, array $options)
11
+  {
12
+    $builder->add('name', 'text', array(
13
+      'required' => true,
14
+    ));
15
+    
16
+    $builder->add('description', 'textarea', array(
17
+      'required' => true,
18
+    ));
19
+    
20
+    $builder->add('open', 'checkbox', array(
21
+      'required' => false,
22
+    ));
23
+        
24
+    $builder->add('tags', 'choice', array(
25
+      'choices'           => $options['tags'],
26
+      'expanded'          => true,
27
+      'multiple'          => true
28
+    ));
29
+  }
30
+
31
+  public function getName()
32
+  {
33
+    return 'group';
34
+  }
35
+  
36
+  public function getDefaultOptions(array $options)
37
+  {
38
+    return array(
39
+      'name' => '',
40
+      'open' => true,
41
+      'tags' => array(),
42
+      'data_class' => 'Muzich\CoreBundle\Entity\Group'
43
+    );
44
+  }
45
+}
46
+
47
+

+ 50 - 0
src/Muzich/CoreBundle/Managers/GroupManager.php 查看文件

@@ -0,0 +1,50 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Managers;
4
+
5
+use Muzich\CoreBundle\Entity\Group;
6
+use Muzich\CoreBundle\Entity\User;
7
+use Doctrine\ORM\EntityManager;
8
+use Symfony\Component\DependencyInjection\Container;
9
+
10
+/**
11
+ * 
12
+ *
13
+ * @author bux
14
+ */
15
+class GroupManager
16
+{
17
+  
18
+  protected $em;
19
+  protected $group;
20
+  protected $container;
21
+  
22
+  public function __construct(Group $group, EntityManager $em, Container $container)
23
+  {
24
+    $this->group = $group;
25
+    $this->em = $em;
26
+    $this->container = $container;
27
+    
28
+    // Slug stuff
29
+    $evm = new \Doctrine\Common\EventManager();
30
+    // ORM and ODM
31
+    $sluggableListener = new \Gedmo\Sluggable\SluggableListener();
32
+    $evm->addEventSubscriber($sluggableListener);
33
+    // now this event manager should be passed to entity manager constructor
34
+    $this->em->getEventManager()->addEventSubscriber($sluggableListener);
35
+  }
36
+  
37
+  /**
38
+   * Procédure chargé de construire le contenu tags.
39
+   * 
40
+   * @param array $tags_ids
41
+   * @param User $owner
42
+   */
43
+  public function proceedTags($tags_ids)
44
+  {
45
+    // La procédure se charge pour le moment des tags
46
+    $this->group->setTags(null);
47
+    $this->group->setTagsWithIds($this->em, $tags_ids);
48
+  }
49
+  
50
+}

+ 60 - 1
src/Muzich/GroupBundle/Controller/DefaultController.php 查看文件

@@ -4,6 +4,10 @@ namespace Muzich\GroupBundle\Controller;
4 4
 
5 5
 use Muzich\CoreBundle\lib\Controller;
6 6
 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
7
+use Muzich\CoreBundle\Entity\Group;
8
+use Muzich\CoreBundle\Form\Group\GroupForm;
9
+use Symfony\Component\HttpFoundation\Request;
10
+use Muzich\CoreBundle\Managers\GroupManager;
7 11
 
8 12
 class DefaultController extends Controller
9 13
 {
@@ -18,9 +22,64 @@ class DefaultController extends Controller
18 22
       'groups_owned'
19 23
     )));
20 24
     
25
+    $new_group = new Group();
26
+    $form_new = $this->createForm(
27
+      new GroupForm(), 
28
+      $new_group,
29
+      array('tags' => $this->getTagsArray())
30
+    );
31
+    
21 32
     return array(
22
-      'groups' => $user->getGroupsOwned()
33
+      'groups'   => $user->getGroupsOwned(),
34
+      'form_new' => $form_new->createView()
23 35
     );
24 36
   }
25 37
   
38
+  /**
39
+   * Procédure d'ajout d'un groupe
40
+   */
41
+  public function addAction(Request $request)
42
+  {
43
+    $user = $this->getUser();
44
+    $em = $this->getDoctrine()->getEntityManager();
45
+    
46
+    $new_group = new Group();
47
+    $new_group->setOwner($user);
48
+    $form_new = $this->createForm(
49
+      new GroupForm(), 
50
+      $new_group,
51
+      array('tags' => $this->getTagsArray())
52
+    );
53
+    
54
+    $form_new->bindRequest($request);
55
+    
56
+    if ($form_new->isValid())
57
+    {
58
+      $factory = new GroupManager($new_group, $em, $this->container);
59
+      $factory->proceedTags($new_group->getTags());
60
+      
61
+      $em->persist($new_group);
62
+      $em->flush();
63
+      
64
+      $this->setFlash('success', 'group.create.success');
65
+      return $this->redirect($this->generateUrl('groups_own_list'));
66
+    }
67
+    else
68
+    {
69
+      $user = $this->getUser(true, array('join' => array(
70
+        'groups_owned'
71
+      )));
72
+      
73
+      $this->setFlash('error', 'group.create.failure');
74
+      
75
+      return $this->render(
76
+        'GroupBundle:Default:myList.html.twig', 
77
+         array(
78
+           'groups'   => $user->getGroupsOwned(),
79
+           'form_new' => $form_new->createView()
80
+         )
81
+      );
82
+    }
83
+  }
84
+  
26 85
 }

+ 4 - 0
src/Muzich/GroupBundle/Resources/config/routing.yml 查看文件

@@ -2,3 +2,7 @@
2 2
 groups_own_list:
3 3
     pattern:  /my-groups
4 4
     defaults: { _controller: MuzichGroupBundle:Default:myList }
5
+
6
+group_add:
7
+    pattern:   /my-groups/add
8
+    defaults: { _controller: MuzichGroupBundle:Default:add }

+ 36 - 0
src/Muzich/GroupBundle/Resources/views/Default/myList.html.twig 查看文件

@@ -22,4 +22,40 @@
22 22
 
23 23
   {% endif %}
24 24
 
25
+  <h2>Ajouter un groupe</h2>
26
+
27
+  <form action="{{ path('group_add') }}" method="post" {{ form_enctype(form_new) }}>
28
+    {{ form_errors(form_new) }}
29
+
30
+    <div class="field">
31
+      {{ form_label(form_new.name, 'name'|trans({}, 'groupform')) }}
32
+      {{ form_errors(form_new.name) }}
33
+      {{ form_widget(form_new.name) }}
34
+    </div>
35
+        
36
+    <div class="field">
37
+      {{ form_label(form_new.description, 'description'|trans({}, 'groupform')) }}
38
+      {{ form_errors(form_new.description) }}
39
+      {{ form_widget(form_new.description) }}
40
+    </div>
41
+      
42
+      <i>{{ 'openhelp'|trans({}, 'groupform') }}</i>
43
+      
44
+    <div class="field">
45
+      {{ form_label(form_new.open, 'open'|trans({}, 'groupform')) }}
46
+      {{ form_errors(form_new.open) }}
47
+      {{ form_widget(form_new.open) }}
48
+    </div>
49
+      
50
+    <div class="field">
51
+      {{ form_label(form_new.tags, 'tags'|trans({}, 'groupform')) }}
52
+      {{ form_errors(form_new.tags) }}
53
+      {{ form_widget(form_new.tags) }}
54
+    </div>
55
+
56
+    {{ form_rest(form_new) }}
57
+
58
+    <input type="submit" />
59
+  </form>
60
+
25 61
 {% endblock %}