Element.php 17KB

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