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