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. * @Groups({"list"})
  89. * @MaxDepth(1)
  90. */
  91. protected $categories;
  92. /**
  93. * @ORM\ManyToMany(targetEntity=Attribute::class, inversedBy="products", cascade={"merge"})
  94. * @ORM\JoinTable(name="pim_individual_attributes")
  95. * @Groups({"list"})
  96. */
  97. protected $individualAttributes;
  98. /**
  99. * @ORM\OneToMany(targetEntity=AttributeValue::class, mappedBy="product", orphanRemoval=true, cascade={"persist"})
  100. */
  101. protected $attributeValues;
  102. /**
  103. * @ORM\ManyToMany(targetEntity=Group::class)
  104. * @ORM\JoinTable(name="pim_product_groups")
  105. * @Groups({"list"})
  106. */
  107. protected $groups;
  108. /**
  109. * @ORM\Column(type="float", nullable=true)
  110. * @Groups({"list"})
  111. */
  112. private $price = null;
  113. /**
  114. * @ORM\Column(type="integer", nullable=true)
  115. * @Groups({"list"})
  116. */
  117. private $stock = null;
  118. public function __construct()
  119. {
  120. $this->attributeValues = new ArrayCollection();
  121. $this->articles = new ArrayCollection();
  122. $this->categories = new ArrayCollection();
  123. $this->groups = new ArrayCollection();
  124. $this->individualAttributes = new ArrayCollection();
  125. }
  126. public function attributes() {
  127. $attributes = new ArrayCollection();
  128. foreach($this->categories as $category) {
  129. /** @var Category $category */
  130. foreach($category->getProductAttributeGroups() as $attributeGroup) {
  131. foreach($attributeGroup->getAttributes() as $attribute) {
  132. $attributes->add($attribute);
  133. }
  134. }
  135. }
  136. // Add individual attributes
  137. foreach($this->individualAttributes as $attribute) {
  138. if (!$attributes->contains($attribute)) {
  139. $attributes->add($attribute);
  140. }
  141. }
  142. return $attributes;
  143. }
  144. public function newValue(AttributeInterface $attribute): AttributeValueInterface
  145. {
  146. return new AttributeValue($attribute);
  147. }
  148. public function getAttributeValues(): Collection
  149. {
  150. return $this->attributeValues;
  151. }
  152. public function setAttributeValues(Collection $collection): AttributeValueAwareInterface
  153. {
  154. $this->attributeValues = $collection;
  155. return $this;
  156. }
  157. public function __toString()
  158. {
  159. return strval($this->title);
  160. }
  161. public function getId(): ?int
  162. {
  163. return $this->id;
  164. }
  165. public function setId(int $id) {
  166. $this->id = $id;
  167. }
  168. public function getTitle(): ?string
  169. {
  170. return $this->title;
  171. }
  172. public function setTitle(string $title): self
  173. {
  174. $this->title = $title;
  175. return $this;
  176. }
  177. /**
  178. * @return Collection|Article[]
  179. */
  180. public function getArticles(): Collection
  181. {
  182. return $this->articles;
  183. }
  184. public function addArticle(Article $article): self
  185. {
  186. if (!$this->articles->contains($article)) {
  187. $this->articles[] = $article;
  188. $article->setProduct($this);
  189. }
  190. return $this;
  191. }
  192. public function removeArticle(Article $article): self
  193. {
  194. if ($this->articles->removeElement($article)) {
  195. // set the owning side to null (unless already changed)
  196. if ($article->getProduct() === $this) {
  197. $article->setProduct(null);
  198. }
  199. }
  200. return $this;
  201. }
  202. /**
  203. * @return Collection|Attribute[]
  204. */
  205. public function getAttributes(): Collection
  206. {
  207. if ($this->attributes === null) {
  208. $this->attributes = new ArrayCollection();
  209. }
  210. return $this->attributes;
  211. }
  212. public function addAttribute(Attribute $attribute): self
  213. {
  214. if ($this->attributes === null) {
  215. $this->attributes = new ArrayCollection();
  216. }
  217. if (!$this->attributes->contains($attribute)) {
  218. $this->attributes[] = $attribute;
  219. }
  220. return $this;
  221. }
  222. public function removeAttribute(Attribute $attribute): self
  223. {
  224. if ($this->attributes === null) {
  225. $this->attributes = new ArrayCollection();
  226. }
  227. $this->attributes->removeElement($attribute);
  228. return $this;
  229. }
  230. /**
  231. * @return Collection|Category[]
  232. */
  233. public function getCategories(): Collection
  234. {
  235. return $this->categories;
  236. }
  237. public function addCategory(Category $category): self
  238. {
  239. if (!$this->categories->contains($category)) {
  240. $this->categories[] = $category;
  241. }
  242. return $this;
  243. }
  244. public function removeCategory(Category $category): self
  245. {
  246. $this->categories->removeElement($category);
  247. return $this;
  248. }
  249. /**
  250. * @return Collection|Attribute[]
  251. */
  252. public function getIndividualAttributes(): Collection
  253. {
  254. return $this->individualAttributes;
  255. }
  256. public function addIndividualAttribute(Attribute $attribute): self
  257. {
  258. if (!$this->individualAttributes->contains($attribute)) {
  259. $this->individualAttributes[] = $attribute;
  260. }
  261. return $this;
  262. }
  263. public function removeIndividualAttribute(Attribute $attribute): self
  264. {
  265. $this->individualAttributes->removeElement($attribute);
  266. return $this;
  267. }
  268. public function setIndividualAttributes(Collection $attributes): self
  269. {
  270. $this->individualAttributes = $attributes;
  271. return $this;
  272. }
  273. /**
  274. * @return mixed
  275. */
  276. public function getName()
  277. {
  278. return $this->name;
  279. }
  280. /**
  281. * @param mixed $name
  282. */
  283. public function setName($name): void
  284. {
  285. $this->name = $name;
  286. }
  287. /**
  288. * @return mixed
  289. */
  290. public function getPreview()
  291. {
  292. return $this->preview;
  293. }
  294. /**
  295. * @param mixed $preview
  296. */
  297. public function setPreview($preview): void
  298. {
  299. $this->preview = $preview;
  300. }
  301. /**
  302. * @return ArrayCollection
  303. */
  304. public function getGroups()
  305. {
  306. return $this->groups;
  307. }
  308. public function hasGroup($groupName)
  309. {
  310. return array_search($groupName, $this->getGroups()) !== false;
  311. }
  312. public function addGroup(Group $group): self
  313. {
  314. if (!$this->groups->contains($group)) {
  315. $this->groups[] = $group;
  316. }
  317. return $this;
  318. }
  319. public function removeGroup(Group $group): self
  320. {
  321. $this->groups->removeElement($group);
  322. return $this;
  323. }
  324. /**
  325. * {@inheritdoc}
  326. */
  327. public function getRoles()
  328. {
  329. $roles = [];
  330. foreach ($this->groups as $group) {
  331. $roles[] = $group->getName();
  332. }
  333. return $roles;
  334. }
  335. /**
  336. * @return mixed
  337. */
  338. public function getDescription()
  339. {
  340. return $this->description;
  341. }
  342. /**
  343. * @param mixed $description
  344. */
  345. public function setDescription($description): void
  346. {
  347. $this->description = $description;
  348. }
  349. /**
  350. * Get the value of cartDisplayMode
  351. */
  352. public function getCartDisplayMode()
  353. {
  354. return $this->cartDisplayMode;
  355. }
  356. /**
  357. * Set the value of cartDisplayMode
  358. *
  359. * @return self
  360. */
  361. public function setCartDisplayMode($cartDisplayMode)
  362. {
  363. $this->cartDisplayMode = $cartDisplayMode;
  364. return $this;
  365. }
  366. public function getArticleByName($name): ?Article {
  367. foreach($this->articles as $article) {
  368. if ($article->getName() == $name) {
  369. return $article;
  370. }
  371. }
  372. return null;
  373. }
  374. public function getTranslatableFields(): Collection
  375. {
  376. return $this->attributes();
  377. }
  378. /**
  379. * @return mixed
  380. */
  381. public function getPrice()
  382. {
  383. return $this->price;
  384. }
  385. /**
  386. * @param mixed $price
  387. */
  388. public function setPrice($price): void
  389. {
  390. $this->price = $price;
  391. }
  392. /**
  393. * @return mixed
  394. */
  395. public function getStock()
  396. {
  397. return $this->stock;
  398. }
  399. /**
  400. * @param mixed $stock
  401. */
  402. public function setStock($stock): void
  403. {
  404. $this->stock = $stock;
  405. }
  406. }