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.

ProductCategoryModel.php 5.3KB

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