User.php 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php
  2. namespace Muzich\CoreBundle\Entity;
  3. use FOS\UserBundle\Entity\User as BaseUser;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use \Doctrine\Common\Collections\ArrayCollection;
  6. use Gedmo\Mapping\Annotation as Gedmo;
  7. use Doctrine\ORM\EntityManager;
  8. use Muzich\CoreBundle\Entity\UsersTagsFavorites;
  9. /**
  10. * Cet entité est l'utilisateur ayant effectué la requête.
  11. *
  12. * @ORM\Entity
  13. * @ORM\Table(name="m_user")
  14. * @ORM\Entity(repositoryClass="Muzich\CoreBundle\Repository\UserRepository")
  15. * @ORM\HasLifecycleCallbacks()
  16. */
  17. class User extends BaseUser
  18. {
  19. /**
  20. * @ORM\Id
  21. * @ORM\Column(type="integer")
  22. * @ORM\generatedValue(strategy="AUTO")
  23. */
  24. protected $id;
  25. /**
  26. * @Gedmo\Slug(fields={"username"})
  27. * @ORM\Column(length=128, unique=true)
  28. */
  29. protected $slug;
  30. /**
  31. * Cet attribut contient les enregistrements UsersTagsFavorites lié
  32. * a cet utilisateur dans le cadre des Tags Favoris.
  33. *
  34. * @ORM\OneToMany(targetEntity="UsersTagsFavorites", mappedBy="user")
  35. */
  36. protected $tags_favorites;
  37. /**
  38. * Cet attribut contient les enregistrements UsersElementsFavorites lié
  39. * a cet utilisateur dans le cadre des éléments Favoris.
  40. *
  41. * @ORM\OneToMany(targetEntity="UsersElementsFavorites", mappedBy="user")
  42. */
  43. protected $elements_favorites;
  44. /**
  45. * Liste des Elements appartenant a cet utilisateur.
  46. *
  47. * @ORM\OneToMany(targetEntity="Element", mappedBy="owner")
  48. */
  49. protected $elements;
  50. /**
  51. * Users que cet utilisateur suit.
  52. *
  53. * @ORM\OneToMany(targetEntity="FollowUser", mappedBy="follower")
  54. */
  55. protected $followeds_users;
  56. /**
  57. * Users qui suivent cet utilisateur.
  58. *
  59. * @ORM\OneToMany(targetEntity="FollowUser", mappedBy="followed")
  60. */
  61. protected $followers_users;
  62. /**
  63. * Cet attribut contient les enregistrements FollowGroup lié
  64. * a cet utilisateur dans le cadre des groupes suivis.
  65. *
  66. * @ORM\OneToMany(targetEntity="FollowGroup", mappedBy="follower")
  67. */
  68. protected $followed_groups;
  69. /**
  70. * Liste des Groupes appartenant a cet utilisateur.
  71. *
  72. * @ORM\OneToMany(targetEntity="Group", mappedBy="owner")
  73. */
  74. protected $groups_owned;
  75. /**
  76. *
  77. */
  78. public function __construct()
  79. {
  80. $this->tags_favorites = new ArrayCollection();
  81. $this->elements = new ArrayCollection();
  82. $this->elements_favorites = new ArrayCollection();
  83. $this->followeds_users = new ArrayCollection();
  84. $this->followers_users = new ArrayCollection();
  85. $this->followed_groups = new ArrayCollection();
  86. $this->groups = new ArrayCollection();
  87. $this->groups_owned = new ArrayCollection();
  88. parent::__construct();
  89. }
  90. public function __toString()
  91. {
  92. return $this->getName();
  93. }
  94. /**
  95. * Get id
  96. *
  97. * @return integer
  98. */
  99. public function getId()
  100. {
  101. return $this->id;
  102. }
  103. /**
  104. * Get tags_favorites
  105. *
  106. * @return Doctrine\Common\Collections\Collection
  107. */
  108. public function getTagsFavorites()
  109. {
  110. return $this->tags_favorites;
  111. }
  112. /**
  113. * Add tags_favorites
  114. *
  115. * @param UsersTagsFavorites $tagsFavorites
  116. */
  117. public function addUsersTagsFavorites(UsersTagsFavorites $tagsFavorites)
  118. {
  119. $this->tags_favorites[] = $tagsFavorites;
  120. }
  121. /**
  122. * Add elements_favorites
  123. *
  124. * @param UsersElementsFavorites $elementsFavorites
  125. */
  126. public function addUsersElementsFavorites(UsersElementsFavorites $elementsFavorites)
  127. {
  128. $this->elements_favorites[] = $elementsFavorites;
  129. }
  130. /**
  131. * Get elements_favorites
  132. *
  133. * @return Doctrine\Common\Collections\Collection
  134. */
  135. public function getElementsFavorites()
  136. {
  137. return $this->elements_favorites;
  138. }
  139. /**
  140. * Add elements
  141. *
  142. * @param Element $elements
  143. */
  144. public function addElement(Element $elements)
  145. {
  146. $this->elements[] = $elements;
  147. }
  148. /**
  149. * Get elements
  150. *
  151. * @return Doctrine\Common\Collections\Collection
  152. */
  153. public function getElements()
  154. {
  155. return $this->elements;
  156. }
  157. /**
  158. * Add followeds_users
  159. *
  160. * @param FollowUser $followedsUsers
  161. */
  162. public function addFollowUser(FollowUser $followedsUsers)
  163. {
  164. $this->followeds_users[] = $followedsUsers;
  165. }
  166. /**
  167. * Get followeds_users
  168. *
  169. * @return Doctrine\Common\Collections\Collection
  170. */
  171. public function getFollowedsUsers()
  172. {
  173. $users = array();
  174. foreach ($this->followeds_users as $follow_user)
  175. {
  176. $users[] = $follow_user->getFollowed();
  177. }
  178. return $users;
  179. }
  180. /**
  181. * Get followers_users
  182. *
  183. * @return Doctrine\Common\Collections\Collection
  184. */
  185. public function getFollowersUsers()
  186. {
  187. $users = array();
  188. foreach ($this->followers_users as $follow_user)
  189. {
  190. $users[] = $follow_user->getFollower();
  191. }
  192. return $users;
  193. }
  194. /**
  195. * Add followed_groups
  196. *
  197. * @param FollowGroup $followedGroups
  198. */
  199. public function addFollowGroup(FollowGroup $followedGroups)
  200. {
  201. $this->followed_groups[] = $followedGroups;
  202. }
  203. /**
  204. * Get followed_groups
  205. *
  206. * @return Doctrine\Common\Collections\Collection
  207. */
  208. public function getFollowedGroups()
  209. {
  210. $groups = array();
  211. foreach ($this->followed_groups as $follow_group)
  212. {
  213. $groups[] = $follow_group->getGroup();
  214. }
  215. return $groups;
  216. }
  217. /**
  218. * Add groups
  219. *
  220. * @param Group $groups
  221. */
  222. public function addGroupOwned(Group $groups)
  223. {
  224. $this->groups[] = $groups;
  225. }
  226. /**
  227. * Get groups
  228. *
  229. * @return Doctrine\Common\Collections\Collection
  230. */
  231. public function getGroupsOwned()
  232. {
  233. return $this->groups_owned;
  234. }
  235. /**
  236. * Get groups in array (id => name)
  237. *
  238. * @return Doctrine\Common\Collections\Collection
  239. */
  240. public function getGroupsOwnedArray()
  241. {
  242. $groups = array();
  243. foreach ($this->groups_owned as $group)
  244. {
  245. $groups[$group->getId()] = $group->getName();
  246. }
  247. return $groups;
  248. }
  249. public function getSlug()
  250. {
  251. return $this->slug;
  252. }
  253. public function setSlug($slug)
  254. {
  255. $this->slug = $slug;
  256. }
  257. /*
  258. *
  259. *
  260. */
  261. public function getName()
  262. {
  263. return $this->getUsername();
  264. }
  265. // /**
  266. // * @ORM\prePersist
  267. // */
  268. // public function setSlug()
  269. // {
  270. // if (!$this->slug)
  271. // {
  272. //
  273. // }
  274. // }
  275. //
  276. /**
  277. * Retourn si l'user_id transmis fait partis des enregistrements
  278. * followed de l'objet.
  279. *
  280. * @param int $user_id
  281. * @return boolean
  282. */
  283. public function isFollowingUser($user_id)
  284. {
  285. foreach ($this->followeds_users as $followed_user)
  286. {
  287. if ($followed_user->getFollowed()->getId() == $user_id)
  288. {
  289. return true;
  290. }
  291. }
  292. return false;
  293. }
  294. /**
  295. * Retourn si l'user_id transmis est l'un des User suivis
  296. *
  297. * @param Symfony\Bundle\DoctrineBundle\Registry doctrine
  298. * @param int $user_id
  299. * @return boolean
  300. */
  301. public function isFollowingUserByQuery($doctrine, $user_id)
  302. {
  303. return $doctrine
  304. ->getRepository('MuzichCoreBundle:User')
  305. ->isFollowingUser($this->getId(), $user_id)
  306. ;
  307. }
  308. /**
  309. * Retourn si l'group_id transmis est l'un des groupe suivis
  310. *
  311. * @param Symfony\Bundle\DoctrineBundle\Registry doctrine
  312. * @param int $user_id
  313. * @return boolean
  314. */
  315. public function isFollowingGroupByQuery($doctrine, $group_id)
  316. {
  317. return $doctrine
  318. ->getRepository('MuzichCoreBundle:User')
  319. ->isFollowingGroup($this->getId(), $group_id)
  320. ;
  321. }
  322. public function getPersonalHash()
  323. {
  324. return hash('sha256', $this->getSalt().$this->getUsername());
  325. }
  326. /**
  327. * Ajoute a l'user les tags transmis (id) comme favoris.
  328. *
  329. * @param EntityManager $em
  330. * @param array $ids
  331. */
  332. public function updateTagsFavoritesById(EntityManager $em, $ids)
  333. {
  334. $ids_to_add = $ids;
  335. // Pour chacun des tags favoris
  336. foreach ($this->tags_favorites as $ii => $tag_favorite)
  337. {
  338. $trouve = false;
  339. foreach ($ids as $i => $id)
  340. {
  341. if ($id == $tag_favorite->getTag()->getId())
  342. {
  343. $trouve = true;
  344. // Si le tag était favoris déjà avant (et aussi maintenant)
  345. // il ne sera ni a ajouter, ni a supprimer.
  346. unset($ids_to_add[$i]);
  347. }
  348. }
  349. if (!$trouve)
  350. {
  351. // Si cet ancien tag n'est plus dans la liste, il faut le supprimer
  352. // (rappel: on supprime ici la relation, pas le tag)
  353. $em->remove($tag_favorite);
  354. }
  355. }
  356. if (count($ids_to_add))
  357. {
  358. $tag_favorite_position_max = $this->getTagFavoritePositionMax();
  359. $tags = $em->getRepository('MuzichCoreBundle:Tag')->findByIds($ids_to_add)->execute();
  360. // Pour les nouveaux ids restants
  361. foreach ($tags as $tag)
  362. {
  363. $tag_favorite = new UsersTagsFavorites();
  364. $tag_favorite->setUser($this);
  365. $tag_favorite->setTag($tag);
  366. $tag_favorite->setPosition($tag_favorite_position_max);
  367. $tag_favorite_position_max++;
  368. $this->addUsersTagsFavorites($tag_favorite);
  369. $em->persist($tag_favorite);
  370. }
  371. }
  372. $em->flush();
  373. }
  374. /**
  375. * Retourne un tableau contenant les ids des tags préférés de l'user
  376. *
  377. * @return type array
  378. */
  379. public function getTagFavoriteIds()
  380. {
  381. $ids = array();
  382. foreach ($this->tags_favorites as $tag_favorite)
  383. {
  384. $ids[$tag_favorite->getTag()->getId()] = $tag_favorite->getTag()->getId();
  385. }
  386. return $ids;
  387. }
  388. /**
  389. * Retourne la position max des tag favoris.
  390. *
  391. * @return int
  392. */
  393. public function getTagFavoritePositionMax()
  394. {
  395. $max = 0;
  396. foreach ($this->tags_favorites as $tag_favorite)
  397. {
  398. if ($tag_favorite->getPosition() > $max)
  399. {
  400. $max = $tag_favorite->getPosition();
  401. }
  402. }
  403. return $max;
  404. }
  405. }