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

Open in your IDE?
  1. <?php
  2. namespace Roothirsch\PimBundle\Entity;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  6. use App\Api\ReportInjectOperation;
  7. use App\Api\UpdatePositionContainerSorting;
  8. use App\Filter\NotFilter;
  9. use Roothirsch\CoreBundle\Behaviors\Attributable\Attribute\AttributeInterface;
  10. use Roothirsch\CoreBundle\Behaviors\Attributable\AttributeValue\AttributeValueAwareInterface;
  11. use Roothirsch\CoreBundle\Behaviors\Attributable\AttributeValue\AttributeValueInterface;
  12. use Roothirsch\CoreBundle\Behaviors\Attributable\MappedSuperclass\AbstractAttributable;
  13. use Roothirsch\CoreBundle\Behaviors\Translatable\TranslatableInterface;
  14. use Roothirsch\CoreBundle\Entity\Group;
  15. use Roothirsch\PimBundle\Repository\ProductRepository;
  16. use Doctrine\Common\Collections\ArrayCollection;
  17. use Doctrine\Common\Collections\Collection;
  18. use Doctrine\ORM\Mapping as ORM;
  19. use Gedmo\Mapping\Annotation as Gedmo;
  20. use Symfony\Component\Serializer\Annotation\Groups;
  21. use Symfony\Component\Serializer\Annotation\Ignore;
  22. use Symfony\Component\Serializer\Annotation\SerializedName;
  23. /**
  24. * @ApiResource(
  25. * normalizationContext={"groups"={"list"}, "enable_max_depth"=true},
  26. * denormalizationContext={"groups"={"list"}},
  27. * shortName="Pim/Product"
  28. * )
  29. * @ApiFilter(SearchFilter::class, properties={
  30. * "name": "partial",
  31. * "categories.title": "partial",
  32. * "categories.id": "exact"
  33. * })
  34. * @ORM\Entity(repositoryClass=ProductRepository::class)
  35. * @ORM\Table(name="pim_product"))
  36. * @ORM\InheritanceType("SINGLE_TABLE")
  37. * @ORM\DiscriminatorColumn(name="type", type="string")
  38. */
  39. class Product extends AbstractAttributable implements TranslatableInterface
  40. {
  41. /**
  42. * @ORM\Id
  43. * @ORM\GeneratedValue
  44. * @ORM\Column(type="integer")
  45. * @Groups({"list"})
  46. */
  47. protected $id;
  48. /**
  49. * @ORM\Column(type="string", length=255)
  50. * @Gedmo\Translatable
  51. * @Groups({"list"})
  52. */
  53. protected $title;
  54. /**
  55. * @ORM\Column(type="string", length=255)
  56. * @Groups({"list"})
  57. */
  58. protected $name;
  59. /**
  60. * @ORM\Column(type="string", length=1024, nullable=true)
  61. * @Gedmo\Translatable
  62. * @Groups({"list"})
  63. */
  64. protected $description;
  65. /**
  66. * @ORM\Column(type="string", nullable=true)
  67. * @Groups({"list"})
  68. */
  69. protected $preview;
  70. /**
  71. * @ORM\Column(type="string", nullable=true)
  72. */
  73. protected $cartDisplayMode = 'product';
  74. /**
  75. * @ORM\OneToMany(targetEntity=Article::class, mappedBy="product", orphanRemoval=true)
  76. * @Groups({"list"})
  77. */
  78. protected $articles;
  79. /**
  80. * @ORM\ManyToMany(targetEntity=Category::class, inversedBy="products")
  81. * @ORM\JoinTable(name="pim_product_categories")
  82. * @Groups({"list"})
  83. */
  84. protected $categories;
  85. /**
  86. * @ORM\OneToMany(targetEntity=AttributeValue::class, mappedBy="product", orphanRemoval=true, cascade={"persist"})
  87. */
  88. protected $attributeValues;
  89. /**
  90. * @ORM\ManyToMany(targetEntity=Group::class)
  91. * @ORM\JoinTable(name="pim_product_groups")
  92. * @Groups({"list"})
  93. */
  94. protected $groups;
  95. public function __construct()
  96. {
  97. $this->attributeValues = new ArrayCollection();
  98. $this->articles = new ArrayCollection();
  99. $this->categories = new ArrayCollection();
  100. $this->groups = new ArrayCollection();
  101. }
  102. public function attributes() {
  103. $attributes = new ArrayCollection();
  104. foreach($this->categories as $category) {
  105. /** @var Category $category */
  106. foreach($category->getProductAttributeGroups() as $attributeGroup) {
  107. foreach($attributeGroup->getAttributes() as $attribute) {
  108. $attributes->add($attribute);
  109. }
  110. }
  111. }
  112. return $attributes;
  113. }
  114. public function newValue(AttributeInterface $attribute): AttributeValueInterface
  115. {
  116. return new AttributeValue($attribute);
  117. }
  118. public function getAttributeValues(): Collection
  119. {
  120. return $this->attributeValues;
  121. }
  122. public function setAttributeValues(Collection $collection): AttributeValueAwareInterface
  123. {
  124. $this->attributeValues = $collection;
  125. return $this;
  126. }
  127. public function __toString()
  128. {
  129. return strval($this->title);
  130. }
  131. public function getId(): ?int
  132. {
  133. return $this->id;
  134. }
  135. public function getTitle(): ?string
  136. {
  137. return $this->title;
  138. }
  139. public function setTitle(string $title): self
  140. {
  141. $this->title = $title;
  142. return $this;
  143. }
  144. /**
  145. * @return Collection|Article[]
  146. */
  147. public function getArticles(): Collection
  148. {
  149. return $this->articles;
  150. }
  151. public function addArticle(Article $article): self
  152. {
  153. if (!$this->articles->contains($article)) {
  154. $this->articles[] = $article;
  155. $article->setProduct($this);
  156. }
  157. return $this;
  158. }
  159. public function removeArticle(Article $article): self
  160. {
  161. if ($this->articles->removeElement($article)) {
  162. // set the owning side to null (unless already changed)
  163. if ($article->getProduct() === $this) {
  164. $article->setProduct(null);
  165. }
  166. }
  167. return $this;
  168. }
  169. /**
  170. * @return Collection|Attribute[]
  171. */
  172. public function getAttributes(): Collection
  173. {
  174. return $this->attributes;
  175. }
  176. public function addAttribute(Attribute $attribute): self
  177. {
  178. if (!$this->attributes->contains($attribute)) {
  179. $this->attributes[] = $attribute;
  180. }
  181. return $this;
  182. }
  183. public function removeAttribute(Attribute $attribute): self
  184. {
  185. $this->attributes->removeElement($attribute);
  186. return $this;
  187. }
  188. /**
  189. * @return Collection|Category[]
  190. */
  191. public function getCategories(): Collection
  192. {
  193. return $this->categories;
  194. }
  195. public function addCategory(Category $category): self
  196. {
  197. if (!$this->categories->contains($category)) {
  198. $this->categories[] = $category;
  199. }
  200. return $this;
  201. }
  202. public function removeCategory(Category $category): self
  203. {
  204. $this->categories->removeElement($category);
  205. return $this;
  206. }
  207. /**
  208. * @return mixed
  209. */
  210. public function getName()
  211. {
  212. return $this->name;
  213. }
  214. /**
  215. * @param mixed $name
  216. */
  217. public function setName($name): void
  218. {
  219. $this->name = $name;
  220. }
  221. /**
  222. * @return mixed
  223. */
  224. public function getPreview()
  225. {
  226. return $this->preview;
  227. }
  228. /**
  229. * @param mixed $preview
  230. */
  231. public function setPreview($preview): void
  232. {
  233. $this->preview = $preview;
  234. }
  235. /**
  236. * @return ArrayCollection
  237. */
  238. public function getGroups()
  239. {
  240. return $this->groups;
  241. }
  242. public function hasGroup($groupName)
  243. {
  244. return array_search($groupName, $this->getGroups()) !== false;
  245. }
  246. public function addGroup(Group $group): self
  247. {
  248. if (!$this->groups->contains($group)) {
  249. $this->groups[] = $group;
  250. }
  251. return $this;
  252. }
  253. public function removeGroup(Group $group): self
  254. {
  255. $this->groups->removeElement($group);
  256. return $this;
  257. }
  258. /**
  259. * {@inheritdoc}
  260. */
  261. public function getRoles()
  262. {
  263. $roles = [];
  264. foreach ($this->groups as $group) {
  265. $roles[] = $group->getName();
  266. }
  267. return $roles;
  268. }
  269. /**
  270. * @return mixed
  271. */
  272. public function getDescription()
  273. {
  274. return $this->description;
  275. }
  276. /**
  277. * @param mixed $description
  278. */
  279. public function setDescription($description): void
  280. {
  281. $this->description = $description;
  282. }
  283. /**
  284. * Get the value of cartDisplayMode
  285. */
  286. public function getCartDisplayMode()
  287. {
  288. return $this->cartDisplayMode;
  289. }
  290. /**
  291. * Set the value of cartDisplayMode
  292. *
  293. * @return self
  294. */
  295. public function setCartDisplayMode($cartDisplayMode)
  296. {
  297. $this->cartDisplayMode = $cartDisplayMode;
  298. return $this;
  299. }
  300. public function getArticleByName($name): ?Article {
  301. foreach($this->articles as $article) {
  302. if ($article->getName() == $name) {
  303. return $article;
  304. }
  305. }
  306. return null;
  307. }
  308. public function getTranslatableFields(): Collection
  309. {
  310. return $this->attributes();
  311. }
  312. }