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

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