|
- <?php
-
- namespace Lc\CaracoleBundle\Model\Product;
-
- use Doctrine\Common\Collections\ArrayCollection;
- use Doctrine\Common\Collections\Collection;
- use Doctrine\ORM\Mapping as ORM;
- use Lc\CaracoleBundle\Doctrine\Extension\FilterSectionInterface;
- use Lc\CaracoleBundle\Model\Section\SectionInterface;
- use Lc\SovBundle\Doctrine\Extension\TreeInterface;
- use Lc\SovBundle\Doctrine\Pattern\AbstractFullEntity;
- use Lc\SovBundle\Model\File\FileInterface;
-
- /**
- * @ORM\MappedSuperclass()
- */
- abstract class ProductCategoryModel extends AbstractFullEntity implements TreeInterface, FilterSectionInterface,
- ProductCategoryInterface
- {
- /**
- * @ORM\ManyToOne(targetEntity="Lc\CaracoleBundle\Model\Section\SectionInterface", inversedBy="productCategories")
- * @ORM\JoinColumn(nullable=false)
- */
- protected $section;
-
- /**
- * @ORM\ManyToOne(targetEntity="Lc\CaracoleBundle\Model\Product\ProductCategoryInterface", inversedBy="childrens", fetch="EAGER")
- */
- protected $parent;
-
- /**
- * @ORM\OneToMany(targetEntity="Lc\CaracoleBundle\Model\Product\ProductCategoryInterface", mappedBy="parent" , fetch="EAGER"))
- * @ORM\OrderBy({"position" = "ASC"})
- */
- protected $childrens;
-
- /**
- * @ORM\ManyToMany(targetEntity="Lc\CaracoleBundle\Model\Product\ProductFamilyInterface", mappedBy="productCategories")
- */
- protected $productFamilies;
-
- /**
- * @ORM\Column(type="boolean")
- */
- protected $saleStatus;
-
- /**
- * @ORM\ManyToOne(targetEntity="Lc\SovBundle\Model\File\FileInterface", cascade={"persist", "remove"})
- */
- protected $image;
-
-
- public function __construct()
- {
- $this->childrens = new ArrayCollection();
- $this->productFamilies = new ArrayCollection();
- }
-
- public function __toString()
- {
- $parent = $this->getParent();
- $title = $parent ? $parent->getTitle() . ' - ' : '';
- $title .= $this->getTitle();
-
- return $title;
- }
-
- public function getSection(): SectionInterface
- {
- return $this->section;
- }
-
- public function setSection(SectionInterface $section): self
- {
- $this->section = $section;
-
- return $this;
- }
-
- public function getParent(): ?self
- {
- return $this->parent;
- }
-
- public function setParent(?ProductCategoryInterface $productCategory): self
- {
- $this->parent = $productCategory;
-
- return $this;
- }
-
- public function getParentCategory()
- {
- if ($this->getParent()) {
- return $this->getParent();
- }
-
- return $this;
- }
-
- /**
- * @return Collection|self[]
- */
- public function getChildrens(): Collection
- {
- // @TODO : les lignes ci-dessous ne devraient pas exister, sert à résoudre le problème d'ordre dans le menu
- $iterator = $this->childrens->getIterator();
- $iterator->uasort(
- function ($a, $b) {
- return ($a->getPosition() < $b->getPosition()) ? -1 : 1;
- }
- );
-
- return new ArrayCollection(iterator_to_array($iterator));
- }
-
- public function addChildren(ProductCategoryInterface $productCategory): self
- {
- if (!$this->childrens->contains($productCategory)) {
- $this->childrens[] = $productCategory;
- $productCategory->setParent($this);
- }
-
- return $this;
- }
-
- public function removeChildren(ProductCategoryInterface $productCategory): self
- {
- if ($this->childrens->contains($productCategory)) {
- $this->childrens->removeElement($productCategory);
- // set the owning side to null (unless already changed)
- if ($productCategory->getParent() === $this) {
- $productCategory->setParent(null);
- }
- }
-
- return $this;
- }
-
- /**
- * @return Collection|ProductFamilyInterface[]
- */
- public function getProductFamilies(): Collection
- {
- return $this->productFamilies;
- }
-
- public function addProductFamily(ProductFamilyInterface $productFamily): self
- {
- if (!$this->productFamilies->contains($productFamily)) {
- $this->productFamilies[] = $productFamily;
- $productFamily->addProductCategory($this);
- }
-
- return $this;
- }
-
- public function removeProductFamily(ProductFamilyInterface $productFamily): self
- {
- if ($this->productFamilies->contains($productFamily)) {
- $this->productFamilies->removeElement($productFamily);
- $productFamily->removeProductCategory($this);
- }
-
- return $this;
- }
-
- public function countProductFamilies($status = null)
- {
- $count = 0;
-
- foreach ($this->getProductFamilies() as $productFamily) {
- if ($status === null || ($status !== null && $productFamily->getStatus() == $status)) {
- $count++;
- }
- }
-
- return $count;
- }
-
- public function getSaleStatus(): ?bool
- {
- return $this->saleStatus;
- }
-
- public function setSaleStatus(bool $saleStatus): self
- {
- $this->saleStatus = $saleStatus;
-
- return $this;
- }
-
- public function getImage(): ?FileInterface
- {
- return $this->image;
- }
-
- public function setImage(?FileInterface $image): self
- {
- $this->image = $image;
-
- return $this;
- }
-
- }
|