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

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={
  26. * "groups"={"list"},
  27. * "enable_max_depth"=true,
  28. * "xml_root_node_name"="product"
  29. * },
  30. * denormalizationContext={"groups"={"list"}},
  31. * shortName="Pim/Product"
  32. * )
  33. * @ApiFilter(SearchFilter::class, properties={
  34. * "name": "partial",
  35. * "categories.title": "partial",
  36. * "categories.id": "exact"
  37. * })
  38. * @ORM\Entity(repositoryClass=ProductRepository::class)
  39. * @ORM\Table(name="pim_product"))
  40. * @ORM\InheritanceType("SINGLE_TABLE")
  41. * @ORM\DiscriminatorColumn(name="type", type="string")
  42. */
  43. class Product extends AbstractAttributable implements TranslatableInterface
  44. {
  45. /**
  46. * @ORM\Id
  47. * @ORM\GeneratedValue
  48. * @ORM\Column(type="integer")
  49. * @Groups({"list"})
  50. */
  51. protected $id;
  52. /**
  53. * @ORM\Column(type="string", length=255)
  54. * @Gedmo\Translatable
  55. * @Groups({"list"})
  56. */
  57. protected $title;
  58. /**
  59. * @ORM\Column(type="string", length=255)
  60. * @Groups({"list"})
  61. */
  62. protected $name;
  63. /**
  64. * @ORM\Column(type="string", length=1024, nullable=true)
  65. * @Gedmo\Translatable
  66. * @Groups({"list"})
  67. */
  68. protected $description;
  69. /**
  70. * @ORM\Column(type="string", nullable=true)
  71. * @Groups({"list"})
  72. */
  73. protected $preview;
  74. /**
  75. * @ORM\Column(type="string", nullable=true)
  76. */
  77. protected $cartDisplayMode = 'product';
  78. /**
  79. * @ORM\OneToMany(targetEntity=Article::class, mappedBy="product", orphanRemoval=true)
  80. * @Groups({"list"})
  81. */
  82. protected $articles;
  83. /**
  84. * @ORM\ManyToMany(targetEntity=Category::class, inversedBy="products")
  85. * @ORM\JoinTable(name="pim_product_categories")
  86. * @Groups({"list"})
  87. */
  88. protected $categories;
  89. /**
  90. * @ORM\ManyToMany(targetEntity=Attribute::class, inversedBy="products", cascade={"merge"})
  91. * @ORM\JoinTable(name="pim_individual_attributes")
  92. * @Groups({"list"})
  93. */
  94. protected $individualAttributes;
  95. /**
  96. * @ORM\OneToMany(targetEntity=AttributeValue::class, mappedBy="product", orphanRemoval=true, cascade={"persist"})
  97. */
  98. protected $attributeValues;
  99. /**
  100. * @ORM\ManyToMany(targetEntity=Group::class)
  101. * @ORM\JoinTable(name="pim_product_groups")
  102. * @Groups({"list"})
  103. */
  104. protected $groups;
  105. public function __construct()
  106. {
  107. $this->attributeValues = new ArrayCollection();
  108. $this->articles = new ArrayCollection();
  109. $this->categories = new ArrayCollection();
  110. $this->groups = new ArrayCollection();
  111. $this->individualAttributes = new ArrayCollection();
  112. }
  113. public function attributes() {
  114. $attributes = new ArrayCollection();
  115. foreach($this->categories as $category) {
  116. /** @var Category $category */
  117. foreach($category->getProductAttributeGroups() as $attributeGroup) {
  118. foreach($attributeGroup->getAttributes() as $attribute) {
  119. $attributes->add($attribute);
  120. }
  121. }
  122. }
  123. // Add individual attributes
  124. foreach($this->individualAttributes as $attribute) {
  125. if (!$attributes->contains($attribute)) {
  126. $attributes->add($attribute);
  127. }
  128. }
  129. return $attributes;
  130. }
  131. public function newValue(AttributeInterface $attribute): AttributeValueInterface
  132. {
  133. return new AttributeValue($attribute);
  134. }
  135. public function getAttributeValues(): Collection
  136. {
  137. return $this->attributeValues;
  138. }
  139. public function setAttributeValues(Collection $collection): AttributeValueAwareInterface
  140. {
  141. $this->attributeValues = $collection;
  142. return $this;
  143. }
  144. public function __toString()
  145. {
  146. return strval($this->title);
  147. }
  148. public function getId(): ?int
  149. {
  150. return $this->id;
  151. }
  152. public function getTitle(): ?string
  153. {
  154. return $this->title;
  155. }
  156. public function setTitle(string $title): self
  157. {
  158. $this->title = $title;
  159. return $this;
  160. }
  161. /**
  162. * @return Collection|Article[]
  163. */
  164. public function getArticles(): Collection
  165. {
  166. return $this->articles;
  167. }
  168. public function addArticle(Article $article): self
  169. {
  170. if (!$this->articles->contains($article)) {
  171. $this->articles[] = $article;
  172. $article->setProduct($this);
  173. }
  174. return $this;
  175. }
  176. public function removeArticle(Article $article): self
  177. {
  178. if ($this->articles->removeElement($article)) {
  179. // set the owning side to null (unless already changed)
  180. if ($article->getProduct() === $this) {
  181. $article->setProduct(null);
  182. }
  183. }
  184. return $this;
  185. }
  186. /**
  187. * @return Collection|Attribute[]
  188. */
  189. public function getAttributes(): Collection
  190. {
  191. if ($this->attributes === null) {
  192. $this->attributes = new ArrayCollection();
  193. }
  194. return $this->attributes;
  195. }
  196. public function addAttribute(Attribute $attribute): self
  197. {
  198. if ($this->attributes === null) {
  199. $this->attributes = new ArrayCollection();
  200. }
  201. if (!$this->attributes->contains($attribute)) {
  202. $this->attributes[] = $attribute;
  203. }
  204. return $this;
  205. }
  206. public function removeAttribute(Attribute $attribute): self
  207. {
  208. if ($this->attributes === null) {
  209. $this->attributes = new ArrayCollection();
  210. }
  211. $this->attributes->removeElement($attribute);
  212. return $this;
  213. }
  214. /**
  215. * @return Collection|Category[]
  216. */
  217. public function getCategories(): Collection
  218. {
  219. return $this->categories;
  220. }
  221. public function addCategory(Category $category): self
  222. {
  223. if (!$this->categories->contains($category)) {
  224. $this->categories[] = $category;
  225. }
  226. return $this;
  227. }
  228. public function removeCategory(Category $category): self
  229. {
  230. $this->categories->removeElement($category);
  231. return $this;
  232. }
  233. /**
  234. * @return Collection|Attribute[]
  235. */
  236. public function getIndividualAttributes(): Collection
  237. {
  238. return $this->individualAttributes;
  239. }
  240. public function addIndividualAttribute(Attribute $attribute): self
  241. {
  242. if (!$this->individualAttributes->contains($attribute)) {
  243. $this->individualAttributes[] = $attribute;
  244. }
  245. return $this;
  246. }
  247. public function removeIndividualAttribute(Attribute $attribute): self
  248. {
  249. $this->individualAttributes->removeElement($attribute);
  250. return $this;
  251. }
  252. public function setIndividualAttributes(Collection $attributes): self
  253. {
  254. $this->individualAttributes = $attributes;
  255. return $this;
  256. }
  257. /**
  258. * @return mixed
  259. */
  260. public function getName()
  261. {
  262. return $this->name;
  263. }
  264. /**
  265. * @param mixed $name
  266. */
  267. public function setName($name): void
  268. {
  269. $this->name = $name;
  270. }
  271. /**
  272. * @return mixed
  273. */
  274. public function getPreview()
  275. {
  276. return $this->preview;
  277. }
  278. /**
  279. * @param mixed $preview
  280. */
  281. public function setPreview($preview): void
  282. {
  283. $this->preview = $preview;
  284. }
  285. /**
  286. * @return ArrayCollection
  287. */
  288. public function getGroups()
  289. {
  290. return $this->groups;
  291. }
  292. public function hasGroup($groupName)
  293. {
  294. return array_search($groupName, $this->getGroups()) !== false;
  295. }
  296. public function addGroup(Group $group): self
  297. {
  298. if (!$this->groups->contains($group)) {
  299. $this->groups[] = $group;
  300. }
  301. return $this;
  302. }
  303. public function removeGroup(Group $group): self
  304. {
  305. $this->groups->removeElement($group);
  306. return $this;
  307. }
  308. /**
  309. * {@inheritdoc}
  310. */
  311. public function getRoles()
  312. {
  313. $roles = [];
  314. foreach ($this->groups as $group) {
  315. $roles[] = $group->getName();
  316. }
  317. return $roles;
  318. }
  319. /**
  320. * @return mixed
  321. */
  322. public function getDescription()
  323. {
  324. return $this->description;
  325. }
  326. /**
  327. * @param mixed $description
  328. */
  329. public function setDescription($description): void
  330. {
  331. $this->description = $description;
  332. }
  333. /**
  334. * Get the value of cartDisplayMode
  335. */
  336. public function getCartDisplayMode()
  337. {
  338. return $this->cartDisplayMode;
  339. }
  340. /**
  341. * Set the value of cartDisplayMode
  342. *
  343. * @return self
  344. */
  345. public function setCartDisplayMode($cartDisplayMode)
  346. {
  347. $this->cartDisplayMode = $cartDisplayMode;
  348. return $this;
  349. }
  350. public function getArticleByName($name): ?Article {
  351. foreach($this->articles as $article) {
  352. if ($article->getName() == $name) {
  353. return $article;
  354. }
  355. }
  356. return null;
  357. }
  358. public function getTranslatableFields(): Collection
  359. {
  360. return $this->attributes();
  361. }
  362. }