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={
  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 setId(int $id) {
  153. $this->id = $id;
  154. }
  155. public function getTitle(): ?string
  156. {
  157. return $this->title;
  158. }
  159. public function setTitle(string $title): self
  160. {
  161. $this->title = $title;
  162. return $this;
  163. }
  164. /**
  165. * @return Collection|Article[]
  166. */
  167. public function getArticles(): Collection
  168. {
  169. return $this->articles;
  170. }
  171. public function addArticle(Article $article): self
  172. {
  173. if (!$this->articles->contains($article)) {
  174. $this->articles[] = $article;
  175. $article->setProduct($this);
  176. }
  177. return $this;
  178. }
  179. public function removeArticle(Article $article): self
  180. {
  181. if ($this->articles->removeElement($article)) {
  182. // set the owning side to null (unless already changed)
  183. if ($article->getProduct() === $this) {
  184. $article->setProduct(null);
  185. }
  186. }
  187. return $this;
  188. }
  189. /**
  190. * @return Collection|Attribute[]
  191. */
  192. public function getAttributes(): Collection
  193. {
  194. if ($this->attributes === null) {
  195. $this->attributes = new ArrayCollection();
  196. }
  197. return $this->attributes;
  198. }
  199. public function addAttribute(Attribute $attribute): self
  200. {
  201. if ($this->attributes === null) {
  202. $this->attributes = new ArrayCollection();
  203. }
  204. if (!$this->attributes->contains($attribute)) {
  205. $this->attributes[] = $attribute;
  206. }
  207. return $this;
  208. }
  209. public function removeAttribute(Attribute $attribute): self
  210. {
  211. if ($this->attributes === null) {
  212. $this->attributes = new ArrayCollection();
  213. }
  214. $this->attributes->removeElement($attribute);
  215. return $this;
  216. }
  217. /**
  218. * @return Collection|Category[]
  219. */
  220. public function getCategories(): Collection
  221. {
  222. return $this->categories;
  223. }
  224. public function addCategory(Category $category): self
  225. {
  226. if (!$this->categories->contains($category)) {
  227. $this->categories[] = $category;
  228. }
  229. return $this;
  230. }
  231. public function removeCategory(Category $category): self
  232. {
  233. $this->categories->removeElement($category);
  234. return $this;
  235. }
  236. /**
  237. * @return Collection|Attribute[]
  238. */
  239. public function getIndividualAttributes(): Collection
  240. {
  241. return $this->individualAttributes;
  242. }
  243. public function addIndividualAttribute(Attribute $attribute): self
  244. {
  245. if (!$this->individualAttributes->contains($attribute)) {
  246. $this->individualAttributes[] = $attribute;
  247. }
  248. return $this;
  249. }
  250. public function removeIndividualAttribute(Attribute $attribute): self
  251. {
  252. $this->individualAttributes->removeElement($attribute);
  253. return $this;
  254. }
  255. public function setIndividualAttributes(Collection $attributes): self
  256. {
  257. $this->individualAttributes = $attributes;
  258. return $this;
  259. }
  260. /**
  261. * @return mixed
  262. */
  263. public function getName()
  264. {
  265. return $this->name;
  266. }
  267. /**
  268. * @param mixed $name
  269. */
  270. public function setName($name): void
  271. {
  272. $this->name = $name;
  273. }
  274. /**
  275. * @return mixed
  276. */
  277. public function getPreview()
  278. {
  279. return $this->preview;
  280. }
  281. /**
  282. * @param mixed $preview
  283. */
  284. public function setPreview($preview): void
  285. {
  286. $this->preview = $preview;
  287. }
  288. /**
  289. * @return ArrayCollection
  290. */
  291. public function getGroups()
  292. {
  293. return $this->groups;
  294. }
  295. public function hasGroup($groupName)
  296. {
  297. return array_search($groupName, $this->getGroups()) !== false;
  298. }
  299. public function addGroup(Group $group): self
  300. {
  301. if (!$this->groups->contains($group)) {
  302. $this->groups[] = $group;
  303. }
  304. return $this;
  305. }
  306. public function removeGroup(Group $group): self
  307. {
  308. $this->groups->removeElement($group);
  309. return $this;
  310. }
  311. /**
  312. * {@inheritdoc}
  313. */
  314. public function getRoles()
  315. {
  316. $roles = [];
  317. foreach ($this->groups as $group) {
  318. $roles[] = $group->getName();
  319. }
  320. return $roles;
  321. }
  322. /**
  323. * @return mixed
  324. */
  325. public function getDescription()
  326. {
  327. return $this->description;
  328. }
  329. /**
  330. * @param mixed $description
  331. */
  332. public function setDescription($description): void
  333. {
  334. $this->description = $description;
  335. }
  336. /**
  337. * Get the value of cartDisplayMode
  338. */
  339. public function getCartDisplayMode()
  340. {
  341. return $this->cartDisplayMode;
  342. }
  343. /**
  344. * Set the value of cartDisplayMode
  345. *
  346. * @return self
  347. */
  348. public function setCartDisplayMode($cartDisplayMode)
  349. {
  350. $this->cartDisplayMode = $cartDisplayMode;
  351. return $this;
  352. }
  353. public function getArticleByName($name): ?Article {
  354. foreach($this->articles as $article) {
  355. if ($article->getName() == $name) {
  356. return $article;
  357. }
  358. }
  359. return null;
  360. }
  361. public function getTranslatableFields(): Collection
  362. {
  363. return $this->attributes();
  364. }
  365. }