Element.php 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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 Symfony\Component\Validator\Constraints as Assert;
  8. use Muzich\CoreBundle\Validator as MuzichAssert;
  9. use Muzich\CoreBundle\Entity\Tag;
  10. /**
  11. * L'Element est l'Element central de l'application. C'est cet
  12. * entité qui stocke le media partagé sur le réseau.
  13. *
  14. * @ORM\Entity
  15. * @ORM\Table(name="element")
  16. * @ORM\Entity(repositoryClass="Muzich\CoreBundle\Repository\ElementRepository")
  17. *
  18. */
  19. class Element
  20. {
  21. /**
  22. * @ORM\Id
  23. * @ORM\Column(type="integer")
  24. * @ORM\GeneratedValue(strategy="AUTO")
  25. * @var type int
  26. */
  27. protected $id;
  28. /**
  29. * Cet attribut stocke le type d'élément.
  30. *
  31. * @ORM\Column(type="string", length=64)
  32. * @Assert\NotBlank()
  33. * @Assert\MaxLength(1024)
  34. */
  35. protected $type;
  36. /**
  37. * Cet attribut stocke la liste des tags liés a cet élément.
  38. *
  39. * @ORM\ManyToMany(targetEntity="Tag", inversedBy="elements")
  40. * @ORM\JoinTable(name="elements_tag")
  41. * @MuzichAssert\Tags()
  42. */
  43. private $tags;
  44. /**
  45. * Propriétaire de l'élément
  46. *
  47. * @ORM\ManyToOne(targetEntity="User", inversedBy="elements")
  48. * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  49. */
  50. protected $owner;
  51. /**
  52. * Groupe de l'élément
  53. *
  54. * @ORM\ManyToOne(targetEntity="Group", inversedBy="elements")
  55. * @ORM\JoinColumn(name="group_id", referencedColumnName="id")
  56. * @MuzichAssert\GroupOwnedOrPublic()
  57. */
  58. protected $group = null;
  59. /**
  60. * Cet attribu stocke les enregistrements UsersElementsFavorites liés
  61. * a ce Tag dans le cadre des Elements favoris.
  62. *
  63. * @ORM\OneToMany(targetEntity="UsersElementsFavorites", mappedBy="element")
  64. */
  65. protected $elements_favorites;
  66. /**
  67. * L'url est l'url du media.
  68. *
  69. * @ORM\Column(type="string", length=1024)
  70. * @Assert\NotBlank(message = "error.element.url.notblank")
  71. * @Assert\MaxLength(limit = 1024, message = "error.element.url.tolong")
  72. * @Assert\Url(message = "error.element.url.invalid")
  73. * @var type string
  74. */
  75. protected $url;
  76. /**
  77. * Libellé du media
  78. *
  79. * @ORM\Column(type = "string", length = 128)
  80. * @Assert\NotBlank(message = "error.element.name.notblank")
  81. * @Assert\MinLength(limit = 3, message = "error.element.name.toshort")
  82. * @Assert\MaxLength(limit = 64, message = "error.element.name.tolong")
  83. * @var type string
  84. */
  85. protected $name;
  86. /**
  87. * Code d'embed
  88. *
  89. * @ORM\Column(type="text", nullable=true)
  90. * @var type string
  91. */
  92. protected $embed;
  93. /**
  94. * @var datetime $created
  95. *
  96. * @Gedmo\Timestampable(on="create")
  97. * @ORM\Column(type="datetime")
  98. */
  99. private $created;
  100. /**
  101. * @var datetime $updated
  102. *
  103. * @ORM\Column(type="datetime")
  104. * @Gedmo\Timestampable(on="update")
  105. */
  106. private $updated;
  107. /**
  108. * @var string $thumbnail_url
  109. *
  110. * @ORM\Column(type="string", length=512, nullable=true)
  111. */
  112. protected $thumbnail_url;
  113. /**
  114. * Commentaires stocké au format json
  115. *
  116. * array(
  117. * array(
  118. * "u" => array( // Des infos sur l'utilisateur auteur du commentaire
  119. * "i" => "IdDuUser", // l'id
  120. * "s" => "LeSlugDuUser", // le slug
  121. * "n" => "NameDuUser" // le name
  122. * ),
  123. * "d" => "LaDate", // Date au format Y-m-d H:i:s
  124. * "c" => "Comment" // Le commentaire
  125. * ),
  126. * [...]
  127. * );
  128. *
  129. * @ORM\Column(type="text", nullable=true)
  130. * @var type string
  131. */
  132. protected $comments;
  133. /**
  134. * Get id
  135. *
  136. * @return integer
  137. */
  138. public function getId()
  139. {
  140. return $this->id;
  141. }
  142. /**
  143. * Set url
  144. *
  145. * @param string $url
  146. */
  147. public function setUrl($url)
  148. {
  149. $this->url = $url;
  150. }
  151. /**
  152. * Get url
  153. *
  154. * @return string
  155. */
  156. public function getUrl()
  157. {
  158. return $this->url;
  159. }
  160. /**
  161. * Set name
  162. *
  163. * @param string $name
  164. */
  165. public function setName($name)
  166. {
  167. $this->name = $name;
  168. }
  169. /**
  170. * Get name
  171. *
  172. * @return string
  173. */
  174. public function getName()
  175. {
  176. return $this->name;
  177. }
  178. /**
  179. * Set type
  180. *
  181. * @param string $type
  182. */
  183. public function setType($type)
  184. {
  185. $this->type = $type;
  186. }
  187. /**
  188. * Get type
  189. *
  190. * @return string
  191. */
  192. public function getType()
  193. {
  194. return $this->type;
  195. }
  196. public function __construct($url = null)
  197. {
  198. //$this->tags = new ArrayCollection();
  199. $this->url = $url;
  200. }
  201. public function __toString()
  202. {
  203. return $this->name;
  204. }
  205. /**
  206. * Add tags
  207. *
  208. * @param Tag $tags
  209. */
  210. public function addTag(Tag $tags)
  211. {
  212. $this->tags[] = $tags;
  213. }
  214. /**
  215. * Get tags
  216. *
  217. * @return Doctrine\Common\Collections\Collection
  218. */
  219. public function getTags()
  220. {
  221. return $this->tags;
  222. }
  223. public function getTagsIdsJson()
  224. {
  225. $ids = array();
  226. if (count($this->getTags()))
  227. {
  228. foreach ($this->getTags() as $tag)
  229. {
  230. $ids[] = $tag->getId();
  231. }
  232. }
  233. return json_encode($ids);
  234. }
  235. public function setTags($tags)
  236. {
  237. $this->tags = $tags;
  238. }
  239. /**
  240. * Set owner
  241. *
  242. * @param User $owner
  243. */
  244. public function setOwner(User $owner)
  245. {
  246. $this->owner = $owner;
  247. }
  248. /**
  249. * Get owner
  250. *
  251. * @return User
  252. */
  253. public function getOwner()
  254. {
  255. return $this->owner;
  256. }
  257. /**
  258. * Add elements_favorites
  259. *
  260. * @param UsersElementsFavorites $elementsFavorites
  261. */
  262. public function addUsersElementsFavorites(UsersElementsFavorites $elementsFavorites)
  263. {
  264. $this->elements_favorites[] = $elementsFavorites;
  265. }
  266. /**
  267. * Get elements_favorites
  268. *
  269. * @return Doctrine\Common\Collections\Collection
  270. */
  271. public function getElementsFavorites()
  272. {
  273. return $this->elements_favorites;
  274. }
  275. /**
  276. * Set group
  277. *
  278. * @param Group $group
  279. */
  280. public function setGroup($group)
  281. {
  282. $this->group = $group;
  283. }
  284. /**
  285. * Get group
  286. *
  287. * @return Group
  288. */
  289. public function getGroup()
  290. {
  291. return $this->group;
  292. }
  293. /**
  294. * Set embed
  295. *
  296. * @param string $code
  297. */
  298. public function setEmbed($code)
  299. {
  300. $this->embed = $code;
  301. }
  302. /**
  303. * Get embed
  304. *
  305. * @return string
  306. */
  307. public function getEmbed()
  308. {
  309. return $this->embed;
  310. }
  311. /**
  312. * Set created
  313. *
  314. * @param date $created
  315. */
  316. public function setCreated($created)
  317. {
  318. $this->created = $created;
  319. }
  320. /**
  321. * Get created
  322. *
  323. * @return date
  324. */
  325. public function getCreated()
  326. {
  327. return $this->created;
  328. }
  329. /**
  330. * Set updated
  331. *
  332. * @param datetime $updated
  333. */
  334. public function setUpdated($updated)
  335. {
  336. $this->updated = $updated;
  337. }
  338. /**
  339. * Get updated
  340. *
  341. * @return datetime
  342. */
  343. public function getUpdated()
  344. {
  345. return $this->updated;
  346. }
  347. /**
  348. * Set thumbnail url
  349. *
  350. * @param string $thumbnail_url
  351. */
  352. public function setThumbnailUrl($thumbnail_url)
  353. {
  354. $this->thumbnail_url = $thumbnail_url;
  355. }
  356. /**
  357. * Get thumbnail url
  358. *
  359. * @return datetime
  360. */
  361. public function getThumbnailUrl()
  362. {
  363. return $this->thumbnail_url;
  364. }
  365. /**
  366. *
  367. * @return type array
  368. */
  369. public function getComments()
  370. {
  371. return json_decode($this->comments, true);
  372. }
  373. /**
  374. *
  375. * @param array $comments
  376. */
  377. public function setComments($comments)
  378. {
  379. $this->comments = json_encode($comments);
  380. }
  381. /**
  382. * Etablie des relation vers des tags.
  383. * (Supprime les anciens tags, au niveau de l'objet seulement)
  384. *
  385. * @param array $ids
  386. */
  387. public function setTagsWithIds(EntityManager $em, $ids)
  388. {
  389. $this->tags = null;
  390. if (count($ids))
  391. {
  392. $tags = $em->getRepository('MuzichCoreBundle:Tag')->findByIds($ids)->execute();
  393. // Pour les nouveaux ids restants
  394. foreach ($tags as $tag)
  395. {
  396. $this->addTag($tag);
  397. }
  398. }
  399. }
  400. public function getCountFavorite()
  401. {
  402. return count($this->elements_favorites);
  403. }
  404. public function setGroupToId()
  405. {
  406. $this->group = $this->group->getId();
  407. }
  408. // public function deleteTag(Tag $tag)
  409. // {
  410. // $this->tags->removeElement($tag);
  411. // }
  412. public function hasTag(Tag $tag_t)
  413. {
  414. foreach ($this->getTags() as $tag)
  415. {
  416. if ($tag_t->getId() == $tag->getId())
  417. {
  418. return true;
  419. }
  420. }
  421. return false;
  422. }
  423. }