<?php
namespace App\Entity;
use App\Utils\File\FileServiceTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass="App\Repository\NewsRepository")
* @Vich\Uploadable
*/
class News implements \JsonSerializable
{
use FileServiceTrait;
const FILE_PICTURE = "picture";
const FILES = [
self::FILE_PICTURE => [
'types' => ['gif', 'jpg', 'jpeg', 'png', 'tif', 'svg'],
'dir' => '/news',
'size' => 5*1024*1024,
]
];
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Company", inversedBy="news")
* @ORM\JoinColumn(nullable=false)
*/
private $company;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\NewsCategory", inversedBy="news")
*/
private $categories;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="string", length=255)
*/
private $author;
/**
* @ORM\Column(type="text")
*/
private $description;
/**
* @ORM\Column(type="datetime", options={"default": "CURRENT_TIMESTAMP"})
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", options={"default": "CURRENT_TIMESTAMP"})
*/
private $publishedAt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private $updatedAt;
/**
* @ORM\Column(type="boolean", options={"default":"0"})
*/
private $featured = FALSE;
/**
* @ORM\Column(type="boolean", options={"default":"0"})
*/
private $companyFeatured = FALSE;
/**
* @ORM\Column(type="boolean", options={"default": "0"})
*/
private $active = FALSE;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @var string|null
*/
private $thumbnail;
/**
* @Vich\UploadableField(mapping="thumbnails", fileNameProperty="thumbnail")
* @var File|null
*/
private $pictureFile;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $slug;
/**
* @ORM\Column(type="boolean", options={"default": "0"})
*/
private $main = FALSE;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $oldId;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $oldUrl;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $canonicalUrl;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $views;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $contentChars;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $contentWords;
/**
* @ORM\Column(type="string", length=1024, nullable=true)
*/
public $keywords;
public function __construct() {
$this->setCreatedAt(new \DateTime());
$this->updatedAt = (new \DateTime());
$this->categories = new ArrayCollection();
}
/**
* @return int|null
*/
public function getId(): ?int
{
return $this->id;
}
/**
* @return Company
*/
public function getCompany(): ?Company {
return $this->company;
}
/**
* @param Company $company
*
* @return self
*/
public function setCompany(Company $company): self {
$this->company = $company;
return $this;
}
/**
* @return Collection|NewsCategory[]
*/
public function getCategories(): Collection
{
return $this->categories;
}
public function addCategory(NewsCategory $category): self
{
if (!$this->categories->contains($category)) {
$this->categories[] = $category;
}
return $this;
}
public function removeCategory(NewsCategory $category): self
{
if ($this->categories->contains($category)) {
$this->categories->removeElement($category);
}
return $this;
}
public function setCategories(ArrayCollection $categories): self
{
$this->categories = $categories;
return $this;
}
/**
* @return string|null
*/
public function getTitle(): ?string
{
return $this->title;
}
/**
* @param string $title
*
* @return self
*/
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
/**
* @return string|null
*/
public function getAuthor(): ?string
{
return $this->author;
}
/**
* @param string $author
*
* @return self
*/
public function setAuthor(string $author): self
{
$this->author = $author;
return $this;
}
/**
* @return string|null
*/
public function getDescription(): ?string
{
return $this->description;
}
/**
* @param string|null $description
*
* @return self
*/
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
/**
* @return \DateTime
*/
public function getCreatedAt(): \DateTime {
return $this->createdAt;
}
/**
* @param \DateTime $createdAt
*
* @return self
*/
public function setCreatedAt(\DateTime $createdAt): self {
$this->createdAt = $createdAt;
return $this;
}
/**
* @return \DateTime
*/
public function getPublishedAt(): ?\DateTime {
return $this->publishedAt;
}
/**
* @param \DateTime $publishedAt
*
* @return self
*/
public function setPublishedAt(\DateTime $publishedAt): self {
$this->publishedAt = $publishedAt;
return $this;
}
/**
* @return bool
*/
public function getFeatured(): bool {
return $this->featured;
}
/**
* @param bool $featured
*
* @return self
*/
public function setFeatured(bool $featured): self {
$this->featured = $featured;
return $this;
}
/**
* @return bool
*/
public function getCompanyFeatured(): bool {
return $this->companyFeatured;
}
/**
* @param bool $companyFeatured
*
* @return self
*/
public function setCompanyFeatured(bool $companyFeatured): self {
$this->companyFeatured = $companyFeatured;
return $this;
}
/**
* @return bool
*/
public function getActive() {
return $this->active;
}
/**
* @param bool $active
*
* @return self
*/
public function setActive(bool $active): self {
$this->active = $active;
return $this;
}
/**
* Get the value of thumbnail
*/
public function getThumbnail()
{
return $this->thumbnail;
}
/**
* Set the value of thumbnail
*
* @return self
*/
public function setThumbnail(?string $thumbnail)
{
$this->thumbnail = $thumbnail;
return $this;
}
/**
* Get the value of pictureFile
*/
public function getPictureFile()
{
return $this->pictureFile;
}
/**
* Set the value of pictureFile
*
* @return self
*/
public function setPictureFile(?UploadedFile $pictureFile = null)
{
$this->pictureFile = $pictureFile;
if ($pictureFile) {
$this->setPicture($pictureFile, null);
$this->updatedAt = (new \DateTime());
}
return $this;
}
/**
* @return string|null
*/
public function getSlug(): ?string
{
return $this->slug;
}
/**
* @param string $slug
*
* @return self
*/
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
/**
* @return bool
*/
public function getMain() {
return $this->main;
}
/**
* @param bool $main
*
* @return self
*/
public function setMain(bool $main): self {
$this->main = $main;
return $this;
}
/**
* @return string|null
*/
public function getCanonicalUrl(): ?string
{
return $this->canonicalUrl;
}
/**
* @param string $slug
*
* @return self
*/
public function setCanonicalUrl(?string $canonicalUrl): self
{
$this->canonicalUrl = $canonicalUrl;
return $this;
}
/**
* @return string|null
*/
public function getPicturePreview(): ?string {
$file = $this->getFileService()->getFile($this, self::FILE_PICTURE);
if ($file) {
$filePath = $file->getFilePath() . DIRECTORY_SEPARATOR . $file->getFileNameHash();
return $filePath;
}
return null;
}
/**
* @return File|null
*/
public function getPicture(){
return $this->getFileService()->getFile($this, self::FILE_PICTURE);
}
/**
* @return self
*/
public function setPicture(?UploadedFile $file, $author = null): self {
if ($file) {
$userId = $this->getCompany() ? $this->getCompany()->getUser()->getId() : 1;
$oldFile = $this->getFileService()->getFile($this, self::FILE_PICTURE);
$newFile = $this->getFileService()->upload($file, $this, self::FILE_PICTURE, true, $author, $userId);
if ($newFile && $oldFile) {
$this->getFileService()->delete($oldFile);
}
}
return $this;
}
/**
* @param UploadedFile|null $file
*
* @return File
*/
public function uploadPicture(?UploadedFile $file): File {
$newFile = null;
if ($file) {
$newFile = $this->getFileService()->upload($file, $this, self::FILE_PICTURE, false);
}
return $newFile;
}
/**
* @return self
*/
public function removePicture(): self{
$file = $this->getFileService()->getFile($this, self::FILE_PICTURE);
if ($file) {
$this->getFileService()->delete($file);
}
return $this;
}
/**
* @return int|null
*/
public function getOldId(): ?int
{
return $this->oldId;
}
/**
* @param string $oldId
*
* @return self
*/
public function setOldId(string $oldId): self
{
$this->oldId = $oldId;
return $this;
}
/**
* @return string|null
*/
public function getOldUrl(): ?string
{
return $this->oldUrl;
}
/**
* @param string $oldUrl
*
* @return self
*/
public function setOldUrl(string $oldUrl): self
{
$this->oldUrl = $oldUrl;
return $this;
}
/**
* @return int
*/
public function getViews(): ?int
{
return $this->views;
}
/**
* @return self
*/
public function setViews(string $views): self
{
$this->views = $views;
return $this;
}
/**
* @return self
*/
public function addViews(): self
{
$this->views++;
return $this;
}
/**
* @return int
*/
public function getContentChars(): ?int
{
return $this->contentChars;
}
/**
* @return self
*/
public function setContentChars(string $contentChars): self
{
$this->contentChars = $contentChars;
return $this;
}
/**
* @return int
*/
public function getContentWords(): ?int
{
return $this->contentWords;
}
/**
* @return self
*/
public function setContentWords(string $contentWords): self
{
$this->contentWords = $contentWords;
return $this;
}
/**
* Get the value of keywords
*/
public function getKeywords(): ?string
{
return $this->keywords;
}
/**
* Set the value of keywords
*
* @return self
*/
public function setKeywords(?string $keywords)
{
$this->keywords = $keywords;
return $this;
}
public function __toString() {
return $this->getTitle();
}
/**
* Specify data which should be serialized to JSON
*
* @link https://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public function jsonSerialize() {
return [
'id' => $this->getId(),
'picture' => $this->getPicture(),
'company' => $this->getCompany(),
'categories' => $this->getCategories()->getValues(),
'title' => $this->getTitle(),
'author' => $this->getAuthor(),
'descriptions' => $this->getDescription(),
'createdAt' => $this->getCreatedAt(),
'publishedAt' => $this->getPublishedAt(),
'featured' => $this->getFeatured(),
'companyFeatured' => $this->getCompanyFeatured(),
'main' => $this->getMain(),
'active' => $this->getActive(),
'slug' => $this->getSlug(),
'oldId' => $this->getOldId(),
'canonicalUrl' => $this->getCanonicalUrl(),
'keywords' => explode(',',$this->getKeywords())
];
}
}