Element.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. * @ORM\Id
  24. * @ORM\Column(type="integer")
  25. * @ORM\GeneratedValue(strategy="AUTO")
  26. * @var type int
  27. */
  28. protected $id;
  29. /**
  30. * Cet attribut stocke le type d'élément.
  31. *
  32. * @ORM\Column(type="string", length=64)
  33. * @Assert\NotBlank()
  34. * @Assert\MaxLength(1024)
  35. */
  36. protected $type;
  37. /**
  38. * Cet attribut stocke la liste des tags liés a cet élément.
  39. *
  40. * @ORM\ManyToMany(targetEntity="Tag", inversedBy="elements")
  41. * @ORM\JoinTable(name="elements_tag")
  42. * @MuzichAssert\Tags()
  43. */
  44. private $tags;
  45. /**
  46. * Propriétaire de l'élément
  47. *
  48. * @ORM\ManyToOne(targetEntity="User", inversedBy="elements")
  49. * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
  50. */
  51. protected $owner;
  52. /**
  53. * Groupe de l'élément
  54. *
  55. * @ORM\ManyToOne(targetEntity="Group", inversedBy="elements")
  56. * @ORM\JoinColumn(name="group_id", referencedColumnName="id")
  57. * @MuzichAssert\GroupOwnedOrPublic()
  58. */
  59. protected $group = null;
  60. /**
  61. * Cet attribu stocke les enregistrements UsersElementsFavorites liés
  62. * a ce Tag dans le cadre des Elements favoris.
  63. *
  64. * @ORM\OneToMany(targetEntity="UsersElementsFavorites", mappedBy="element")
  65. */
  66. protected $elements_favorites;
  67. /**
  68. * Propositions de tags
  69. *
  70. * @ORM\OneToMany(targetEntity="ElementTagsProposition", mappedBy="element")
  71. */
  72. protected $tags_propositions;
  73. /**
  74. * L'url est l'url du media.
  75. *
  76. * @ORM\Column(type="string", length=1024)
  77. * @Assert\NotBlank(message = "error.element.url.notblank")
  78. * @Assert\MaxLength(limit = 1024, message = "error.element.url.tolong")
  79. * @Assert\Url(message = "error.element.url.invalid")
  80. * @var type string
  81. */
  82. protected $url;
  83. /**
  84. * Libellé du media
  85. *
  86. * @ORM\Column(type = "string", length = 128)
  87. * @Assert\NotBlank(message = "error.element.name.notblank")
  88. * @Assert\MinLength(limit = 3, message = "error.element.name.toshort")
  89. * @Assert\MaxLength(limit = 64, message = "error.element.name.tolong")
  90. * @var type string
  91. */
  92. protected $name;
  93. /**
  94. * Code d'embed
  95. *
  96. * @ORM\Column(type="text", nullable=true)
  97. * @var type string
  98. */
  99. protected $embed;
  100. /**
  101. * @var datetime $created
  102. *
  103. * @Gedmo\Timestampable(on="create")
  104. * @ORM\Column(type="datetime")
  105. */
  106. private $created;
  107. /**
  108. * @var datetime $updated
  109. *
  110. * @ORM\Column(type="datetime")
  111. * @Gedmo\Timestampable(on="update")
  112. */
  113. private $updated;
  114. /**
  115. * @var string $thumbnail_url
  116. *
  117. * @ORM\Column(type="string", length=512, nullable=true)
  118. */
  119. protected $thumbnail_url;
  120. /**
  121. * Commentaires stocké au format json
  122. *
  123. * array(
  124. * array(
  125. * "u" => array( // Des infos sur l'utilisateur auteur du commentaire
  126. * "i" => "IdDuUser", // l'id
  127. * "s" => "LeSlugDuUser", // le slug
  128. * "n" => "NameDuUser" // le name
  129. * ),
  130. * "d" => "LaDate", // Date au format Y-m-d H:i:s
  131. * "c" => "Comment" // Le commentaire
  132. * ),
  133. * [...]
  134. * );
  135. *
  136. * @ORM\Column(type="text", nullable=true)
  137. * @var type string
  138. */
  139. protected $comments;
  140. /**
  141. * Compteur de signalements
  142. *
  143. * @ORM\Column(type="integer", nullable=true)
  144. * @var int
  145. */
  146. protected $count_report;
  147. /**
  148. * array json des id users ayant reporté l'élément
  149. *
  150. * @ORM\Column(type="text", nullable=true)
  151. * @var string
  152. */
  153. protected $report_ids;
  154. /**
  155. * array json des id users ayant voté +1
  156. *
  157. * @ORM\Column(type="text", nullable=true)
  158. * @var string
  159. */
  160. protected $vote_good_ids;
  161. /**
  162. * Compteur de points
  163. *
  164. * @ORM\Column(type="integer", nullable=true)
  165. * @var int
  166. */
  167. protected $points;
  168. /**
  169. * Get id
  170. *
  171. * @return integer
  172. */
  173. public function getId()
  174. {
  175. return $this->id;
  176. }
  177. /**
  178. * Set url
  179. *
  180. * @param string $url
  181. */
  182. public function setUrl($url)
  183. {
  184. $this->url = $url;
  185. }
  186. /**
  187. * Get url
  188. *
  189. * @return string
  190. */
  191. public function getUrl()
  192. {
  193. return $this->url;
  194. }
  195. /**
  196. * Set name
  197. *
  198. * @param string $name
  199. */
  200. public function setName($name)
  201. {
  202. $this->name = $name;
  203. }
  204. /**
  205. * Get name
  206. *
  207. * @return string
  208. */
  209. public function getName()
  210. {
  211. return $this->name;
  212. }
  213. /**
  214. * Set type
  215. *
  216. * @param string $type
  217. */
  218. public function setType($type)
  219. {
  220. $this->type = $type;
  221. }
  222. /**
  223. * Get type
  224. *
  225. * @return string
  226. */
  227. public function getType()
  228. {
  229. return $this->type;
  230. }
  231. public function __construct($url = null)
  232. {
  233. //$this->tags = new ArrayCollection();
  234. $this->url = $url;
  235. }
  236. public function __toString()
  237. {
  238. return $this->name;
  239. }
  240. /**
  241. * Add tags
  242. *
  243. * @param Tag $tags
  244. */
  245. public function addTag(Tag $tags)
  246. {
  247. $this->tags[] = $tags;
  248. }
  249. /**
  250. * Get tags
  251. *
  252. * @return Doctrine\Common\Collections\Collection
  253. */
  254. public function getTags()
  255. {
  256. return $this->tags;
  257. }
  258. public function getTagsIdsJson()
  259. {
  260. $ids = array();
  261. if (count($this->getTags()))
  262. {
  263. foreach ($this->getTags() as $tag)
  264. {
  265. $ids[] = $tag->getId();
  266. }
  267. }
  268. return json_encode($ids);
  269. }
  270. public function setTags($tags)
  271. {
  272. $this->tags = $tags;
  273. }
  274. /**
  275. * Set owner
  276. *
  277. * @param User $owner
  278. */
  279. public function setOwner(User $owner)
  280. {
  281. $this->owner = $owner;
  282. }
  283. /**
  284. * Get owner
  285. *
  286. * @return User
  287. */
  288. public function getOwner()
  289. {
  290. return $this->owner;
  291. }
  292. /**
  293. * Add elements_favorites
  294. *
  295. * @param UsersElementsFavorites $elementsFavorites
  296. */
  297. public function addUsersElementsFavorites(UsersElementsFavorites $elementsFavorites)
  298. {
  299. $this->elements_favorites[] = $elementsFavorites;
  300. }
  301. /**
  302. * Get elements_favorites
  303. *
  304. * @return Doctrine\Common\Collections\Collection
  305. */
  306. public function getElementsFavorites()
  307. {
  308. return $this->elements_favorites;
  309. }
  310. /**
  311. * Set group
  312. *
  313. * @param Group $group
  314. */
  315. public function setGroup($group)
  316. {
  317. $this->group = $group;
  318. }
  319. /**
  320. * Get group
  321. *
  322. * @return Group
  323. */
  324. public function getGroup()
  325. {
  326. return $this->group;
  327. }
  328. /**
  329. * Set embed
  330. *
  331. * @param string $code
  332. */
  333. public function setEmbed($code)
  334. {
  335. $this->embed = $code;
  336. }
  337. /**
  338. * Get embed
  339. *
  340. * @return string
  341. */
  342. public function getEmbed()
  343. {
  344. return $this->embed;
  345. }
  346. /**
  347. * Set created
  348. *
  349. * @param date $created
  350. */
  351. public function setCreated($created)
  352. {
  353. $this->created = $created;
  354. }
  355. /**
  356. * Get created
  357. *
  358. * @return date
  359. */
  360. public function getCreated()
  361. {
  362. return $this->created;
  363. }
  364. /**
  365. * Set updated
  366. *
  367. * @param datetime $updated
  368. */
  369. public function setUpdated($updated)
  370. {
  371. $this->updated = $updated;
  372. }
  373. /**
  374. * Get updated
  375. *
  376. * @return datetime
  377. */
  378. public function getUpdated()
  379. {
  380. return $this->updated;
  381. }
  382. /**
  383. * Set thumbnail url
  384. *
  385. * @param string $thumbnail_url
  386. */
  387. public function setThumbnailUrl($thumbnail_url)
  388. {
  389. $this->thumbnail_url = $thumbnail_url;
  390. }
  391. /**
  392. * Get thumbnail url
  393. *
  394. * @return datetime
  395. */
  396. public function getThumbnailUrl()
  397. {
  398. return $this->thumbnail_url;
  399. }
  400. /**
  401. *
  402. * @return type array
  403. */
  404. public function getComments()
  405. {
  406. return json_decode($this->comments, true);
  407. }
  408. public function getCountReport()
  409. {
  410. return $this->count_report;
  411. }
  412. public function setCountReport($count)
  413. {
  414. $this->count_report = $count;
  415. }
  416. public function getReportIds()
  417. {
  418. return json_decode($this->report_ids, true);
  419. }
  420. public function setReportIds($report_ids)
  421. {
  422. $this->report_ids = json_encode($report_ids);
  423. }
  424. /**
  425. *
  426. * @param array $comments
  427. */
  428. public function setComments($comments)
  429. {
  430. $this->comments = json_encode($comments);
  431. }
  432. /**
  433. * Etablie des relation vers des tags.
  434. * (Supprime les anciens tags, au niveau de l'objet seulement)
  435. *
  436. * @param array $ids
  437. */
  438. public function setTagsWithIds(EntityManager $em, $ids)
  439. {
  440. $this->tags = null;
  441. if (count($ids))
  442. {
  443. $tags = $em->getRepository('MuzichCoreBundle:Tag')->findByIds($ids)->execute();
  444. // Pour les nouveaux ids restants
  445. foreach ($tags as $tag)
  446. {
  447. $this->addTag($tag);
  448. }
  449. }
  450. }
  451. /**
  452. * Retourne le nombre de fois que cet élément a été msi en favoris
  453. *
  454. * @return int
  455. */
  456. public function getCountFavorite()
  457. {
  458. return count($this->elements_favorites);
  459. }
  460. public function setGroupToId()
  461. {
  462. $this->group = $this->group->getId();
  463. }
  464. // public function deleteTag(Tag $tag)
  465. // {
  466. // $this->tags->removeElement($tag);
  467. // }
  468. /**
  469. * Répond vrai si le tag transmis fait partis des tags de l'élément
  470. *
  471. * @param Tag $tag_t
  472. * @return boolean
  473. */
  474. public function hasTag(Tag $tag_t)
  475. {
  476. foreach ($this->getTags() as $tag)
  477. {
  478. if ($tag_t->getId() == $tag->getId())
  479. {
  480. return true;
  481. }
  482. }
  483. return false;
  484. }
  485. public function getPoints()
  486. {
  487. if ($this->points === null)
  488. {
  489. return '0';
  490. }
  491. return $this->points;
  492. }
  493. public function setPoints($points)
  494. {
  495. $this->points = $points;
  496. }
  497. public function getVoteGoodIds()
  498. {
  499. return json_decode($this->vote_good_ids, true);
  500. }
  501. public function setVoteGoodIds($votes_ids)
  502. {
  503. $this->vote_good_ids = json_encode($votes_ids);
  504. }
  505. /**
  506. * ajoute le vote de l'user_id aux votes good
  507. *
  508. * @param int $user_id
  509. */
  510. public function addVoteGood($user_id)
  511. {
  512. $votes = $this->getVoteGoodIds();
  513. if (!count($votes))
  514. {
  515. $votes = array();
  516. }
  517. if (!$this->hasVoteGood($user_id))
  518. {
  519. $votes[] = (string)$user_id;
  520. $this->setPoints(count($votes));
  521. }
  522. $this->setVoteGoodIds($votes);
  523. }
  524. /**
  525. * Retire le vote_good de l'user_id
  526. *
  527. * @param int $user_id
  528. */
  529. public function removeVoteGood($user_id)
  530. {
  531. if (count($votes = $this->getVoteGoodIds()))
  532. {
  533. $votes_n = array();
  534. foreach ($votes as $id)
  535. {
  536. if ($id != $user_id)
  537. {
  538. $votes_n[] = (string)$id;
  539. }
  540. }
  541. $this->setPoints(count($votes_n));
  542. $this->setVoteGoodIds($votes_n);
  543. }
  544. }
  545. /**
  546. * Répond vrai si l'user_id a déjà voté good.
  547. *
  548. * @param int $user_id
  549. * @return boolean
  550. */
  551. public function hasVoteGood($user_id)
  552. {
  553. if (count($votes = $this->getVoteGoodIds()))
  554. {
  555. foreach ($votes as $id)
  556. {
  557. if ($id == $user_id)
  558. {
  559. return true;
  560. }
  561. }
  562. }
  563. return false;
  564. }
  565. /**
  566. * Retourne vrai si l'utilisateur a demandé qa suivre les commentaires
  567. *
  568. * @param int $user_id identifiant de l'utilisateur
  569. * @return boolean
  570. */
  571. public function userFollowComments($user_id)
  572. {
  573. $cm = new CommentsManager($this->getComments());
  574. return $cm->userFollow($user_id);
  575. }
  576. }