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

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