src/Entity/Product.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Fce\F_PP;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\Common\Collections\Collection;
  6. use Doctrine\Common\Collections\Criteria;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use App\Entity\Behaviors\TranslatableTrait;
  9. use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
  10. use Symfony\Component\HttpFoundation\File\File;
  11. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  12. use Symfony\Component\Validator\Constraints as Assert;
  13. use JMS\Serializer\Annotation as Serializer;
  14. /**
  15. * @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
  16. * @ORM\EntityListeners({"App\Listener\EntityListener"})
  17. * @Vich\Uploadable()
  18. */
  19. class Product implements TranslatableInterface
  20. {
  21. use TranslatableTrait;
  22. /**
  23. * @ORM\Id()
  24. * @ORM\GeneratedValue()
  25. * @ORM\Column(type="integer")
  26. */
  27. private $id;
  28. /**
  29. * @Vich\UploadableField(mapping="product_image", fileNameProperty="image")
  30. * @Assert\Image(mimeTypes={"image/png", "image/jpeg", "image/jpg", "image/gif"})
  31. * @var File|null
  32. */
  33. private $imageFile;
  34. /**
  35. * @ORM\Column(type="string", length=199, nullable=true)
  36. */
  37. private $image;
  38. /**
  39. * @Serializer\Groups({"api"})
  40. * @Serializer\SerializedName("code")
  41. * @ORM\Column(type="string", length=50, nullable=true)
  42. */
  43. private $code;
  44. /**
  45. * @ORM\Column(type="string", length=250, nullable=true)
  46. */
  47. private $hash;
  48. /**
  49. * @ORM\Column(type="boolean")
  50. */
  51. private $isActive;
  52. /**
  53. * @ORM\Column(type="boolean")
  54. */
  55. private $onlyForAdults = false;
  56. /**
  57. * @Serializer\Groups({"api"})
  58. * @Serializer\Inline()
  59. * @ORM\ManyToOne(targetEntity="App\Entity\Brand", inversedBy="products")
  60. * @ORM\JoinColumn(nullable=false)
  61. */
  62. private $brand;
  63. /**
  64. * @ORM\ManyToOne(targetEntity="App\Entity\Dealer", inversedBy="products")
  65. * @ORM\JoinColumn(nullable=false)
  66. */
  67. private $dealer;
  68. /**
  69. * @ORM\Column(type="datetime")
  70. */
  71. private $createts;
  72. /**
  73. * @ORM\ManyToOne(targetEntity="App\Entity\User")
  74. * @ORM\JoinColumn(nullable=false)
  75. */
  76. private $createUser;
  77. /**
  78. * @ORM\Column(type="datetime", nullable=true)
  79. */
  80. private $editts;
  81. /**
  82. * @ORM\Column(type="boolean")
  83. */
  84. private $translated = false;
  85. /**
  86. * @ORM\ManyToOne(targetEntity="App\Entity\User")
  87. */
  88. private $editUser;
  89. /**
  90. * @ORM\OneToMany(targetEntity="App\Entity\ProductImage", mappedBy="product", cascade={"persist", "remove"}, orphanRemoval=true)
  91. * @ORM\OrderBy({"orderBy" = "ASC"})
  92. */
  93. private $productImages;
  94. /**
  95. * @ORM\OneToMany(targetEntity="App\Entity\Variant", mappedBy="product", cascade={"persist", "remove"}, orphanRemoval=true)
  96. * @ORM\OrderBy({"orderBy" = "ASC"})
  97. */
  98. private $variants;
  99. /**
  100. * @ORM\OneToMany(targetEntity="App\Entity\Pricelist", mappedBy="product", cascade={"persist", "remove"}, orphanRemoval=true, indexBy="currency")
  101. */
  102. private $pricelists;
  103. /**
  104. * @ORM\OneToMany(targetEntity="App\Entity\OrderItem", mappedBy="product")
  105. */
  106. private $orderItems;
  107. /**
  108. * @ORM\ManyToOne(targetEntity="App\Entity\Category", inversedBy="products")
  109. */
  110. private $category;
  111. /**
  112. * @Serializer\Groups({"api"})
  113. * @Serializer\SerializedName("image")
  114. */
  115. private $productImage;
  116. /**
  117. * @Serializer\Groups({"api"})
  118. * @Serializer\SerializedName("url")
  119. */
  120. private $url;
  121. /**
  122. * @Serializer\Groups({"api"})
  123. * @Serializer\SerializedName("category")
  124. */
  125. private $appCategory;
  126. /**
  127. * @Serializer\VirtualProperty()
  128. * @Serializer\Groups({"api"})
  129. * @Serializer\Inline()
  130. */
  131. public function getVirtualTranslation()
  132. {
  133. return $this->translate();
  134. }
  135. public function __construct()
  136. {
  137. $this->productImages = new ArrayCollection();
  138. $this->variants = new ArrayCollection();
  139. $this->pricelists = new ArrayCollection();
  140. $this->orderItems = new ArrayCollection();
  141. }
  142. public function checkPricelists($currencies){
  143. $diff = array_diff($currencies,array_keys($this->getPricelists()->toArray()));
  144. if(count($diff)>0){
  145. foreach ($diff as $curr){
  146. $price = new Pricelist($curr);
  147. $this->addPricelist($price);
  148. }
  149. }
  150. }
  151. public function getId(): ?int
  152. {
  153. return $this->id;
  154. }
  155. public function getImage(): ?string
  156. {
  157. return $this->image;
  158. }
  159. public function setImage(?string $image): self
  160. {
  161. $this->image = $image;
  162. return $this;
  163. }
  164. public function getIsActive(): ?bool
  165. {
  166. return $this->isActive;
  167. }
  168. public function setIsActive(bool $isActive): self
  169. {
  170. $this->isActive = $isActive;
  171. return $this;
  172. }
  173. public function getBrand(): ?Brand
  174. {
  175. return $this->brand;
  176. }
  177. public function setBrand(?Brand $brand): self
  178. {
  179. $this->brand = $brand;
  180. return $this;
  181. }
  182. public function getDealer(): ?Dealer
  183. {
  184. return $this->dealer;
  185. }
  186. public function setDealer(?Dealer $dealer): self
  187. {
  188. $this->dealer = $dealer;
  189. return $this;
  190. }
  191. public function getCreatets(): ?\DateTimeInterface
  192. {
  193. return $this->createts;
  194. }
  195. public function setCreatets(\DateTimeInterface $createts): self
  196. {
  197. $this->createts = $createts;
  198. return $this;
  199. }
  200. public function getCreateUser(): ?User
  201. {
  202. return $this->createUser;
  203. }
  204. public function setCreateUser(?User $createUser): self
  205. {
  206. $this->createUser = $createUser;
  207. return $this;
  208. }
  209. public function getEditts(): ?\DateTimeInterface
  210. {
  211. return $this->editts;
  212. }
  213. public function setEditts(?\DateTimeInterface $editts): self
  214. {
  215. $this->editts = $editts;
  216. return $this;
  217. }
  218. public function getEditUser(): ?User
  219. {
  220. return $this->editUser;
  221. }
  222. public function setEditUser(?User $editUser): self
  223. {
  224. $this->editUser = $editUser;
  225. return $this;
  226. }
  227. public function __call($method, $arguments)
  228. {
  229. $ret = $this->proxyCurrentLocaleTranslation($method, $arguments);
  230. if(empty($ret)) {
  231. $ret = call_user_func_array( [ $this->translate( \Locale::getDefault() ), $method ], $arguments );
  232. }
  233. return $ret;
  234. }
  235. /**
  236. * Get ImageFile
  237. *
  238. * @return null|File
  239. */
  240. public function getImageFile()
  241. {
  242. return $this->imageFile;
  243. }
  244. /**
  245. * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $imageFile
  246. */
  247. public function setImageFile(?File $imageFile = null): void
  248. {
  249. $this->imageFile = $imageFile;
  250. if (null !== $imageFile) {
  251. $this->editts = new \DateTime();
  252. }
  253. }
  254. /**
  255. * @return Collection|ProductImage[]
  256. */
  257. public function getProductImages(): Collection
  258. {
  259. return $this->productImages;
  260. }
  261. public function addProductImage(ProductImage $productImage): self
  262. {
  263. if (!$this->productImages->contains($productImage)) {
  264. $this->productImages[] = $productImage;
  265. $productImage->setProduct($this);
  266. }
  267. return $this;
  268. }
  269. public function removeProductImage(ProductImage $productImage): self
  270. {
  271. if ($this->productImages->contains($productImage)) {
  272. $this->productImages->removeElement($productImage);
  273. // set the owning side to null (unless already changed)
  274. if ($productImage->getProduct() === $this) {
  275. $productImage->setProduct(null);
  276. }
  277. }
  278. return $this;
  279. }
  280. /**
  281. * @return Collection|Variant[]
  282. */
  283. public function getVariants(): Collection
  284. {
  285. return $this->variants;
  286. }
  287. public function addVariant(Variant $variant): self
  288. {
  289. if (!$this->variants->contains($variant)) {
  290. $this->variants[] = $variant;
  291. $variant->setProduct($this);
  292. }
  293. return $this;
  294. }
  295. public function removeVariant(Variant $variant): self
  296. {
  297. if ($this->variants->contains($variant)) {
  298. $this->variants->removeElement($variant);
  299. // set the owning side to null (unless already changed)
  300. if ($variant->getProduct() === $this) {
  301. $variant->setProduct(null);
  302. }
  303. }
  304. return $this;
  305. }
  306. /**
  307. * @return Collection|Pricelist[]
  308. */
  309. public function getPricelists(): Collection
  310. {
  311. return $this->pricelists;
  312. }
  313. public function addPricelist(Pricelist $pricelist): self
  314. {
  315. if (!$this->pricelists->contains($pricelist)) {
  316. $this->pricelists[$pricelist->getCurrency()] = $pricelist;
  317. $pricelist->setProduct($this);
  318. }
  319. return $this;
  320. }
  321. public function removePricelist(Pricelist $pricelist): self
  322. {
  323. if ($this->pricelists->contains($pricelist)) {
  324. $this->pricelists->removeElement($pricelist);
  325. // set the owning side to null (unless already changed)
  326. if ($pricelist->getProduct() === $this) {
  327. $pricelist->setProduct(null);
  328. }
  329. }
  330. return $this;
  331. }
  332. /**
  333. * @return Collection|OrderItem[]
  334. */
  335. public function getOrderItems(): Collection
  336. {
  337. return $this->orderItems;
  338. }
  339. public function addOrderItem(OrderItem $orderItem): self
  340. {
  341. if (!$this->orderItems->contains($orderItem)) {
  342. $this->orderItems[] = $orderItem;
  343. $orderItem->setProduct($this);
  344. }
  345. return $this;
  346. }
  347. public function removeOrderItem(OrderItem $orderItem): self
  348. {
  349. if ($this->orderItems->contains($orderItem)) {
  350. $this->orderItems->removeElement($orderItem);
  351. // set the owning side to null (unless already changed)
  352. if ($orderItem->getProduct() === $this) {
  353. $orderItem->setProduct(null);
  354. }
  355. }
  356. return $this;
  357. }
  358. /**
  359. * Get Category
  360. *
  361. * @return mixed
  362. */
  363. public function getCategory()
  364. {
  365. return $this->category;
  366. }
  367. /**
  368. * Set Category
  369. *
  370. * @param mixed $category
  371. */
  372. public function setCategory( $category )
  373. {
  374. $this->category = $category;
  375. }
  376. /**
  377. * Get Code
  378. *
  379. * @return mixed
  380. */
  381. public function getCode()
  382. {
  383. return $this->code;
  384. }
  385. /**
  386. * Set Code
  387. *
  388. * @param mixed $code
  389. */
  390. public function setCode( $code )
  391. {
  392. $this->code = $code;
  393. }
  394. /**
  395. * Get Hash
  396. *
  397. * @return mixed
  398. */
  399. public function getHash()
  400. {
  401. return $this->hash;
  402. }
  403. /**
  404. * Set Hash
  405. *
  406. * @param mixed $hash
  407. */
  408. public function setHash( $hash )
  409. {
  410. $this->hash = $hash;
  411. }
  412. /**
  413. * @Serializer\VirtualProperty()
  414. * @Serializer\Groups({"api"})
  415. * @Serializer\Inline()
  416. */
  417. public function getPrice( $curr = null )
  418. {
  419. if($curr === null){$curr = \Locale::getDefault() === 'cs'?'CZK':'EUR';}
  420. return $this->getPricelists()[$curr];
  421. }
  422. /**
  423. * Get Translated
  424. *
  425. * @return mixed
  426. */
  427. public function getTranslated()
  428. {
  429. return $this->translated;
  430. }
  431. /**
  432. * Set Translated
  433. *
  434. * @param mixed $translated
  435. */
  436. public function setTranslated( $translated )
  437. {
  438. $this->translated = $translated;
  439. }
  440. /**
  441. * Get OnlyForAdults
  442. *
  443. * @return mixed
  444. */
  445. public function getOnlyForAdults()
  446. {
  447. return $this->onlyForAdults;
  448. }
  449. /**
  450. * Set OnlyForAdults
  451. *
  452. * @param mixed $onlyForAdults
  453. */
  454. public function setOnlyForAdults( $onlyForAdults )
  455. {
  456. $this->onlyForAdults = $onlyForAdults;
  457. }
  458. /**
  459. * Get ProductImage
  460. *
  461. * @return mixed
  462. */
  463. public function getProductImage()
  464. {
  465. return $this->productImage;
  466. }
  467. /**
  468. * Set ProductImage
  469. *
  470. * @param mixed $productImage
  471. */
  472. public function setProductImage( $productImage )
  473. {
  474. $this->productImage = $productImage;
  475. }
  476. /**
  477. * Get Url
  478. *
  479. * @return mixed
  480. */
  481. public function getUrl()
  482. {
  483. return $this->url;
  484. }
  485. /**
  486. * Set Url
  487. *
  488. * @param mixed $url
  489. */
  490. public function setUrl( $url )
  491. {
  492. $this->url = $url;
  493. }
  494. /**
  495. * Get AppCategory
  496. *
  497. * @return mixed
  498. */
  499. public function getAppCategory()
  500. {
  501. return $this->appCategory;
  502. }
  503. /**
  504. * Set AppCategory
  505. *
  506. * @param mixed $appCategory
  507. */
  508. public function setAppCategory( $appCategory )
  509. {
  510. $this->appCategory = $appCategory;
  511. }
  512. }