<?php
namespace App\Entity;
use App\Entity\Traits\Taggable;
use App\Repository\BlogPostRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass=BlogPostRepository::class)
* @Vich\Uploadable()
* @UniqueEntity(fields={"slug"})
* @ORM\HasLifecycleCallbacks
*/
class BlogPost extends SeoEntity
{
use Taggable;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="string", length=255, unique=true)
*/
private $slug;
/**
* @ORM\Column(type="datetime_immutable")
*/
private $createdAt;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private $updatedAt;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $poster;
/**
* @var File|null
* @Vich\UploadableField(mapping="blog", fileNameProperty="poster")
* @Assert\File(
* maxSize= "500k"
* )
*/
private $posterImageFile;
/**
* @ORM\ManyToMany(targetEntity=BlogTag::class, inversedBy="blogPosts")
*/
protected $tags;
/**
* @ORM\Column(type="boolean")
*/
private $published = false;
/**
* @ORM\OneToMany(targetEntity=BlogContent::class, mappedBy="post", orphanRemoval=true, cascade={"persist","remove"})
*/
private $blogContents;
private $blocks;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $shortDescription;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $buttonText;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $buttonLink;
public function __construct()
{
$this->createdAt = new \DateTimeImmutable();
$this->tags = new ArrayCollection();
$this->blocks = new ArrayCollection();
$this->blogContents = new ArrayCollection();
$this->addBlock(new BlogContent());
}
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 getCreatedAt(): ?\DateTimeImmutable
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeImmutable $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeImmutable
{
return $this->updatedAt;
}
public function setUpdatedAt(?\DateTimeImmutable $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getPoster(): ?string
{
return $this->poster;
}
public function setPoster(?string $poster): self
{
$this->poster = $poster;
return $this;
}
/**
* @return Collection<int, BlogTag>
*/
public function getTags(): Collection
{
return $this->tags;
}
public function addTag(BlogTag $tag): self
{
if (NULL === $tag->getName()) {
return $this;
}
if (!$this->tags->contains($tag)) {
$this->tags[] = $tag;
}
return $this;
}
public function removeTag(TagEntityInterface $tag): self
{
$this->tags->removeElement($tag);
return $this;
}
public function isPublished(): ?bool
{
return $this->published;
}
public function setPublished(bool $published): self
{
$this->published = $published;
return $this;
}
/**
* @return File|null
*/
public function getPosterImageFile(): ?File
{
return $this->posterImageFile;
}
/**
* @param File|null $posterImageFile
*/
public function setPosterImageFile(?File $posterImageFile): self
{
$this->posterImageFile = $posterImageFile;
if ($posterImageFile) {
$this->updatedAt = new \DateTimeImmutable();
}
return $this;
}
/**
* @return Collection<int, BlogContent>
*/
public function getBlogContents(): Collection
{
return $this->blogContents;
}
public function addBlogContent(BlogContent $blogContent): self
{
if (!$this->blogContents->contains($blogContent)) {
$this->blogContents[] = $blogContent;
$blogContent->setPost($this);
}
return $this;
}
public function removeBlogContent(BlogContent $blogContent): self
{
if ($this->blogContents->removeElement($blogContent)) {
// set the owning side to null (unless already changed)
if ($blogContent->getPost() === $this) {
$blogContent->setPost(null);
}
}
return $this;
}
/**
* @return ArrayCollection<int, BlogContent>
*/
public function getBlock(): Collection
{
if ($this->blocks) {
return $this->blocks;
}
return $this->blocks = new ArrayCollection($this->getBlogContents()->toArray());
}
/**
* @param BlogContent $blogContent
*/
public function addBlock(BlogContent $blogContent)
{
$this->blocks->add($blogContent);
}
public function removeBlock(BlogContent $blogContent): self
{
if ($this->blocks->removeElement($blogContent)) {
// set the owning side to null (unless already changed)
if ($blogContent->getPost() === $this) {
$blogContent->setPost(null);
}
}
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getShortDescription(): ?string
{
return $this->shortDescription;
}
public function setShortDescription(?string $shortDescription): self
{
$this->shortDescription = $shortDescription;
return $this;
}
public function getButtonText(): ?string
{
return $this->buttonText;
}
public function setButtonText(?string $buttonText): self
{
$this->buttonText = $buttonText;
return $this;
}
public function getButtonLink(): ?string
{
return $this->buttonLink;
}
public function setButtonLink(?string $buttonLink): self
{
$this->buttonLink = $buttonLink;
return $this;
}
/**
* Триггер только на обновление.
* @ORM\PreUpdate
*/
public function onPreUpdate()
{
$this->updatedAt = new \DateTimeImmutable();
}
}