You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

187 lines
4.8KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Model\Product;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Lc\CaracoleBundle\Doctrine\Extension\FilterSectionInterface;
  7. use Lc\CaracoleBundle\Model\Section\SectionInterface;
  8. use Lc\SovBundle\Doctrine\Extension\TreeInterface;
  9. use Lc\SovBundle\Doctrine\Pattern\AbstractFullEntity;
  10. /**
  11. * @ORM\MappedSuperclass()
  12. */
  13. abstract class ProductCategoryModel extends AbstractFullEntity implements TreeInterface, FilterSectionInterface
  14. {
  15. /**
  16. * @ORM\ManyToOne(targetEntity="Lc\CaracoleBundle\Model\Section\SectionInterface")
  17. * @ORM\JoinColumn(nullable=false)
  18. */
  19. protected $section;
  20. /**
  21. * @ORM\ManyToOne(targetEntity="Lc\CaracoleBundle\Model\Product\ProductCategoryInterface", inversedBy="childrens")
  22. */
  23. protected $parent;
  24. /**
  25. * @ORM\OneToMany(targetEntity="Lc\CaracoleBundle\Model\Product\ProductCategoryInterface", mappedBy="parent" , fetch="EAGER"))
  26. * @ORM\OrderBy({"position" = "ASC"})
  27. */
  28. protected $childrens;
  29. /**
  30. * @ORM\ManyToMany(targetEntity="Lc\CaracoleBundle\Model\Product\ProductFamilyInterface", mappedBy="productCategories")
  31. */
  32. protected $productFamilies;
  33. /**
  34. * @ORM\Column(type="boolean")
  35. */
  36. protected $saleStatus;
  37. public function __construct()
  38. {
  39. $this->childrens = new ArrayCollection();
  40. $this->productFamilies = new ArrayCollection();
  41. }
  42. public function __toString()
  43. {
  44. $parent = $this->getParent();
  45. $title = $parent ? $parent->getTitle() . ' - ' : '';
  46. $title .= $this->getTitle();
  47. return $title;
  48. }
  49. public function getSection(): SectionInterface
  50. {
  51. return $this->section;
  52. }
  53. public function setSection(SectionInterface $section): self
  54. {
  55. $this->section = $section;
  56. return $this;
  57. }
  58. public function getParent(): ?self
  59. {
  60. return $this->parent;
  61. }
  62. public function setParent(?self $productCategory): self
  63. {
  64. $this->parent = $productCategory;
  65. return $this;
  66. }
  67. public function getParentCategory()
  68. {
  69. if ($this->getParent()) {
  70. return $this->getParent();
  71. }
  72. return $this;
  73. }
  74. /**
  75. * @return Collection|self[]
  76. */
  77. public function getChildrens(): Collection
  78. {
  79. //TODO les lignes ci-dessous ne devraient pas exister, sert à résoudre le problème d'ordre dans le menu
  80. $iterator = $this->childrens->getIterator();
  81. $iterator->uasort(
  82. function ($a, $b) {
  83. return ($a->getPosition() < $b->getPosition()) ? -1 : 1;
  84. }
  85. );
  86. return new ArrayCollection(iterator_to_array($iterator));
  87. }
  88. public function addChildren(self $productCategory): self
  89. {
  90. if (!$this->childrens->contains($productCategory)) {
  91. $this->childrens[] = $productCategory;
  92. $productCategory->setParent($this);
  93. }
  94. return $this;
  95. }
  96. public function removeChildren(self $productCategory): self
  97. {
  98. if ($this->childrens->contains($productCategory)) {
  99. $this->childrens->removeElement($productCategory);
  100. // set the owning side to null (unless already changed)
  101. if ($productCategory->getParent() === $this) {
  102. $productCategory->setParent(null);
  103. }
  104. }
  105. return $this;
  106. }
  107. /**
  108. * @return Collection|ProductFamilyInterface[]
  109. */
  110. public function getProductFamilies(): Collection
  111. {
  112. return $this->productFamilies;
  113. }
  114. public function addProductFamily(ProductFamilyInterface $productFamily): self
  115. {
  116. if (!$this->productFamilies->contains($productFamily)) {
  117. $this->productFamilies[] = $productFamily;
  118. $productFamily->addProductCategory($this);
  119. }
  120. return $this;
  121. }
  122. public function removeProductFamily(ProductFamilyInterface $productFamily): self
  123. {
  124. if ($this->productFamilies->contains($productFamily)) {
  125. $this->productFamilies->removeElement($productFamily);
  126. $productFamily->removeProductCategory($this);
  127. }
  128. return $this;
  129. }
  130. public function countProductFamilies($status = null)
  131. {
  132. $count = 0;
  133. foreach ($this->getProductFamilies() as $productFamily) {
  134. if ($status === null || ($status !== null && $productFamily->getStatus() == $status)) {
  135. $count++;
  136. }
  137. }
  138. return $count;
  139. }
  140. public function getSaleStatus(): ?bool
  141. {
  142. return $this->saleStatus;
  143. }
  144. public function setSaleStatus(bool $saleStatus): self
  145. {
  146. $this->saleStatus = $saleStatus;
  147. return $this;
  148. }
  149. }