<?php
namespace App\Entity;
use App\Repository\CategoryRepository;
use Cofondateur\SocleTechniqueBundle\Annotation\CrudField;
use Cofondateur\SocleTechniqueBundle\Traits\SEOInterface;
use Cofondateur\SocleTechniqueBundle\Traits\SEOTrait;
use Cofondateur\SocleTechniqueBundle\Traits\SluggableInterface;
use Cofondateur\SocleTechniqueBundle\Traits\SluggableTrait;
use Cofondateur\SocleTechniqueBundle\Traits\SortableTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Vich\Uploadable
* @ORM\Entity(repositoryClass=CategoryRepository::class)
*/
class Category implements SEOInterface, SluggableInterface
{
use SEOTrait;
use SortableTrait;
use SluggableTrait;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @CrudField(label="Titre", index=true)
*/
private $title;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @CrudField(label="Sur-titre (page d'accueil)")
*/
private $subTitle;
/**
* @ORM\Column(type="text", nullable=true)
* @CrudField(label="Résumé (page d'accueil)", ckeditor=true)
*/
private $intro;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @CrudField(label="Sous-titre (page détail)")
*/
private $subTitleDetail;
/**
* @ORM\Column(type="text", nullable=true)
* @CrudField(label="Description (page détail)", ckeditor=true)
*/
private $description;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
* @Vich\UploadableField(mapping="default", fileNameProperty="categoryName", size="categorySize")
* @Assert\Expression("this.getCategoryFile() or this.getCategoryName()", message = "Veuillez sélectionner un fichier")
* @Assert\File(
* maxSize = "1200k"
* )
* @CrudField(label="Illustration")
* @var File
*/
private $categoryFile;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @var string
*/
private $categoryName;
/**
* @ORM\Column(type="integer", nullable=true)
*
* @var integer
*/
private $categorySize;
/**
* @ORM\Column(type="datetime", nullable=true)
*
* @var \DateTime
*/
private $categoryFileUpdatedAt;
/**
* @ORM\OneToMany(targetEntity=Product::class, mappedBy="category")
*/
private $products;
public function __construct()
{
$this->products = new ArrayCollection();
}
public function __toString(): string
{
return $this->getTitle() ?? $this->getId() ?? "N/A";
}
public function getId(): ?int
{
return $this->id;
}
/**
* 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 $image
*/
public function setCategoryFile(File $image = null)
{
$this->categoryFile = $image;
if (null !== $image) {
// 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->categoryFileUpdatedAt = new \DateTimeImmutable();
}
}
public function getCategoryFile()
{
return $this->categoryFile;
}
public function getCategoryName(): ?string
{
return $this->categoryName;
}
public function setCategoryName(?string $categoryName): self
{
$this->categoryName = $categoryName;
return $this;
}
public function getCategorySize(): ?int
{
return $this->categorySize;
}
public function setCategorySize(?int $categorySize): self
{
$this->categorySize = $categorySize;
return $this;
}
public function getCategoryFileUpdatedAt(): ?\DateTimeInterface
{
return $this->categoryFileUpdatedAt;
}
public function setCategoryFileUpdatedAt(?\DateTimeInterface $categoryFileUpdatedAt): self
{
$this->categoryFileUpdatedAt = $categoryFileUpdatedAt;
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
public function getSubTitle(): ?string
{
return $this->subTitle;
}
public function setSubTitle(?string $subTitle): self
{
$this->subTitle = $subTitle;
return $this;
}
public function getIntro(): ?string
{
return $this->intro;
}
public function setIntro(?string $intro): self
{
$this->intro = $intro;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
/**
* @return Collection|Product[]
*/
public function getProducts(): Collection
{
return $this->products;
}
public function addProduct(Product $product): self
{
if (!$this->products->contains($product)) {
$this->products[] = $product;
$product->setCategory($this);
}
return $this;
}
public function removeProduct(Product $product): self
{
if ($this->products->removeElement($product)) {
// set the owning side to null (unless already changed)
if ($product->getCategory() === $this) {
$product->setCategory(null);
}
}
return $this;
}
public function getSubTitleDetail(): ?string
{
return $this->subTitleDetail;
}
public function setSubTitleDetail(?string $subTitleDetail): self
{
$this->subTitleDetail = $subTitleDetail;
return $this;
}
}