src/Entity/Category.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CategoryRepository;
  4. use Cofondateur\SocleTechniqueBundle\Annotation\CrudField;
  5. use Cofondateur\SocleTechniqueBundle\Traits\SEOInterface;
  6. use Cofondateur\SocleTechniqueBundle\Traits\SEOTrait;
  7. use Cofondateur\SocleTechniqueBundle\Traits\SluggableInterface;
  8. use Cofondateur\SocleTechniqueBundle\Traits\SluggableTrait;
  9. use Cofondateur\SocleTechniqueBundle\Traits\SortableTrait;
  10. use Doctrine\Common\Collections\ArrayCollection;
  11. use Doctrine\Common\Collections\Collection;
  12. use Doctrine\ORM\Mapping as ORM;
  13. use Symfony\Component\HttpFoundation\File\File;
  14. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  15. use Symfony\Component\Validator\Constraints as Assert;
  16. /**
  17.  * @Vich\Uploadable
  18.  * @ORM\Entity(repositoryClass=CategoryRepository::class)
  19.  */
  20. class Category implements SEOInterfaceSluggableInterface
  21. {
  22.     
  23.     use SEOTrait;
  24.     use SortableTrait;
  25.     use SluggableTrait;
  26.     
  27.     /**
  28.      * @ORM\Id
  29.      * @ORM\GeneratedValue
  30.      * @ORM\Column(type="integer")
  31.      */
  32.     private $id;
  33.     /**
  34.      * @ORM\Column(type="string", length=255, nullable=true)
  35.      * @CrudField(label="Titre", index=true)
  36.      */
  37.     private $title;
  38.     /**
  39.      * @ORM\Column(type="string", length=255, nullable=true)
  40.      * @CrudField(label="Sur-titre (page d'accueil)")
  41.      */
  42.     private $subTitle;
  43.     /**
  44.      * @ORM\Column(type="text", nullable=true)
  45.      * @CrudField(label="Résumé (page d'accueil)", ckeditor=true)
  46.      */
  47.     private $intro;
  48.     /**
  49.      * @ORM\Column(type="string", length=255, nullable=true)
  50.      * @CrudField(label="Sous-titre (page détail)")
  51.      */
  52.     private $subTitleDetail;
  53.     /**
  54.      * @ORM\Column(type="text", nullable=true)
  55.      * @CrudField(label="Description (page détail)", ckeditor=true)
  56.      */
  57.     private $description;
  58.     /**
  59.      * NOTE: This is not a mapped field of entity metadata, just a simple property.
  60.      * @Vich\UploadableField(mapping="default", fileNameProperty="categoryName", size="categorySize")
  61.      * @Assert\Expression("this.getCategoryFile() or this.getCategoryName()", message = "Veuillez sélectionner un fichier")
  62.      * @Assert\File(
  63.      *     maxSize = "1200k"
  64.      * )
  65.      *  @CrudField(label="Illustration")
  66.      * @var File
  67.      */
  68.     private $categoryFile;
  69.     /**
  70.      * @ORM\Column(type="string", length=255, nullable=true)
  71.      *
  72.      * @var string
  73.      */
  74.     private $categoryName;
  75.     /**
  76.      * @ORM\Column(type="integer", nullable=true)
  77.      *
  78.      * @var integer
  79.      */
  80.     private $categorySize;
  81.     /**
  82.      * @ORM\Column(type="datetime", nullable=true)
  83.      *
  84.      * @var \DateTime
  85.      */
  86.     private $categoryFileUpdatedAt;
  87.     /**
  88.      * @ORM\OneToMany(targetEntity=Product::class, mappedBy="category")
  89.      */
  90.     private $products;
  91.     public function __construct()
  92.     {
  93.         $this->products = new ArrayCollection();
  94.     }
  95.     public function __toString(): string
  96.     {
  97.         return $this->getTitle() ?? $this->getId() ?? "N/A";
  98.     }
  99.     public function getId(): ?int
  100.     {
  101.         return $this->id;
  102.     }
  103.     /**
  104.      * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
  105.      * of 'UploadedFile' is injected into this setter to trigger the  update. If this
  106.      * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
  107.      * must be able to accept an instance of 'File' as the bundle will inject one here
  108.      * during Doctrine hydration.
  109.      *
  110.      * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
  111.      */
  112.     public function setCategoryFile(File $image null)
  113.     {
  114.         $this->categoryFile $image;
  115.         if (null !== $image) {
  116.             // It is required that at least one field changes if you are using doctrine
  117.             // otherwise the event listeners won't be called and the file is lost
  118.             $this->categoryFileUpdatedAt = new \DateTimeImmutable();
  119.         }
  120.     }
  121.     public function getCategoryFile()
  122.     {
  123.         return $this->categoryFile;
  124.     }
  125.     public function getCategoryName(): ?string
  126.     {
  127.         return $this->categoryName;
  128.     }
  129.     public function setCategoryName(?string $categoryName): self
  130.     {
  131.         $this->categoryName $categoryName;
  132.         return $this;
  133.     }
  134.     public function getCategorySize(): ?int
  135.     {
  136.         return $this->categorySize;
  137.     }
  138.     public function setCategorySize(?int $categorySize): self
  139.     {
  140.         $this->categorySize $categorySize;
  141.         return $this;
  142.     }
  143.     public function getCategoryFileUpdatedAt(): ?\DateTimeInterface
  144.     {
  145.         return $this->categoryFileUpdatedAt;
  146.     }
  147.     public function setCategoryFileUpdatedAt(?\DateTimeInterface $categoryFileUpdatedAt): self
  148.     {
  149.         $this->categoryFileUpdatedAt $categoryFileUpdatedAt;
  150.         return $this;
  151.     }
  152.     public function getTitle(): ?string
  153.     {
  154.         return $this->title;
  155.     }
  156.     public function setTitle(?string $title): self
  157.     {
  158.         $this->title $title;
  159.         return $this;
  160.     }
  161.     public function getSubTitle(): ?string
  162.     {
  163.         return $this->subTitle;
  164.     }
  165.     public function setSubTitle(?string $subTitle): self
  166.     {
  167.         $this->subTitle $subTitle;
  168.         return $this;
  169.     }
  170.     public function getIntro(): ?string
  171.     {
  172.         return $this->intro;
  173.     }
  174.     public function setIntro(?string $intro): self
  175.     {
  176.         $this->intro $intro;
  177.         return $this;
  178.     }
  179.     public function getDescription(): ?string
  180.     {
  181.         return $this->description;
  182.     }
  183.     public function setDescription(?string $description): self
  184.     {
  185.         $this->description $description;
  186.         return $this;
  187.     }
  188.     /**
  189.      * @return Collection|Product[]
  190.      */
  191.     public function getProducts(): Collection
  192.     {
  193.         return $this->products;
  194.     }
  195.     public function addProduct(Product $product): self
  196.     {
  197.         if (!$this->products->contains($product)) {
  198.             $this->products[] = $product;
  199.             $product->setCategory($this);
  200.         }
  201.         return $this;
  202.     }
  203.     public function removeProduct(Product $product): self
  204.     {
  205.         if ($this->products->removeElement($product)) {
  206.             // set the owning side to null (unless already changed)
  207.             if ($product->getCategory() === $this) {
  208.                 $product->setCategory(null);
  209.             }
  210.         }
  211.         return $this;
  212.     }
  213.     public function getSubTitleDetail(): ?string
  214.     {
  215.         return $this->subTitleDetail;
  216.     }
  217.     public function setSubTitleDetail(?string $subTitleDetail): self
  218.     {
  219.         $this->subTitleDetail $subTitleDetail;
  220.         return $this;
  221.     }
  222. }