User.php 10KB

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