Playlist.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. namespace Muzich\CoreBundle\Entity;
  3. use Doctrine\ORM\Mapping as ORM;
  4. use Gedmo\Mapping\Annotation as Gedmo;
  5. use Symfony\Component\Validator\Constraints as Assert;
  6. use \Doctrine\Common\Collections\ArrayCollection;
  7. use \Doctrine\Common\Collections\Collection;
  8. use Muzich\CoreBundle\lib\Collection\ElementCollectionManager;
  9. use Muzich\CoreBundle\lib\Collection\TagCollectionManager;
  10. /**
  11. * @ORM\Entity
  12. * @ORM\Entity(repositoryClass="Muzich\CoreBundle\Repository\PlaylistRepository")
  13. */
  14. class Playlist
  15. {
  16. /**
  17. * @ORM\Id
  18. * @ORM\Column(type="integer")
  19. * @ORM\GeneratedValue(strategy="AUTO")
  20. * @var type int
  21. */
  22. protected $id;
  23. /**
  24. * @ORM\Column(type="string", length=128, unique=false)
  25. * @Assert\NotBlank()
  26. * @Assert\Length(min = 3, max = 64)
  27. * @var type string
  28. */
  29. protected $name;
  30. /**
  31. * @ORM\ManyToOne(targetEntity="User", inversedBy="playlists_owneds")
  32. * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  33. */
  34. protected $owner;
  35. /**
  36. * @ORM\ManyToOne(targetEntity="Playlist", inversedBy="copys")
  37. * @ORM\JoinColumn(name="copied_id", referencedColumnName="id")
  38. */
  39. protected $copied;
  40. /**
  41. * @ORM\OneToMany(targetEntity="Playlist", mappedBy="copied")
  42. */
  43. protected $copys;
  44. /**
  45. * @ORM\Column(type="boolean")
  46. */
  47. protected $public = false;
  48. /**
  49. * @ORM\OneToMany(targetEntity="UserPlaylistPicked", mappedBy="playlist")
  50. */
  51. protected $user_playlists_pickeds;
  52. /**
  53. * @ORM\Column(type="text", unique=false, nullable=true)
  54. */
  55. private $tags;
  56. /**
  57. * @ORM\Column(type="text", unique=false, nullable=true)
  58. */
  59. private $elements;
  60. public function __construct()
  61. {
  62. $this->user_playlists_pickeds = new ArrayCollection();
  63. $this->copys = new ArrayCollection();
  64. $this->tags = json_encode(array());
  65. $this->elements = json_encode(array());
  66. }
  67. public function getId()
  68. {
  69. return $this->id;
  70. }
  71. public function getName()
  72. {
  73. return $this->name;
  74. }
  75. public function setName($name)
  76. {
  77. $this->name = $name;
  78. }
  79. /** @return User */
  80. public function getOwner()
  81. {
  82. return $this->owner;
  83. }
  84. public function setOwner(User $owner)
  85. {
  86. $this->owner = $owner;
  87. }
  88. public function isPublic()
  89. {
  90. return ($this->public)?true:false;
  91. }
  92. public function setPublic($public)
  93. {
  94. ($public) ? $this->public = true : $this->public = false;
  95. }
  96. /** @return Collection */
  97. public function getUserPlaylistsPickeds()
  98. {
  99. return $this->user_playlists_pickeds;
  100. }
  101. public function setUserPlaylistsPickeds(Collection $user_playlists_pickeds = null)
  102. {
  103. $this->user_playlists_pickeds = $user_playlists_pickeds;
  104. }
  105. public function addPickedUser(User $user)
  106. {
  107. $this->addUserPlaylistPicked($user);
  108. }
  109. public function addUserPlaylistPicked(User $user)
  110. {
  111. // TODO
  112. $user_playlist_picked = new UserPlaylistPicked();
  113. $user_playlist_picked->setUser($user);
  114. $user_playlist_picked->setPlaylist($this);
  115. $this->getUserPlaylistsPickeds()->add($user_playlist_picked);
  116. }
  117. public function removePickedUser(User $user)
  118. {
  119. // TODO: à coder
  120. }
  121. /** @return array */
  122. public function getTags()
  123. {
  124. return json_decode($this->tags, true);
  125. }
  126. /** @param tags array */
  127. public function setTags($tags)
  128. {
  129. $this->tags = json_encode($tags);
  130. }
  131. public function addTag(Tag $tag)
  132. {
  133. $tags_manager = new TagCollectionManager(json_decode($this->tags, true));
  134. $tags_manager->add($tag);
  135. $this->setTags($tags_manager->getContent());
  136. }
  137. public function removeTag(Tag $tag)
  138. {
  139. $tags_manager = new TagCollectionManager(json_decode($this->tags, true));
  140. $tags_manager->remove($tag);
  141. $this->setTags($tags_manager->getContent());
  142. }
  143. public function cleanTags()
  144. {
  145. $this->setTags(array());
  146. }
  147. public function getTagsIds()
  148. {
  149. $tags_manager = new TagCollectionManager(json_decode($this->tags, true));
  150. return $tags_manager->getAttributes(TagCollectionManager::ATTRIBUTE_ID);
  151. }
  152. /** @return array */
  153. public function getElements()
  154. {
  155. return json_decode($this->elements, true);
  156. }
  157. /** @param tags array */
  158. public function setElements($elements)
  159. {
  160. $this->elements = json_encode($elements);
  161. }
  162. public function addElement(Element $element)
  163. {
  164. $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
  165. $elements_manager->add($element);
  166. $this->setElements($elements_manager->getContent());
  167. }
  168. public function removeElement(Element $element)
  169. {
  170. $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
  171. $elements_manager->remove($element);
  172. $this->setElements($elements_manager->getContent());
  173. }
  174. public function removeElementWithId($element_id)
  175. {
  176. $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
  177. $elements_manager->removeWithReference($element_id);
  178. $this->setElements($elements_manager->getContent());
  179. }
  180. public function getElementsIds()
  181. {
  182. $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
  183. return $elements_manager->getAttributes(ElementCollectionManager::ATTRIBUTE_ID);
  184. }
  185. public function haveElement(Element $element)
  186. {
  187. $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
  188. return $elements_manager->have($element);
  189. }
  190. public function setCopied(Playlist $copied)
  191. {
  192. $this->copied = $copied;
  193. }
  194. public function getCopied()
  195. {
  196. return $this->copied;
  197. }
  198. public function addCopy(Playlist $playlist)
  199. {
  200. $this->copys->add($playlist);
  201. }
  202. public function getCopys()
  203. {
  204. return $this->copys;
  205. }
  206. }