<?php
namespace App\Entity\Forum;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use App\Traits\CreatedUpdatedTrait;
use Gedmo\Mapping\Annotation as Gedmo;
use JMS\Serializer\Annotation\Exclude;
use JMS\Serializer\Annotation as JMS;
use JMS\Serializer\Annotation as Serializer;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Table(name="v_forum_post")
* @ORM\Entity(repositoryClass="App\Repository\Forum\PostRepository")
* @Vich\Uploadable
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
*/
class Post
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=55)
*/
private $title;
/**
* @var string
* @Gedmo\Slug(fields={"title"}, updatable=false)
* @ORM\Column(type="string", length=255)
*/
private $slug;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $content;
/**
* @ORM\Column(type="boolean")
*/
private $approved = true;
/**
* @ORM\Column(type="boolean")
*/
private $anonymous = false;
/**
* @ORM\Column(type="boolean")
*/
private $recommended = false;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Forum\Category")
* @Exclude
*/
private $category;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User")
* @Exclude
*/
private $user;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Admin")
* @Exclude
*/
private $admin;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Forum\Group")
* @Exclude
*/
private $group;
/**
* @ORM\Column(name="deleted_at", type="datetime", nullable=true)
*/
private $deletedAt;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Forum\PostReply", mappedBy="post")
* @Exclude
* @ORM\OrderBy({"id": "DESC"})
*/
private $replies;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Forum\PostLike", mappedBy="post")
* @Exclude
**/
private $likes;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Forum\PostSave", mappedBy="post")
* @Exclude
**/
private $saves;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="forum_files", fileNameProperty="image")
*
* @var File
* @Exclude
*/
private $imageFile;
/**
* @ORM\Column(name="image", type="string", length=255, nullable=true)
*
* @var string
*/
private $image;
/**
* @var string
*
* @ORM\Column(name="view_count", type="integer", nullable=true)
*/
private $viewCount = 0;
/**
* @ORM\Column(type="boolean")
*/
private $banned = false;
private $liked = false;
private $saved = false;
/**
* @ORM\Column(name="posted_by_name", type="string", length=255, nullable=true)
*/
private $postedByName;
/**
* @ORM\Column(name="posted_by_image", type="string", length=255, nullable=true)
*/
private $postedByImage;
/**
* @ORM\Column(type="boolean")
*/
private $archived = false;
/**
* @return mixed
*/
public function getLikes()
{
return $this->likes;
}
use CreatedUpdatedTrait;
public function __construct(){
$this->replies = new ArrayCollection();
}
public function __toString()
{
return (string) $this->title;
}
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 getSlug(): ?string
{
return $this->slug;
}
public function setSlug(?string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(?string $content): self
{
$this->content = $content;
return $this;
}
/**
* @return bool
*/
public function isApproved(): bool
{
return $this->approved;
}
/**
* @param bool $approved
*/
public function setApproved(?bool $approved): void
{
$this->approved = $approved;
}
/**
* @return bool
*/
public function isAnonymous(): ?bool
{
return $this->anonymous;
}
/**
* @param bool $anonymous
*/
public function setAnonymous(?bool $anonymous): void
{
$this->anonymous = $anonymous;
}
/**
* @return bool
*/
public function isRecommended(): ?bool
{
return $this->recommended;
}
/**
* @param bool $recommended
*/
public function setRecommended(?bool $recommended): void
{
$this->recommended = $recommended;
}
public function getCategory(): ?Category
{
return $this->category;
}
public function setCategory(?Category $category): self
{
$this->category = $category;
return $this;
}
public function getGroup(): ?Group
{
return $this->group;
}
public function setGroup(?Group $group): self
{
$this->group = $group;
return $this;
}
/**
* @return mixed
*/
public function getDeletedAt()
{
return $this->deletedAt;
}
/**
* @param mixed $deletedAt
*/
public function setDeletedAt($deletedAt): void
{
$this->deletedAt = $deletedAt;
}
/**
* @return mixed
*/
public function getUser()
{
return $this->user;
}
/**
* @param mixed $user
*/
public function setUser($user): void
{
$this->user = $user;
$this->postedByName = $user->getName();
}
/**
* @return mixed
*/
public function getAdmin()
{
return $this->admin;
}
/**
* @param mixed $admin
*/
public function setAdmin($admin): void
{
$this->admin = $admin;
$this->postedByName = $admin->getName();
}
/**
* @JMS\VirtualProperty
* @JMS\SerializedName("user_id")
*
* @return string
*/
public function getUserId()
{
return $this->getUser() ? $this->getUser()->getId() : $this->getAdmin()->getId();
}
/**
* @JMS\VirtualProperty
* @JMS\SerializedName("user_name")
*
* @return string
*/
public function getUserName()
{
if(!is_null($this->deletedAt)){
return $this->getPostedByName() ?: 'Anonymous';
}
if($this->getAdmin()){
return $this->getAdmin() ? $this->getAdmin()->getName() : 'Anonymous';
}
return $this->getUser() ? $this->getUser()->getName() : 'Anonymous';
}
/**
* @JMS\VirtualProperty
* @JMS\SerializedName("is_admin_post")
*
* @return string
*/
public function isAdminPost()
{
return $this->getAdmin() ? true : false;
}
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $file
*/
public function setImageFile(File $file = null)
{
$this->imageFile = $file;
if ($file) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updated = new \DateTime('now');
}
}
/**
* @return File
*/
public function getImageFile()
{
return $this->imageFile;
}
/**
* @param string $image
*/
public function setImage($image)
{
$this->image = $image;
}
/**
* @return string
*/
public function getImage()
{
return $this->image;
}
/**
* @return mixed
*/
public function getReplies()
{
return $this->replies;
}
/**
* @Serializer\VirtualProperty
* @Serializer\SerializedName("replies")
* @Serializer\Expose
*/
public function getFilteredReplies()
{
$filteredReplies = [];
foreach ($this->replies as $reply){
if(is_null($reply->getDeletedAt())) {
$filteredReplies[] = $reply;
}
}
return $filteredReplies;
}
/**
* @return mixed
*/
public function getSaves()
{
return $this->saves;
}
/**
* @return integer
*/
public function getViewCount()
{
return $this->viewCount;
}
/**
* @param integer $viewCount
*/
public function setViewCount($viewCount)
{
$this->viewCount = $viewCount;
}
public function incrementViewCount()
{
$this->viewCount++;
}
/**
* @return bool
*/
public function isBanned(): ?bool
{
return $this->banned;
}
/**
* @param bool $banned
*/
public function setBanned(?bool $banned): void
{
$this->banned = $banned;
}
/**
* @return mixed
*/
public function getPostedByName()
{
return $this->postedByName;
}
/**
* @param mixed $postedByName
*/
public function setPostedByName($postedByName): void
{
$this->postedByName = $postedByName;
}
/**
* @return mixed
*/
public function getPostedByImage()
{
return $this->postedByImage;
}
/**
* @param mixed $postedByImage
*/
public function setPostedByImage($postedByImage): void
{
$this->postedByImage = $postedByImage;
}
/**
* @return bool
*/
public function isArchived(): ?bool
{
return $this->archived;
}
/**
* @param bool $archived
*/
public function setArchived(?bool $archived): void
{
$this->archived = $archived;
}
}