User.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. * Compteur de signalements inutiles
  88. *
  89. * @ORM\Column(type="integer", nullable=true)
  90. * @var int
  91. */
  92. protected $bad_report_count;
  93. /**
  94. *
  95. */
  96. public function __construct()
  97. {
  98. $this->tags_favorites = new ArrayCollection();
  99. $this->elements = new ArrayCollection();
  100. $this->elements_favorites = new ArrayCollection();
  101. $this->followeds_users = new ArrayCollection();
  102. $this->followers_users = new ArrayCollection();
  103. $this->followed_groups = new ArrayCollection();
  104. $this->groups = new ArrayCollection();
  105. $this->groups_owned = new ArrayCollection();
  106. parent::__construct();
  107. }
  108. public function __toString()
  109. {
  110. return $this->getName();
  111. }
  112. /**
  113. * Get id
  114. *
  115. * @return integer
  116. */
  117. public function getId()
  118. {
  119. return $this->id;
  120. }
  121. /**
  122. * Get tags_favorites
  123. *
  124. * @return Doctrine\Common\Collections\Collection
  125. */
  126. public function getTagsFavorites()
  127. {
  128. return $this->tags_favorites;
  129. }
  130. /**
  131. * Add tags_favorites
  132. *
  133. * @param UsersTagsFavorites $tagsFavorites
  134. */
  135. public function addUsersTagsFavorites(UsersTagsFavorites $tagsFavorites)
  136. {
  137. $this->tags_favorites[] = $tagsFavorites;
  138. }
  139. /**
  140. * Add elements_favorites
  141. *
  142. * @param UsersElementsFavorites $elementsFavorites
  143. */
  144. public function addUsersElementsFavorites(UsersElementsFavorites $elementsFavorites)
  145. {
  146. $this->elements_favorites[] = $elementsFavorites;
  147. }
  148. /**
  149. * Get elements_favorites
  150. *
  151. * @return Doctrine\Common\Collections\Collection
  152. */
  153. public function getElementsFavorites()
  154. {
  155. return $this->elements_favorites;
  156. }
  157. /**
  158. * Add elements
  159. *
  160. * @param Element $elements
  161. */
  162. public function addElement(Element $elements)
  163. {
  164. $this->elements[] = $elements;
  165. }
  166. /**
  167. * Get elements
  168. *
  169. * @return Doctrine\Common\Collections\Collection
  170. */
  171. public function getElements()
  172. {
  173. return $this->elements;
  174. }
  175. /**
  176. * Add followeds_users
  177. *
  178. * @param FollowUser $followedsUsers
  179. */
  180. public function addFollowUser(FollowUser $followedsUsers)
  181. {
  182. $this->followeds_users[] = $followedsUsers;
  183. }
  184. /**
  185. * Get followeds_users
  186. *
  187. * @return Doctrine\Common\Collections\Collection
  188. */
  189. public function getFollowedsUsers()
  190. {
  191. $users = array();
  192. foreach ($this->followeds_users as $follow_user)
  193. {
  194. $users[] = $follow_user->getFollowed();
  195. }
  196. return $users;
  197. }
  198. /**
  199. * Get followers_users
  200. *
  201. * @return Doctrine\Common\Collections\Collection
  202. */
  203. public function getFollowersUsers()
  204. {
  205. $users = array();
  206. foreach ($this->followers_users as $follow_user)
  207. {
  208. $users[] = $follow_user->getFollower();
  209. }
  210. return $users;
  211. }
  212. /**
  213. * Add followed_groups
  214. *
  215. * @param FollowGroup $followedGroups
  216. */
  217. public function addFollowGroup(FollowGroup $followedGroups)
  218. {
  219. $this->followed_groups[] = $followedGroups;
  220. }
  221. /**
  222. * Get followed_groups
  223. *
  224. * @return Doctrine\Common\Collections\Collection
  225. */
  226. public function getFollowedGroups()
  227. {
  228. $groups = array();
  229. foreach ($this->followed_groups as $follow_group)
  230. {
  231. $groups[] = $follow_group->getGroup();
  232. }
  233. return $groups;
  234. }
  235. /**
  236. * Add groups
  237. *
  238. * @param Group $groups
  239. */
  240. public function addGroupOwned(Group $groups)
  241. {
  242. $this->groups[] = $groups;
  243. }
  244. /**
  245. * Get groups
  246. *
  247. * @return Doctrine\Common\Collections\Collection
  248. */
  249. public function getGroupsOwned()
  250. {
  251. return $this->groups_owned;
  252. }
  253. /**
  254. * Get groups in array (id => name)
  255. *
  256. * @return Doctrine\Common\Collections\Collection
  257. */
  258. public function getGroupsOwnedArray()
  259. {
  260. $groups = array();
  261. foreach ($this->groups_owned as $group)
  262. {
  263. $groups[$group->getId()] = $group->getName();
  264. }
  265. return $groups;
  266. }
  267. public function getSlug()
  268. {
  269. return $this->slug;
  270. }
  271. public function setSlug($slug)
  272. {
  273. $this->slug = $slug;
  274. }
  275. public function getEmailRequested()
  276. {
  277. return $this->email_requested;
  278. }
  279. public function setEmailRequested($email_requested)
  280. {
  281. $this->email_requested = $email_requested;
  282. }
  283. public function getBadReportCount()
  284. {
  285. return $this->bad_report_count;
  286. }
  287. public function setBadReportCount($count)
  288. {
  289. $this->bad_report_count = $count;
  290. }
  291. public function addBadReport()
  292. {
  293. $this->setBadReportCount($this->getBadReportCount()+1);
  294. }
  295. /*
  296. *
  297. *
  298. */
  299. public function getName()
  300. {
  301. return $this->getUsername();
  302. }
  303. // /**
  304. // * @ORM\prePersist
  305. // */
  306. // public function setSlug()
  307. // {
  308. // if (!$this->slug)
  309. // {
  310. //
  311. // }
  312. // }
  313. //
  314. /**
  315. * Retourn si l'user_id transmis fait partis des enregistrements
  316. * followed de l'objet.
  317. *
  318. * @param int $user_id
  319. * @return boolean
  320. */
  321. public function isFollowingUser($user_id)
  322. {
  323. foreach ($this->followeds_users as $followed_user)
  324. {
  325. if ($followed_user->getFollowed()->getId() == $user_id)
  326. {
  327. return true;
  328. }
  329. }
  330. return false;
  331. }
  332. /**
  333. * Retourn si l'user_id transmis est l'un des User suivis
  334. *
  335. * @param Symfony\Bundle\DoctrineBundle\Registry doctrine
  336. * @param int $user_id
  337. * @return boolean
  338. */
  339. public function isFollowingUserByQuery($doctrine, $user_id)
  340. {
  341. return $doctrine
  342. ->getRepository('MuzichCoreBundle:User')
  343. ->isFollowingUser($this->getId(), $user_id)
  344. ;
  345. }
  346. /**
  347. * Retourn si l'group_id transmis est l'un des groupe suivis
  348. *
  349. * @param Symfony\Bundle\DoctrineBundle\Registry doctrine
  350. * @param int $user_id
  351. * @return boolean
  352. */
  353. public function isFollowingGroupByQuery($doctrine, $group_id)
  354. {
  355. return $doctrine
  356. ->getRepository('MuzichCoreBundle:User')
  357. ->isFollowingGroup($this->getId(), $group_id)
  358. ;
  359. }
  360. public function getPersonalHash()
  361. {
  362. return hash('sha256', $this->getSalt().$this->getUsername());
  363. }
  364. /**
  365. * Ajoute a l'user les tags transmis (id) comme favoris.
  366. *
  367. * @param EntityManager $em
  368. * @param array $ids
  369. */
  370. public function updateTagsFavoritesById(EntityManager $em, $ids)
  371. {
  372. $ids = json_decode($ids);
  373. $ids_to_add = $ids;
  374. // Pour chacun des tags favoris
  375. foreach ($this->tags_favorites as $ii => $tag_favorite)
  376. {
  377. $trouve = false;
  378. foreach ($ids as $i => $id)
  379. {
  380. if ($id == $tag_favorite->getTag()->getId())
  381. {
  382. $trouve = true;
  383. // Si le tag était favoris déjà avant (et aussi maintenant)
  384. // il ne sera ni a ajouter, ni a supprimer.
  385. unset($ids_to_add[$i]);
  386. }
  387. }
  388. if (!$trouve)
  389. {
  390. // Si cet ancien tag n'est plus dans la liste, il faut le supprimer
  391. // (rappel: on supprime ici la relation, pas le tag)
  392. $em->remove($tag_favorite);
  393. }
  394. }
  395. if (count($ids_to_add))
  396. {
  397. $ids_to_add = array_merge($ids_to_add);
  398. $tag_favorite_position_max = $this->getTagFavoritePositionMax();
  399. $tags = $em->getRepository('MuzichCoreBundle:Tag')->findByIds($ids_to_add)->execute();
  400. // Pour les nouveaux ids restants
  401. foreach ($tags as $tag)
  402. {
  403. $tag_favorite = new UsersTagsFavorites();
  404. $tag_favorite->setUser($this);
  405. $tag_favorite->setTag($tag);
  406. $tag_favorite->setPosition($tag_favorite_position_max);
  407. $tag_favorite_position_max++;
  408. $this->addUsersTagsFavorites($tag_favorite);
  409. $em->persist($tag_favorite);
  410. }
  411. }
  412. $em->flush();
  413. }
  414. /**
  415. * Retourne un tableau contenant les ids des tags préférés de l'user
  416. *
  417. * @return type array
  418. */
  419. public function getTagFavoriteIds()
  420. {
  421. $ids = array();
  422. foreach ($this->tags_favorites as $tag_favorite)
  423. {
  424. $ids[$tag_favorite->getTag()->getId()] = $tag_favorite->getTag()->getId();
  425. }
  426. return $ids;
  427. }
  428. /**
  429. * Retourne la position max des tag favoris.
  430. *
  431. * @return int
  432. */
  433. public function getTagFavoritePositionMax()
  434. {
  435. $max = 0;
  436. foreach ($this->tags_favorites as $tag_favorite)
  437. {
  438. if ($tag_favorite->getPosition() > $max)
  439. {
  440. $max = $tag_favorite->getPosition();
  441. }
  442. }
  443. return $max;
  444. }
  445. /**
  446. * Set email_requested_datetime
  447. *
  448. * @param integer $emailRequestedDatetime
  449. */
  450. public function setEmailRequestedDatetime($emailRequestedDatetime)
  451. {
  452. $this->email_requested_datetime = $emailRequestedDatetime;
  453. }
  454. /**
  455. * Get email_requested_datetime
  456. *
  457. * @return integer
  458. */
  459. public function getEmailRequestedDatetime()
  460. {
  461. return $this->email_requested_datetime;
  462. }
  463. }