Element.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. * Compteur de signalements
  135. *
  136. * @ORM\Column(type="integer", nullable=true)
  137. * @var int
  138. */
  139. protected $count_report;
  140. /**
  141. * array json des id users ayant reporté l'élément
  142. *
  143. * @ORM\Column(type="text", nullable=true)
  144. * @var string
  145. */
  146. protected $report_ids;
  147. /**
  148. * array json des id users ayant voté +1
  149. *
  150. * @ORM\Column(type="text", nullable=true)
  151. * @var string
  152. */
  153. protected $vote_good_ids;
  154. /**
  155. * Compteur de points
  156. *
  157. * @ORM\Column(type="integer", nullable=true)
  158. * @var int
  159. */
  160. protected $points;
  161. /**
  162. * Get id
  163. *
  164. * @return integer
  165. */
  166. public function getId()
  167. {
  168. return $this->id;
  169. }
  170. /**
  171. * Set url
  172. *
  173. * @param string $url
  174. */
  175. public function setUrl($url)
  176. {
  177. $this->url = $url;
  178. }
  179. /**
  180. * Get url
  181. *
  182. * @return string
  183. */
  184. public function getUrl()
  185. {
  186. return $this->url;
  187. }
  188. /**
  189. * Set name
  190. *
  191. * @param string $name
  192. */
  193. public function setName($name)
  194. {
  195. $this->name = $name;
  196. }
  197. /**
  198. * Get name
  199. *
  200. * @return string
  201. */
  202. public function getName()
  203. {
  204. return $this->name;
  205. }
  206. /**
  207. * Set type
  208. *
  209. * @param string $type
  210. */
  211. public function setType($type)
  212. {
  213. $this->type = $type;
  214. }
  215. /**
  216. * Get type
  217. *
  218. * @return string
  219. */
  220. public function getType()
  221. {
  222. return $this->type;
  223. }
  224. public function __construct($url = null)
  225. {
  226. //$this->tags = new ArrayCollection();
  227. $this->url = $url;
  228. }
  229. public function __toString()
  230. {
  231. return $this->name;
  232. }
  233. /**
  234. * Add tags
  235. *
  236. * @param Tag $tags
  237. */
  238. public function addTag(Tag $tags)
  239. {
  240. $this->tags[] = $tags;
  241. }
  242. /**
  243. * Get tags
  244. *
  245. * @return Doctrine\Common\Collections\Collection
  246. */
  247. public function getTags()
  248. {
  249. return $this->tags;
  250. }
  251. public function getTagsIdsJson()
  252. {
  253. $ids = array();
  254. if (count($this->getTags()))
  255. {
  256. foreach ($this->getTags() as $tag)
  257. {
  258. $ids[] = $tag->getId();
  259. }
  260. }
  261. return json_encode($ids);
  262. }
  263. public function setTags($tags)
  264. {
  265. $this->tags = $tags;
  266. }
  267. /**
  268. * Set owner
  269. *
  270. * @param User $owner
  271. */
  272. public function setOwner(User $owner)
  273. {
  274. $this->owner = $owner;
  275. }
  276. /**
  277. * Get owner
  278. *
  279. * @return User
  280. */
  281. public function getOwner()
  282. {
  283. return $this->owner;
  284. }
  285. /**
  286. * Add elements_favorites
  287. *
  288. * @param UsersElementsFavorites $elementsFavorites
  289. */
  290. public function addUsersElementsFavorites(UsersElementsFavorites $elementsFavorites)
  291. {
  292. $this->elements_favorites[] = $elementsFavorites;
  293. }
  294. /**
  295. * Get elements_favorites
  296. *
  297. * @return Doctrine\Common\Collections\Collection
  298. */
  299. public function getElementsFavorites()
  300. {
  301. return $this->elements_favorites;
  302. }
  303. /**
  304. * Set group
  305. *
  306. * @param Group $group
  307. */
  308. public function setGroup($group)
  309. {
  310. $this->group = $group;
  311. }
  312. /**
  313. * Get group
  314. *
  315. * @return Group
  316. */
  317. public function getGroup()
  318. {
  319. return $this->group;
  320. }
  321. /**
  322. * Set embed
  323. *
  324. * @param string $code
  325. */
  326. public function setEmbed($code)
  327. {
  328. $this->embed = $code;
  329. }
  330. /**
  331. * Get embed
  332. *
  333. * @return string
  334. */
  335. public function getEmbed()
  336. {
  337. return $this->embed;
  338. }
  339. /**
  340. * Set created
  341. *
  342. * @param date $created
  343. */
  344. public function setCreated($created)
  345. {
  346. $this->created = $created;
  347. }
  348. /**
  349. * Get created
  350. *
  351. * @return date
  352. */
  353. public function getCreated()
  354. {
  355. return $this->created;
  356. }
  357. /**
  358. * Set updated
  359. *
  360. * @param datetime $updated
  361. */
  362. public function setUpdated($updated)
  363. {
  364. $this->updated = $updated;
  365. }
  366. /**
  367. * Get updated
  368. *
  369. * @return datetime
  370. */
  371. public function getUpdated()
  372. {
  373. return $this->updated;
  374. }
  375. /**
  376. * Set thumbnail url
  377. *
  378. * @param string $thumbnail_url
  379. */
  380. public function setThumbnailUrl($thumbnail_url)
  381. {
  382. $this->thumbnail_url = $thumbnail_url;
  383. }
  384. /**
  385. * Get thumbnail url
  386. *
  387. * @return datetime
  388. */
  389. public function getThumbnailUrl()
  390. {
  391. return $this->thumbnail_url;
  392. }
  393. /**
  394. *
  395. * @return type array
  396. */
  397. public function getComments()
  398. {
  399. return json_decode($this->comments, true);
  400. }
  401. public function getCountReport()
  402. {
  403. return $this->count_report;
  404. }
  405. public function setCountReport($count)
  406. {
  407. $this->count_report = $count;
  408. }
  409. public function getReportIds()
  410. {
  411. return json_decode($this->report_ids, true);
  412. }
  413. public function setReportIds($report_ids)
  414. {
  415. $this->report_ids = json_encode($report_ids);
  416. }
  417. /**
  418. *
  419. * @param array $comments
  420. */
  421. public function setComments($comments)
  422. {
  423. $this->comments = json_encode($comments);
  424. }
  425. /**
  426. * Etablie des relation vers des tags.
  427. * (Supprime les anciens tags, au niveau de l'objet seulement)
  428. *
  429. * @param array $ids
  430. */
  431. public function setTagsWithIds(EntityManager $em, $ids)
  432. {
  433. $this->tags = null;
  434. if (count($ids))
  435. {
  436. $tags = $em->getRepository('MuzichCoreBundle:Tag')->findByIds($ids)->execute();
  437. // Pour les nouveaux ids restants
  438. foreach ($tags as $tag)
  439. {
  440. $this->addTag($tag);
  441. }
  442. }
  443. }
  444. public function getCountFavorite()
  445. {
  446. return count($this->elements_favorites);
  447. }
  448. public function setGroupToId()
  449. {
  450. $this->group = $this->group->getId();
  451. }
  452. // public function deleteTag(Tag $tag)
  453. // {
  454. // $this->tags->removeElement($tag);
  455. // }
  456. public function hasTag(Tag $tag_t)
  457. {
  458. foreach ($this->getTags() as $tag)
  459. {
  460. if ($tag_t->getId() == $tag->getId())
  461. {
  462. return true;
  463. }
  464. }
  465. return false;
  466. }
  467. public function getPoints()
  468. {
  469. if ($this->points === null)
  470. {
  471. return '0';
  472. }
  473. return $this->points;
  474. }
  475. public function setPoints($points)
  476. {
  477. $this->points = $points;
  478. }
  479. public function getVoteGoodIds()
  480. {
  481. return json_decode($this->vote_good_ids, true);
  482. }
  483. public function setVoteGoodIds($votes_ids)
  484. {
  485. $this->vote_good_ids = json_encode($votes_ids);
  486. }
  487. public function addVoteGood($user_id)
  488. {
  489. $votes = $this->getVoteGoodIds();
  490. if (!count($votes))
  491. {
  492. $votes = array();
  493. }
  494. if (!$this->hasVoteGood($user_id))
  495. {
  496. $votes[] = (string)$user_id;
  497. }
  498. $this->setPoints($this->getPoints()+1);
  499. $this->setVoteGoodIds($votes);
  500. }
  501. public function removeVoteGood($user_id)
  502. {
  503. if (count($votes = $this->getVoteGoodIds()))
  504. {
  505. $votes_n = array();
  506. foreach ($votes as $id)
  507. {
  508. if ($id != $user_id)
  509. {
  510. $votes_n[] = (string)$id;
  511. }
  512. }
  513. $this->setPoints($this->getPoints()-1);
  514. $this->setVoteGoodIds($votes_n);
  515. }
  516. }
  517. public function hasVoteGood($user_id)
  518. {
  519. if (count($votes = $this->getVoteGoodIds()))
  520. {
  521. foreach ($votes as $id)
  522. {
  523. if ($id == $user_id)
  524. {
  525. return true;
  526. }
  527. }
  528. }
  529. return false;
  530. }
  531. }