User.php 23KB

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