src/Entity/ForumPost.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\ForumPostRepository;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\DBAL\Types\Types;
  7. use Doctrine\ORM\Mapping as ORM;
  8. /**
  9.  * @ORM\Entity(repositoryClass=ForumPostRepository::class)
  10.  */
  11. class ForumPost
  12. {
  13.     /**
  14.      * @ORM\Id
  15.      * @ORM\GeneratedValue
  16.      * @ORM\Column(type="integer")
  17.      */
  18.     private $id;
  19.     /**
  20.      * @ORM\Column(type="string", length=255, nullable=true)
  21.      */
  22.     private ?string $title;
  23.     /**
  24.      * @ORM\Column(type="text", nullable=true)
  25.      */
  26.     private ?string $content "";
  27.     /**
  28.      * @ORM\Column(type="datetime")
  29.      */
  30.     private ?\DateTimeInterface $createdAt;
  31.     /**
  32.      * @ORM\ManyToOne(targetEntity=ForumUser::class, inversedBy="posts")
  33.      */
  34.     private ?ForumUser $forumUser;
  35.     /**
  36.      * @ORM\ManyToOne(targetEntity=ForumTopic::class, inversedBy="posts")
  37.      */
  38.     private ?ForumTopic $topic;
  39.     /**
  40.      * @ORM\OneToMany(targetEntity=ForumComment::class, mappedBy="post")
  41.      */
  42.     private Collection $comments;
  43.     /**
  44.      * @ORM\ManyToMany(targetEntity=ForumUser::class, inversedBy="forumPostViews")
  45.      */
  46.     private Collection $userViews;
  47.     /**
  48.      * @ORM\ManyToMany(targetEntity=ForumUser::class, mappedBy="favoritePost")
  49.      */
  50.     private Collection $forumUserFavorites;
  51.     /**
  52.      * @ORM\OneToMany(targetEntity=ForumNotificationSubscriber::class, mappedBy="post")
  53.      */
  54.     private Collection $notificationSubscribers;
  55.     /**
  56.      * @ORM\Column(type="boolean", nullable=true)
  57.      */
  58.     private ?bool $actived false;
  59.     /**
  60.      * @ORM\Column(type="boolean", nullable=true)
  61.      */
  62.     private ?bool $closed false;
  63.     /**
  64.      * @ORM\Column(type="datetime", nullable=true)
  65.      */
  66.     private ?\DateTimeInterface $closedAt;
  67.     /**
  68.      * @ORM\Column(type="string", length=255, nullable=true, unique=true)
  69.      */
  70.     private ?string $slug;
  71.     public function __construct()
  72.     {
  73.         $this->comments = new ArrayCollection();
  74.         $this->createdAt = new \DateTime();
  75.         $this->userViews = new ArrayCollection();
  76.         $this->forumUserFavorites = new ArrayCollection();
  77.         $this->notificationSubscribers = new ArrayCollection();
  78.     }
  79.     //----------------------------------------------------------------
  80.     private function dateCurrentDiffToString($date): string
  81.     {
  82.         $today = new \DateTime();
  83.         $interval $today->diff($date);
  84.         if ($interval->days == 0) {
  85.             return 'today';
  86.         } elseif ($interval->days == 1) {
  87.             return '1 day ago';
  88.         } elseif ($interval->days >= && $interval->days <= 6) {
  89.             return '' $interval->days ' days ago';
  90.         } elseif ($interval->days >= && $interval->days 30) {
  91.             $weeks floor($interval->days 7);
  92.             return '' $weeks ' week' . ($weeks 's' '') . ' ago';
  93.         } elseif ($interval->days >= 30) {
  94.             $months floor($interval->days 30);
  95.             return '' $months ' month' . ($months 's' '') . ' ago';
  96.         } else {
  97.             return 'on ' $date->format('j M');
  98.         }
  99.     }
  100.     public function getTimeCreateString(): string
  101.     {
  102.         return "Posted ".$this->dateCurrentDiffToString($this->createdAt);
  103.     }
  104.     public function getTimeCloseString(): string
  105.     {
  106.         return "Closed ".$this->dateCurrentDiffToString($this->closedAt);
  107.     }
  108.     public function getLastComment()
  109.     {
  110.         /** @var ForumComment $lastComment */
  111.         $lastComment null;
  112.         /** @var Collection<ForumPost> $posts */
  113.         $comments $this->comments;
  114.         if (count($comments) > 0) {
  115.             $lastComment $comments->last();
  116.         }
  117.         return $lastComment;
  118.     }
  119.     /**
  120.      *
  121.      * GET IF POST IS FAVORITE FOR USER
  122.      *
  123.      * @param ForumUser $forumUser
  124.      * @return boolean
  125.      */
  126.     public function isFavorite(ForumUser $forumUser): bool
  127.     {
  128.         /** @var Collection<ForumUser> $userFavorites */
  129.         $userFavorites $this->forumUserFavorites;
  130.         foreach ($userFavorites as $userFavorite){
  131.             if ($userFavorite === $forumUser){
  132.                 return true;
  133.             }
  134.         }
  135.         return false;
  136.     }
  137.     public function setContent(?string $content): self
  138.     {
  139.         $purify \HTMLPurifier::getInstance();
  140.         $htmlPurified $purify->purify($content);
  141.         $this->content $htmlPurified;
  142.         return $this;
  143.     }
  144.     //----------------------------------------------------------------
  145.     public function getId(): ?int
  146.     {
  147.         return $this->id;
  148.     }
  149.     public function getTitle(): ?string
  150.     {
  151.         return $this->title;
  152.     }
  153.     public function setTitle(?string $title): self
  154.     {
  155.         $this->title $title;
  156.         return $this;
  157.     }
  158.     public function getContent(): ?string
  159.     {
  160.         return $this->content;
  161.     }
  162.     public function getCreatedAt(): ?\DateTimeInterface
  163.     {
  164.         return $this->createdAt;
  165.     }
  166.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  167.     {
  168.         $this->createdAt $createdAt;
  169.         return $this;
  170.     }
  171.     public function getForumUser(): ?ForumUser
  172.     {
  173.         return $this->forumUser;
  174.     }
  175.     public function setForumUser(?ForumUser $forumUser): self
  176.     {
  177.         $this->forumUser $forumUser;
  178.         return $this;
  179.     }
  180.     public function getTopic(): ?ForumTopic
  181.     {
  182.         return $this->topic;
  183.     }
  184.     public function setTopic(?ForumTopic $topic): self
  185.     {
  186.         $this->topic $topic;
  187.         return $this;
  188.     }
  189.     /**
  190.      * @return Collection<int, ForumComment>
  191.      */
  192.     public function getComments(): Collection
  193.     {
  194.         return $this->comments;
  195.     }
  196.     public function addComment(ForumComment $comment): self
  197.     {
  198.         if (!$this->comments->contains($comment)) {
  199.             $this->comments[] = $comment;
  200.             $comment->setPost($this);
  201.         }
  202.         return $this;
  203.     }
  204.     public function removeComment(ForumComment $comment): self
  205.     {
  206.         if ($this->comments->removeElement($comment)) {
  207.             // set the owning side to null (unless already changed)
  208.             if ($comment->getPost() === $this) {
  209.                 $comment->setPost(null);
  210.             }
  211.         }
  212.         return $this;
  213.     }
  214.     /**
  215.      * @return Collection<int, ForumUser>
  216.      */
  217.     public function getUserViews(): Collection
  218.     {
  219.         return $this->userViews;
  220.     }
  221.     public function addUserView(ForumUser $userView): self
  222.     {
  223.         if (!$this->userViews->contains($userView)) {
  224.             $this->userViews[] = $userView;
  225.         }
  226.         return $this;
  227.     }
  228.     public function removeUserView(ForumUser $userView): self
  229.     {
  230.         $this->userViews->removeElement($userView);
  231.         return $this;
  232.     }
  233.     /**
  234.      * @return Collection<int, ForumUser>
  235.      */
  236.     public function getForumUserFavorites(): Collection
  237.     {
  238.         return $this->forumUserFavorites;
  239.     }
  240.     public function addForumUserFavorite(ForumUser $forumUserFavorite): self
  241.     {
  242.         if (!$this->forumUserFavorites->contains($forumUserFavorite)) {
  243.             $this->forumUserFavorites[] = $forumUserFavorite;
  244.             $forumUserFavorite->addFavoritePost($this);
  245.         }
  246.         return $this;
  247.     }
  248.     public function removeForumUserFavorite(ForumUser $forumUserFavorite): self
  249.     {
  250.         if ($this->forumUserFavorites->removeElement($forumUserFavorite)) {
  251.             $forumUserFavorite->removeFavoritePost($this);
  252.         }
  253.         return $this;
  254.     }
  255.     /**
  256.      * @return Collection<int, ForumNotificationSubscriber>
  257.      */
  258.     public function getNotificationSubscribers(): Collection
  259.     {
  260.         return $this->notificationSubscribers;
  261.     }
  262.     public function addNotificationSubscriber(ForumNotificationSubscriber $notificationSubscriber): self
  263.     {
  264.         if (!$this->notificationSubscribers->contains($notificationSubscriber)) {
  265.             $this->notificationSubscribers[] = $notificationSubscriber;
  266.             $notificationSubscriber->setPost($this);
  267.         }
  268.         return $this;
  269.     }
  270.     public function removeNotificationSubscriber(ForumNotificationSubscriber $notificationSubscriber): self
  271.     {
  272.         if ($this->notificationSubscribers->removeElement($notificationSubscriber)) {
  273.             // set the owning side to null (unless already changed)
  274.             if ($notificationSubscriber->getPost() === $this) {
  275.                 $notificationSubscriber->setPost(null);
  276.             }
  277.         }
  278.         return $this;
  279.     }
  280.     public function isActived(): ?bool
  281.     {
  282.         return $this->actived;
  283.     }
  284.     public function setActived(?bool $actived): self
  285.     {
  286.         $this->actived $actived;
  287.         return $this;
  288.     }
  289.     public function getSlug(): ?string
  290.     {
  291.         return $this->slug;
  292.     }
  293.     public function setSlug(?string $slug): self
  294.     {
  295.         $this->slug $slug;
  296.         return $this;
  297.     }
  298.     public function isClosed(): ?bool
  299.     {
  300.         return $this->closed;
  301.     }
  302.     public function setClosed(?bool $closed): self
  303.     {
  304.         $this->closed $closed;
  305.         return $this;
  306.     }
  307.     public function getClosedAt(): ?\DateTimeInterface
  308.     {
  309.         return $this->closedAt;
  310.     }
  311.     public function setClosedAt(?\DateTimeInterface $closedAt): self
  312.     {
  313.         $this->closedAt $closedAt;
  314.         return $this;
  315.     }
  316. }