User.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. /**
  8. * Cet entité est l'utilisateur ayant effectué la requête.
  9. *
  10. * @ORM\Entity
  11. * @ORM\Table(name="m_user")
  12. * @ORM\Entity(repositoryClass="Muzich\CoreBundle\Repository\UserRepository")
  13. * @ORM\HasLifecycleCallbacks()
  14. */
  15. class User extends BaseUser
  16. {
  17. /**
  18. * @ORM\Id
  19. * @ORM\Column(type="integer")
  20. * @ORM\generatedValue(strategy="AUTO")
  21. */
  22. protected $id;
  23. /**
  24. * @Gedmo\Slug(fields={"username"})
  25. * @ORM\Column(length=128, unique=true)
  26. */
  27. protected $slug;
  28. /**
  29. * Cet attribut contient les enregistrements UsersTagsFavorites lié
  30. * a cet utilisateur dans le cadre des Tags Favoris.
  31. *
  32. * @ORM\OneToMany(targetEntity="UsersTagsFavorites", mappedBy="user")
  33. */
  34. protected $tags_favorites;
  35. /**
  36. * Cet attribut contient les enregistrements UsersElementsFavorites lié
  37. * a cet utilisateur dans le cadre des éléments Favoris.
  38. *
  39. * @ORM\OneToMany(targetEntity="UsersElementsFavorites", mappedBy="user")
  40. */
  41. protected $elements_favorites;
  42. /**
  43. * Liste des Elements appartenant a cet utilisateur.
  44. *
  45. * @ORM\OneToMany(targetEntity="Element", mappedBy="owner")
  46. */
  47. protected $elements;
  48. /**
  49. * Users que cet utilisateur suit.
  50. *
  51. * @ORM\OneToMany(targetEntity="FollowUser", mappedBy="follower")
  52. */
  53. protected $followeds_users;
  54. /**
  55. * Users qui suivent cet utilisateur.
  56. *
  57. * @ORM\OneToMany(targetEntity="FollowUser", mappedBy="followed")
  58. */
  59. protected $followers_users;
  60. /**
  61. * Cet attribut contient les enregistrements FollowGroup lié
  62. * a cet utilisateur dans le cadre des groupes suivis.
  63. *
  64. * @ORM\OneToMany(targetEntity="FollowGroup", mappedBy="follower")
  65. */
  66. protected $followed_groups;
  67. /**
  68. * Liste des Groupes appartenant a cet utilisateur.
  69. *
  70. * @ORM\OneToMany(targetEntity="Group", mappedBy="owner")
  71. */
  72. protected $groups_owned;
  73. /**
  74. *
  75. */
  76. public function __construct()
  77. {
  78. $this->tags_favorites = new ArrayCollection();
  79. $this->elements = new ArrayCollection();
  80. $this->elements_favorites = new ArrayCollection();
  81. $this->followeds_users = new ArrayCollection();
  82. $this->followers_users = new ArrayCollection();
  83. $this->followed_groups = new ArrayCollection();
  84. $this->groups = new ArrayCollection();
  85. parent::__construct();
  86. }
  87. /**
  88. * Get id
  89. *
  90. * @return integer
  91. */
  92. public function getId()
  93. {
  94. return $this->id;
  95. }
  96. /**
  97. * Get tags_favorites
  98. *
  99. * @return Doctrine\Common\Collections\Collection
  100. */
  101. public function getTagsFavorites()
  102. {
  103. return $this->tags_favorites;
  104. }
  105. /**
  106. * Add tags_favorites
  107. *
  108. * @param UsersTagsFavorites $tagsFavorites
  109. */
  110. public function addUsersTagsFavorites(UsersTagsFavorites $tagsFavorites)
  111. {
  112. $this->tags_favorites[] = $tagsFavorites;
  113. }
  114. /**
  115. * Add elements_favorites
  116. *
  117. * @param UsersElementsFavorites $elementsFavorites
  118. */
  119. public function addUsersElementsFavorites(UsersElementsFavorites $elementsFavorites)
  120. {
  121. $this->elements_favorites[] = $elementsFavorites;
  122. }
  123. /**
  124. * Get elements_favorites
  125. *
  126. * @return Doctrine\Common\Collections\Collection
  127. */
  128. public function getElementsFavorites()
  129. {
  130. return $this->elements_favorites;
  131. }
  132. /**
  133. * Add elements
  134. *
  135. * @param Element $elements
  136. */
  137. public function addElement(Element $elements)
  138. {
  139. $this->elements[] = $elements;
  140. }
  141. /**
  142. * Get elements
  143. *
  144. * @return Doctrine\Common\Collections\Collection
  145. */
  146. public function getElements()
  147. {
  148. return $this->elements;
  149. }
  150. /**
  151. * Add followeds_users
  152. *
  153. * @param FollowUser $followedsUsers
  154. */
  155. public function addFollowUser(FollowUser $followedsUsers)
  156. {
  157. $this->followeds_users[] = $followedsUsers;
  158. }
  159. /**
  160. * Get followeds_users
  161. *
  162. * @return Doctrine\Common\Collections\Collection
  163. */
  164. public function getFollowedsUsers()
  165. {
  166. $users = array();
  167. foreach ($this->followeds_users as $follow_user)
  168. {
  169. $users[] = $follow_user->getFollowed();
  170. }
  171. return $users;
  172. }
  173. /**
  174. * Get followers_users
  175. *
  176. * @return Doctrine\Common\Collections\Collection
  177. */
  178. public function getFollowersUsers()
  179. {
  180. $users = array();
  181. foreach ($this->followers_users as $follow_user)
  182. {
  183. $users[] = $follow_user->getFollower();
  184. }
  185. return $users;
  186. }
  187. /**
  188. * Add followed_groups
  189. *
  190. * @param FollowGroup $followedGroups
  191. */
  192. public function addFollowGroup(FollowGroup $followedGroups)
  193. {
  194. $this->followed_groups[] = $followedGroups;
  195. }
  196. /**
  197. * Get followed_groups
  198. *
  199. * @return Doctrine\Common\Collections\Collection
  200. */
  201. public function getFollowedGroups()
  202. {
  203. $groups = array();
  204. foreach ($this->followed_groups as $follow_group)
  205. {
  206. $groups[] = $follow_group->getGroup();
  207. }
  208. return $groups;
  209. }
  210. /**
  211. * Add groups
  212. *
  213. * @param Group $groups
  214. */
  215. public function addGroupOwned(Group $groups)
  216. {
  217. $this->groups[] = $groups;
  218. }
  219. /**
  220. * Get groups
  221. *
  222. * @return Doctrine\Common\Collections\Collection
  223. */
  224. public function getGroupsOnwed()
  225. {
  226. return $this->groups;
  227. }
  228. public function getSlug()
  229. {
  230. return $this->slug;
  231. }
  232. public function setSlug($slug)
  233. {
  234. $this->slug = $slug;
  235. }
  236. /*
  237. *
  238. *
  239. */
  240. public function getName()
  241. {
  242. return $this->getUsername();
  243. }
  244. // /**
  245. // * @ORM\prePersist
  246. // */
  247. // public function setSlug()
  248. // {
  249. // if (!$this->slug)
  250. // {
  251. //
  252. // }
  253. // }
  254. //
  255. /**
  256. * Retourn si l'user_id transmis fait partis des enregistrements
  257. * followed de l'objet.
  258. *
  259. * @param int $user_id
  260. * @return boolean
  261. */
  262. public function isFollowingUser($user_id)
  263. {
  264. foreach ($this->followeds_users as $followed_user)
  265. {
  266. if ($followed_user->getFollowed()->getId() == $user_id)
  267. {
  268. return true;
  269. }
  270. }
  271. return false;
  272. }
  273. /**
  274. * Retourn si l'user_id transmis est l'un des User suivis
  275. *
  276. * @param Symfony\Bundle\DoctrineBundle\Registry doctrine
  277. * @param int $user_id
  278. * @return boolean
  279. */
  280. public function isFollowingUserByQuery($doctrine, $user_id)
  281. {
  282. return $doctrine
  283. ->getRepository('MuzichCoreBundle:User')
  284. ->isFollowingUser($this->getId(), $user_id)
  285. ;
  286. }
  287. /**
  288. * Retourn si l'group_id transmis est l'un des groupe suivis
  289. *
  290. * @param Symfony\Bundle\DoctrineBundle\Registry doctrine
  291. * @param int $user_id
  292. * @return boolean
  293. */
  294. public function isFollowingGroupByQuery($doctrine, $group_id)
  295. {
  296. return $doctrine
  297. ->getRepository('MuzichCoreBundle:User')
  298. ->isFollowingGroup($this->getId(), $group_id)
  299. ;
  300. }
  301. public function getPersonalHash()
  302. {
  303. return hash('sha256', $this->getSalt().$this->getUsername());
  304. }
  305. }