Преглед на файлове

Sur la page start: Mise en place du formulaire de gestion des tags favoris.

bastien преди 12 години
родител
ревизия
3681c05c8f

+ 95 - 0
src/Muzich/CoreBundle/Entity/User.php Целия файл

@@ -6,6 +6,8 @@ use FOS\UserBundle\Entity\User as BaseUser;
6 6
 use Doctrine\ORM\Mapping as ORM;
7 7
 use \Doctrine\Common\Collections\ArrayCollection;
8 8
 use Gedmo\Mapping\Annotation as Gedmo;
9
+use Doctrine\ORM\EntityManager;
10
+use Muzich\CoreBundle\Entity\UsersTagsFavorites;
9 11
 
10 12
 /**
11 13
  * Cet entité est l'utilisateur ayant effectué la requête.
@@ -337,4 +339,97 @@ class User extends BaseUser
337 339
   {
338 340
     return hash('sha256', $this->getSalt().$this->getUsername());
339 341
   }
342
+  
343
+  /**
344
+   * Ajoute a l'user les tags transmis (id) comme favoris.
345
+   * 
346
+   * @param EntityManager $em 
347
+   * @param array $ids 
348
+   */
349
+  public function addTagsFavoritesById(EntityManager $em, $ids)
350
+  {
351
+    // TODO: attention aux relations déjà existantes.
352
+    // TODO penser a supprimer celles qui n'existes plus.
353
+    
354
+    $ids_to_add = $ids;
355
+    
356
+    // Pour chacun des tags favoris 
357
+    foreach ($this->tags_favorites as $tag_favorite)
358
+    {
359
+      $trouve = false;
360
+      foreach ($ids as $i => $id)
361
+      {
362
+        if ($id == $tag_favorite->getTag()->getId())
363
+        {
364
+          $trouve = true;
365
+          // Si le tag était favoris déjà avant (et aussi maintenant)
366
+          // il ne sera ni a ajouter, ni a supprimer.
367
+          unset($ids_to_add[$i]);
368
+        }
369
+      }
370
+      
371
+      if (!$trouve)
372
+      {
373
+        // Si cet ancien tag n'est plus dans la liste, il faut le supprimer
374
+        // (rappel: on supprime ici la relation, pas le tag)
375
+        $em->remove($tag_favorite);
376
+      }
377
+    }
378
+    
379
+    $tag_favorite_position_max = $this->getTagFavoritePositionMax();
380
+    
381
+    // Pour les nouveaux ids restants
382
+    foreach ($ids as $id)
383
+    {
384
+      $tag = $em->getRepository('MuzichCoreBundle:Tag')
385
+        ->findOneById($id)
386
+      ;
387
+      
388
+      $tag_favorite = new UsersTagsFavorites();
389
+      $tag_favorite->setUser($this);
390
+      $tag_favorite->setTag($tag);
391
+      $tag_favorite->setPosition($tag_favorite_position_max);
392
+      $tag_favorite_position_max++;
393
+      
394
+      $this->addUsersTagsFavorites($tag_favorite);
395
+      $em->persist($tag_favorite);
396
+    }
397
+    
398
+    $em->flush();
399
+    
400
+  }
401
+  
402
+  /**
403
+   * Retourne un tableau contenant les ids des tags préférés de l'user
404
+   * 
405
+   * @return type array
406
+   */
407
+  public function getTagFavoriteIds()
408
+  {
409
+    $ids = array();
410
+    foreach ($this->tags_favorites as $tag_favorite)
411
+    {
412
+      $ids[$tag_favorite->getTag()->getId()] = $tag_favorite->getTag()->getId();
413
+    }
414
+    return $ids;
415
+  }
416
+  
417
+  /**
418
+   * Retourne la position max des tag favoris.
419
+   * 
420
+   * @return int 
421
+   */
422
+  public function getTagFavoritePositionMax()
423
+  {
424
+    $max = 0;
425
+    foreach ($this->tags_favorites as $tag_favorite)
426
+    {
427
+      if ($tag_favorite->getPosition() > $max)
428
+      {
429
+        $max = $tag_favorite->getPosition();
430
+      }
431
+    }
432
+    return $max;
433
+  }
434
+  
340 435
 }

+ 2 - 2
src/Muzich/CoreBundle/Entity/UsersTagsFavorites.php Целия файл

@@ -25,7 +25,7 @@ class UsersTagsFavorites
25 25
   /**
26 26
    * Cet attribut contient l'objet User lié
27 27
    * 
28
-   * @ORM\ManyToOne(targetEntity="User", inversedBy="tags")
28
+   * @ORM\ManyToOne(targetEntity="User", inversedBy="tags_favorites")
29 29
    * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
30 30
    */
31 31
   protected $user;
@@ -33,7 +33,7 @@ class UsersTagsFavorites
33 33
   /**
34 34
    * Cet attribut contient l'objet Tag lié
35 35
    * 
36
-   * @ORM\ManyToOne(targetEntity="Tag", inversedBy="users")
36
+   * @ORM\ManyToOne(targetEntity="Tag", inversedBy="users_favorites")
37 37
    * @ORM\JoinColumn(name="tag_id", referencedColumnName="id")
38 38
    */
39 39
   protected $tag;

+ 30 - 0
src/Muzich/CoreBundle/Form/Tag/TagFavoritesForm.php Целия файл

@@ -0,0 +1,30 @@
1
+<?php
2
+
3
+namespace Muzich\CoreBundle\Form\Tag;
4
+
5
+use Symfony\Component\Form\AbstractType;
6
+use Symfony\Component\Form\FormBuilder;
7
+
8
+class TagFavoritesForm extends AbstractType
9
+{
10
+  public function buildForm(FormBuilder $builder, array $options)
11
+  {        
12
+    $builder->add('tags', 'choice', array(
13
+      'choices'           => $options['tags'],
14
+      'expanded'          => true,
15
+      'multiple'          => true
16
+    ));
17
+  }
18
+
19
+  public function getName()
20
+  {
21
+    return 'tag_favorites_form';
22
+  }
23
+  
24
+  public function getDefaultOptions(array $options)
25
+  {
26
+    return array(
27
+      'tags' => array(),
28
+    );
29
+  }
30
+}

+ 7 - 1
src/Muzich/CoreBundle/Repository/UserRepository.php Целия файл

@@ -40,6 +40,12 @@ class UserRepository extends EntityRepository
40 40
       $join   .= ' JOIN u.followed_groups fdg JOIN fdg.group fdg_g';
41 41
     }
42 42
     
43
+    if (in_array('favorites_tags', $join_list))
44
+    {
45
+      $select .= ', tf, tf_t';
46
+      $join   .= ' LEFT JOIN u.tags_favorites tf LEFT JOIN tf.tag tf_t';
47
+    }
48
+    
43 49
 //    if (array_key_exists('followed_user_id', $join_list))
44 50
 //    {
45 51
 //      $select .= ', fu, fu_u';
@@ -67,7 +73,7 @@ class UserRepository extends EntityRepository
67 73
    * @param int $limit
68 74
    * @return array 
69 75
    */
70
-  public function getTagIdsFavorites($user_id, $limit)
76
+  public function getTagIdsFavorites($user_id, $limit = null)
71 77
   {
72 78
     $tag_ids = array();
73 79
     foreach ($this->getEntityManager()

+ 60 - 1
src/Muzich/UserBundle/Controller/UserController.php Целия файл

@@ -7,6 +7,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
7 7
 use Symfony\Component\HttpFoundation\RedirectResponse;
8 8
 use Symfony\Component\Security\Core\Exception\AccessDeniedException;
9 9
 use FOS\UserBundle\Model\UserInterface;
10
+use Muzich\CoreBundle\Form\Tag\TagFavoritesForm;
10 11
 
11 12
 class UserController extends Controller
12 13
 {
@@ -95,8 +96,66 @@ class UserController extends Controller
95 96
    */
96 97
   public function startAction()
97 98
   {
99
+    $user = $this->getUser();
100
+    
101
+    $form = $this->createForm(
102
+      new TagFavoritesForm(), 
103
+      array('tags' => $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
104
+        ->getTagIdsFavorites($user->getId())
105
+      ),
106
+      array('tags' => $this->getTagsArray())
107
+    );
108
+    
109
+    return array(
110
+      'form' => $form->createView()
111
+    );
112
+  }
113
+  
114
+  /**
115
+   *
116
+   * @param string $redirect 
117
+   */
118
+  public function updateTagFavoritesAction($redirect)
119
+  {
120
+    $request = $this->getRequest();
121
+    $user = $this->getUser(true, array('join' => array('favorites_tags')));
122
+    
123
+    $form = $this->createForm(
124
+      new TagFavoritesForm(), 
125
+      array('tags' => $this->getDoctrine()->getRepository('MuzichCoreBundle:User')
126
+        ->getTagIdsFavorites($user->getId())
127
+      ),
128
+      array('tags' => $this->getTagsArray())
129
+    );
130
+    
131
+    if ($request->getMethod() == 'POST')
132
+    {
133
+      $form->bindRequest($request);
134
+      if ($form->isValid())
135
+      {
136
+        $data = $form->getData();
137
+        $user->addTagsFavoritesById($this->getDoctrine()->getEntityManager(), $data['tags']);
138
+      }
139
+      else
140
+      {
141
+        return $this->container->get('templating')->renderResponse(
142
+          'MuzichUserBundle:User:start.html.twig',
143
+          array(
144
+            'form' => $form->createView()
145
+          )
146
+        );
147
+      }
148
+    }
98 149
     
99
-    return array();
150
+    // (Il y aura aussi une redirection vers "mon compte / tags")
151
+    if ($redirect == 'home')
152
+    {
153
+      return $this->redirect($this->generateUrl('home'));
154
+    }
155
+    else
156
+    {
157
+      return $this->redirect($this->generateUrl('home'));
158
+    }
100 159
   }
101 160
     
102 161
 }

+ 5 - 1
src/Muzich/UserBundle/Resources/config/routing.yml Целия файл

@@ -15,4 +15,8 @@ register:
15 15
 start:
16 16
   pattern: /start
17 17
   defaults: { _controller: MuzichUserBundle:User:start }
18
-  
18
+  
19
+update_tag_favorites:
20
+  pattern: /account/update-tag-favorites/{redirect}
21
+  defaults: { _controller: MuzichUserBundle:User:updateTagFavorites, redirect: home }
22
+    

+ 21 - 1
src/Muzich/UserBundle/Resources/views/User/start.html.twig Целия файл

@@ -4,6 +4,26 @@
4 4
 
5 5
 {% block content %}
6 6
 
7
-  <h2>Inscription terminé !</h2>
7
+  <h2>Bienvenue sur Muzich !</h2>
8
+  
9
+  <p>
10
+    Bla bla bla ...
11
+  </p>
12
+  
13
+  <h3>Choisissez vos tags préférés</h3>
14
+  
15
+  <form action="{{ path('update_tag_favorites') }}" method="post" {{ form_enctype(form) }}>
16
+    {{ form_errors(form) }}
17
+
18
+    {{ form_row(form.tags) }}
19
+
20
+    {{ form_rest(form) }}
21
+
22
+    <input type="submit" />
23
+  </form>
24
+  
25
+  <p>
26
+    Vous pourrez egallement modifier vos tags préféré dans la partie 'mon compte'.
27
+  </p>
8 28
       
9 29
 {% endblock %}