Element.php 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  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. use Muzich\CoreBundle\Managers\CommentsManager;
  11. /**
  12. * L'Element est l'Element central de l'application. C'est cet
  13. * entité qui stocke le media partagé sur le réseau.
  14. *
  15. * @ORM\Entity
  16. * @ORM\Table(name="element")
  17. * @ORM\Entity(repositoryClass="Muzich\CoreBundle\Repository\ElementRepository")
  18. *
  19. */
  20. class Element
  21. {
  22. /**
  23. * Identifiant de l'objet externe
  24. * @var string
  25. */
  26. const DATA_REF_ID = "data_ref_id";
  27. /**
  28. * Adresse HTTP(S) de la jaquette
  29. * @var string
  30. */
  31. const DATA_THUMB_URL = "data_thumb_url";
  32. /**
  33. * Titre de l'objet externe
  34. * @var string
  35. */
  36. const DATA_TITLE = "data_title";
  37. /**
  38. * Nom de l'artiste
  39. * @var string
  40. */
  41. const DATA_ARTIST = "data_artist";
  42. /**
  43. * Tags de l'objets externe array("tag1", "tag2", [...])
  44. * @var string
  45. */
  46. const DATA_TAGS = "data_tags";
  47. /**
  48. * Type de l'objet track|album|
  49. * @var string
  50. */
  51. const DATA_TYPE = "data_type";
  52. /**
  53. * Contenu téléchargeable ?
  54. * @var string
  55. */
  56. const DATA_DOWNLOAD = "data_download";
  57. /**
  58. * Adresse du contenu téléchargeable
  59. * @var string
  60. */
  61. const DATA_DOWNLOAD_URL = "data_download_url";
  62. /**
  63. * Don possible ?
  64. * @var string
  65. */
  66. const DATA_GIFT = "data_gift";
  67. /**
  68. * Adresse pour effectuer un don
  69. * @var string
  70. */
  71. const DATA_GIFT_URL = "data_gift_url";
  72. /**
  73. * @ORM\Id
  74. * @ORM\Column(type="integer")
  75. * @ORM\GeneratedValue(strategy="AUTO")
  76. * @var type int
  77. */
  78. protected $id;
  79. /**
  80. * Cet attribut stocke le type d'élément.
  81. *
  82. * @ORM\Column(type="string", length=64)
  83. * @Assert\NotBlank()
  84. * @Assert\MaxLength(1024)
  85. */
  86. protected $type;
  87. /**
  88. * Cet attribut stocke la liste des tags liés a cet élément.
  89. *
  90. * @ORM\ManyToMany(targetEntity="Tag", inversedBy="elements")
  91. * @ORM\JoinTable(name="elements_tag")
  92. * @MuzichAssert\Tags()
  93. */
  94. private $tags;
  95. /**
  96. * Propriétaire de l'élément
  97. *
  98. * @ORM\ManyToOne(targetEntity="User", inversedBy="elements")
  99. * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  100. */
  101. protected $owner;
  102. /**
  103. * Objet parent (si a été repartagé)
  104. *
  105. * @ORM\ManyToOne(targetEntity="Element", inversedBy="childs")
  106. * @ORM\JoinColumn(name="element_parent_id", referencedColumnName="id")
  107. */
  108. protected $parent;
  109. /**
  110. * Liste des Elements qui on reshare cet element
  111. *
  112. * @ORM\OneToMany(targetEntity="Element", mappedBy="parent")
  113. */
  114. protected $childs;
  115. /**
  116. * Groupe de l'élément
  117. *
  118. * @ORM\ManyToOne(targetEntity="Group", inversedBy="elements")
  119. * @ORM\JoinColumn(name="group_id", referencedColumnName="id")
  120. * @MuzichAssert\GroupOwnedOrPublic()
  121. */
  122. protected $group = null;
  123. /**
  124. * Cet attribu stocke les enregistrements UsersElementsFavorites liés
  125. * a ce Tag dans le cadre des Elements favoris.
  126. *
  127. * @ORM\OneToMany(targetEntity="UsersElementsFavorites", mappedBy="element")
  128. */
  129. protected $elements_favorites;
  130. /**
  131. * Propositions de tags
  132. *
  133. * @ORM\OneToMany(targetEntity="ElementTagsProposition", mappedBy="element")
  134. */
  135. protected $tags_propositions;
  136. /**
  137. * Permet de savoir sans faire de gros calculs en base si il y a des
  138. * propositions de tags en cours sur cet élément.
  139. *
  140. * @ORM\Column(type="boolean", nullable = true)
  141. * @var type string
  142. */
  143. protected $has_tags_proposition = false;
  144. /**
  145. * L'url est l'url du media.
  146. *
  147. * @ORM\Column(type="string", length=1024)
  148. * @Assert\NotBlank(message = "error.element.url.notblank")
  149. * @Assert\MaxLength(limit = 1024, message = "error.element.url.tolong")
  150. * @Assert\Url(message = "error.element.url.invalid")
  151. * @var type string
  152. */
  153. protected $url;
  154. /**
  155. * Libellé du media
  156. *
  157. * @ORM\Column(type = "string", length = 128)
  158. * @Assert\NotBlank(message = "error.element.name.notblank")
  159. * @Assert\MinLength(limit = 3, message = "error.element.name.toshort")
  160. * @Assert\MaxLength(limit = 64, message = "error.element.name.tolong")
  161. * @var type string
  162. */
  163. protected $name;
  164. /**
  165. * Code d'embed
  166. *
  167. * @ORM\Column(type="text", nullable=true)
  168. * @var type string
  169. */
  170. protected $embed;
  171. /**
  172. * @var datetime $created
  173. *
  174. * @Gedmo\Timestampable(on="create")
  175. * @ORM\Column(type="datetime")
  176. */
  177. private $created;
  178. /**
  179. * @var datetime $updated
  180. *
  181. * @ORM\Column(type="datetime")
  182. * @Gedmo\Timestampable(on="update")
  183. */
  184. private $updated;
  185. /**
  186. * @var string $thumbnail_url
  187. *
  188. * @ORM\Column(type="string", length=512, nullable=true)
  189. */
  190. protected $thumbnail_url;
  191. /**
  192. * Commentaires stocké au format json
  193. *
  194. * array(
  195. * array(
  196. * "u" => array( // Des infos sur l'utilisateur auteur du commentaire
  197. * "i" => "IdDuUser", // l'id
  198. * "s" => "LeSlugDuUser", // le slug
  199. * "n" => "NameDuUser" // le name
  200. * ),
  201. * "d" => "LaDate", // Date au format Y-m-d H:i:s
  202. * "c" => "Comment" // Le commentaire
  203. * ),
  204. * [...]
  205. * );
  206. *
  207. * @ORM\Column(type="text", nullable=true)
  208. * @var type string
  209. */
  210. protected $comments;
  211. /**
  212. * Compteur de signalements
  213. *
  214. * @ORM\Column(type="integer", nullable=true)
  215. * @var int
  216. */
  217. protected $count_report;
  218. /**
  219. * array json des id users ayant reporté l'élément
  220. *
  221. * @ORM\Column(type="text", nullable=true)
  222. * @var string
  223. */
  224. protected $report_ids;
  225. /**
  226. * array json des id users ayant voté +1
  227. *
  228. * @ORM\Column(type="text", nullable=true)
  229. * @var string
  230. */
  231. protected $vote_good_ids;
  232. /**
  233. * Compteur de points
  234. *
  235. * @ORM\Column(type="integer", nullable=true)
  236. * @var int
  237. */
  238. protected $points;
  239. /**
  240. * Booléen permettant de savoir si un des commentaires de cet élément
  241. * a été signalé a la modération.
  242. *
  243. * @ORM\Column(type="integer", nullable=true)
  244. * @var int
  245. */
  246. protected $count_comment_report = false;
  247. /**
  248. * Données de l'objet chez le service externe
  249. *
  250. * @ORM\Column(type="text", nullable=true)
  251. * @var type string
  252. */
  253. protected $datas;
  254. /**
  255. *
  256. * @ORM\Column(type="boolean", nullable=true)
  257. * @var boolean
  258. */
  259. protected $need_tags = false;
  260. /**
  261. * Get id
  262. *
  263. * @return integer
  264. */
  265. public function getId()
  266. {
  267. return $this->id;
  268. }
  269. /**
  270. * Set url
  271. *
  272. * @param string $url
  273. */
  274. public function setUrl($url)
  275. {
  276. $this->url = $url;
  277. }
  278. /**
  279. * Get url
  280. *
  281. * @return string
  282. */
  283. public function getUrl()
  284. {
  285. return $this->url;
  286. }
  287. /**
  288. * Set name
  289. *
  290. * @param string $name
  291. */
  292. public function setName($name)
  293. {
  294. $this->name = $name;
  295. }
  296. /**
  297. * Get name
  298. *
  299. * @return string
  300. */
  301. public function getName()
  302. {
  303. return $this->name;
  304. }
  305. /**
  306. * Set type
  307. *
  308. * @param string $type
  309. */
  310. public function setType($type)
  311. {
  312. $this->type = $type;
  313. }
  314. /**
  315. * Get type
  316. *
  317. * @return string
  318. */
  319. public function getType()
  320. {
  321. return $this->type;
  322. }
  323. public function __construct($url = null)
  324. {
  325. //$this->tags = new ArrayCollection();
  326. $this->url = $url;
  327. }
  328. public function __toString()
  329. {
  330. return $this->name;
  331. }
  332. /**
  333. * Add tags
  334. *
  335. * @param Tag $tags
  336. */
  337. public function addTag(Tag $tags)
  338. {
  339. $this->tags[] = $tags;
  340. }
  341. /**
  342. * Get tags
  343. *
  344. * @return Doctrine\Common\Collections\Collection
  345. */
  346. public function getTags()
  347. {
  348. return $this->tags;
  349. }
  350. public function getTagsIdsJson()
  351. {
  352. $ids = array();
  353. if (count($this->getTags()))
  354. {
  355. foreach ($this->getTags() as $tag)
  356. {
  357. $ids[] = $tag->getId();
  358. }
  359. }
  360. return json_encode($ids);
  361. }
  362. public function setTags($tags)
  363. {
  364. $this->tags = $tags;
  365. }
  366. /**
  367. *
  368. * @param Collection|Array $tags
  369. */
  370. public function addTags($tags)
  371. {
  372. foreach ($tags as $tag)
  373. {
  374. $this->addTag($tag);
  375. }
  376. }
  377. /**
  378. * Set owner
  379. *
  380. * @param User $owner
  381. */
  382. public function setOwner(User $owner)
  383. {
  384. $this->owner = $owner;
  385. }
  386. /**
  387. * Get owner
  388. *
  389. * @return User
  390. */
  391. public function getOwner()
  392. {
  393. return $this->owner;
  394. }
  395. /**
  396. * Add elements_favorites
  397. *
  398. * @param UsersElementsFavorites $elementsFavorites
  399. */
  400. public function addUsersElementsFavorites(UsersElementsFavorites $elementsFavorites)
  401. {
  402. $this->elements_favorites[] = $elementsFavorites;
  403. }
  404. /**
  405. * Get elements_favorites
  406. *
  407. * @return Doctrine\Common\Collections\Collection
  408. */
  409. public function getElementsFavorites()
  410. {
  411. return $this->elements_favorites;
  412. }
  413. /**
  414. * Set group
  415. *
  416. * @param Group $group
  417. */
  418. public function setGroup($group)
  419. {
  420. $this->group = $group;
  421. }
  422. /**
  423. * Get group
  424. *
  425. * @return Group
  426. */
  427. public function getGroup()
  428. {
  429. return $this->group;
  430. }
  431. /**
  432. * Set embed
  433. *
  434. * @param string $code
  435. */
  436. public function setEmbed($code)
  437. {
  438. $this->embed = $code;
  439. }
  440. /**
  441. * Get embed
  442. *
  443. * @return string
  444. */
  445. public function getEmbed()
  446. {
  447. return $this->embed;
  448. }
  449. /**
  450. * Set created
  451. *
  452. * @param date $created
  453. */
  454. public function setCreated($created)
  455. {
  456. $this->created = $created;
  457. }
  458. /**
  459. * Get created
  460. *
  461. * @return date
  462. */
  463. public function getCreated()
  464. {
  465. return $this->created;
  466. }
  467. /**
  468. * Set updated
  469. *
  470. * @param datetime $updated
  471. */
  472. public function setUpdated($updated)
  473. {
  474. $this->updated = $updated;
  475. }
  476. /**
  477. * Get updated
  478. *
  479. * @return datetime
  480. */
  481. public function getUpdated()
  482. {
  483. return $this->updated;
  484. }
  485. /**
  486. * Set thumbnail url
  487. *
  488. * @param string $thumbnail_url
  489. */
  490. public function setThumbnailUrl($thumbnail_url)
  491. {
  492. $this->thumbnail_url = $thumbnail_url;
  493. }
  494. /**
  495. * Get thumbnail url
  496. *
  497. * @return datetime
  498. */
  499. public function getThumbnailUrl()
  500. {
  501. return $this->thumbnail_url;
  502. }
  503. /**
  504. *
  505. * @return type array
  506. */
  507. public function getComments()
  508. {
  509. return json_decode($this->comments, true);
  510. }
  511. public function getCountReport()
  512. {
  513. return $this->count_report;
  514. }
  515. public function setCountReport($count)
  516. {
  517. $this->count_report = $count;
  518. }
  519. public function getReportIds()
  520. {
  521. return json_decode($this->report_ids, true);
  522. }
  523. public function setReportIds($report_ids)
  524. {
  525. $this->report_ids = json_encode($report_ids);
  526. }
  527. /**
  528. *
  529. * @param array $comments
  530. */
  531. public function setComments($comments)
  532. {
  533. $this->comments = json_encode($comments);
  534. }
  535. /**
  536. *
  537. * @return type array
  538. */
  539. public function getDatas()
  540. {
  541. if ($this->datas === null)
  542. {
  543. return array();
  544. }
  545. return json_decode($this->datas, true);
  546. }
  547. /**
  548. *
  549. * @param string $data_id
  550. * @return all
  551. */
  552. public function getData($data_id)
  553. {
  554. $datas = $this->getDatas();
  555. if (array_key_exists($data_id, $datas))
  556. {
  557. return $datas[$data_id];
  558. }
  559. return null;
  560. }
  561. /**
  562. *
  563. * @param array $datas
  564. */
  565. public function setDatas($datas)
  566. {
  567. $this->datas = json_encode($datas);
  568. }
  569. /**
  570. *
  571. * @param string $data_id
  572. * @param all $data_value
  573. */
  574. public function setData($data_id, $data_value)
  575. {
  576. $datas = $this->getDatas();
  577. $datas[$data_id] = $data_value;
  578. $this->setDatas($datas);
  579. }
  580. public function setHasTagProposition($has_prop)
  581. {
  582. $this->has_tags_proposition = $has_prop;
  583. }
  584. public function hasTagProposition()
  585. {
  586. if ($this->has_tags_proposition === null)
  587. {
  588. return false;
  589. }
  590. return $this->has_tags_proposition;
  591. }
  592. public function getTagsProposition()
  593. {
  594. return $this->tags_propositions;
  595. }
  596. public function setTagsPRopositions($propositions)
  597. {
  598. $this->tags_propositions = $propositions;
  599. }
  600. public function getCountCommentReport()
  601. {
  602. return $this->count_comment_report;
  603. }
  604. public function setCountCommentReport($count)
  605. {
  606. $this->count_comment_report = $count;
  607. }
  608. /**
  609. * Etablie des relation vers des tags.
  610. * (Supprime les anciens tags, au niveau de l'objet seulement)
  611. *
  612. * @param array $ids
  613. */
  614. public function setTagsWithIds(EntityManager $em, $ids)
  615. {
  616. $this->tags = null;
  617. if (count($ids))
  618. {
  619. $tags = $em->getRepository('MuzichCoreBundle:Tag')->findByIds($ids)->execute();
  620. // Pour les nouveaux ids restants
  621. foreach ($tags as $tag)
  622. {
  623. $this->addTag($tag);
  624. }
  625. }
  626. }
  627. /**
  628. * Retourne le nombre de fois que cet élément a été msi en favoris
  629. *
  630. * @return int
  631. */
  632. public function getCountFavorite()
  633. {
  634. return count($this->elements_favorites);
  635. }
  636. public function setGroupToId()
  637. {
  638. $this->group = $this->group->getId();
  639. }
  640. // public function deleteTag(Tag $tag)
  641. // {
  642. // $this->tags->removeElement($tag);
  643. // }
  644. /**
  645. * Répond vrai si le tag transmis fait partis des tags de l'élément
  646. *
  647. * @param Tag $tag_t
  648. * @return boolean
  649. */
  650. public function hasTag(Tag $tag_t)
  651. {
  652. foreach ($this->getTags() as $tag)
  653. {
  654. if ($tag_t->getId() == $tag->getId())
  655. {
  656. return true;
  657. }
  658. }
  659. return false;
  660. }
  661. public function getPoints()
  662. {
  663. if ($this->points === null)
  664. {
  665. return '0';
  666. }
  667. return $this->points;
  668. }
  669. public function setPoints($points)
  670. {
  671. $this->points = $points;
  672. }
  673. public function getVoteGoodIds()
  674. {
  675. return json_decode($this->vote_good_ids, true);
  676. }
  677. public function setVoteGoodIds($votes_ids)
  678. {
  679. $this->vote_good_ids = json_encode($votes_ids);
  680. }
  681. /**
  682. * ajoute le vote de l'user_id aux votes good
  683. *
  684. * @param int $user_id
  685. */
  686. public function addVoteGood($user_id)
  687. {
  688. $votes = $this->getVoteGoodIds();
  689. if (!count($votes))
  690. {
  691. $votes = array();
  692. }
  693. if (!$this->hasVoteGood($user_id))
  694. {
  695. $votes[] = (string)$user_id;
  696. $this->setPoints(count($votes));
  697. }
  698. $this->setVoteGoodIds($votes);
  699. }
  700. /**
  701. * Retire le vote_good de l'user_id
  702. *
  703. * @param int $user_id
  704. */
  705. public function removeVoteGood($user_id)
  706. {
  707. if (count($votes = $this->getVoteGoodIds()))
  708. {
  709. $votes_n = array();
  710. foreach ($votes as $id)
  711. {
  712. if ($id != $user_id)
  713. {
  714. $votes_n[] = (string)$id;
  715. }
  716. }
  717. $this->setPoints(count($votes_n));
  718. $this->setVoteGoodIds($votes_n);
  719. }
  720. }
  721. /**
  722. * Répond vrai si l'user_id a déjà voté good.
  723. *
  724. * @param int $user_id
  725. * @return boolean
  726. */
  727. public function hasVoteGood($user_id)
  728. {
  729. if (count($votes = $this->getVoteGoodIds()))
  730. {
  731. foreach ($votes as $id)
  732. {
  733. if ($id == $user_id)
  734. {
  735. return true;
  736. }
  737. }
  738. }
  739. return false;
  740. }
  741. /**
  742. * Retourne vrai si l'utilisateur a demandé qa suivre les commentaires
  743. *
  744. * @param int $user_id identifiant de l'utilisateur
  745. * @return boolean
  746. */
  747. public function userFollowComments($user_id)
  748. {
  749. $cm = new CommentsManager($this->getComments());
  750. return $cm->userFollow($user_id);
  751. }
  752. /**
  753. *
  754. * @param Element $element
  755. */
  756. public function setParent(Element $element = null)
  757. {
  758. $this->parent = $element;
  759. }
  760. /**
  761. *
  762. * @return Element
  763. */
  764. public function getParent()
  765. {
  766. return $this->parent;
  767. }
  768. public function setChilds($elements)
  769. {
  770. $this->childs = $elements;
  771. }
  772. public function getChilds()
  773. {
  774. return $this->childs;
  775. }
  776. /**
  777. *
  778. * @param boolean $need
  779. */
  780. public function setNeedTags($need)
  781. {
  782. if ($need)
  783. {
  784. $this->need_tags = true;
  785. }
  786. else
  787. {
  788. $this->need_tags = false;
  789. }
  790. }
  791. /**
  792. *
  793. * @return boolean
  794. */
  795. public function getNeedTags()
  796. {
  797. if ($this->need_tags)
  798. {
  799. return true;
  800. }
  801. return false;
  802. }
  803. public function getProposedName()
  804. {
  805. if (($title = $this->getData(self::DATA_TITLE)))
  806. {
  807. if (($artist = $this->getData(self::DATA_ARTIST)))
  808. {
  809. $artist = ' - '.$artist;
  810. }
  811. return $title.$artist;
  812. }
  813. return null;
  814. }
  815. /**
  816. *
  817. * @return type
  818. */
  819. public function getProposedTags()
  820. {
  821. if (count($tags = $this->getData(self::DATA_TAGS)))
  822. {
  823. return $tags;
  824. }
  825. return array();
  826. }
  827. }