<?php
namespace App\Entity;
use App\Repository\AchievementRepository;
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;
use App\Form\PhotoFormType;
/**
* @Vich\Uploadable
* @ORM\Entity(repositoryClass=AchievementRepository::class)
*/
class Achievement implements SEOInterface, SluggableInterface
{
use SEOTrait;
use SortableTrait;
use SluggableTrait;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=Product::class, inversedBy="achievements")
* @ORM\JoinColumn(nullable=false)
* @CrudField(label="Produit", index=true)
*/
private $product;
/**
* @ORM\Column(type="string", length=255)
* @CrudField(label="Titre", index=true)
*/
private $title;
/**
* @ORM\Column(type="text", nullable=true)
* @CrudField(label="Description", ckeditor=true)
*/
private $description;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
* @Vich\UploadableField(mapping="default", fileNameProperty="achievementName", size="achievementSize")
* @Assert\Expression("this.getAchievementFile() or this.getAchievementName()", message = "Veuillez sélectionner un fichier")
* @Assert\File(
* maxSize = "1200k"
* )
* @CrudField(label="Fichier")
* @var File
*/
private $achievementFile;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @var string
*/
private $achievementName;
/**
* @ORM\Column(type="integer", nullable=true)
*
* @var integer
*/
private $achievementSize;
/**
* @ORM\Column(type="datetime", nullable=true)
*
* @var \DateTime
*/
private $achievementFileUpdatedAt;
/**
* @ORM\OneToMany(targetEntity=Photo::class, mappedBy="achievement", cascade={"persist", "remove"})
* @CrudField(label="Photos", formType=PhotoFormType::class, tab="Photos")
* @ORM\OrderBy({"position" = "ASC"})
*/
private $photos;
/**
* @ORM\Column(type="datetime")
*/
private $updatedAt;
public function __construct()
{
$this->photos = new ArrayCollection();
$this->updatedAt = new \DateTime();
}
public function __toString(): string
{
return $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 setAchievementFile(File $image = null)
{
$this->achievementFile = $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->achievementFileUpdatedAt = new \DateTimeImmutable();
}
$this->updatedAt = new \DateTime();
}
public function getAchievementFile()
{
return $this->achievementFile;
}
public function getAchievementName(): ?string
{
return $this->achievementName;
}
public function setAchievementName(?string $achievementName): self
{
$this->achievementName = $achievementName;
$this->updatedAt = new \DateTime();
return $this;
}
public function getAchievementSize(): ?int
{
return $this->achievementSize;
}
public function setAchievementSize(?int $achievementSize): self
{
$this->achievementSize = $achievementSize;
$this->updatedAt = new \DateTime();
return $this;
}
public function getAchievementFileUpdatedAt(): ?\DateTimeInterface
{
return $this->achievementFileUpdatedAt;
}
public function setAchievementFileUpdatedAt(?\DateTimeInterface $achievementFileUpdatedAt): self
{
$this->achievementFileUpdatedAt = $achievementFileUpdatedAt;
$this->updatedAt = new \DateTime();
return $this;
}
public function getProduct(): ?Product
{
return $this->product;
}
public function setProduct(?Product $product): self
{
$this->product = $product;
$this->updatedAt = new \DateTime();
return $this;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
$this->updatedAt = new \DateTime();
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
$this->updatedAt = new \DateTime();
return $this;
}
/**
* @return Collection|Photo[]
*/
public function getPhotos(): Collection
{
return $this->photos;
}
public function addPhoto(Photo $photo): self
{
if (!$this->photos->contains($photo)) {
$this->photos[] = $photo;
$photo->setAchievement($this);
}
$this->updatedAt = new \DateTime();
return $this;
}
public function removePhoto(Photo $photo): self
{
if ($this->photos->removeElement($photo)) {
// set the owning side to null (unless already changed)
if ($photo->getAchievement() === $this) {
$photo->setAchievement(null);
}
}
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
}