vendor/roothirsch/pim-bundle/Entity/Category.php line 35

Open in your IDE?
  1. <?php
  2. namespace Roothirsch\PimBundle\Entity;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use Roothirsch\CoreBundle\Entity\Traits\TimetrackedTrait;
  5. use Roothirsch\PimBundle\API\UpdateSorting;
  6. use Roothirsch\PimBundle\Repository\CategoryRepository;
  7. use Doctrine\Common\Collections\ArrayCollection;
  8. use Doctrine\Common\Collections\Collection;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use \Roothirsch\PimBundle\Entity\Article;
  11. use Gedmo\Mapping\Annotation as Gedmo;
  12. use \Roothirsch\PimBundle\Entity\AttributeGroup;
  13. use \Roothirsch\PimBundle\Entity\DataSource;
  14. /**
  15. * @Gedmo\Tree(type="nested")
  16. * @ApiResource(
  17. * shortName="Pim/Category",
  18. * itemOperations={
  19. * "get",
  20. * "put",
  21. * "delete",
  22. * "update_sorting"={
  23. * "method"="PUT",
  24. * "path"="/pim/categories/{id}/update-sorting",
  25. * "controller"=UpdateSorting::class
  26. * },
  27. * }
  28. * )
  29. * @ORM\Entity(repositoryClass=CategoryRepository::class)
  30. * @ORM\Table(name="pim_category")
  31. */
  32. class Category
  33. {
  34. use TimetrackedTrait;
  35. /**
  36. * @ORM\Id
  37. * @ORM\GeneratedValue
  38. * @ORM\Column(type="integer")
  39. */
  40. private $id;
  41. /**
  42. * @ORM\Column(type="string", length=255)
  43. */
  44. private $title;
  45. /**
  46. * @Gedmo\TreeLeft
  47. * @ORM\Column(name="lft", type="integer")
  48. */
  49. private $lft;
  50. /**
  51. * @Gedmo\TreeLevel
  52. * @ORM\Column(name="lvl", type="integer")
  53. */
  54. private $lvl;
  55. /**
  56. * @Gedmo\TreeRight
  57. * @ORM\Column(name="rgt", type="integer")
  58. */
  59. private $rgt;
  60. /**
  61. * @Gedmo\TreeRoot
  62. * @ORM\ManyToOne(targetEntity="Category")
  63. * @ORM\JoinColumn(name="tree_root", referencedColumnName="id", onDelete="CASCADE")
  64. */
  65. private $root;
  66. /**
  67. * @Gedmo\TreeParent
  68. * @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
  69. * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
  70. */
  71. private $parent;
  72. /**
  73. * @ORM\OneToMany(targetEntity="Category", mappedBy="parent_id")
  74. * @ORM\OrderBy({"lft" = "ASC"})
  75. */
  76. private $children;
  77. /**
  78. * @ORM\ManyToMany(targetEntity=Product::class, mappedBy="categories")
  79. * @ORM\JoinTable(name="pim_category_products")
  80. */
  81. private $products;
  82. /**
  83. * @ORM\ManyToMany(targetEntity=AttributeGroup::class, inversedBy="categories")
  84. * @ORM\JoinTable(name="pim_category_product_attribute_groups")
  85. */
  86. private $productAttributeGroups;
  87. /**
  88. * @ORM\ManyToMany(targetEntity=AttributeGroup::class, inversedBy="categories")
  89. * @ORM\JoinTable(name="pim_category_article_attribute_groups")
  90. */
  91. private $articleAttributeGroups;
  92. /**
  93. * @ORM\OneToMany(targetEntity=DataSource::class, mappedBy="category", orphanRemoval=true, cascade={"persist"})
  94. */
  95. private $dataSources;
  96. public function __construct()
  97. {
  98. $this->articles = new ArrayCollection();
  99. $this->children = new ArrayCollection();
  100. $this->products = new ArrayCollection();
  101. $this->productAttributeGroups = new ArrayCollection();
  102. $this->articleAttributeGroups = new ArrayCollection();
  103. $this->dataSources = new ArrayCollection();
  104. }
  105. public function __toString()
  106. {
  107. return $this->title;
  108. }
  109. public function getId(): ?int
  110. {
  111. return $this->id;
  112. }
  113. /**
  114. * @param mixed $id
  115. */
  116. public function setId($id): void
  117. {
  118. $this->id = $id;
  119. }
  120. public function getTitle(): ?string
  121. {
  122. return $this->title;
  123. }
  124. public function setTitle(string $title): self
  125. {
  126. $this->title = $title;
  127. return $this;
  128. }
  129. public function getRoot()
  130. {
  131. return $this->root;
  132. }
  133. public function setParent($parent = null)
  134. {
  135. $this->parent = $parent;
  136. }
  137. public function getParent()
  138. {
  139. return $this->parent;
  140. }
  141. /**
  142. * @return Collection|Product[]
  143. */
  144. public function getProducts(): Collection
  145. {
  146. return $this->products;
  147. }
  148. public function addProduct(Product $product): self
  149. {
  150. if (!$this->products->contains($product)) {
  151. $this->products[] = $product;
  152. $product->addCategory($this);
  153. }
  154. return $this;
  155. }
  156. public function removeProduct(Product $product): self
  157. {
  158. if ($this->products->removeElement($product)) {
  159. $product->removeCategory($this);
  160. }
  161. return $this;
  162. }
  163. /**
  164. * @return Collection|AttributeGroup[]
  165. */
  166. public function getProductAttributeGroups(): Collection
  167. {
  168. return $this->productAttributeGroups;
  169. }
  170. public function addProductAttributeGroup(AttributeGroup $productAttributeGroup): self
  171. {
  172. if (!$this->productAttributeGroups->contains($productAttributeGroup)) {
  173. $this->productAttributeGroups[] = $productAttributeGroup;
  174. }
  175. return $this;
  176. }
  177. public function removeProductAttributeGroup(AttributeGroup $productAttributeGroup): self
  178. {
  179. $this->productAttributeGroups->removeElement($productAttributeGroup);
  180. return $this;
  181. }
  182. /**
  183. * @return Collection|AttributeGroup[]
  184. */
  185. public function getArticleAttributeGroups(): Collection
  186. {
  187. return $this->articleAttributeGroups;
  188. }
  189. public function addArticleAttributeGroup(AttributeGroup $articleAttributeGroup): self
  190. {
  191. if (!$this->articleAttributeGroups->contains($articleAttributeGroup)) {
  192. $this->articleAttributeGroups[] = $articleAttributeGroup;
  193. }
  194. return $this;
  195. }
  196. public function removeArticleAttributeGroup(AttributeGroup $articleAttributeGroup): self
  197. {
  198. $this->articleAttributeGroups->removeElement($articleAttributeGroup);
  199. return $this;
  200. }
  201. /**
  202. * @return Collection|DataSource[]
  203. */
  204. public function getDataSources(): Collection
  205. {
  206. return $this->dataSources;
  207. }
  208. public function addDataSource(DataSource $dataSource): self
  209. {
  210. if (!$this->dataSources->contains($dataSource)) {
  211. $this->dataSources[] = $dataSource;
  212. $dataSource->setCategory($this);
  213. }
  214. return $this;
  215. }
  216. public function removeDataSource(DataSource $dataSource): self
  217. {
  218. if ($this->dataSources->removeElement($dataSource)) {
  219. // set the owning side to null (unless already changed)
  220. if ($dataSource->getCategory() === $this) {
  221. $dataSource->setCategory(null);
  222. }
  223. }
  224. return $this;
  225. }
  226. public function getProduct($name) {
  227. foreach($this->products as $product) {
  228. if (strtolower($product->getName()) == strtolower($name)) {
  229. return $product;
  230. }
  231. }
  232. return null;
  233. }
  234. public function getArticle($name) {
  235. foreach($this->products as $product) {
  236. foreach($product->getArticles() as $article) {
  237. if (strtolower($article->getName()) == strtolower($name)) {
  238. return $article;
  239. }
  240. }
  241. }
  242. return null;
  243. }
  244. }