<?php
namespace App\Entity;
use App\Dictionary\ContentStatus;
use App\Dictionary\ContentType;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\JoinColumn;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToOne;
use Doctrine\ORM\PersistentCollection;
use JMS\Serializer\Annotation\Groups;
/**
* @ORM\Table(name="Content", indexes={
*
* @ORM\Index(name="publish_date_idx", columns={"publish_date"}),
* @ORM\Index(name="publish_date_status_idx", columns={"publish_date", "status"}),
* @ORM\Index(name="id_status_idx", columns={"id", "status"}),
* @ORM\Index(name="status_idx", columns={"status"})
* })
*
* @ORM\Entity(repositoryClass="App\Repository\ContentRepository")
*
* @ORM\HasLifecycleCallbacks()
*/
class Content
{
/**
* @Groups({"contentlist", "waitrating", "conversationmessageindex"})
*
* @ORM\Id()
*
* @ORM\GeneratedValue()
*
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="ContentComment", mappedBy="content", cascade={"persist"})
*/
private $comments;
/**
* @Groups({"waitrating", "contentlist", "conversationmessageindex"})
*
* @OneToOne(targetEntity="ContentVideo",cascade={"persist"}, inversedBy="content")
*
* @JoinColumn(name="video_id", referencedColumnName="id", onDelete="SET NULL")
*/
private $video;
/**
* @Groups({"waitrating", "contentlist", "video_search", "imageset_search"})
*
* @ManyToOne(targetEntity="Member",cascade={"persist"}, fetch="EAGER")
*
* @JoinColumn(name="member_id", referencedColumnName="id", onDelete="SET NULL")
*/
private $member;
/**
* @Groups({"waitrating", "contentlist", "conversationmessageindex"})
*
* @OneToOne(targetEntity="ContentImageset",cascade={"persist"}, inversedBy="content")
*
* @JoinColumn(name="imageset_id", referencedColumnName="id", onDelete="SET NULL")
*/
private $imageset;
/**
* @Groups({"contentlist", "video_search", "imageset_search"})
*
* @ORM\Column(type="decimal", precision=10, scale=0, nullable=true)
*/
private $price;
/**
* @Groups({"contentlist", "adminindex", "video_search", "imageset_search"})
*
* @ORM\Column(type="datetime", nullable=true)
*/
private $publish_date;
/**
* @Groups({"contentdetail", "adminindex", "contentstatus"})
*
* @ORM\Column(type="smallint")
*/
private $status = ContentStatus::NONE;
/**
* @Groups({"waitrating", "adminindex", "contentdetail", "video_search", "imageset_search"})
*
* @ORM\Column(type="datetime")
*/
private $created_at;
/**
* @ORM\Column(type="datetime")
*/
private $updated_at;
/**
* @Groups({"contentlist", "conversationmessageindex"})
*
* @ORM\Column(type="smallint")
*/
private $type;
/**
* @Groups({"conversationmessageindex", "contentlist", "video_search", "imageset_search"})
*
* @ORM\Column(type="smallint", nullable=true)
*/
protected ?int $preview_image = 1;
/**
* @Groups({"contentlist", "video_search", "imageset_search"})
*
* @OneToOne(targetEntity="ContentRanking", mappedBy="content")
*/
private $ranking;
/**
* @Groups({"contentdetail", "video_search", "imageset_search"})
*
* @ORM\Column(type="boolean")
*/
protected ?bool $is_hardcore = false;
/**
* @Groups({"contentdetail", "video_search", "imageset_search"})
*
* @ORM\Column(type="string", nullable=true)
*/
protected $meta_keywords;
/**
* @Groups({"contentdetail", "video_search", "imageset_search"})
*
* @ORM\Column(type="string", nullable=true)
*/
protected $meta_description;
/**
* @Groups({"actorlist"})
*
* @ORM\ManyToMany(targetEntity="Actor", cascade={"persist"})
*
* @ORM\JoinTable(name="ContentActor",
* joinColumns={@ORM\JoinColumn(name="content_id", referencedColumnName="id", onDelete="RESTRICT")},
* inverseJoinColumns={@ORM\JoinColumn(name="actor_id", referencedColumnName="id", onDelete="RESTRICT")}
* )
*/
private $actors;
/**
* @Groups({"contentdetail", "video_search", "imageset_search"})
*
* @ORM\OneToMany(targetEntity="App\Entity\ContentCategory", mappedBy="content", cascade={"persist"})
*/
private $categories;
/**
* @Groups({"adminindex"})
*/
protected int $nfsSize = 0;
/**
* @Groups({"adminindex"})
*
* @ORM\OneToMany(targetEntity="App\Entity\MemberContent", mappedBy="content", cascade={"persist"})
*/
private $memberContents;
public function __construct()
{
$this->actors = new ArrayCollection();
$this->categories = new ArrayCollection();
$this->comments = new ArrayCollection();
$this->memberContents = new ArrayCollection();
}
/**
* @return $this
*/
public function setNfsSize(int $size): Content
{
$this->nfsSize = $size;
return $this;
}
/**
* @ORM\PrePersist()
*/
public function prePersist()
{
if (!$this->getCreatedAt()) {
$this->setCreatedAt(new \DateTime());
}
if (!$this->getUpdatedAt()) {
$this->setUpdatedAt(new \DateTime());
}
}
public function getContent(): ?ContentTypeAbstract
{
if (ContentType::VIDEO == $this->type) {
return $this->getVideo();
} elseif (ContentType::IMAGESET == $this->type) {
return $this->getImageset();
}
return null;
}
public function getId()
{
return $this->id;
}
public function setRanking(?ContentRanking $ranking): self
{
$this->ranking = $ranking;
return $this;
}
public function getRanking(): ?ContentRanking
{
return $this->ranking;
}
public function getComments(): Collection
{
return $this->comments;
}
/**
* @return PersistentCollection
*/
public function getActors(): Collection
{
return $this->actors;
}
/**
* @return PersistentCollection
*/
public function getMemberContents(): Collection
{
return $this->memberContents;
}
public function getImageset(): ?ContentImageset
{
return $this->imageset;
}
public function setImageset(?ContentImageset $imageset): self
{
$this->imageset = $imageset;
return $this;
}
public function getPrice(): ?float
{
return $this->price;
}
public function setPrice(?float $price): self
{
$this->price = $price;
return $this;
}
public function getPublishDate(): ?\DateTimeInterface
{
return $this->publish_date;
}
public function isPublished(string $when = 'now'): bool
{
if (!$this->publish_date instanceof \DateTimeInterface) {
return true;
}
return $this->publish_date->format('U') < (new \DateTime($when))->format('U');
}
public function setPublishDate(?\DateTimeInterface $publish_date): self
{
$this->publish_date = $publish_date;
return $this;
}
public function getStatus(): ?int
{
return $this->status;
}
public function setStatus(int $status): self
{
$this->status = $status;
return $this;
}
public function getCreatedAt(): ?\DateTime
{
return $this->created_at;
}
public function setCreatedAt(\DateTime $created_at): self
{
$this->created_at = $created_at;
return $this;
}
public function getUpdatedAt(): ?\DateTime
{
return $this->updated_at;
}
public function setUpdatedAt(\DateTime $updated_at): self
{
$this->updated_at = $updated_at;
return $this;
}
public function getType(): ?int
{
return $this->type;
}
public function getTypeWord(): ?string
{
if (ContentType::VIDEO === $this->type) {
return 'video';
}
if (ContentType::IMAGESET === $this->type) {
return 'imageset';
}
return null;
}
public function setType(int $type): self
{
$this->type = $type;
return $this;
}
public function getTitle(): ?string
{
if ($this->isImageset()) {
return $this->getImageset()->getTitle();
}
if ($this->isVideo()) {
return $this->getVideo()->getTitle();
}
return null;
}
public function getSlug(): ?string
{
if ($this->isImageset()) {
return $this->getImageset()?->getSlug();
}
if ($this->isVideo()) {
return $this->getVideo()?->getSlug();
}
return null;
}
public function isSlugEmpty(): bool
{
return empty($this->getSlug());
}
public function getVideo(): ?ContentVideo
{
return $this->video;
}
public function setVideo(?ContentVideo $video): self
{
$this->video = $video;
if ($video) {
$this->setType(ContentType::VIDEO);
}
return $this;
}
public function getMember(): ?Member
{
return $this->member;
}
public function setMember(?Member $member): self
{
$this->member = $member;
return $this;
}
public function getPreviewImage(): ?int
{
return $this->preview_image;
}
public function setPreviewImage(?int $index): self
{
$this->preview_image = $index;
return $this;
}
public function setIsHardcore(bool $is_hardcore): self
{
$this->is_hardcore = $is_hardcore;
return $this;
}
public function getIsHardcore(): ?bool
{
return $this->is_hardcore;
}
public function setMetaDescription(?string $meta_description): self
{
$this->meta_description = $meta_description;
return $this;
}
public function getMetaDescription(): ?string
{
return $this->meta_description;
}
public function setMetaKeywords(?string $meta_keywords): self
{
$this->meta_keywords = $meta_keywords;
return $this;
}
public function getMetaKeywords(): ?string
{
return $this->meta_keywords;
}
/**
* @return Collection|ContentCategory[]
*/
public function getCategories(): Collection
{
return $this->categories;
}
public function addCategory(ContentCategory $category): self
{
if (!$this->categories->contains($category)) {
$this->categories[] = $category;
$category->setContent($this);
}
return $this;
}
public function removeCategory(ContentCategory $category): self
{
if ($this->categories->contains($category)) {
$this->categories->removeElement($category);
// set the owning side to null (unless already changed)
if ($category->getContent() === $this) {
$category->setContent(null);
}
}
return $this;
}
/**
* @param int|ContentCategory $category
*/
public function hasCategory($category): bool
{
if ($category instanceof ContentCategory) {
$id = $category->getId();
} else {
$id = $category;
}
foreach ($this->getCategories() as $cat) {
if ($cat->getId() === $id) {
return true;
}
}
return false;
}
public function isStatusActive(): bool
{
return ContentStatus::ACTIVE === $this->status;
}
public function isImageset(): bool
{
return ContentType::IMAGESET === $this->type;
}
public function isVideo(): bool
{
return ContentType::VIDEO === $this->type;
}
}