<?php
namespace Shopping\Entity;
use Core\Entity\Traits\EntityTrait;
use Core\Entity\Traits\PageTrait;
use Core\Entity\Traits\TranslatableTrait;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\HttpFoundation\File\File;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @Vich\Uploadable
* @ORM\Entity(repositoryClass="Shopping\Repository\CategoryRepository")
* @ORM\Table(name="shopping_category")
*/
class Category
{
use TranslatableTrait;
use EntityTrait {
EntityTrait::__construct as private __entityConstruct;
}
use PageTrait {
PageTrait::__construct as private __pageConstruct;
}
/**
* @ORM\Column(type="string", length=255)
*/
private $reference;
/**
* @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
* @ORM\JoinColumn(name="parent", referencedColumnName="id", nullable=true)
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity="Category", mappedBy="parent", cascade={"remove"}, orphanRemoval=true)
*/
private $children;
/**
* @ORM\ManyToMany(targetEntity="Product", mappedBy="categories", orphanRemoval=true)
*/
private $products;
/**
* @Gedmo\Slug(fields={"reference"})
* @ORM\Column(length=128)
*/
private $slug;
/**
* Image de la catégorie
*
* @ORM\Column(type="string", length=255, nullable=true)
* @var string
*/
private $image;
/**
* @Vich\UploadableField(mapping="images", fileNameProperty="image")
* @var File
*/
private $imageFile;
/**
* Image de la catégorie
*
* @ORM\Column(type="string", length=255, nullable=true)
* @var string
*/
private $cover;
/**
* @Vich\UploadableField(mapping="images", fileNameProperty="cover")
* @var File
*/
private $coverFile;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $navigation_active;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $navigation_sortorder;
/**
* @ORM\OneToMany(targetEntity="App\Entity\PageBreadcrumb", mappedBy="page_default", cascade={"persist", "remove"}, orphanRemoval=true)
*/
private $breadcrumbs;
private $collections = [
'breadcrumb'
];
/**
* Constructeur
* @throws \Exception
*/
public function __construct()
{
$this->__entityConstruct();
$this->__pageConstruct();
$this->children = new ArrayCollection();
$this->products = new ArrayCollection();
$this->breadcrumbs = new ArrayCollection();
$this->setReference(uniqid());
}
public function getCollections()
{
return $this->collections;
}
/**
* Collection - Breadcrumbs
*/
public function getBreadcrumbs()
{
return $this->breadcrumbs;
}
public function setBreadcrumbs($breadcrumbs)
{
$this->breadcrumbs = $breadcrumbs;
return $this;
}
public function addBreadcrumb($breadcrumb)
{
$this->breadcrumbs->add($breadcrumb);
$breadcrumb->setPageDefault($this);
return $this;
}
public function removeBreadcrumb($breadcrumb)
{
$this->breadcrumbs->removeElement($breadcrumb);
return $this;
}
/**
* @return mixed
*/
public function getNavigationActive()
{
return $this->navigation_active;
}
/**
* @param mixed $navigation_active
*/
public function setNavigationActive($navigation_active): void
{
$this->navigation_active = $navigation_active;
}
/**
* @return mixed
*/
public function getNavigationSortorder()
{
return $this->navigation_sortorder;
}
/**
* @param mixed $navigation_sortorder
*/
public function setNavigationSortorder($navigation_sortorder): void
{
$this->navigation_sortorder = $navigation_sortorder;
}
/**
* @return mixed
*/
public function getReference()
{
return $this->reference;
}
/**
* @param mixed $reference
*/
public function setReference($reference): void
{
$this->reference = $reference;
}
/**
* @return mixed
*/
public function getParent()
{
return $this->parent;
}
/**
* @param Category $parent
*/
public function setParent(Category $parent)
{
$this->parent = $parent;
}
/**
* @return ArrayCollection
*/
public function getChildren()
{
return $this->children;
}
/**
* @param Category $child
*/
public function addChild(Category $child)
{
$this->children[] = $child;
$child->setParent($this);
}
/**
* @return ArrayCollection
*/
public function getProducts()
{
return $this->products;
}
/**
* @param Product $item
*/
public function addProduct(Product $item)
{
$this->products[] = $item;
}
/**
* @return string
*/
public function getImage()
{
if ($this->image == '') {
return 'no_image.png';
}
return $this->image;
}
/**
* @param string $image
*/
public function setImage($image)
{
$this->image = $image;
}
/**
* @return File
*/
public function getImageFile()
{
return $this->imageFile;
}
/**
* @param File $imageFile
* @throws \Exception
*/
public function setImageFile($imageFile)
{
$this->imageFile = $imageFile;
if ($imageFile) {
$this->setUpdatedAt(new \DateTime('now'));
}
}
public function getCover()
{
if ($this->cover == '') {
return 'no_image.png';
}
return $this->cover;
}
public function setCover($cover)
{
$this->cover = $cover;
}
public function getCoverFile()
{
return $this->coverFile;
}
public function setCoverFile($coverFile)
{
$this->coverFile = $coverFile;
if ($coverFile) {
$this->setUpdatedAt(new \DateTime('now'));
}
}
/**
* Retourne L'URL personnalisé si elle existe sinon le slug par defaut
*/
public function getSlug()
{
if ($this->getUri() != '' && $this->getUri() != '/') {
$slug = $this->getUri();
} else {
$slug = '/' . $this->slug;
}
return substr_replace($slug, '/' . $this->getId() . '-', 0, 1);
}
/**
* Retourne toute la file de catégorie parents
*/
public function getParentsCategory()
{
if ($this->getParent() != null) {
return array_merge([$this->getParent()], $this->getParent()->getParentsCategory());
} else {
return [];
}
}
/**
* @return string
*/
public function __toString()
{
return (string)$this->getReference();
}
}