src/ShoppingBundle/Entity/Category.php line 19

Open in your IDE?
  1. <?php
  2. namespace Shopping\Entity;
  3. use Core\Entity\Traits\EntityTrait;
  4. use Core\Entity\Traits\PageTrait;
  5. use Core\Entity\Traits\TranslatableTrait;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Symfony\Component\HttpFoundation\File\File;
  8. use Doctrine\ORM\Mapping as ORM;
  9. use Gedmo\Mapping\Annotation as Gedmo;
  10. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  11. /**
  12.  * @Vich\Uploadable
  13.  * @ORM\Entity(repositoryClass="Shopping\Repository\CategoryRepository")
  14.  * @ORM\Table(name="shopping_category")
  15.  */
  16. class Category
  17. {
  18.     use TranslatableTrait;
  19.     use EntityTrait {
  20.         EntityTrait::__construct as private __entityConstruct;
  21.     }
  22.     use PageTrait {
  23.         PageTrait::__construct as private __pageConstruct;
  24.     }
  25.     /**
  26.      * @ORM\Column(type="string", length=255)
  27.      */
  28.     private $reference;
  29.     /**
  30.      * @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
  31.      * @ORM\JoinColumn(name="parent", referencedColumnName="id", nullable=true)
  32.      */
  33.     private $parent;
  34.     /**
  35.      * @ORM\OneToMany(targetEntity="Category", mappedBy="parent", cascade={"remove"}, orphanRemoval=true)
  36.      */
  37.     private $children;
  38.     /**
  39.      * @ORM\ManyToMany(targetEntity="Product", mappedBy="categories", orphanRemoval=true)
  40.      */
  41.     private $products;
  42.     /**
  43.      * @Gedmo\Slug(fields={"reference"})
  44.      * @ORM\Column(length=128)
  45.      */
  46.     private $slug;
  47.     /**
  48.      * Image de la catégorie
  49.      *
  50.      * @ORM\Column(type="string", length=255, nullable=true)
  51.      * @var string
  52.      */
  53.     private $image;
  54.     /**
  55.      * @Vich\UploadableField(mapping="images", fileNameProperty="image")
  56.      * @var File
  57.      */
  58.     private $imageFile;
  59.     /**
  60.      * Image de la catégorie
  61.      *
  62.      * @ORM\Column(type="string", length=255, nullable=true)
  63.      * @var string
  64.      */
  65.     private $cover;
  66.     /**
  67.      * @Vich\UploadableField(mapping="images", fileNameProperty="cover")
  68.      * @var File
  69.      */
  70.     private $coverFile;
  71.     /**
  72.      * @ORM\Column(type="boolean", nullable=true)
  73.      */
  74.     private $navigation_active;
  75.     /**
  76.      * @ORM\Column(type="integer", nullable=true)
  77.      */
  78.     private $navigation_sortorder;
  79.     /**
  80.      * @ORM\OneToMany(targetEntity="App\Entity\PageBreadcrumb", mappedBy="page_default", cascade={"persist", "remove"}, orphanRemoval=true)
  81.      */
  82.     private $breadcrumbs;
  83.     private $collections = [
  84.         'breadcrumb'
  85.     ];
  86.     /**
  87.      * Constructeur
  88.      * @throws \Exception
  89.      */
  90.     public function __construct()
  91.     {
  92.         $this->__entityConstruct();
  93.         $this->__pageConstruct();
  94.         $this->children = new ArrayCollection();
  95.         $this->products = new ArrayCollection();
  96.         $this->breadcrumbs = new ArrayCollection();
  97.         $this->setReference(uniqid());
  98.     }
  99.     public function getCollections()
  100.     {
  101.         return $this->collections;
  102.     }
  103.     /**
  104.      * Collection - Breadcrumbs
  105.      */
  106.     public function getBreadcrumbs()
  107.     {
  108.         return $this->breadcrumbs;
  109.     }
  110.     public function setBreadcrumbs($breadcrumbs)
  111.     {
  112.         $this->breadcrumbs $breadcrumbs;
  113.         return $this;
  114.     }
  115.     public function addBreadcrumb($breadcrumb)
  116.     {
  117.         $this->breadcrumbs->add($breadcrumb);
  118.         $breadcrumb->setPageDefault($this);
  119.         return $this;
  120.     }
  121.     public function removeBreadcrumb($breadcrumb)
  122.     {
  123.         $this->breadcrumbs->removeElement($breadcrumb);
  124.         return $this;
  125.     }
  126.     /**
  127.      * @return mixed
  128.      */
  129.     public function getNavigationActive()
  130.     {
  131.         return $this->navigation_active;
  132.     }
  133.     /**
  134.      * @param mixed $navigation_active
  135.      */
  136.     public function setNavigationActive($navigation_active): void
  137.     {
  138.         $this->navigation_active $navigation_active;
  139.     }
  140.     /**
  141.      * @return mixed
  142.      */
  143.     public function getNavigationSortorder()
  144.     {
  145.         return $this->navigation_sortorder;
  146.     }
  147.     /**
  148.      * @param mixed $navigation_sortorder
  149.      */
  150.     public function setNavigationSortorder($navigation_sortorder): void
  151.     {
  152.         $this->navigation_sortorder $navigation_sortorder;
  153.     }
  154.     /**
  155.      * @return mixed
  156.      */
  157.     public function getReference()
  158.     {
  159.         return $this->reference;
  160.     }
  161.     /**
  162.      * @param mixed $reference
  163.      */
  164.     public function setReference($reference): void
  165.     {
  166.         $this->reference $reference;
  167.     }
  168.     /**
  169.      * @return mixed
  170.      */
  171.     public function getParent()
  172.     {
  173.         return $this->parent;
  174.     }
  175.     /**
  176.      * @param Category $parent
  177.      */
  178.     public function setParent(Category $parent)
  179.     {
  180.         $this->parent $parent;
  181.     }
  182.     /**
  183.      * @return ArrayCollection
  184.      */
  185.     public function getChildren()
  186.     {
  187.         return $this->children;
  188.     }
  189.     /**
  190.      * @param Category $child
  191.      */
  192.     public function addChild(Category $child)
  193.     {
  194.         $this->children[] = $child;
  195.         $child->setParent($this);
  196.     }
  197.     /**
  198.      * @return ArrayCollection
  199.      */
  200.     public function getProducts()
  201.     {
  202.         return $this->products;
  203.     }
  204.     /**
  205.      * @param Product $item
  206.      */
  207.     public function addProduct(Product $item)
  208.     {
  209.         $this->products[] = $item;
  210.     }
  211.     /**
  212.      * @return string
  213.      */
  214.     public function getImage()
  215.     {
  216.         if ($this->image == '') {
  217.             return 'no_image.png';
  218.         }
  219.         return $this->image;
  220.     }
  221.     /**
  222.      * @param string $image
  223.      */
  224.     public function setImage($image)
  225.     {
  226.         $this->image $image;
  227.     }
  228.     /**
  229.      * @return File
  230.      */
  231.     public function getImageFile()
  232.     {
  233.         return $this->imageFile;
  234.     }
  235.     /**
  236.      * @param File $imageFile
  237.      * @throws \Exception
  238.      */
  239.     public function setImageFile($imageFile)
  240.     {
  241.         $this->imageFile $imageFile;
  242.         if ($imageFile) {
  243.             $this->setUpdatedAt(new \DateTime('now'));
  244.         }
  245.     }
  246.     public function getCover()
  247.     {
  248.         if ($this->cover == '') {
  249.             return 'no_image.png';
  250.         }
  251.         return $this->cover;
  252.     }
  253.     public function setCover($cover)
  254.     {
  255.         $this->cover $cover;
  256.     }
  257.     public function getCoverFile()
  258.     {
  259.         return $this->coverFile;
  260.     }
  261.     public function setCoverFile($coverFile)
  262.     {
  263.         $this->coverFile $coverFile;
  264.         if ($coverFile) {
  265.             $this->setUpdatedAt(new \DateTime('now'));
  266.         }
  267.     }
  268.     /**
  269.      * Retourne L'URL personnalisé si elle existe sinon le slug par defaut
  270.      */
  271.     public function getSlug()
  272.     {
  273.         if ($this->getUri() != '' && $this->getUri() != '/') {
  274.             $slug $this->getUri();
  275.         } else {
  276.             $slug '/' $this->slug;
  277.         }
  278.         return substr_replace($slug'/' $this->getId() . '-'01);
  279.     }
  280.     /**
  281.      * Retourne toute la file de catégorie parents
  282.      */
  283.     public function getParentsCategory()
  284.     {
  285.         if ($this->getParent() != null) {
  286.             return array_merge([$this->getParent()], $this->getParent()->getParentsCategory());
  287.         } else {
  288.             return [];
  289.         }
  290.     }
  291.     /**
  292.      * @return string
  293.      */
  294.     public function __toString()
  295.     {
  296.         return (string)$this->getReference();
  297.     }
  298. }