Group.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. namespace Muzich\CoreBundle\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use \Doctrine\Common\Collections\ArrayCollection;
  5. use Gedmo\Mapping\Annotation as Gedmo;
  6. use Doctrine\ORM\EntityManager;
  7. use Muzich\CoreBundle\Entity\GroupsTagsFavorites;
  8. /**
  9. * Le groupe est une sorte de liste de diffusion, a laquelle les
  10. * users peuvent s'abonner (follow). Un groupe peut tout aussi bien
  11. * être "Les fans de la tek du sud est" qu'un sound système,
  12. * un artiste ...
  13. *
  14. * @ORM\Entity
  15. * @ORM\Table(name="m_group")
  16. * @ORM\Entity(repositoryClass="Muzich\CoreBundle\Repository\GroupRepository")
  17. */
  18. class Group
  19. {
  20. /**
  21. * @ORM\Id
  22. * @ORM\Column(type="integer")
  23. * @ORM\GeneratedValue(strategy="AUTO")
  24. * @var type int
  25. */
  26. protected $id;
  27. /**
  28. * Nom du groupe
  29. *
  30. * @ORM\Column(type="string", length=128)
  31. * @var type string
  32. */
  33. protected $name;
  34. /**
  35. * @Gedmo\Slug(fields={"name"})
  36. * @ORM\Column(length=128, unique=true)
  37. */
  38. protected $slug;
  39. /**
  40. * Description
  41. *
  42. * @ORM\Column(type="text")
  43. * @var type string
  44. */
  45. protected $description;
  46. /**
  47. * Si open est a vrai, cela traduit que les followers peuvent
  48. * diffuser leur element en tant qu'élément de ce groupe.
  49. *
  50. * @ORM\Column(type="boolean")
  51. * @var type string
  52. */
  53. protected $open = false;
  54. /**
  55. * Cet attribut contient les enregistrements FollowGroup lié
  56. * a ce Groupe dans le cadre des groupes suivis.
  57. *
  58. * @ORM\OneToMany(targetEntity="FollowGroup", mappedBy="group")
  59. */
  60. protected $followers;
  61. /**
  62. * Propriétaire
  63. *
  64. * @ORM\ManyToOne(targetEntity="User", inversedBy="groups_owned")
  65. * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  66. */
  67. protected $owner;
  68. /**
  69. * Cet attribut contient les enregistrements GroupsTagsFavorites lié
  70. * a ce Groupe dans le cadre des tags de groupe.
  71. *
  72. * @ORM\OneToMany(targetEntity="GroupsTagsFavorites", mappedBy="group")
  73. */
  74. protected $tags;
  75. /**
  76. * Cet attribut contient les enregistrements Element lié
  77. * a ce Groupe.
  78. *
  79. * @ORM\OneToMany(targetEntity="Element", mappedBy="group")
  80. */
  81. protected $elements;
  82. /**
  83. *
  84. */
  85. public function __construct()
  86. {
  87. $this->followers = new ArrayCollection();
  88. }
  89. public function __toString()
  90. {
  91. return $this->getName();
  92. }
  93. /**
  94. * Get id
  95. *
  96. * @return integer
  97. */
  98. public function getId()
  99. {
  100. return $this->id;
  101. }
  102. /**
  103. * Set name
  104. *
  105. * @param string $name
  106. */
  107. public function setName($name)
  108. {
  109. $this->name = $name;
  110. }
  111. /**
  112. * Get name
  113. *
  114. * @return string
  115. */
  116. public function getName()
  117. {
  118. return $this->name;
  119. }
  120. /**
  121. * Set slug
  122. *
  123. * @param string $slug
  124. */
  125. public function setSlug($slug)
  126. {
  127. $this->slug = $slug;
  128. }
  129. /**
  130. * Get slug
  131. *
  132. * @return string
  133. */
  134. public function getSlug()
  135. {
  136. return $this->slug;
  137. }
  138. /**
  139. * Set description
  140. *
  141. * @param string $description
  142. */
  143. public function setDescription($description)
  144. {
  145. $this->description = $description;
  146. }
  147. /**
  148. * Get description
  149. *
  150. * @return string
  151. */
  152. public function getDescription()
  153. {
  154. return $this->description;
  155. }
  156. /**
  157. * Set open
  158. *
  159. * @param boolean $open
  160. */
  161. public function setOpen($open)
  162. {
  163. $this->open = $open;
  164. }
  165. /**
  166. * Get open
  167. *
  168. * @return boolean
  169. */
  170. public function getOpen()
  171. {
  172. return $this->open;
  173. }
  174. /**
  175. * Add followers
  176. *
  177. * @param FollowGroup $followers
  178. */
  179. public function addFollowGroup(FollowGroup $followers)
  180. {
  181. $this->followers[] = $followers;
  182. }
  183. /**
  184. * Get followers
  185. *
  186. * @return Doctrine\Common\Collections\Collection
  187. */
  188. public function getFollowers()
  189. {
  190. return $this->followers;
  191. }
  192. /**
  193. * Set owner
  194. *
  195. * @param User $owner
  196. */
  197. public function setOwner(User $owner = null)
  198. {
  199. $this->owner = $owner;
  200. }
  201. /**
  202. * Get owner
  203. *
  204. * @return User
  205. */
  206. public function getOwner()
  207. {
  208. return $this->owner;
  209. }
  210. /**
  211. * Add tags
  212. *
  213. * @param GroupsTagsFavorites $tags
  214. */
  215. public function addGroupsTagsFavorites(GroupsTagsFavorites $tags)
  216. {
  217. $this->tags[] = $tags;
  218. }
  219. /**
  220. * Get tags
  221. *
  222. * @return Doctrine\Common\Collections\Collection
  223. */
  224. public function getTags()
  225. {
  226. return $this->tags;
  227. }
  228. /**
  229. * Add tag
  230. *
  231. * @param Tag $tag
  232. */
  233. public function addTag(Tag $tag, EntityManager $em)
  234. {
  235. $GroupsTagsFavorites = new GroupsTagsFavorites();
  236. $GroupsTagsFavorites->setTag($tag);
  237. $GroupsTagsFavorites->setPosition(0);
  238. $GroupsTagsFavorites->setGroup($this);
  239. $em->persist($GroupsTagsFavorites);
  240. $this->tags[] = $GroupsTagsFavorites;
  241. }
  242. public function setTags($tags)
  243. {
  244. $this->tags = $tags;
  245. }
  246. /**
  247. * Add elements
  248. *
  249. * @param Element $elements
  250. */
  251. public function addElement(Element $elements)
  252. {
  253. $this->elements[] = $elements;
  254. }
  255. /**
  256. * Get elements
  257. *
  258. * @return Doctrine\Common\Collections\Collection
  259. */
  260. public function getElements()
  261. {
  262. return $this->elements;
  263. }
  264. /**
  265. * Definis les relation vers des tags.
  266. *
  267. * @param array $ids
  268. */
  269. public function setTagsWithIds(EntityManager $em, $ids)
  270. {
  271. $tags = $em->getRepository('MuzichCoreBundle:Tag')->findByIds($ids)->execute();
  272. // Pour les nouveaux ids restants
  273. foreach ($tags as $tag)
  274. {
  275. $this->addTag($tag, $em);
  276. }
  277. }
  278. public function setTagsToIds()
  279. {
  280. $tags_id = array();
  281. foreach ($this->getTags() as $tag_r)
  282. {
  283. $tags_id[] = $tag_r->getTag()->getId();
  284. }
  285. $this->tags = $tags_id;
  286. }
  287. /**
  288. * Retourne vrai si l'user_id peut poster dans ce groupe.
  289. *
  290. * @param int $user_id
  291. * @return boolean
  292. */
  293. public function userCanAddElement($user_id)
  294. {
  295. if ($this->open)
  296. {
  297. return true;
  298. }
  299. if ($this->getOwner()->getId() == $user_id)
  300. {
  301. return true;
  302. }
  303. return false;
  304. }
  305. }