Playlist.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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", onDelete="SET NULL")
  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", cascade={"remove"})
  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 = null)
  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 getPickedsUsers()
  102. {
  103. $users = new ArrayCollection();
  104. foreach ($this->getUserPlaylistsPickeds() as $user_playlist_picked)
  105. {
  106. $users->add($user_playlist_picked->getUser());
  107. }
  108. return $users;
  109. }
  110. public function havePickerUser(User $user)
  111. {
  112. foreach ($this->getPickedsUsers() as $user_picker)
  113. {
  114. if ($user_picker->getId() == $user->getId())
  115. {
  116. return true;
  117. }
  118. }
  119. return false;
  120. }
  121. public function setUserPlaylistsPickeds(Collection $user_playlists_pickeds = null)
  122. {
  123. $this->user_playlists_pickeds = $user_playlists_pickeds;
  124. }
  125. public function addPickedUser(User $user)
  126. {
  127. $this->addUserPlaylistPicked($user);
  128. }
  129. public function addUserPlaylistPicked(User $user)
  130. {
  131. // TODO
  132. $user_playlist_picked = new UserPlaylistPicked();
  133. $user_playlist_picked->setUser($user);
  134. $user_playlist_picked->setPlaylist($this);
  135. $this->getUserPlaylistsPickeds()->add($user_playlist_picked);
  136. }
  137. public function removePickedUser(User $user)
  138. {
  139. // TODO: à coder
  140. }
  141. /** @return array */
  142. public function getTags()
  143. {
  144. return json_decode($this->tags, true);
  145. }
  146. /** @param tags array */
  147. public function setTags($tags)
  148. {
  149. $this->tags = json_encode($tags);
  150. }
  151. public function addTag(Tag $tag)
  152. {
  153. $tags_manager = new TagCollectionManager(json_decode($this->tags, true));
  154. $tags_manager->add($tag);
  155. $this->setTags($tags_manager->getContent());
  156. }
  157. public function removeTag(Tag $tag)
  158. {
  159. $tags_manager = new TagCollectionManager(json_decode($this->tags, true));
  160. $tags_manager->remove($tag);
  161. $this->setTags($tags_manager->getContent());
  162. }
  163. public function cleanTags()
  164. {
  165. $this->setTags(array());
  166. }
  167. public function getTagsIds()
  168. {
  169. $tags_manager = new TagCollectionManager(json_decode($this->tags, true));
  170. return $tags_manager->getAttributes(TagCollectionManager::ATTRIBUTE_ID);
  171. }
  172. /** @return array */
  173. public function getElements()
  174. {
  175. return json_decode($this->elements, true);
  176. }
  177. /** @param tags array */
  178. public function setElements($elements)
  179. {
  180. $this->elements = json_encode($elements);
  181. }
  182. public function addElement(Element $element)
  183. {
  184. $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
  185. $elements_manager->add($element);
  186. $this->setElements($elements_manager->getContent());
  187. }
  188. public function removeElement(Element $element)
  189. {
  190. $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
  191. $elements_manager->remove($element);
  192. $this->setElements($elements_manager->getContent());
  193. }
  194. public function removeElementWithId($element_id)
  195. {
  196. $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
  197. $elements_manager->removeWithReference($element_id);
  198. $this->setElements($elements_manager->getContent());
  199. }
  200. public function getElementIndex(Element $element)
  201. {
  202. $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
  203. return $elements_manager->index($element);
  204. }
  205. public function removeElementWithIndex($index)
  206. {
  207. $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
  208. $elements_manager->removeWithIndex($index);
  209. $this->setElements($elements_manager->getContent());
  210. }
  211. public function getElementsIds()
  212. {
  213. $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
  214. return $elements_manager->getAttributes(ElementCollectionManager::ATTRIBUTE_ID);
  215. }
  216. public function haveElement(Element $element)
  217. {
  218. $elements_manager = new ElementCollectionManager(json_decode($this->elements, true));
  219. return $elements_manager->have($element);
  220. }
  221. public function setCopied(Playlist $copied)
  222. {
  223. $this->copied = $copied;
  224. }
  225. public function getCopied()
  226. {
  227. return $this->copied;
  228. }
  229. public function addCopy(Playlist $playlist)
  230. {
  231. $this->copys->add($playlist);
  232. }
  233. public function getCopys()
  234. {
  235. return $this->copys;
  236. }
  237. public function setCopys($copys)
  238. {
  239. $this->copys = $copys;
  240. }
  241. public function isOwned(User $user)
  242. {
  243. if ($this->getOwner()->getId() == $user->getId())
  244. {
  245. return true;
  246. }
  247. return false;
  248. }
  249. }