User.php 27KB

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