<?php
namespace App\Entity;
use App\Repository\ProductRepository;
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\SortableInterface;
use Cofondateur\SocleTechniqueBundle\Traits\SortableTrait;
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;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use App\Form\ParagraphFormType;
/**
* @Vich\Uploadable
* @ORM\Entity(repositoryClass=ProductRepository::class)
*/
class Product implements SEOInterface, SluggableInterface, SortableInterface
{
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="text", nullable=true)
* @CrudField(label="Introduction", ckeditor=true)
*/
private $intro;
/**
* @ORM\ManyToOne(targetEntity=Category::class, inversedBy="products")
* @CrudField(label="Catégorie", index=true)
*/
private $category;
/**
* @ORM\OneToMany(targetEntity=Paragraph::class, mappedBy="product", cascade={"persist", "remove"})
* @CrudField(label="Paragraphes", formType=ParagraphFormType::class, tab="Paragraphes")
*/
private $paragraphs;
/**
* @ORM\OneToMany(targetEntity=Achievement::class, mappedBy="product")
*/
private $achievements;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
* @Vich\UploadableField(mapping="default", fileNameProperty="productName", size="productSize")
* @Assert\Expression("this.getProductFile() or this.getProductName()", message = "Veuillez sélectionner un fichier")
* @Assert\File(
* maxSize = "1200k"
* )
* @CrudField(label="Visuel principal")
* @var File
*/
private $productFile;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @var string
*/
private $productName;
/**
* @ORM\Column(type="integer", nullable=true)
*
* @var integer
*/
private $productSize;
/**
* @ORM\Column(type="datetime", nullable=true)
*
* @var \DateTime
*/
private $productFileUpdatedAt;
/**
* @ORM\Column(type="boolean")
* @CrudField(label="Affiché", index=true)
*/
private $display = 1;
public function __construct()
{
$this->paragraphs = new ArrayCollection();
$this->achievements = 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 setProductFile(File $image = null)
{
$this->productFile = $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->productFileUpdatedAt = new \DateTimeImmutable();
}
}
public function getProductFile()
{
return $this->productFile;
}
public function getProductName(): ?string
{
return $this->productName;
}
public function setProductName(?string $productName): self
{
$this->productName = $productName;
return $this;
}
public function getProductSize(): ?int
{
return $this->productSize;
}
public function setProductSize(?int $productSize): self
{
$this->productSize = $productSize;
return $this;
}
public function getProductFileUpdatedAt(): ?\DateTimeInterface
{
return $this->productFileUpdatedAt;
}
public function setProductFileUpdatedAt(?\DateTimeInterface $productFileUpdatedAt): self
{
$this->productFileUpdatedAt = $productFileUpdatedAt;
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
public function getIntro(): ?string
{
return $this->intro;
}
public function setIntro(?string $intro): self
{
$this->intro = $intro;
return $this;
}
public function getCategory(): ?Category
{
return $this->category;
}
public function setCategory(?Category $category): self
{
$this->category = $category;
return $this;
}
/**
* @return Collection|Paragraph[]
*/
public function getParagraphs(): Collection
{
return $this->paragraphs;
}
public function addParagraph(Paragraph $paragraph): self
{
if (!$this->paragraphs->contains($paragraph)) {
$this->paragraphs[] = $paragraph;
$paragraph->setProduct($this);
}
return $this;
}
public function removeParagraph(Paragraph $paragraph): self
{
if ($this->paragraphs->removeElement($paragraph)) {
// set the owning side to null (unless already changed)
if ($paragraph->getProduct() === $this) {
$paragraph->setProduct(null);
}
}
return $this;
}
/**
* @return Collection|Achievement[]
*/
public function getAchievements(): Collection
{
return $this->achievements;
}
public function addAchievement(Achievement $achievement): self
{
if (!$this->achievements->contains($achievement)) {
$this->achievements[] = $achievement;
$achievement->setProduct($this);
}
return $this;
}
public function removeAchievement(Achievement $achievement): self
{
if ($this->achievements->removeElement($achievement)) {
// set the owning side to null (unless already changed)
if ($achievement->getProduct() === $this) {
$achievement->setProduct(null);
}
}
return $this;
}
public function getDisplay(): ?bool
{
return $this->display;
}
public function setDisplay(bool $display): self
{
$this->display = $display;
return $this;
}
}