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.

206 lines
5.4KB

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