User.php 21KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  1. <?php
  2. namespace Muzich\CoreBundle\Entity;
  3. use FOS\UserBundle\Entity\User as BaseUser;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use \Doctrine\Common\Collections\ArrayCollection;
  6. use Gedmo\Mapping\Annotation as Gedmo;
  7. use Doctrine\ORM\EntityManager;
  8. use Muzich\CoreBundle\Entity\UsersTagsFavorites;
  9. use Symfony\Component\Validator\Constraints as Assert;
  10. use Muzich\CoreBundle\Validator as MuzichAssert;
  11. use Muzich\CoreBundle\Entity\ElementTagsProposition;
  12. use Muzich\CoreBundle\Entity\Tag;
  13. /**
  14. * Cet entité est l'utilisateur ayant effectué la requête.
  15. *
  16. * @ORM\Entity
  17. * @ORM\Table(name="m_user")
  18. * @ORM\Entity(repositoryClass="Muzich\CoreBundle\Repository\UserRepository")
  19. * @ORM\HasLifecycleCallbacks()
  20. */
  21. class User extends BaseUser
  22. {
  23. /**
  24. * Data ordre des tags de sa page favoris
  25. * @var string
  26. */
  27. const DATA_TAGS_ORDER_PAGE_FAV = "data_tags_order_page_fav";
  28. /**
  29. * Data ordre des tags de ses diffusions
  30. * @var string
  31. */
  32. const DATA_TAGS_ORDER_DIFF = "data_tags_order_diff";
  33. /**
  34. * Data, les favoris ont ils été modifiés
  35. * @var string
  36. */
  37. const DATA_FAV_UPDATED = "data_fav_updated";
  38. /**
  39. * Data, les favoris ont ils été modifiés
  40. * @var string
  41. */
  42. const DATA_DIFF_UPDATED = "data_diff_updated";
  43. /**
  44. * @ORM\Id
  45. * @ORM\Column(type="integer")
  46. * @ORM\generatedValue(strategy="AUTO")
  47. */
  48. protected $id;
  49. /**
  50. * @Gedmo\Slug(fields={"username"})
  51. * @ORM\Column(length=128, unique=true)
  52. */
  53. protected $slug;
  54. /**
  55. * @ORM\Column(type="text", nullable=true)
  56. * @ORM\Column(length=256)
  57. */
  58. protected $email_requested;
  59. /**
  60. * @ORM\Column(type="integer", nullable=true)
  61. */
  62. protected $email_requested_datetime;
  63. /**
  64. * Cet attribut contient les enregistrements UsersTagsFavorites lié
  65. * a cet utilisateur dans le cadre des Tags Favoris.
  66. *
  67. * @ORM\OneToMany(targetEntity="UsersTagsFavorites", mappedBy="user")
  68. */
  69. protected $tags_favorites;
  70. /**
  71. * Cet attribut contient les enregistrements UsersElementsFavorites lié
  72. * a cet utilisateur dans le cadre des éléments Favoris.
  73. *
  74. * @ORM\OneToMany(targetEntity="UsersElementsFavorites", mappedBy="user")
  75. */
  76. protected $elements_favorites;
  77. /**
  78. * Liste des Elements appartenant a cet utilisateur.
  79. *
  80. * @ORM\OneToMany(targetEntity="Element", mappedBy="owner")
  81. */
  82. protected $elements;
  83. /**
  84. * Liste des propositions de tags effectués par cet utilisateur
  85. *
  86. * @ORM\OneToMany(targetEntity="ElementTagsProposition", mappedBy="user")
  87. */
  88. protected $element_tags_propositions;
  89. /**
  90. * Users que cet utilisateur suit.
  91. *
  92. * @ORM\OneToMany(targetEntity="FollowUser", mappedBy="follower")
  93. */
  94. protected $followeds_users;
  95. /**
  96. * Users qui suivent cet utilisateur.
  97. *
  98. * @ORM\OneToMany(targetEntity="FollowUser", mappedBy="followed")
  99. */
  100. protected $followers_users;
  101. /**
  102. * Cet attribut contient les enregistrements FollowGroup lié
  103. * a cet utilisateur dans le cadre des groupes suivis.
  104. *
  105. * @ORM\OneToMany(targetEntity="FollowGroup", mappedBy="follower")
  106. */
  107. protected $followed_groups;
  108. /**
  109. * Liste des Groupes appartenant a cet utilisateur.
  110. *
  111. * @ORM\OneToMany(targetEntity="Group", mappedBy="owner")
  112. */
  113. protected $groups_owned;
  114. /**
  115. * Compteur de signalements inutiles
  116. *
  117. * @ORM\Column(type="integer", nullable=true)
  118. * @var int
  119. */
  120. protected $bad_report_count;
  121. /**
  122. * Compteur de contenus refusés par la modération
  123. *
  124. * @ORM\Column(type="integer", nullable=true)
  125. * @var int
  126. */
  127. protected $moderated_element_count;
  128. /**
  129. * Compteur de contenus refusés par la modération
  130. *
  131. * @ORM\Column(type="integer", nullable=true)
  132. * @var int
  133. */
  134. protected $moderated_tag_count;
  135. /**
  136. * Compteur de contenus refusés par la modération
  137. *
  138. * @ORM\Column(type="integer", nullable=true)
  139. * @var int
  140. */
  141. protected $moderated_comment_count;
  142. /**
  143. * @ORM\Column(type="text", nullable=true)
  144. * @ORM\Column(length=256)
  145. */
  146. protected $town;
  147. /**
  148. * @ORM\Column(type="text", nullable=true)
  149. * @ORM\Column(length=128)
  150. */
  151. protected $country;
  152. /**
  153. * Reputation
  154. *
  155. * @ORM\Column(type="integer", nullable=true)
  156. * @var int
  157. */
  158. protected $reputation;
  159. /**
  160. * Liste des Events appartenant a cet utilisateur.
  161. *
  162. * @ORM\OneToMany(targetEntity="Event", mappedBy="user")
  163. */
  164. protected $events;
  165. /**
  166. * Contient des données pratique pour par exemple influencer l'affichange dans twig.
  167. *
  168. * @var array
  169. */
  170. protected $live_datas = array();
  171. /**
  172. * Contient des données pratique, comme l'ordre des tags de sa page favoris etc.
  173. *
  174. * @ORM\Column(type="text", nullable=true)
  175. * @var type string
  176. */
  177. protected $datas = null;
  178. /**
  179. * Tableau contenant les id => name des tags favoris
  180. * de l'user. Ces donnée sont faites pour optimiser les calculs.
  181. * Ce chamsp est mis ajour a chaque fois qu'un UsersTagsFavorite est manipulé.
  182. *
  183. * @ORM\Column(type="text", unique=false, nullable=true)
  184. * @var array
  185. */
  186. private $tags_favorites_quick;
  187. /**
  188. * @Assert\Image(maxSize="6000000")
  189. */
  190. public $avatar;
  191. /**
  192. * @ORM\Column(type="text", length=255, nullable=true)
  193. */
  194. public $avatar_path;
  195. /**
  196. * @ORM\Column(type="text", unique=false, nullable=true)
  197. * @var array
  198. */
  199. private $help_tour;
  200. /**
  201. * @ORM\Column(type="boolean")
  202. * @Assert\NotBlank()
  203. * @var type boolean
  204. */
  205. public $cgu_accepted = false;
  206. /**
  207. * @ORM\Column(type="boolean")
  208. * @var type boolean
  209. */
  210. public $mail_newsletter = true;
  211. /**
  212. * @ORM\Column(type="boolean")
  213. * @var type boolean
  214. */
  215. public $mail_partner = true;
  216. /**
  217. *
  218. */
  219. public function __construct()
  220. {
  221. $this->tags_favorites = new ArrayCollection();
  222. $this->elements = new ArrayCollection();
  223. $this->elements_favorites = new ArrayCollection();
  224. $this->followeds_users = new ArrayCollection();
  225. $this->followers_users = new ArrayCollection();
  226. $this->followed_groups = new ArrayCollection();
  227. $this->groups = new ArrayCollection();
  228. $this->groups_owned = new ArrayCollection();
  229. $this->help_tour = json_encode(array(
  230. 'home' => true,
  231. ));
  232. parent::__construct();
  233. }
  234. public function __toString()
  235. {
  236. return $this->getName();
  237. }
  238. /**
  239. * Get id
  240. *
  241. * @return integer
  242. */
  243. public function getId()
  244. {
  245. return $this->id;
  246. }
  247. /**
  248. * Get tags_favorites
  249. *
  250. * @return Doctrine\Common\Collections\Collection
  251. */
  252. public function getTagsFavorites()
  253. {
  254. return $this->tags_favorites;
  255. }
  256. /**
  257. * Add tags_favorites
  258. *
  259. * @param UsersTagsFavorites $tagsFavorites
  260. */
  261. public function addUsersTagsFavorites(UsersTagsFavorites $tagsFavorites)
  262. {
  263. $this->tags_favorites[] = $tagsFavorites;
  264. }
  265. /**
  266. * Add elements_favorites
  267. *
  268. * @param UsersElementsFavorites $elementsFavorites
  269. */
  270. public function addUsersElementsFavorites(UsersElementsFavorites $elementsFavorites)
  271. {
  272. $this->elements_favorites[] = $elementsFavorites;
  273. }
  274. /**
  275. * Get elements_favorites
  276. *
  277. * @return Doctrine\Common\Collections\Collection
  278. */
  279. public function getElementsFavorites()
  280. {
  281. return $this->elements_favorites;
  282. }
  283. /**
  284. * Add elements
  285. *
  286. * @param Element $elements
  287. */
  288. public function addElement(Element $elements)
  289. {
  290. $this->elements[] = $elements;
  291. }
  292. /**
  293. * Get elements
  294. *
  295. * @return Doctrine\Common\Collections\Collection
  296. */
  297. public function getElements()
  298. {
  299. return $this->elements;
  300. }
  301. /**
  302. * Add elements
  303. *
  304. * @param Element $elements
  305. */
  306. public function addElementTagsProposition(ElementTagsProposition $proposition)
  307. {
  308. $this->element_tags_propositions[] = $proposition;
  309. }
  310. /**
  311. * Get elements
  312. *
  313. * @return Doctrine\Common\Collections\Collection
  314. */
  315. public function getElementTagsPropositions()
  316. {
  317. return $this->element_tags_propositions;
  318. }
  319. /**
  320. * Add followeds_users
  321. *
  322. * @param FollowUser $followedsUsers
  323. */
  324. public function addFollowUser(FollowUser $followedsUsers)
  325. {
  326. $this->followeds_users[] = $followedsUsers;
  327. }
  328. /**
  329. * Get followeds_users
  330. *
  331. * @return Doctrine\Common\Collections\Collection
  332. */
  333. public function getFollowedsUsers()
  334. {
  335. $users = array();
  336. foreach ($this->followeds_users as $follow_user)
  337. {
  338. $users[] = $follow_user->getFollowed();
  339. }
  340. return $users;
  341. }
  342. /**
  343. * Get followers_users
  344. *
  345. * @return Doctrine\Common\Collections\Collection
  346. */
  347. public function getFollowersUsers()
  348. {
  349. $users = array();
  350. foreach ($this->followers_users as $follow_user)
  351. {
  352. $users[] = $follow_user->getFollower();
  353. }
  354. return $users;
  355. }
  356. /**
  357. * Add followed_groups
  358. *
  359. * @param FollowGroup $followedGroups
  360. */
  361. public function addFollowGroup(FollowGroup $followedGroups)
  362. {
  363. $this->followed_groups[] = $followedGroups;
  364. }
  365. /**
  366. * Get followed_groups
  367. *
  368. * @return Doctrine\Common\Collections\Collection
  369. */
  370. public function getFollowedGroups()
  371. {
  372. $groups = array();
  373. foreach ($this->followed_groups as $follow_group)
  374. {
  375. $groups[] = $follow_group->getGroup();
  376. }
  377. return $groups;
  378. }
  379. /**
  380. * Add groups
  381. *
  382. * @param Group $groups
  383. */
  384. public function addGroupOwned(Group $groups)
  385. {
  386. $this->groups[] = $groups;
  387. }
  388. /**
  389. * Get groups
  390. *
  391. * @return Doctrine\Common\Collections\Collection
  392. */
  393. public function getGroupsOwned()
  394. {
  395. return $this->groups_owned;
  396. }
  397. /**
  398. * Get groups in array (id => name)
  399. *
  400. * @return Doctrine\Common\Collections\Collection
  401. */
  402. public function getGroupsOwnedArray()
  403. {
  404. $groups = array();
  405. foreach ($this->groups_owned as $group)
  406. {
  407. $groups[$group->getId()] = $group->getName();
  408. }
  409. return $groups;
  410. }
  411. public function getSlug()
  412. {
  413. return $this->slug;
  414. }
  415. public function setSlug($slug)
  416. {
  417. $this->slug = $slug;
  418. }
  419. public function getEmailRequested()
  420. {
  421. return $this->email_requested;
  422. }
  423. public function setEmailRequested($email_requested)
  424. {
  425. $this->email_requested = $email_requested;
  426. }
  427. public function getBadReportCount()
  428. {
  429. return $this->bad_report_count;
  430. }
  431. public function setBadReportCount($count)
  432. {
  433. $this->bad_report_count = $count;
  434. }
  435. public function addBadReport()
  436. {
  437. $this->setBadReportCount($this->getBadReportCount()+1);
  438. }
  439. public function getTown()
  440. {
  441. return $this->town;
  442. }
  443. public function setTown($town)
  444. {
  445. $this->town = $town;
  446. }
  447. public function getCountry()
  448. {
  449. return $this->country;
  450. }
  451. public function setCountry($country)
  452. {
  453. $this->country = $country;
  454. }
  455. public function setReputation($reputation)
  456. {
  457. $this->reputation = $reputation;
  458. }
  459. public function getReputation()
  460. {
  461. if ($this->reputation === null)
  462. {
  463. return 0;
  464. }
  465. return $this->reputation;
  466. }
  467. public function getEvents()
  468. {
  469. return $this->events;
  470. }
  471. public function setEvents($events)
  472. {
  473. $this->events = $events;
  474. }
  475. public function getModeratedElementCount()
  476. {
  477. if ($this->moderated_element_count === null)
  478. {
  479. return 0;
  480. }
  481. return $this->moderated_element_count;
  482. }
  483. public function setModeratedElementCount($count)
  484. {
  485. $this->moderated_element_count = $count;
  486. }
  487. public function addModeratedElementCount()
  488. {
  489. $this->setModeratedElementCount($this->getModeratedElementCount()+1);
  490. }
  491. public function getModeratedTagCount()
  492. {
  493. if ($this->moderated_tag_count === null)
  494. {
  495. return 0;
  496. }
  497. return $this->moderated_tag_count;
  498. }
  499. public function setModeratedTagCount($count)
  500. {
  501. $this->moderated_tag_count = $count;
  502. }
  503. public function addModeratedTagCount()
  504. {
  505. $this->setModeratedTagCount($this->getModeratedTagCount()+1);
  506. }
  507. public function getModeratedCommentCount()
  508. {
  509. if ($this->moderated_comment_count === null)
  510. {
  511. return 0;
  512. }
  513. return $this->moderated_comment_count;
  514. }
  515. public function setModeratedCommentCount($count)
  516. {
  517. $this->moderated_comment_count = $count;
  518. }
  519. public function addModeratedCommentCount()
  520. {
  521. $this->setModeratedCommentCount($this->getModeratedCommentCount()+1);
  522. }
  523. /*
  524. *
  525. *
  526. */
  527. public function getName()
  528. {
  529. return $this->getUsername();
  530. }
  531. // public function getLocalisationExploded()
  532. // {
  533. // $town = null;
  534. // $country = null;
  535. // if ($this->localisation)
  536. // {
  537. // if (($explode = explode(', ', $this->localisation)))
  538. // {
  539. // $town = $explode[0];
  540. // $country = $explode[1];
  541. // }
  542. // }
  543. // return array(
  544. // 'town' => $town,
  545. // 'country' => $country
  546. // );
  547. // }
  548. //
  549. // public function setLocalisationExploded($town, $country)
  550. // {
  551. // $town = str_replace(', ', '', $town);
  552. // $town = str_replace(',', '', $town);
  553. // $country = str_replace(', ', '', $country);
  554. // $country = str_replace(',', '', $country);
  555. //
  556. // $this->localisation = $town. ', ' .$country;
  557. // }
  558. // /**
  559. // * @ORM\PrePersist
  560. // */
  561. // public function setSlug()
  562. // {
  563. // if (!$this->slug)
  564. // {
  565. //
  566. // }
  567. // }
  568. //
  569. /**
  570. * Retourn si l'user_id transmis fait partis des enregistrements
  571. * followed de l'objet.
  572. *
  573. * @param int $user_id
  574. * @return boolean
  575. */
  576. public function isFollowingUser($user_id)
  577. {
  578. foreach ($this->followeds_users as $followed_user)
  579. {
  580. if ($followed_user->getFollowed()->getId() == $user_id)
  581. {
  582. return true;
  583. }
  584. }
  585. return false;
  586. }
  587. /**
  588. * Retourn si l'user_id transmis est l'un des User suivis
  589. *
  590. * @param Doctrine\Bundle\DoctrineBundle\Registry doctrine
  591. * @param int $user_id
  592. * @return boolean
  593. */
  594. public function isFollowingUserByQuery($doctrine, $user_id)
  595. {
  596. return $doctrine
  597. ->getRepository('MuzichCoreBundle:User')
  598. ->isFollowingUser($this->getId(), $user_id)
  599. ;
  600. }
  601. /**
  602. * Retourn si l'group_id transmis est l'un des groupe suivis
  603. *
  604. * @param Doctrine\Bundle\DoctrineBundle\Registry doctrine
  605. * @param int $user_id
  606. * @return boolean
  607. */
  608. public function isFollowingGroupByQuery($doctrine, $group_id)
  609. {
  610. return $doctrine
  611. ->getRepository('MuzichCoreBundle:User')
  612. ->isFollowingGroup($this->getId(), $group_id)
  613. ;
  614. }
  615. public function getPersonalHash($salt_context = null)
  616. {
  617. return hash('sha256', $this->getSalt().$this->getUsername().$salt_context);
  618. }
  619. /**
  620. * Ajoute a l'user les tags transmis (id) comme favoris.
  621. *
  622. * @param EntityManager $em
  623. * @param array $ids
  624. */
  625. public function updateTagsFavoritesById(EntityManager $em, $ids)
  626. {
  627. $ids = json_decode($ids);
  628. $ids_to_add = $ids;
  629. // Pour chacun des tags favoris
  630. foreach ($this->tags_favorites as $ii => $tag_favorite)
  631. {
  632. $trouve = false;
  633. foreach ($ids as $i => $id)
  634. {
  635. if ($id == $tag_favorite->getTag()->getId())
  636. {
  637. $trouve = true;
  638. // Si le tag était favoris déjà avant (et aussi maintenant)
  639. // il ne sera ni a ajouter, ni a supprimer.
  640. unset($ids_to_add[$i]);
  641. }
  642. }
  643. if (!$trouve)
  644. {
  645. // Si cet ancien tag n'est plus dans la liste, il faut le supprimer
  646. // (rappel: on supprime ici la relation, pas le tag)
  647. $em->remove($tag_favorite);
  648. }
  649. }
  650. if (count($ids_to_add))
  651. {
  652. $ids_to_add = array_merge($ids_to_add);
  653. $tag_favorite_position_max = $this->getTagFavoritePositionMax();
  654. $tags = $em->getRepository('MuzichCoreBundle:Tag')->findByIds($ids_to_add)->execute();
  655. // Pour les nouveaux ids restants
  656. foreach ($tags as $tag)
  657. {
  658. $tag_favorite = new UsersTagsFavorites();
  659. $tag_favorite->setUser($this);
  660. $tag_favorite->setTag($tag);
  661. $tag_favorite->setPosition($tag_favorite_position_max);
  662. $tag_favorite_position_max++;
  663. $this->addUsersTagsFavorites($tag_favorite);
  664. $em->persist($tag_favorite);
  665. }
  666. }
  667. $em->flush();
  668. }
  669. /**
  670. * Retourne un tableau contenant les ids des tags préférés de l'user
  671. *
  672. * @return type array
  673. */
  674. public function getTagFavoriteIds()
  675. {
  676. $ids = array();
  677. foreach ($this->tags_favorites as $tag_favorite)
  678. {
  679. $ids[$tag_favorite->getTag()->getId()] = $tag_favorite->getTag()->getId();
  680. }
  681. return $ids;
  682. }
  683. /**
  684. * Retourne la position max des tag favoris.
  685. *
  686. * @return int
  687. */
  688. public function getTagFavoritePositionMax()
  689. {
  690. $max = 0;
  691. foreach ($this->tags_favorites as $tag_favorite)
  692. {
  693. if ($tag_favorite->getPosition() > $max)
  694. {
  695. $max = $tag_favorite->getPosition();
  696. }
  697. }
  698. return $max;
  699. }
  700. /**
  701. * Set email_requested_datetime
  702. *
  703. * @param integer $emailRequestedDatetime
  704. */
  705. public function setEmailRequestedDatetime($emailRequestedDatetime)
  706. {
  707. $this->email_requested_datetime = $emailRequestedDatetime;
  708. }
  709. /**
  710. * Get email_requested_datetime
  711. *
  712. * @return integer
  713. */
  714. public function getEmailRequestedDatetime()
  715. {
  716. return $this->email_requested_datetime;
  717. }
  718. public function addLiveData($id, $data)
  719. {
  720. $this->live_datas[$id] = $data;
  721. }
  722. public function removeLiveData($id)
  723. {
  724. if (array_key_exists($id, $this->live_datas))
  725. {
  726. unset($this->live_datas[$id]);
  727. }
  728. }
  729. public function hasLiveData($id, $data = null)
  730. {
  731. if (array_key_exists($id, $this->live_datas))
  732. {
  733. if ($this->live_datas[$id] == $data)
  734. {
  735. return true;
  736. }
  737. }
  738. return false;
  739. }
  740. public function getTagsFavoritesQuick()
  741. {
  742. if ($this->tags_favorites_quick == null)
  743. {
  744. return array();
  745. }
  746. return json_decode($this->tags_favorites_quick, true);
  747. }
  748. /**
  749. *
  750. * @param array $tags_favorites_quick (id => name)
  751. */
  752. public function setTagsFavoritesQuick($tags_favorites_quick)
  753. {
  754. $this->tags_favorites_quick = json_encode($tags_favorites_quick);
  755. }
  756. /**
  757. *
  758. * @param \Muzich\CoreBundle\Entity\Tag $tag
  759. */
  760. public function addTagFavoriteQuick(Tag $tag)
  761. {
  762. $tags_favorites_quick = $this->getTagsFavoritesQuick();
  763. if (!array_key_exists($tag->getId(), $tags_favorites_quick))
  764. {
  765. $tags_favorites_quick[$tag->getId()] = $tag->getName();
  766. }
  767. $this->setTagsFavoritesQuick($tags_favorites_quick);
  768. }
  769. /**
  770. *
  771. * @param \Muzich\CoreBundle\Entity\Tag $tag
  772. */
  773. public function removeTagFavoriteQuick(Tag $tag)
  774. {
  775. $tags_favorites_quick = $this->getTagsFavoritesQuick();
  776. if (array_key_exists($tag->getId(), $tags_favorites_quick))
  777. {
  778. unset($tags_favorites_quick[$tag->getId()]);
  779. }
  780. $this->setTagsFavoritesQuick($tags_favorites_quick);
  781. }
  782. /**
  783. * Retourne vrai si le tag_id transmis fait partis des tags favoris de
  784. * l'utilisateur
  785. *
  786. * @param int $tag_id
  787. * @return boolean
  788. */
  789. public function haveTagsFavorite($tag_id)
  790. {
  791. $tags_favorites_quick = $this->getTagsFavoritesQuick();
  792. if (array_key_exists($tag_id, $tags_favorites_quick))
  793. {
  794. return true;
  795. }
  796. return false;
  797. }
  798. /**
  799. *
  800. * @return type array
  801. */
  802. public function getDatas()
  803. {
  804. if ($this->datas === null)
  805. {
  806. return array();
  807. }
  808. return json_decode($this->datas, true);
  809. }
  810. /**
  811. *
  812. * @param string $data_id
  813. * @param ~ $default
  814. * @return all
  815. */
  816. public function getData($data_id, $default)
  817. {
  818. $datas = $this->getDatas();
  819. if (array_key_exists($data_id, $datas))
  820. {
  821. return $datas[$data_id];
  822. }
  823. return $default;
  824. }
  825. /**
  826. *
  827. * @param array $datas
  828. */
  829. public function setDatas($datas)
  830. {
  831. $this->datas = json_encode($datas);
  832. }
  833. /**
  834. *
  835. * @param string $data_id
  836. * @param all $data_value
  837. */
  838. public function setData($data_id, $data_value)
  839. {
  840. $datas = $this->getDatas();
  841. $datas[$data_id] = $data_value;
  842. $this->setDatas($datas);
  843. }
  844. public function getAvatarAbsolutePath()
  845. {
  846. return null === $this->avatar_path
  847. ? null
  848. : $this->getAvatarUploadRootDir().'/'.$this->avatar_path;
  849. }
  850. public function getAvatarWebPath()
  851. {
  852. return null === $this->avatar_path
  853. ? null
  854. : $this->getAvatarUploadDir().'/'.$this->avatar_path;
  855. }
  856. protected function getAvatarUploadRootDir()
  857. {
  858. return __DIR__.'/../../../../web/'.$this->getAvatarUploadDir();
  859. }
  860. protected function getAvatarUploadDir()
  861. {
  862. return 'files/avatars';
  863. }
  864. /**
  865. * @ORM\PrePersist()
  866. * @ORM\PreUpdate()
  867. */
  868. public function preUploadAvatar()
  869. {
  870. if (null !== $this->avatar) {
  871. $this->avatar_path = $this->getPersonalHash($this->avatar->getClientOriginalName()).'.'.$this->avatar->guessExtension();
  872. }
  873. }
  874. /**
  875. * @ORM\PostPersist()
  876. * @ORM\PostUpdate()
  877. */
  878. public function uploadAvatar()
  879. {
  880. if (null === $this->avatar) {
  881. return;
  882. }
  883. $this->avatar->move($this->getAvatarUploadRootDir(), $this->avatar_path);
  884. $this->avatar = null;
  885. }
  886. /**
  887. * @ORM\PostRemove()
  888. */
  889. public function removeUpload()
  890. {
  891. if ($file = $this->getAvatarAbsolutePath()) {
  892. unlink($file);
  893. }
  894. }
  895. }