User.php 27KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  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 \Doctrine\Common\Collections\Collection;
  7. use Gedmo\Mapping\Annotation as Gedmo;
  8. use Doctrine\ORM\EntityManager;
  9. use Muzich\CoreBundle\Entity\UsersTagsFavorites;
  10. use Symfony\Component\Validator\Constraints as Assert;
  11. use Muzich\CoreBundle\Entity\ElementTagsProposition;
  12. use Muzich\CoreBundle\Entity\Tag;
  13. use Muzich\CoreBundle\Managers\UserPrivacy as PrivacyManager;
  14. /**
  15. * Cet entité est l'utilisateur ayant effectué la requête.
  16. *
  17. * @ORM\Entity
  18. * @ORM\Table(name="m_user")
  19. * @ORM\Entity(repositoryClass="Muzich\CoreBundle\Repository\UserRepository")
  20. * @ORM\HasLifecycleCallbacks()
  21. */
  22. class User extends BaseUser
  23. {
  24. const DATA_FAV_UPDATED = "data_fav_updated";
  25. const DATA_DIFF_UPDATED = "data_diff_updated";
  26. const DATA_PLAY_UPDATED = "data_play_updated";
  27. const HELP_TOUR_HOME = "home";
  28. /**
  29. * @ORM\Id
  30. * @ORM\Column(type="integer")
  31. * @ORM\GeneratedValue(strategy="AUTO")
  32. */
  33. protected $id;
  34. /**
  35. * @Gedmo\Slug(fields={"username"})
  36. * @ORM\Column(length=128, unique=true)
  37. */
  38. protected $slug;
  39. /**
  40. * @ORM\Column(type="text", nullable=true)
  41. * @ORM\Column(length=256)
  42. */
  43. protected $email_requested;
  44. /**
  45. * @ORM\Column(type="integer", nullable=true)
  46. */
  47. protected $email_requested_datetime;
  48. /**
  49. * Cet attribut contient les enregistrements UsersTagsFavorites lié
  50. * a cet utilisateur dans le cadre des Tags Favoris.
  51. *
  52. * @ORM\OneToMany(targetEntity="UsersTagsFavorites", mappedBy="user")
  53. */
  54. protected $tags_favorites;
  55. /**
  56. * @ORM\OneToMany(targetEntity="UserPlaylistPicked", mappedBy="user")
  57. */
  58. protected $user_playlists_pickeds;
  59. /**
  60. * Cet attribut contient les enregistrements UsersElementsFavorites lié
  61. * a cet utilisateur dans le cadre des éléments Favoris.
  62. *
  63. * @ORM\OneToMany(targetEntity="UsersElementsFavorites", mappedBy="user")
  64. */
  65. protected $elements_favorites;
  66. /**
  67. * Liste des Elements appartenant a cet utilisateur.
  68. *
  69. * @ORM\OneToMany(targetEntity="Element", mappedBy="owner")
  70. */
  71. protected $elements;
  72. /**
  73. * Liste des propositions de tags effectués par cet utilisateur
  74. *
  75. * @ORM\OneToMany(targetEntity="ElementTagsProposition", mappedBy="user")
  76. */
  77. protected $element_tags_propositions;
  78. /**
  79. * Users que cet utilisateur suit.
  80. *
  81. * @ORM\OneToMany(targetEntity="FollowUser", mappedBy="follower")
  82. */
  83. protected $followeds_users;
  84. /**
  85. * Users qui suivent cet utilisateur.
  86. *
  87. * @ORM\OneToMany(targetEntity="FollowUser", mappedBy="followed")
  88. */
  89. protected $followers_users;
  90. /**
  91. * Cet attribut contient les enregistrements FollowGroup lié
  92. * a cet utilisateur dans le cadre des groupes suivis.
  93. *
  94. * @ORM\OneToMany(targetEntity="FollowGroup", mappedBy="follower")
  95. */
  96. protected $followed_groups;
  97. /**
  98. * Liste des Groupes appartenant a cet utilisateur.
  99. *
  100. * @ORM\OneToMany(targetEntity="Group", mappedBy="owner")
  101. */
  102. protected $groups_owned;
  103. /**
  104. * @ORM\OneToMany(targetEntity="Playlist", mappedBy="owner")
  105. */
  106. protected $playlists_owneds;
  107. /**
  108. * @ORM\Column(type="integer", nullable=true)
  109. * @var int
  110. */
  111. protected $bad_count;
  112. /**
  113. * Compteur de signalements inutiles
  114. *
  115. * @ORM\Column(type="integer", nullable=true)
  116. * @var int
  117. */
  118. protected $bad_report_count;
  119. /**
  120. * Compteur de contenus refusés par la modération
  121. *
  122. * @ORM\Column(type="integer", nullable=true)
  123. * @var int
  124. */
  125. protected $moderated_element_count;
  126. /**
  127. * Compteur de contenus refusés par la modération
  128. *
  129. * @ORM\Column(type="integer", nullable=true)
  130. * @var int
  131. */
  132. protected $moderated_tag_count;
  133. /**
  134. * Compteur de contenus refusés par la modération
  135. *
  136. * @ORM\Column(type="integer", nullable=true)
  137. * @var int
  138. */
  139. protected $moderated_comment_count;
  140. /**
  141. * @ORM\Column(type="text", nullable=true)
  142. * @ORM\Column(length=256)
  143. */
  144. protected $town;
  145. /**
  146. * @ORM\Column(type="text", nullable=true)
  147. * @ORM\Column(length=128)
  148. */
  149. protected $country;
  150. /**
  151. * Reputation
  152. *
  153. * @ORM\Column(type="integer", nullable=true)
  154. * @var int
  155. */
  156. protected $reputation;
  157. /**
  158. * Liste des Events appartenant a cet utilisateur.
  159. *
  160. * @ORM\OneToMany(targetEntity="Event", mappedBy="user")
  161. */
  162. protected $events;
  163. /**
  164. * Contient des données pratique pour par exemple influencer l'affichange dans twig.
  165. *
  166. * @var array
  167. */
  168. protected $live_datas = array();
  169. /**
  170. * Contient des données pratique, comme l'ordre des tags de sa page favoris etc.
  171. *
  172. * @ORM\Column(type="text", nullable=true)
  173. * @var type string
  174. */
  175. protected $datas = null;
  176. /**
  177. * Tableau contenant les id => name des tags favoris
  178. * de l'user. Ces donnée sont faites pour optimiser les calculs.
  179. * Ce chamsp est mis ajour a chaque fois qu'un UsersTagsFavorite est manipulé.
  180. *
  181. * @ORM\Column(type="text", unique=false, nullable=true)
  182. * @var array
  183. */
  184. private $tags_favorites_quick;
  185. /**
  186. * @Assert\Image(maxSize="6000000")
  187. */
  188. public $avatar;
  189. /**
  190. * @ORM\Column(type="text", length=255, nullable=true)
  191. */
  192. public $avatar_path;
  193. /**
  194. * @ORM\Column(type="text", unique=false, nullable=true)
  195. * @var array
  196. */
  197. private $help_tour;
  198. /**
  199. * @ORM\Column(type="boolean")
  200. * @Assert\NotBlank()
  201. * @var type boolean
  202. */
  203. public $cgu_accepted = false;
  204. /**
  205. * @ORM\Column(type="boolean")
  206. * @var type boolean
  207. */
  208. public $mail_newsletter = true;
  209. /**
  210. * @ORM\Column(type="boolean")
  211. * @var type boolean
  212. */
  213. public $mail_partner = true;
  214. /**
  215. * @ORM\Column(type="boolean", nullable=true)
  216. * @var type boolean
  217. */
  218. private $username_updatable = false;
  219. /**
  220. * @ORM\Column(type="boolean", nullable=false)
  221. * @var type boolean
  222. */
  223. private $email_confirmed = true;
  224. /**
  225. * @ORM\Column(type="integer")
  226. */
  227. protected $email_confirmation_sent_timestamp = 0;
  228. /**
  229. * @ORM\Column(type="boolean", nullable=false)
  230. * @var type boolean
  231. */
  232. private $password_set = true;
  233. /** @ORM\Column(name="facebook_id", type="string", length=255, nullable=true) */
  234. protected $facebook_id;
  235. /**
  236. * @ORM\Column(type="text", unique=false, nullable=true)
  237. */
  238. protected $privacy;
  239. /**
  240. *
  241. */
  242. public function __construct()
  243. {
  244. $this->tags_favorites = new ArrayCollection();
  245. $this->elements = new ArrayCollection();
  246. $this->elements_favorites = new ArrayCollection();
  247. $this->followeds_users = new ArrayCollection();
  248. $this->followers_users = new ArrayCollection();
  249. $this->followed_groups = new ArrayCollection();
  250. $this->groups = new ArrayCollection();
  251. $this->groups_owned = new ArrayCollection();
  252. $this->user_playlists_pickeds = new ArrayCollection();
  253. $this->playlists_owneds = new ArrayCollection();
  254. $this->help_tour = json_encode(array(
  255. self::HELP_TOUR_HOME => true
  256. ));
  257. parent::__construct();
  258. }
  259. public function __toString()
  260. {
  261. return $this->getName();
  262. }
  263. /**
  264. * Get id
  265. *
  266. * @return integer
  267. */
  268. public function getId()
  269. {
  270. return $this->id;
  271. }
  272. /**
  273. * Get tags_favorites
  274. *
  275. * @return Doctrine\Common\Collections\Collection
  276. */
  277. public function getTagsFavorites()
  278. {
  279. return $this->tags_favorites;
  280. }
  281. /**
  282. * Add tags_favorites
  283. *
  284. * @param UsersTagsFavorites $tagsFavorites
  285. */
  286. public function addUsersTagsFavorites(UsersTagsFavorites $tagsFavorites)
  287. {
  288. $this->tags_favorites[] = $tagsFavorites;
  289. }
  290. /**
  291. * Add elements_favorites
  292. *
  293. * @param UsersElementsFavorites $elementsFavorites
  294. */
  295. public function addUsersElementsFavorites(UsersElementsFavorites $elementsFavorites)
  296. {
  297. $this->elements_favorites[] = $elementsFavorites;
  298. }
  299. /**
  300. * Get elements_favorites
  301. *
  302. * @return Doctrine\Common\Collections\Collection
  303. */
  304. public function getElementsFavorites()
  305. {
  306. return $this->elements_favorites;
  307. }
  308. public function getElementsFavoritesElements()
  309. {
  310. if (!$this->elements_favorites)
  311. return new ArrayCollection();
  312. $elements = array();
  313. foreach ($this->elements_favorites as $element_favorite)
  314. {
  315. $elements[] = $element_favorite->getElement();
  316. }
  317. return $elements;
  318. }
  319. /**
  320. * Add elements
  321. *
  322. * @param Element $elements
  323. */
  324. public function addElement(Element $elements)
  325. {
  326. $this->elements[] = $elements;
  327. }
  328. /**
  329. * Get elements
  330. *
  331. * @return Doctrine\Common\Collections\Collection
  332. */
  333. public function getElements()
  334. {
  335. return $this->elements;
  336. }
  337. /**
  338. * Add elements
  339. *
  340. * @param Element $elements
  341. */
  342. public function addElementTagsProposition(ElementTagsProposition $proposition)
  343. {
  344. $this->element_tags_propositions[] = $proposition;
  345. }
  346. /**
  347. * Get elements
  348. *
  349. * @return Doctrine\Common\Collections\Collection
  350. */
  351. public function getElementTagsPropositions()
  352. {
  353. return $this->element_tags_propositions;
  354. }
  355. /**
  356. * Add followeds_users
  357. *
  358. * @param FollowUser $followedsUsers
  359. */
  360. public function addFollowUser(FollowUser $followedsUsers)
  361. {
  362. $this->followeds_users[] = $followedsUsers;
  363. }
  364. /**
  365. * Get followeds_users
  366. *
  367. * @return Doctrine\Common\Collections\Collection
  368. */
  369. public function getFollowedsUsers()
  370. {
  371. $users = array();
  372. foreach ($this->followeds_users as $follow_user)
  373. {
  374. $users[] = $follow_user->getFollowed();
  375. }
  376. return $users;
  377. }
  378. /**
  379. * Get followers_users
  380. *
  381. * @return Doctrine\Common\Collections\Collection
  382. */
  383. public function getFollowersUsers()
  384. {
  385. $users = array();
  386. foreach ($this->followers_users as $follow_user)
  387. {
  388. $users[] = $follow_user->getFollower();
  389. }
  390. return $users;
  391. }
  392. /**
  393. * Add followed_groups
  394. *
  395. * @param FollowGroup $followedGroups
  396. */
  397. public function addFollowGroup(FollowGroup $followedGroups)
  398. {
  399. $this->followed_groups[] = $followedGroups;
  400. }
  401. /**
  402. * Get followed_groups
  403. *
  404. * @return Doctrine\Common\Collections\Collection
  405. */
  406. public function getFollowedGroups()
  407. {
  408. $groups = array();
  409. foreach ($this->followed_groups as $follow_group)
  410. {
  411. $groups[] = $follow_group->getGroup();
  412. }
  413. return $groups;
  414. }
  415. /**
  416. * Add groups
  417. *
  418. * @param Group $groups
  419. */
  420. public function addGroupOwned(Group $groups)
  421. {
  422. $this->groups[] = $groups;
  423. }
  424. /**
  425. * Get groups
  426. *
  427. * @return Doctrine\Common\Collections\Collection
  428. */
  429. public function getGroupsOwned()
  430. {
  431. return $this->groups_owned;
  432. }
  433. /**
  434. * Get groups in array (id => name)
  435. *
  436. * @return Doctrine\Common\Collections\Collection
  437. */
  438. public function getGroupsOwnedArray()
  439. {
  440. $groups = array();
  441. foreach ($this->groups_owned as $group)
  442. {
  443. $groups[$group->getId()] = $group->getName();
  444. }
  445. return $groups;
  446. }
  447. public function getSlug()
  448. {
  449. return $this->slug;
  450. }
  451. public function setSlug($slug)
  452. {
  453. $this->slug = $slug;
  454. }
  455. public function getEmailRequested()
  456. {
  457. return $this->email_requested;
  458. }
  459. public function setEmailRequested($email_requested)
  460. {
  461. $this->email_requested = $email_requested;
  462. }
  463. public function getBadReportCount()
  464. {
  465. return $this->bad_report_count;
  466. }
  467. public function setBadReportCount($count)
  468. {
  469. $this->bad_report_count = $count;
  470. $this->updateBadCount();
  471. }
  472. public function addBadReport()
  473. {
  474. $this->setBadReportCount($this->getBadReportCount()+1);
  475. }
  476. public function getTown()
  477. {
  478. return $this->town;
  479. }
  480. public function setTown($town)
  481. {
  482. $this->town = $town;
  483. }
  484. public function getCountry()
  485. {
  486. return $this->country;
  487. }
  488. public function setCountry($country)
  489. {
  490. $this->country = $country;
  491. }
  492. public function setReputation($reputation)
  493. {
  494. $this->reputation = $reputation;
  495. }
  496. public function getReputation()
  497. {
  498. if ($this->reputation === null)
  499. {
  500. return 0;
  501. }
  502. return $this->reputation;
  503. }
  504. public function getEvents()
  505. {
  506. return $this->events;
  507. }
  508. public function setEvents($events)
  509. {
  510. $this->events = $events;
  511. }
  512. public function getModeratedElementCount()
  513. {
  514. if ($this->moderated_element_count === null)
  515. {
  516. return 0;
  517. }
  518. return $this->moderated_element_count;
  519. }
  520. public function setModeratedElementCount($count)
  521. {
  522. $this->moderated_element_count = $count;
  523. $this->updateBadCount();
  524. }
  525. public function addModeratedElementCount()
  526. {
  527. $this->setModeratedElementCount($this->getModeratedElementCount()+1);
  528. }
  529. public function getModeratedTagCount()
  530. {
  531. if ($this->moderated_tag_count === null)
  532. {
  533. return 0;
  534. }
  535. return $this->moderated_tag_count;
  536. }
  537. public function setModeratedTagCount($count)
  538. {
  539. $this->moderated_tag_count = $count;
  540. $this->updateBadCount();
  541. }
  542. public function addModeratedTagCount()
  543. {
  544. $this->setModeratedTagCount($this->getModeratedTagCount()+1);
  545. }
  546. public function getModeratedCommentCount()
  547. {
  548. if ($this->moderated_comment_count === null)
  549. {
  550. return 0;
  551. }
  552. return $this->moderated_comment_count;
  553. }
  554. public function setModeratedCommentCount($count)
  555. {
  556. $this->moderated_comment_count = $count;
  557. $this->updateBadCount();
  558. }
  559. public function addModeratedCommentCount()
  560. {
  561. $this->setModeratedCommentCount($this->getModeratedCommentCount()+1);
  562. }
  563. /*
  564. *
  565. *
  566. */
  567. public function getName()
  568. {
  569. return $this->getUsername();
  570. }
  571. // public function getLocalisationExploded()
  572. // {
  573. // $town = null;
  574. // $country = null;
  575. // if ($this->localisation)
  576. // {
  577. // if (($explode = explode(', ', $this->localisation)))
  578. // {
  579. // $town = $explode[0];
  580. // $country = $explode[1];
  581. // }
  582. // }
  583. // return array(
  584. // 'town' => $town,
  585. // 'country' => $country
  586. // );
  587. // }
  588. //
  589. // public function setLocalisationExploded($town, $country)
  590. // {
  591. // $town = str_replace(', ', '', $town);
  592. // $town = str_replace(',', '', $town);
  593. // $country = str_replace(', ', '', $country);
  594. // $country = str_replace(',', '', $country);
  595. //
  596. // $this->localisation = $town. ', ' .$country;
  597. // }
  598. // /**
  599. // * @ORM\PrePersist
  600. // */
  601. // public function setSlug()
  602. // {
  603. // if (!$this->slug)
  604. // {
  605. //
  606. // }
  607. // }
  608. //
  609. /**
  610. * Retourn si l'user_id transmis fait partis des enregistrements
  611. * followed de l'objet.
  612. *
  613. * @param int $user_id
  614. * @return boolean
  615. */
  616. public function isFollowingUser($user_id)
  617. {
  618. foreach ($this->followeds_users as $followed_user)
  619. {
  620. if ($followed_user->getFollowed()->getId() == $user_id)
  621. {
  622. return true;
  623. }
  624. }
  625. return false;
  626. }
  627. /**
  628. * Retourn si l'user_id transmis est l'un des User suivis
  629. *
  630. * @param Doctrine\Bundle\DoctrineBundle\Registry doctrine
  631. * @param int $user_id
  632. * @return boolean
  633. */
  634. public function isFollowingUserByQuery($doctrine, $user_id)
  635. {
  636. return $doctrine
  637. ->getRepository('MuzichCoreBundle:User')
  638. ->isFollowingUser($this->getId(), $user_id)
  639. ;
  640. }
  641. /**
  642. * Retourn si l'group_id transmis est l'un des groupe suivis
  643. *
  644. * @param Doctrine\Bundle\DoctrineBundle\Registry doctrine
  645. * @param int $user_id
  646. * @return boolean
  647. */
  648. public function isFollowingGroupByQuery($doctrine, $group_id)
  649. {
  650. return $doctrine
  651. ->getRepository('MuzichCoreBundle:User')
  652. ->isFollowingGroup($this->getId(), $group_id)
  653. ;
  654. }
  655. public function getPersonalHash($salt_context = null)
  656. {
  657. return hash('sha256', $this->getSalt().$this->getUsername().$salt_context);
  658. }
  659. /**
  660. * Ajoute a l'user les tags transmis (id) comme favoris.
  661. *
  662. * @param EntityManager $em
  663. * @param array $ids
  664. */
  665. public function updateTagsFavoritesById(EntityManager $em, $ids)
  666. {
  667. $ids = json_decode($ids);
  668. $ids_to_add = $ids;
  669. // Pour chacun des tags favoris
  670. foreach ($this->tags_favorites as $ii => $tag_favorite)
  671. {
  672. $trouve = false;
  673. foreach ($ids as $i => $id)
  674. {
  675. if ($id == $tag_favorite->getTag()->getId())
  676. {
  677. $trouve = true;
  678. // Si le tag était favoris déjà avant (et aussi maintenant)
  679. // il ne sera ni a ajouter, ni a supprimer.
  680. unset($ids_to_add[$i]);
  681. }
  682. }
  683. if (!$trouve)
  684. {
  685. // Si cet ancien tag n'est plus dans la liste, il faut le supprimer
  686. // (rappel: on supprime ici la relation, pas le tag)
  687. $em->remove($tag_favorite);
  688. }
  689. }
  690. if (count($ids_to_add))
  691. {
  692. $ids_to_add = array_merge($ids_to_add);
  693. $tag_favorite_position_max = $this->getTagFavoritePositionMax();
  694. $tags = $em->getRepository('MuzichCoreBundle:Tag')->findByIds($ids_to_add)->execute();
  695. // Pour les nouveaux ids restants
  696. foreach ($tags as $tag)
  697. {
  698. $tag_favorite = new UsersTagsFavorites();
  699. $tag_favorite->setUser($this);
  700. $tag_favorite->setTag($tag);
  701. $tag_favorite->setPosition($tag_favorite_position_max);
  702. $tag_favorite_position_max++;
  703. $this->addUsersTagsFavorites($tag_favorite);
  704. $em->persist($tag_favorite);
  705. }
  706. }
  707. $em->flush();
  708. }
  709. /**
  710. * Retourne un tableau contenant les ids des tags préférés de l'user
  711. *
  712. * @return type array
  713. */
  714. public function getTagFavoriteIds()
  715. {
  716. $ids = array();
  717. foreach ($this->tags_favorites as $tag_favorite)
  718. {
  719. $ids[$tag_favorite->getTag()->getId()] = $tag_favorite->getTag()->getId();
  720. }
  721. return $ids;
  722. }
  723. /**
  724. * Retourne la position max des tag favoris.
  725. *
  726. * @return int
  727. */
  728. public function getTagFavoritePositionMax()
  729. {
  730. $max = 0;
  731. foreach ($this->tags_favorites as $tag_favorite)
  732. {
  733. if ($tag_favorite->getPosition() > $max)
  734. {
  735. $max = $tag_favorite->getPosition();
  736. }
  737. }
  738. return $max;
  739. }
  740. /**
  741. * Set email_requested_datetime
  742. *
  743. * @param integer $emailRequestedDatetime
  744. */
  745. public function setEmailRequestedDatetime($emailRequestedDatetime)
  746. {
  747. $this->email_requested_datetime = $emailRequestedDatetime;
  748. }
  749. /**
  750. * Get email_requested_datetime
  751. *
  752. * @return integer
  753. */
  754. public function getEmailRequestedDatetime()
  755. {
  756. return $this->email_requested_datetime;
  757. }
  758. public function addLiveData($id, $data)
  759. {
  760. $this->live_datas[$id] = $data;
  761. }
  762. public function removeLiveData($id)
  763. {
  764. if (array_key_exists($id, $this->live_datas))
  765. {
  766. unset($this->live_datas[$id]);
  767. }
  768. }
  769. public function hasLiveData($id, $data = null)
  770. {
  771. if (array_key_exists($id, $this->live_datas))
  772. {
  773. if ($this->live_datas[$id] == $data)
  774. {
  775. return true;
  776. }
  777. }
  778. return false;
  779. }
  780. public function getTagsFavoritesQuick()
  781. {
  782. if ($this->tags_favorites_quick == null)
  783. {
  784. return array();
  785. }
  786. return json_decode($this->tags_favorites_quick, true);
  787. }
  788. public function getTagsFavoritesQuickIds()
  789. {
  790. $tags_favorites_ids = array();
  791. $tags_favorites_data = $this->getTagsFavoritesQuick();
  792. foreach ($tags_favorites_data as $tag_id => $tag_name)
  793. {
  794. $tags_favorites_ids[] = $tag_id;
  795. }
  796. return $tags_favorites_ids;
  797. }
  798. /**
  799. *
  800. * @param array $tags_favorites_quick (id => name)
  801. */
  802. public function setTagsFavoritesQuick($tags_favorites_quick)
  803. {
  804. $this->tags_favorites_quick = json_encode($tags_favorites_quick);
  805. }
  806. /**
  807. *
  808. * @param \Muzich\CoreBundle\Entity\Tag $tag
  809. */
  810. public function addTagFavoriteQuick(Tag $tag)
  811. {
  812. $tags_favorites_quick = $this->getTagsFavoritesQuick();
  813. if (!array_key_exists($tag->getId(), $tags_favorites_quick))
  814. {
  815. $tags_favorites_quick[$tag->getId()] = $tag->getName();
  816. }
  817. $this->setTagsFavoritesQuick($tags_favorites_quick);
  818. }
  819. /**
  820. *
  821. * @param \Muzich\CoreBundle\Entity\Tag $tag
  822. */
  823. public function removeTagFavoriteQuick(Tag $tag)
  824. {
  825. $tags_favorites_quick = $this->getTagsFavoritesQuick();
  826. if (array_key_exists($tag->getId(), $tags_favorites_quick))
  827. {
  828. unset($tags_favorites_quick[$tag->getId()]);
  829. }
  830. $this->setTagsFavoritesQuick($tags_favorites_quick);
  831. }
  832. /**
  833. * Retourne vrai si le tag_id transmis fait partis des tags favoris de
  834. * l'utilisateur
  835. *
  836. * @param int $tag_id
  837. * @return boolean
  838. */
  839. public function haveTagsFavorite($tag_id)
  840. {
  841. $tags_favorites_quick = $this->getTagsFavoritesQuick();
  842. if (array_key_exists($tag_id, $tags_favorites_quick))
  843. {
  844. return true;
  845. }
  846. return false;
  847. }
  848. /**
  849. *
  850. * @return type array
  851. */
  852. public function getDatas()
  853. {
  854. if ($this->datas === null)
  855. {
  856. return array();
  857. }
  858. return json_decode($this->datas, true);
  859. }
  860. /**
  861. *
  862. * @param string $data_id
  863. * @param ~ $default
  864. * @return all
  865. */
  866. public function getData($data_id, $default)
  867. {
  868. $datas = $this->getDatas();
  869. if (array_key_exists($data_id, $datas))
  870. {
  871. return $datas[$data_id];
  872. }
  873. return $default;
  874. }
  875. /**
  876. *
  877. * @param array $datas
  878. */
  879. public function setDatas($datas)
  880. {
  881. $this->datas = json_encode($datas);
  882. }
  883. /**
  884. *
  885. * @param string $data_id
  886. * @param all $data_value
  887. */
  888. public function setData($data_id, $data_value)
  889. {
  890. $datas = $this->getDatas();
  891. $datas[$data_id] = $data_value;
  892. $this->setDatas($datas);
  893. }
  894. public function getAvatarAbsolutePath()
  895. {
  896. return null === $this->avatar_path
  897. ? null
  898. : $this->getAvatarUploadRootDir().'/'.$this->avatar_path;
  899. }
  900. public function getAvatarWebPath()
  901. {
  902. return null === $this->avatar_path
  903. ? null
  904. : $this->getAvatarUploadDir().'/'.$this->avatar_path;
  905. }
  906. protected function getAvatarUploadRootDir()
  907. {
  908. return __DIR__.'/../../../../web/'.$this->getAvatarUploadDir();
  909. }
  910. protected function getAvatarUploadDir()
  911. {
  912. return 'files/avatars';
  913. }
  914. /**
  915. * @ORM\PrePersist()
  916. * @ORM\PreUpdate()
  917. */
  918. public function preUploadAvatar()
  919. {
  920. if (null !== $this->avatar) {
  921. $this->avatar_path = $this->getPersonalHash($this->avatar->getClientOriginalName()).'.'.$this->avatar->guessExtension();
  922. }
  923. }
  924. /**
  925. * @ORM\PostPersist()
  926. * @ORM\PostUpdate()
  927. */
  928. public function uploadAvatar()
  929. {
  930. if (null === $this->avatar) {
  931. return;
  932. }
  933. $this->avatar->move($this->getAvatarUploadRootDir(), $this->avatar_path);
  934. $this->avatar = null;
  935. }
  936. /**
  937. * @ORM\PostRemove()
  938. */
  939. public function removeUpload()
  940. {
  941. if ($file = $this->getAvatarAbsolutePath()) {
  942. unlink($file);
  943. }
  944. }
  945. public function getCguAccepted()
  946. {
  947. return $this->cgu_accepted;
  948. }
  949. public function setCguAccepted($accepted)
  950. {
  951. if ($accepted)
  952. $this->cgu_accepted = true;
  953. else
  954. $this->cgu_accepted = false;
  955. }
  956. public function getMailNewsletter()
  957. {
  958. return $this->mail_newsletter;
  959. }
  960. public function getMailPartner()
  961. {
  962. return $this->mail_partner;
  963. }
  964. public function getHelpTour()
  965. {
  966. return json_decode($this->help_tour, true);
  967. }
  968. public function setHelpTour($help_tour)
  969. {
  970. $this->help_tour = json_encode($help_tour);
  971. }
  972. public function wantSeeHelp($help_id)
  973. {
  974. $help_tour_status = $this->getHelpTour();
  975. if (array_key_exists($help_id, $help_tour_status))
  976. {
  977. return $help_tour_status[$help_id];
  978. }
  979. return false;
  980. }
  981. public function setSeeHelp($help_id, $boolean)
  982. {
  983. $help_tour_status = $this->getHelpTour();
  984. if (array_key_exists($help_id, $help_tour_status))
  985. {
  986. $help_tour_status[$help_id] = ($boolean)?true:false;
  987. }
  988. $this->setHelpTour($help_tour_status);
  989. }
  990. public function getBadCount()
  991. {
  992. if (is_null($this->bad_count))
  993. {
  994. return 0;
  995. }
  996. return $this->bad_count;
  997. }
  998. public function updateBadCount()
  999. {
  1000. $this->bad_count = $this->getBadReportCount()
  1001. + $this->getModeratedCommentCount()
  1002. + $this->getModeratedElementCount()
  1003. + $this->getModeratedTagCount()
  1004. ;
  1005. }
  1006. public function setUsernameUpdatable($updatable)
  1007. {
  1008. if ($updatable)
  1009. $this->username_updatable = true;
  1010. else
  1011. $this->username_updatable = false;
  1012. }
  1013. public function isUsernameUpdatable()
  1014. {
  1015. if ($this->username_updatable)
  1016. return true;
  1017. return false;
  1018. }
  1019. public function isEmailConfirmed()
  1020. {
  1021. return ($this->email_confirmed)?true:false;
  1022. }
  1023. public function setEmailConfirmed($confirmed)
  1024. {
  1025. $this->email_confirmed = ($confirmed)?true:false;
  1026. }
  1027. public function setEmailConfirmationSentTimestamp($timestamp)
  1028. {
  1029. $this->email_confirmation_sent_timestamp = $timestamp;
  1030. }
  1031. public function getEmailConfirmationSentTimestamp()
  1032. {
  1033. return $this->email_confirmation_sent_timestamp;
  1034. }
  1035. public function isPasswordSet()
  1036. {
  1037. return ($this->password_set)?true:false;
  1038. }
  1039. public function setPasswordSet($set)
  1040. {
  1041. $this->password_set = ($set)?true:false;
  1042. }
  1043. public function setPlainPassword($password)
  1044. {
  1045. parent::setPlainPassword($password);
  1046. $this->setPasswordSet(true);
  1047. }
  1048. /** @return void */
  1049. public function setFacebookId($facebook_id)
  1050. {
  1051. $this->facebook_id = $facebook_id;
  1052. $this->password_set = true;
  1053. $this->setEmailConfirmed(true);
  1054. }
  1055. /** @return string */
  1056. public function getFacebookId()
  1057. {
  1058. return $this->facebook_id;
  1059. }
  1060. /** @param Array */
  1061. public function setFBData($fbdata) // C'est dans cette méthode que vous ajouterez vos informations
  1062. {
  1063. if (isset($fbdata['id'])) {
  1064. $this->setFacebookId($fbdata['id']);
  1065. $this->addRole('ROLE_FACEBOOK');
  1066. }
  1067. if (isset($fbdata['email'])) {
  1068. $this->setEmail($fbdata['email']);
  1069. }
  1070. }
  1071. public function getUserPlaylistsPickeds()
  1072. {
  1073. return $this->user_playlists_pickeds;
  1074. }
  1075. public function setUserPlaylistsPickeds(Collection $user_playlists_pickeds)
  1076. {
  1077. $this->user_playlists_pickeds = $user_playlists_pickeds;
  1078. }
  1079. public function havePlaylistPicked(Playlist $playlist)
  1080. {
  1081. foreach ($this->getPickedsPlaylists() as $playlist_picked)
  1082. {
  1083. if ($playlist_picked->getId() == $playlist->getId())
  1084. {
  1085. return true;
  1086. }
  1087. }
  1088. return false;
  1089. }
  1090. public function getPickedsPlaylists()
  1091. {
  1092. $playlists = new ArrayCollection();
  1093. foreach ($this->user_playlists_pickeds as $user_playlist_picked)
  1094. {
  1095. $playlists->add($user_playlist_picked->getPlaylist());
  1096. }
  1097. return $playlists;
  1098. }
  1099. public function getPlaylistsOwneds()
  1100. {
  1101. return $this->playlists_owneds;
  1102. }
  1103. public function setPlaylistsOwneds(Collection $playlists_owneds)
  1104. {
  1105. $this->playlists_owneds = $playlists_owneds;
  1106. }
  1107. public function getPrivacy()
  1108. {
  1109. return json_decode($this->privacy, true);
  1110. }
  1111. public function setPrivacy($privacy)
  1112. {
  1113. $this->privacy = json_encode($privacy);
  1114. }
  1115. public function isFavoritesPublics()
  1116. {
  1117. $privacy_manager = new PrivacyManager($this);
  1118. return $privacy_manager->get(PrivacyManager::CONF_FAVORITES_PUBLIC);
  1119. }
  1120. public function setFavoritesPublics($public)
  1121. {
  1122. $privacy_manager = new PrivacyManager($this);
  1123. return $privacy_manager->set(PrivacyManager::CONF_FAVORITES_PUBLIC, $public);
  1124. }
  1125. public function setDataFavoriteNoMoreUpdated()
  1126. {
  1127. $this->setData(self::DATA_FAV_UPDATED, false);
  1128. }
  1129. public function setDataDiffusionsNoMoreUpdated()
  1130. {
  1131. $this->setData(self::DATA_DIFF_UPDATED, false);
  1132. }
  1133. public function setDataPlaylistNoMoreUpdated()
  1134. {
  1135. $this->setData(self::DATA_PLAY_UPDATED, false);
  1136. }
  1137. }