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. * @ORM\Column(type="string", length=255, nullable=true)
  47. */
  48. private $name;
  49. /**
  50. * @Gedmo\TreeLeft
  51. * @ORM\Column(name="lft", type="integer")
  52. */
  53. private $lft;
  54. /**
  55. * @Gedmo\TreeLevel
  56. * @ORM\Column(name="lvl", type="integer")
  57. */
  58. private $lvl;
  59. /**
  60. * @Gedmo\TreeRight
  61. * @ORM\Column(name="rgt", type="integer")
  62. */
  63. private $rgt;
  64. /**
  65. * @Gedmo\TreeRoot
  66. * @ORM\ManyToOne(targetEntity="Category")
  67. * @ORM\JoinColumn(name="tree_root", referencedColumnName="id", onDelete="CASCADE")
  68. */
  69. private $root;
  70. /**
  71. * @Gedmo\TreeParent
  72. * @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
  73. * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
  74. */
  75. private $parent;
  76. /**
  77. * @ORM\OneToMany(targetEntity="Category", mappedBy="parent_id")
  78. * @ORM\OrderBy({"lft" = "ASC"})
  79. */
  80. private $children;
  81. /**
  82. * @ORM\ManyToMany(targetEntity=Product::class, mappedBy="categories")
  83. * @ORM\JoinTable(name="pim_category_products")
  84. */
  85. private $products;
  86. /**
  87. * @ORM\ManyToMany(targetEntity=AttributeGroup::class, inversedBy="categories")
  88. * @ORM\JoinTable(name="pim_category_product_attribute_groups")
  89. */
  90. private $productAttributeGroups;
  91. /**
  92. * @ORM\ManyToMany(targetEntity=AttributeGroup::class, inversedBy="categories")
  93. * @ORM\JoinTable(name="pim_category_article_attribute_groups")
  94. */
  95. private $articleAttributeGroups;
  96. /**
  97. * @ORM\OneToMany(targetEntity=DataSource::class, mappedBy="category", orphanRemoval=true, cascade={"persist"})
  98. */
  99. private $dataSources;
  100. public function __construct()
  101. {
  102. $this->articles = new ArrayCollection();
  103. $this->children = new ArrayCollection();
  104. $this->products = new ArrayCollection();
  105. $this->productAttributeGroups = new ArrayCollection();
  106. $this->articleAttributeGroups = new ArrayCollection();
  107. $this->dataSources = new ArrayCollection();
  108. }
  109. public function __toString()
  110. {
  111. return $this->title;
  112. }
  113. public function getId(): ?int
  114. {
  115. return $this->id;
  116. }
  117. /**
  118. * @param mixed $id
  119. */
  120. public function setId($id): void
  121. {
  122. $this->id = $id;
  123. }
  124. public function getTitle(): ?string
  125. {
  126. return $this->title;
  127. }
  128. public function setTitle(string $title): self
  129. {
  130. $this->title = $title;
  131. return $this;
  132. }
  133. public function getRoot()
  134. {
  135. return $this->root;
  136. }
  137. public function setParent($parent = null)
  138. {
  139. $this->parent = $parent;
  140. }
  141. public function getParent()
  142. {
  143. return $this->parent;
  144. }
  145. /**
  146. * @return Collection|Product[]
  147. */
  148. public function getProducts(): Collection
  149. {
  150. return $this->products;
  151. }
  152. public function addProduct(Product $product): self
  153. {
  154. if (!$this->products->contains($product)) {
  155. $this->products[] = $product;
  156. $product->addCategory($this);
  157. }
  158. return $this;
  159. }
  160. public function removeProduct(Product $product): self
  161. {
  162. if ($this->products->removeElement($product)) {
  163. $product->removeCategory($this);
  164. }
  165. return $this;
  166. }
  167. /**
  168. * @return Collection|AttributeGroup[]
  169. */
  170. public function getProductAttributeGroups(): Collection
  171. {
  172. return $this->productAttributeGroups;
  173. }
  174. public function addProductAttributeGroup(AttributeGroup $productAttributeGroup): self
  175. {
  176. if (!$this->productAttributeGroups->contains($productAttributeGroup)) {
  177. $this->productAttributeGroups[] = $productAttributeGroup;
  178. }
  179. return $this;
  180. }
  181. public function removeProductAttributeGroup(AttributeGroup $productAttributeGroup): self
  182. {
  183. $this->productAttributeGroups->removeElement($productAttributeGroup);
  184. return $this;
  185. }
  186. /**
  187. * @return Collection|AttributeGroup[]
  188. */
  189. public function getArticleAttributeGroups(): Collection
  190. {
  191. return $this->articleAttributeGroups;
  192. }
  193. public function addArticleAttributeGroup(AttributeGroup $articleAttributeGroup): self
  194. {
  195. if (!$this->articleAttributeGroups->contains($articleAttributeGroup)) {
  196. $this->articleAttributeGroups[] = $articleAttributeGroup;
  197. }
  198. return $this;
  199. }
  200. public function removeArticleAttributeGroup(AttributeGroup $articleAttributeGroup): self
  201. {
  202. $this->articleAttributeGroups->removeElement($articleAttributeGroup);
  203. return $this;
  204. }
  205. /**
  206. * @return Collection|DataSource[]
  207. */
  208. public function getDataSources(): Collection
  209. {
  210. return $this->dataSources;
  211. }
  212. public function addDataSource(DataSource $dataSource): self
  213. {
  214. if (!$this->dataSources->contains($dataSource)) {
  215. $this->dataSources[] = $dataSource;
  216. $dataSource->setCategory($this);
  217. }
  218. return $this;
  219. }
  220. public function removeDataSource(DataSource $dataSource): self
  221. {
  222. if ($this->dataSources->removeElement($dataSource)) {
  223. // set the owning side to null (unless already changed)
  224. if ($dataSource->getCategory() === $this) {
  225. $dataSource->setCategory(null);
  226. }
  227. }
  228. return $this;
  229. }
  230. public function getProduct($name) {
  231. foreach($this->products as $product) {
  232. if (strtolower($product->getName()) == strtolower($name)) {
  233. return $product;
  234. }
  235. }
  236. return null;
  237. }
  238. public function getArticle($name) {
  239. foreach($this->products as $product) {
  240. foreach($product->getArticles() as $article) {
  241. if (strtolower($article->getName()) == strtolower($name)) {
  242. return $article;
  243. }
  244. }
  245. }
  246. return null;
  247. }
  248. /**
  249. * @return mixed
  250. */
  251. public function getName()
  252. {
  253. return $this->name;
  254. }
  255. /**
  256. * @param mixed $name
  257. */
  258. public function setName($name): void
  259. {
  260. $this->name = $name;
  261. }
  262. }