123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520 |
- <?php
-
- namespace Muzich\CoreBundle\Entity;
-
- use FOS\UserBundle\Entity\User as BaseUser;
- use Doctrine\ORM\Mapping as ORM;
- use \Doctrine\Common\Collections\ArrayCollection;
- use Gedmo\Mapping\Annotation as Gedmo;
- use Doctrine\ORM\EntityManager;
- use Muzich\CoreBundle\Entity\UsersTagsFavorites;
- use Symfony\Component\Validator\Constraints as Assert;
- use Muzich\CoreBundle\Validator as MuzichAssert;
-
- /**
- * Cet entité est l'utilisateur ayant effectué la requête.
- *
- * @ORM\Entity
- * @ORM\Table(name="m_user")
- * @ORM\Entity(repositoryClass="Muzich\CoreBundle\Repository\UserRepository")
- * @ORM\HasLifecycleCallbacks()
- */
- class User extends BaseUser
- {
-
- /**
- * @ORM\Id
- * @ORM\Column(type="integer")
- * @ORM\generatedValue(strategy="AUTO")
- */
- protected $id;
-
- /**
- * @Gedmo\Slug(fields={"username"})
- * @ORM\Column(length=128, unique=true)
- */
- protected $slug;
-
- /**
- * @ORM\Column(type="text", nullable=true)
- * @ORM\Column(length=256)
- */
- protected $email_requested;
-
- /**
- * @ORM\Column(type="integer", nullable=true)
- */
- protected $email_requested_datetime;
-
- /**
- * Cet attribut contient les enregistrements UsersTagsFavorites lié
- * a cet utilisateur dans le cadre des Tags Favoris.
- *
- * @ORM\OneToMany(targetEntity="UsersTagsFavorites", mappedBy="user")
- */
- protected $tags_favorites;
-
- /**
- * Cet attribut contient les enregistrements UsersElementsFavorites lié
- * a cet utilisateur dans le cadre des éléments Favoris.
- *
- * @ORM\OneToMany(targetEntity="UsersElementsFavorites", mappedBy="user")
- */
- protected $elements_favorites;
-
- /**
- * Liste des Elements appartenant a cet utilisateur.
- *
- * @ORM\OneToMany(targetEntity="Element", mappedBy="owner")
- */
- protected $elements;
-
- /**
- * Users que cet utilisateur suit.
- *
- * @ORM\OneToMany(targetEntity="FollowUser", mappedBy="follower")
- */
- protected $followeds_users;
-
- /**
- * Users qui suivent cet utilisateur.
- *
- * @ORM\OneToMany(targetEntity="FollowUser", mappedBy="followed")
- */
- protected $followers_users;
-
- /**
- * Cet attribut contient les enregistrements FollowGroup lié
- * a cet utilisateur dans le cadre des groupes suivis.
- *
- * @ORM\OneToMany(targetEntity="FollowGroup", mappedBy="follower")
- */
- protected $followed_groups;
-
- /**
- * Liste des Groupes appartenant a cet utilisateur.
- *
- * @ORM\OneToMany(targetEntity="Group", mappedBy="owner")
- */
- protected $groups_owned;
-
- /**
- * Compteur de signalements inutiles
- *
- * @ORM\Column(type="integer", nullable=true)
- * @var int
- */
- protected $bad_report_count;
-
- /**
- *
- */
- public function __construct()
- {
- $this->tags_favorites = new ArrayCollection();
- $this->elements = new ArrayCollection();
- $this->elements_favorites = new ArrayCollection();
- $this->followeds_users = new ArrayCollection();
- $this->followers_users = new ArrayCollection();
- $this->followed_groups = new ArrayCollection();
- $this->groups = new ArrayCollection();
- $this->groups_owned = new ArrayCollection();
- parent::__construct();
- }
-
- public function __toString()
- {
- return $this->getName();
- }
-
- /**
- * Get id
- *
- * @return integer
- */
- public function getId()
- {
- return $this->id;
- }
-
- /**
- * Get tags_favorites
- *
- * @return Doctrine\Common\Collections\Collection
- */
- public function getTagsFavorites()
- {
- return $this->tags_favorites;
- }
-
- /**
- * Add tags_favorites
- *
- * @param UsersTagsFavorites $tagsFavorites
- */
- public function addUsersTagsFavorites(UsersTagsFavorites $tagsFavorites)
- {
- $this->tags_favorites[] = $tagsFavorites;
- }
-
- /**
- * Add elements_favorites
- *
- * @param UsersElementsFavorites $elementsFavorites
- */
- public function addUsersElementsFavorites(UsersElementsFavorites $elementsFavorites)
- {
- $this->elements_favorites[] = $elementsFavorites;
- }
-
- /**
- * Get elements_favorites
- *
- * @return Doctrine\Common\Collections\Collection
- */
- public function getElementsFavorites()
- {
- return $this->elements_favorites;
- }
-
- /**
- * Add elements
- *
- * @param Element $elements
- */
- public function addElement(Element $elements)
- {
- $this->elements[] = $elements;
- }
-
- /**
- * Get elements
- *
- * @return Doctrine\Common\Collections\Collection
- */
- public function getElements()
- {
- return $this->elements;
- }
-
- /**
- * Add followeds_users
- *
- * @param FollowUser $followedsUsers
- */
- public function addFollowUser(FollowUser $followedsUsers)
- {
- $this->followeds_users[] = $followedsUsers;
- }
-
- /**
- * Get followeds_users
- *
- * @return Doctrine\Common\Collections\Collection
- */
- public function getFollowedsUsers()
- {
- $users = array();
- foreach ($this->followeds_users as $follow_user)
- {
- $users[] = $follow_user->getFollowed();
- }
- return $users;
- }
-
- /**
- * Get followers_users
- *
- * @return Doctrine\Common\Collections\Collection
- */
- public function getFollowersUsers()
- {
- $users = array();
- foreach ($this->followers_users as $follow_user)
- {
- $users[] = $follow_user->getFollower();
- }
- return $users;
- }
-
- /**
- * Add followed_groups
- *
- * @param FollowGroup $followedGroups
- */
- public function addFollowGroup(FollowGroup $followedGroups)
- {
- $this->followed_groups[] = $followedGroups;
- }
-
- /**
- * Get followed_groups
- *
- * @return Doctrine\Common\Collections\Collection
- */
- public function getFollowedGroups()
- {
- $groups = array();
- foreach ($this->followed_groups as $follow_group)
- {
- $groups[] = $follow_group->getGroup();
- }
- return $groups;
- }
-
- /**
- * Add groups
- *
- * @param Group $groups
- */
- public function addGroupOwned(Group $groups)
- {
- $this->groups[] = $groups;
- }
-
- /**
- * Get groups
- *
- * @return Doctrine\Common\Collections\Collection
- */
- public function getGroupsOwned()
- {
- return $this->groups_owned;
- }
-
- /**
- * Get groups in array (id => name)
- *
- * @return Doctrine\Common\Collections\Collection
- */
- public function getGroupsOwnedArray()
- {
- $groups = array();
- foreach ($this->groups_owned as $group)
- {
- $groups[$group->getId()] = $group->getName();
- }
- return $groups;
- }
-
- public function getSlug()
- {
- return $this->slug;
- }
-
- public function setSlug($slug)
- {
- $this->slug = $slug;
- }
-
- public function getEmailRequested()
- {
- return $this->email_requested;
- }
-
- public function setEmailRequested($email_requested)
- {
- $this->email_requested = $email_requested;
- }
-
- public function getBadReportCount()
- {
- return $this->bad_report_count;
- }
-
- public function setBadReportCount($count)
- {
- $this->bad_report_count = $count;
- }
-
- public function addBadReport()
- {
- $this->setBadReportCount($this->getBadReportCount()+1);
- }
-
- /*
- *
- *
- */
-
- public function getName()
- {
- return $this->getUsername();
- }
-
- // /**
- // * @ORM\prePersist
- // */
- // public function setSlug()
- // {
- // if (!$this->slug)
- // {
- //
- // }
- // }
- //
- /**
- * Retourn si l'user_id transmis fait partis des enregistrements
- * followed de l'objet.
- *
- * @param int $user_id
- * @return boolean
- */
- public function isFollowingUser($user_id)
- {
- foreach ($this->followeds_users as $followed_user)
- {
- if ($followed_user->getFollowed()->getId() == $user_id)
- {
- return true;
- }
- }
- return false;
- }
-
- /**
- * Retourn si l'user_id transmis est l'un des User suivis
- *
- * @param Symfony\Bundle\DoctrineBundle\Registry doctrine
- * @param int $user_id
- * @return boolean
- */
- public function isFollowingUserByQuery($doctrine, $user_id)
- {
- return $doctrine
- ->getRepository('MuzichCoreBundle:User')
- ->isFollowingUser($this->getId(), $user_id)
- ;
- }
-
- /**
- * Retourn si l'group_id transmis est l'un des groupe suivis
- *
- * @param Symfony\Bundle\DoctrineBundle\Registry doctrine
- * @param int $user_id
- * @return boolean
- */
- public function isFollowingGroupByQuery($doctrine, $group_id)
- {
- return $doctrine
- ->getRepository('MuzichCoreBundle:User')
- ->isFollowingGroup($this->getId(), $group_id)
- ;
- }
-
- public function getPersonalHash()
- {
- return hash('sha256', $this->getSalt().$this->getUsername());
- }
-
- /**
- * Ajoute a l'user les tags transmis (id) comme favoris.
- *
- * @param EntityManager $em
- * @param array $ids
- */
- public function updateTagsFavoritesById(EntityManager $em, $ids)
- {
- $ids = json_decode($ids);
- $ids_to_add = $ids;
-
- // Pour chacun des tags favoris
- foreach ($this->tags_favorites as $ii => $tag_favorite)
- {
- $trouve = false;
- foreach ($ids as $i => $id)
- {
- if ($id == $tag_favorite->getTag()->getId())
- {
- $trouve = true;
- // Si le tag était favoris déjà avant (et aussi maintenant)
- // il ne sera ni a ajouter, ni a supprimer.
- unset($ids_to_add[$i]);
- }
- }
-
- if (!$trouve)
- {
- // Si cet ancien tag n'est plus dans la liste, il faut le supprimer
- // (rappel: on supprime ici la relation, pas le tag)
- $em->remove($tag_favorite);
- }
- }
-
- if (count($ids_to_add))
- {
- $ids_to_add = array_merge($ids_to_add);
- $tag_favorite_position_max = $this->getTagFavoritePositionMax();
- $tags = $em->getRepository('MuzichCoreBundle:Tag')->findByIds($ids_to_add)->execute();
-
- // Pour les nouveaux ids restants
- foreach ($tags as $tag)
- {
- $tag_favorite = new UsersTagsFavorites();
- $tag_favorite->setUser($this);
- $tag_favorite->setTag($tag);
- $tag_favorite->setPosition($tag_favorite_position_max);
- $tag_favorite_position_max++;
-
- $this->addUsersTagsFavorites($tag_favorite);
- $em->persist($tag_favorite);
- }
- }
-
- $em->flush();
- }
-
- /**
- * Retourne un tableau contenant les ids des tags préférés de l'user
- *
- * @return type array
- */
- public function getTagFavoriteIds()
- {
- $ids = array();
- foreach ($this->tags_favorites as $tag_favorite)
- {
- $ids[$tag_favorite->getTag()->getId()] = $tag_favorite->getTag()->getId();
- }
- return $ids;
- }
-
- /**
- * Retourne la position max des tag favoris.
- *
- * @return int
- */
- public function getTagFavoritePositionMax()
- {
- $max = 0;
- foreach ($this->tags_favorites as $tag_favorite)
- {
- if ($tag_favorite->getPosition() > $max)
- {
- $max = $tag_favorite->getPosition();
- }
- }
- return $max;
- }
-
- /**
- * Set email_requested_datetime
- *
- * @param integer $emailRequestedDatetime
- */
- public function setEmailRequestedDatetime($emailRequestedDatetime)
- {
- $this->email_requested_datetime = $emailRequestedDatetime;
- }
-
- /**
- * Get email_requested_datetime
- *
- * @return integer
- */
- public function getEmailRequestedDatetime()
- {
- return $this->email_requested_datetime;
- }
-
- }
|