<?php
namespace App\Entity;
use App\Repository\ForumPostRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=ForumPostRepository::class)
*/
class ForumPost
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $title;
/**
* @ORM\Column(type="text", nullable=true)
*/
private ?string $content = "";
/**
* @ORM\Column(type="datetime")
*/
private ?\DateTimeInterface $createdAt;
/**
* @ORM\ManyToOne(targetEntity=ForumUser::class, inversedBy="posts")
*/
private ?ForumUser $forumUser;
/**
* @ORM\ManyToOne(targetEntity=ForumTopic::class, inversedBy="posts")
*/
private ?ForumTopic $topic;
/**
* @ORM\OneToMany(targetEntity=ForumComment::class, mappedBy="post")
*/
private Collection $comments;
/**
* @ORM\ManyToMany(targetEntity=ForumUser::class, inversedBy="forumPostViews")
*/
private Collection $userViews;
/**
* @ORM\ManyToMany(targetEntity=ForumUser::class, mappedBy="favoritePost")
*/
private Collection $forumUserFavorites;
/**
* @ORM\OneToMany(targetEntity=ForumNotificationSubscriber::class, mappedBy="post")
*/
private Collection $notificationSubscribers;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private ?bool $actived = false;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private ?bool $closed = false;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private ?\DateTimeInterface $closedAt;
/**
* @ORM\Column(type="string", length=255, nullable=true, unique=true)
*/
private ?string $slug;
public function __construct()
{
$this->comments = new ArrayCollection();
$this->createdAt = new \DateTime();
$this->userViews = new ArrayCollection();
$this->forumUserFavorites = new ArrayCollection();
$this->notificationSubscribers = new ArrayCollection();
}
//----------------------------------------------------------------
private function dateCurrentDiffToString($date): string
{
$today = new \DateTime();
$interval = $today->diff($date);
if ($interval->days == 0) {
return 'today';
} elseif ($interval->days == 1) {
return '1 day ago';
} elseif ($interval->days >= 2 && $interval->days <= 6) {
return '' . $interval->days . ' days ago';
} elseif ($interval->days >= 7 && $interval->days < 30) {
$weeks = floor($interval->days / 7);
return '' . $weeks . ' week' . ($weeks > 1 ? 's' : '') . ' ago';
} elseif ($interval->days >= 30) {
$months = floor($interval->days / 30);
return '' . $months . ' month' . ($months > 1 ? 's' : '') . ' ago';
} else {
return 'on ' . $date->format('j M');
}
}
public function getTimeCreateString(): string
{
return "Posted ".$this->dateCurrentDiffToString($this->createdAt);
}
public function getTimeCloseString(): string
{
return "Closed ".$this->dateCurrentDiffToString($this->closedAt);
}
public function getLastComment()
{
/** @var ForumComment $lastComment */
$lastComment = null;
/** @var Collection<ForumPost> $posts */
$comments = $this->comments;
if (count($comments) > 0) {
$lastComment = $comments->last();
}
return $lastComment;
}
/**
*
* GET IF POST IS FAVORITE FOR USER
*
* @param ForumUser $forumUser
* @return boolean
*/
public function isFavorite(ForumUser $forumUser): bool
{
/** @var Collection<ForumUser> $userFavorites */
$userFavorites = $this->forumUserFavorites;
foreach ($userFavorites as $userFavorite){
if ($userFavorite === $forumUser){
return true;
}
}
return false;
}
public function setContent(?string $content): self
{
$purify = \HTMLPurifier::getInstance();
$htmlPurified = $purify->purify($content);
$this->content = $htmlPurified;
return $this;
}
//----------------------------------------------------------------
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getForumUser(): ?ForumUser
{
return $this->forumUser;
}
public function setForumUser(?ForumUser $forumUser): self
{
$this->forumUser = $forumUser;
return $this;
}
public function getTopic(): ?ForumTopic
{
return $this->topic;
}
public function setTopic(?ForumTopic $topic): self
{
$this->topic = $topic;
return $this;
}
/**
* @return Collection<int, ForumComment>
*/
public function getComments(): Collection
{
return $this->comments;
}
public function addComment(ForumComment $comment): self
{
if (!$this->comments->contains($comment)) {
$this->comments[] = $comment;
$comment->setPost($this);
}
return $this;
}
public function removeComment(ForumComment $comment): self
{
if ($this->comments->removeElement($comment)) {
// set the owning side to null (unless already changed)
if ($comment->getPost() === $this) {
$comment->setPost(null);
}
}
return $this;
}
/**
* @return Collection<int, ForumUser>
*/
public function getUserViews(): Collection
{
return $this->userViews;
}
public function addUserView(ForumUser $userView): self
{
if (!$this->userViews->contains($userView)) {
$this->userViews[] = $userView;
}
return $this;
}
public function removeUserView(ForumUser $userView): self
{
$this->userViews->removeElement($userView);
return $this;
}
/**
* @return Collection<int, ForumUser>
*/
public function getForumUserFavorites(): Collection
{
return $this->forumUserFavorites;
}
public function addForumUserFavorite(ForumUser $forumUserFavorite): self
{
if (!$this->forumUserFavorites->contains($forumUserFavorite)) {
$this->forumUserFavorites[] = $forumUserFavorite;
$forumUserFavorite->addFavoritePost($this);
}
return $this;
}
public function removeForumUserFavorite(ForumUser $forumUserFavorite): self
{
if ($this->forumUserFavorites->removeElement($forumUserFavorite)) {
$forumUserFavorite->removeFavoritePost($this);
}
return $this;
}
/**
* @return Collection<int, ForumNotificationSubscriber>
*/
public function getNotificationSubscribers(): Collection
{
return $this->notificationSubscribers;
}
public function addNotificationSubscriber(ForumNotificationSubscriber $notificationSubscriber): self
{
if (!$this->notificationSubscribers->contains($notificationSubscriber)) {
$this->notificationSubscribers[] = $notificationSubscriber;
$notificationSubscriber->setPost($this);
}
return $this;
}
public function removeNotificationSubscriber(ForumNotificationSubscriber $notificationSubscriber): self
{
if ($this->notificationSubscribers->removeElement($notificationSubscriber)) {
// set the owning side to null (unless already changed)
if ($notificationSubscriber->getPost() === $this) {
$notificationSubscriber->setPost(null);
}
}
return $this;
}
public function isActived(): ?bool
{
return $this->actived;
}
public function setActived(?bool $actived): self
{
$this->actived = $actived;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(?string $slug): self
{
$this->slug = $slug;
return $this;
}
public function isClosed(): ?bool
{
return $this->closed;
}
public function setClosed(?bool $closed): self
{
$this->closed = $closed;
return $this;
}
public function getClosedAt(): ?\DateTimeInterface
{
return $this->closedAt;
}
public function setClosedAt(?\DateTimeInterface $closedAt): self
{
$this->closedAt = $closedAt;
return $this;
}
}