Group.php 6.9KB

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