Element.php 17KB

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