|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?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\FilterMerchantInterface;
- use Lc\CaracoleBundle\Model\Merchant\MerchantInterface;
- use Lc\CaracoleBundle\Model\Section\SectionInterface;
- use Lc\SovBundle\Doctrine\Extension\TreeInterface;
- use Lc\SovBundle\Doctrine\Pattern\AbstractFullEntity;
-
- /**
- * @ORM\MappedSuperclass()
- */
- abstract class ProductCategoryModel extends AbstractFullEntity implements TreeInterface, FilterMerchantInterface
- {
-
- /**
- * @ORM\ManyToOne(targetEntity="Lc\CaracoleBundle\Model\Merchant\MerchantInterface", inversedBy="productCategories")
- * @ORM\JoinColumn(nullable=false)
- */
- protected $merchant;
-
- /**
- * @ORM\ManyToOne(targetEntity="Lc\CaracoleBundle\Model\Product\ProductCategoryInterface", inversedBy="childrens")
- */
- 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\CaracoleBundle\Model\Section\SectionInterface", inversedBy="productCategories")
- * @ORM\JoinColumn(nullable=false)
- */
- protected $section;
-
- 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 getParent(): ?self
- {
- return $this->parent;
- }
-
- public function setParent(?self $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(self $productCategory): self
- {
- if (!$this->childrens->contains($productCategory)) {
- $this->childrens[] = $productCategory;
- $productCategory->setParent($this);
- }
-
- return $this;
- }
-
- public function removeChildren(self $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 getMerchant(): ?MerchantInterface
- {
- return $this->merchant;
- }
-
- public function setMerchant(?MerchantInterface $merchant): self
- {
- $this->merchant = $merchant;
-
- return $this;
- }
-
- public function getSaleStatus(): ?bool
- {
- return $this->saleStatus;
- }
-
- public function setSaleStatus(bool $saleStatus): self
- {
- $this->saleStatus = $saleStatus;
-
- return $this;
- }
-
- public function getSection(): ?SectionInterface
- {
- return $this->section;
- }
-
- public function setSection(?SectionInterface $section): self
- {
- $this->section = $section;
-
- return $this;
- }
-
- }
|