src/ProductBundle/Entity/Product.php line 37

Open in your IDE?
  1. <?php
  2. namespace App\ProductBundle\Entity;
  3. use App\ContentBundle\Entity\Post;
  4. use App\ContentBundle\Entity\Translation\PostTranslation;
  5. use App\CurrencyBundle\Entity\Currency;
  6. use App\ECommerceBundle\Entity\CouponHasProduct;
  7. use App\MediaBundle\Entity\Image;
  8. use App\ProductBundle\Entity\Translation\ProductTranslation;
  9. use App\SeoBundle\Entity\Seo;
  10. use App\ThreeSixtyViewBundle\Entity\ThreeSixtyView;
  11. use App\VendorBundle\Entity\StoreAddress;
  12. use App\VendorBundle\Entity\Vendor;
  13. use Doctrine\Common\Collections\ArrayCollection;
  14. use Doctrine\Common\Collections\Collection;
  15. use Doctrine\Common\Collections\Criteria;
  16. use Doctrine\DBAL\Types\Types;
  17. use Doctrine\ORM\Mapping as ORM;
  18. use Gedmo\Mapping\Annotation as Gedmo;
  19. use PN\LocaleBundle\Model\LocaleTrait;
  20. use PN\LocaleBundle\Model\Translatable;
  21. use PN\ServiceBundle\Interfaces\DateTimeInterface;
  22. use PN\ServiceBundle\Interfaces\UUIDInterface;
  23. use PN\ServiceBundle\Model\DateTimeTrait;
  24. use PN\ServiceBundle\Model\UuidTrait;
  25. use PN\ServiceBundle\Model\VirtualDeleteTrait;
  26. use PN\ServiceBundle\Utils\SearchText;
  27. use Symfony\Component\Validator\Constraints as Assert;
  28. /**
  29. * @ORM\HasLifecycleCallbacks
  30. * @ORM\Table(name="product", indexes={@ORM\Index(columns={"normalized_text"}, flags={"fulltext"})})
  31. * @ORM\Entity(repositoryClass="App\ProductBundle\Repository\ProductRepository")
  32. * @Gedmo\Loggable
  33. */
  34. class Product implements Translatable, DateTimeInterface, UUIDInterface
  35. {
  36. use VirtualDeleteTrait,
  37. DateTimeTrait,
  38. LocaleTrait,
  39. UuidTrait;
  40. /**
  41. * @ORM\Id
  42. * @ORM\Column(name="id", type="integer")
  43. * @ORM\GeneratedValue(strategy="AUTO")
  44. */
  45. private ?int $id = null;
  46. /**
  47. * @ORM\OneToOne(targetEntity="\App\SeoBundle\Entity\Seo", cascade={"persist", "remove" })
  48. */
  49. private ?Seo $seo = null;
  50. /**
  51. * Short Description
  52. * Description
  53. * Gallery
  54. *
  55. * @ORM\OneToOne(targetEntity="\App\ContentBundle\Entity\Post", cascade={"persist", "remove" })
  56. */
  57. private ?Post $post = null;
  58. /**
  59. * @Gedmo\Versioned
  60. * @Assert\NotNull
  61. * @ORM\ManyToOne(targetEntity="Category", cascade={"persist"})
  62. */
  63. private ?Category $category = null;
  64. /**
  65. * @Gedmo\Versioned
  66. * @ORM\ManyToOne(targetEntity="App\VendorBundle\Entity\Vendor")
  67. */
  68. private ?Vendor $vendor = null;
  69. /**
  70. * @Gedmo\Versioned
  71. * @ORM\ManyToOne(targetEntity="App\VendorBundle\Entity\StoreAddress")
  72. */
  73. private ?StoreAddress $storeAddress = null;
  74. /**
  75. * @Gedmo\Versioned
  76. * @ORM\ManyToOne(targetEntity="Brand")
  77. */
  78. private ?Brand $brand = null;
  79. /**
  80. * @ORM\ManyToOne(targetEntity="App\CurrencyBundle\Entity\Currency")
  81. */
  82. private ?Currency $currency = null;
  83. /**
  84. * @ORM\ManyToOne(targetEntity="App\MediaBundle\Entity\Image", cascade={"persist"})
  85. * @ORM\JoinColumn(name="main_image_id", referencedColumnName="id", onDelete="SET NULL")
  86. */
  87. public ?Image $mainImage = null;
  88. /**
  89. * @ORM\OneToOne(targetEntity="App\ThreeSixtyViewBundle\Entity\ThreeSixtyView", cascade={"persist", "remove" })
  90. */
  91. private ?ThreeSixtyView $threeSixtyView = null;
  92. /**
  93. * @Gedmo\Versioned
  94. * @Assert\NotNull
  95. * @ORM\Column(name="title", type="string", length=255)
  96. */
  97. private ?string $title = null;
  98. /**
  99. * @Gedmo\Versioned
  100. * @ORM\Column(name="sku", type="string", length=100, nullable=true)
  101. */
  102. private ?string $sku = null;
  103. /**
  104. * @Gedmo\Versioned
  105. * @ORM\Column(name="tarteb", type="smallint", nullable=true)
  106. */
  107. private ?int $tarteb = null;
  108. /**
  109. * @Gedmo\Versioned
  110. * @ORM\Column(name="search_terms", type="text", nullable=true)
  111. */
  112. private ?string $searchTerms = null;
  113. /**
  114. * @ORM\Column(name="normalized_text", type="text", nullable=true)
  115. */
  116. protected ?string $normalizedTxt = null;
  117. /**
  118. * @Gedmo\Versioned
  119. * @ORM\Column(name="featured", type="boolean")
  120. */
  121. private bool $featured = false;
  122. /**
  123. * @Gedmo\Versioned
  124. * @ORM\Column(name="new_arrival", type="boolean")
  125. */
  126. private bool $newArrival = false;
  127. /**
  128. * @Gedmo\Versioned
  129. * @ORM\Column(name="enable_variants", type="boolean")
  130. */
  131. private bool $enableVariants = false;
  132. /**
  133. * @Gedmo\Versioned
  134. * @ORM\Column(name="publish", type="boolean")
  135. */
  136. private bool $publish = true;
  137. /**
  138. * @Assert\NotNull
  139. * @ORM\OneToMany(targetEntity="App\ProductBundle\Entity\ProductPrice", mappedBy="product", cascade={"persist"})
  140. */
  141. private Collection $prices;
  142. /**
  143. * @ORM\OneToMany(targetEntity="App\ProductBundle\Entity\ProductFavorite", mappedBy="product", cascade={"persist"})
  144. */
  145. private Collection $favorites;
  146. /**
  147. * @ORM\OneToMany(targetEntity="App\ProductBundle\Entity\Translation\ProductTranslation", mappedBy="translatable", cascade={"ALL"}, orphanRemoval=true)
  148. */
  149. private Collection $translations;
  150. /**
  151. * @ORM\OneToOne(targetEntity="App\ProductBundle\Entity\ProductDetails", mappedBy="product", cascade={"ALL"}, orphanRemoval=true)
  152. */
  153. private ?ProductDetails $details = null;
  154. /**
  155. * @ORM\OneToMany(targetEntity="ProductHasAttribute", mappedBy="product", cascade={"persist"})
  156. */
  157. private Collection $productHasAttributes;
  158. /**
  159. * @ORM\OneToMany(targetEntity="App\ProductBundle\Entity\ProductHasCollection", mappedBy="product", cascade={"persist"}, orphanRemoval=true)
  160. */
  161. private Collection $productHasCollections;
  162. /**
  163. * @ORM\OneToMany(targetEntity="App\ProductBundle\Entity\ProductHasOccasion", mappedBy="product", cascade={"persist"}, orphanRemoval=true)
  164. */
  165. private Collection $productHasOccasions;
  166. /**
  167. * @ORM\OneToMany(targetEntity="App\ECommerceBundle\Entity\CouponHasProduct", mappedBy="product", cascade={"persist"}, orphanRemoval=true)
  168. */
  169. private Collection $couponHasProducts;
  170. public function __toString()
  171. {
  172. return $this->getTitle();
  173. }
  174. public function getObj(): array
  175. {
  176. return [
  177. "title" => (string)$this->getTitle(),
  178. "newArrival" => (bool)$this->isNewArrival(),
  179. "featured" => (bool)$this->isFeatured(),
  180. ];
  181. }
  182. public function __construct()
  183. {
  184. $this->translations = new ArrayCollection();
  185. $this->productHasAttributes = new ArrayCollection();
  186. $this->productHasCollections = new ArrayCollection();
  187. $this->productHasOccasions = new ArrayCollection();
  188. $this->prices = new ArrayCollection();
  189. $this->couponHasProducts = new ArrayCollection();
  190. $this->favorites = new ArrayCollection();
  191. }
  192. /**
  193. * Now we tell doctrine that before we persist or update we call the updatedTimestamps() function.
  194. *
  195. * @ORM\PrePersist
  196. * @ORM\PreUpdate
  197. */
  198. public function updatedTimestamps()
  199. {
  200. $this->updateNormalizedTxt();
  201. }
  202. public function __clone()
  203. {
  204. if ($this->id) {
  205. $this->id = null;
  206. $this->seo = clone $this->seo;
  207. $newPost = new Post();
  208. $newPost->setContent($this->post->getContent());
  209. foreach ($this->post->getTranslations() as $translation) {
  210. $newPostTrans = new PostTranslation();
  211. $newPostTrans->setContent($translation->getContent());
  212. $newPostTrans->setLanguage($translation->getLanguage());
  213. $newPost->addTranslation($newPostTrans);
  214. }
  215. $newPost->getImages()->clear();
  216. $this->post = $newPost;
  217. $details = clone $this->details;
  218. $details->setProduct($this);
  219. $this->details = $details;
  220. $translationsClone = new ArrayCollection();
  221. foreach ($this->getTranslations() as $translation) {
  222. $itemClone = clone $translation;
  223. $itemClone->setTranslatable($this);
  224. $translationsClone->add($itemClone);
  225. }
  226. $this->translations = $translationsClone;
  227. $pricesClone = new ArrayCollection();
  228. foreach ($this->getPrices() as $price) {
  229. $itemClone = clone $price;
  230. $itemClone->setProduct($this);
  231. $pricesClone->add($itemClone);
  232. }
  233. $this->prices = $pricesClone;
  234. $specsClone = new ArrayCollection();
  235. foreach ($this->getProductHasAttributes() as $productHasAttribute) {
  236. $itemClone = clone $productHasAttribute;
  237. $itemClone->setProduct($this);
  238. $specsClone->add($itemClone);
  239. }
  240. $this->productHasAttributes = $specsClone;
  241. $collectionsClone = new ArrayCollection();
  242. foreach ($this->getProductHasCollections() as $productHasCollection) {
  243. $itemClone = clone $productHasCollection;
  244. $itemClone->setProduct($this);
  245. $collectionsClone->add($itemClone);
  246. }
  247. $this->productHasCollections = $collectionsClone;
  248. $occasionsClone = new ArrayCollection();
  249. foreach ($this->getProductHasOccasions() as $productHasOccasion) {
  250. $itemClone = clone $productHasOccasion;
  251. $itemClone->setProduct($this);
  252. $occasionsClone->add($itemClone);
  253. }
  254. $this->productHasOccasions = $occasionsClone;
  255. }
  256. }
  257. /**
  258. * @return Collection|ProductPrice[]
  259. */
  260. public function getPrices(?int $unitPriceGreaterThan = null): Collection
  261. {
  262. $criteria = Criteria::create();
  263. $criteria->where(Criteria::expr()->eq('deleted', null));
  264. if ($unitPriceGreaterThan !== null) {
  265. $criteria->andWhere(Criteria::expr()->gt('unitPrice', $unitPriceGreaterThan));
  266. }
  267. return $this->prices->matching($criteria);
  268. }
  269. public function removePrice(ProductPrice $productPrice): self
  270. {
  271. if ($this->prices->removeElement($productPrice)) {
  272. $productPrice->setDeleted(new \DateTime());
  273. }
  274. return $this;
  275. }
  276. private function updateNormalizedTxt()
  277. {
  278. $keywords = [
  279. $this->getTitle(),
  280. $this->getSku(),
  281. $this->getSearchTerms(),
  282. ];
  283. foreach ($this->getTranslations() as $translation) {
  284. $keywords[] = $translation->getTitle();
  285. }
  286. $searchableKeyword = SearchText::makeSearchableKeywords($keywords);
  287. $this->setNormalizedTxt($searchableKeyword);
  288. }
  289. public function getProductHasAttributesByAttributeId(Attribute $attribute): Collection
  290. {
  291. $criteria = Criteria::create();
  292. $criteria->where(Criteria::expr()->eq('attribute', $attribute));
  293. return $this->productHasAttributes->matching($criteria);
  294. }
  295. public function getTitle(): ?string
  296. {
  297. return !$this->currentTranslation ? $this->title : $this->currentTranslation->getTitle();
  298. }
  299. public function getId(): ?int
  300. {
  301. return $this->id;
  302. }
  303. public function setTitle(string $title): self
  304. {
  305. $this->title = $title;
  306. return $this;
  307. }
  308. public function getSku(): ?string
  309. {
  310. return $this->sku;
  311. }
  312. public function setSku(?string $sku): self
  313. {
  314. $this->sku = $sku;
  315. return $this;
  316. }
  317. public function getTarteb(): ?int
  318. {
  319. return $this->tarteb;
  320. }
  321. public function setTarteb(?int $tarteb): self
  322. {
  323. $this->tarteb = $tarteb;
  324. return $this;
  325. }
  326. public function getSearchTerms(): ?string
  327. {
  328. return $this->searchTerms;
  329. }
  330. public function setSearchTerms(?string $searchTerms): self
  331. {
  332. $this->searchTerms = $searchTerms;
  333. return $this;
  334. }
  335. public function getNormalizedTxt(): ?string
  336. {
  337. return $this->normalizedTxt;
  338. }
  339. public function setNormalizedTxt(?string $normalizedTxt): self
  340. {
  341. $this->normalizedTxt = $normalizedTxt;
  342. return $this;
  343. }
  344. public function isFeatured(): ?bool
  345. {
  346. return $this->featured;
  347. }
  348. public function setFeatured(bool $featured): self
  349. {
  350. $this->featured = $featured;
  351. return $this;
  352. }
  353. public function isNewArrival(): ?bool
  354. {
  355. return $this->newArrival;
  356. }
  357. public function setNewArrival(bool $newArrival): self
  358. {
  359. $this->newArrival = $newArrival;
  360. return $this;
  361. }
  362. public function isPublish(): ?bool
  363. {
  364. return $this->publish;
  365. }
  366. public function setPublish(bool $publish): self
  367. {
  368. $this->publish = $publish;
  369. return $this;
  370. }
  371. public function getSeo(): ?Seo
  372. {
  373. return $this->seo;
  374. }
  375. public function setSeo(?Seo $seo): self
  376. {
  377. $this->seo = $seo;
  378. return $this;
  379. }
  380. public function getPost(): ?Post
  381. {
  382. return $this->post;
  383. }
  384. public function setPost(?Post $post): self
  385. {
  386. $this->post = $post;
  387. return $this;
  388. }
  389. public function getCategory(): ?Category
  390. {
  391. return $this->category;
  392. }
  393. public function setCategory(?Category $category): self
  394. {
  395. $this->category = $category;
  396. return $this;
  397. }
  398. public function getCurrency(): ?Currency
  399. {
  400. return $this->currency;
  401. }
  402. public function setCurrency(?Currency $currency): self
  403. {
  404. $this->currency = $currency;
  405. return $this;
  406. }
  407. public function addPrice(ProductPrice $price): self
  408. {
  409. if (!$this->prices->contains($price)) {
  410. $this->prices->add($price);
  411. $price->setProduct($this);
  412. }
  413. return $this;
  414. }
  415. /**
  416. * @return Collection<int, ProductTranslation>
  417. */
  418. public function getTranslations(): Collection
  419. {
  420. return $this->translations;
  421. }
  422. public function addTranslation(ProductTranslation $translation): self
  423. {
  424. if (!$this->translations->contains($translation)) {
  425. $this->translations->add($translation);
  426. $translation->setTranslatable($this);
  427. }
  428. return $this;
  429. }
  430. public function removeTranslation(ProductTranslation $translation): self
  431. {
  432. if ($this->translations->removeElement($translation)) {
  433. // set the owning side to null (unless already changed)
  434. if ($translation->getTranslatable() === $this) {
  435. $translation->setTranslatable(null);
  436. }
  437. }
  438. return $this;
  439. }
  440. public function getDetails(): ?ProductDetails
  441. {
  442. return $this->details;
  443. }
  444. public function setDetails(?ProductDetails $details): self
  445. {
  446. // unset the owning side of the relation if necessary
  447. if ($details === null && $this->details !== null) {
  448. $this->details->setProduct(null);
  449. }
  450. // set the owning side of the relation if necessary
  451. if ($details !== null && $details->getProduct() !== $this) {
  452. $details->setProduct($this);
  453. }
  454. $this->details = $details;
  455. return $this;
  456. }
  457. /**
  458. * @return Collection<int, ProductHasAttribute>
  459. */
  460. public function getProductHasAttributes(): Collection
  461. {
  462. return $this->productHasAttributes;
  463. }
  464. public function addProductHasAttribute(ProductHasAttribute $productHasAttribute): self
  465. {
  466. if (!$this->productHasAttributes->contains($productHasAttribute)) {
  467. $this->productHasAttributes->add($productHasAttribute);
  468. $productHasAttribute->setProduct($this);
  469. }
  470. return $this;
  471. }
  472. public function removeProductHasAttribute(ProductHasAttribute $productHasAttribute): self
  473. {
  474. if ($this->productHasAttributes->removeElement($productHasAttribute)) {
  475. // set the owning side to null (unless already changed)
  476. if ($productHasAttribute->getProduct() === $this) {
  477. $productHasAttribute->setProduct(null);
  478. }
  479. }
  480. return $this;
  481. }
  482. /**
  483. * @return Collection<int, ProductHasCollection>
  484. */
  485. public function getProductHasCollections(): Collection
  486. {
  487. return $this->productHasCollections;
  488. }
  489. public function addProductHasCollection(ProductHasCollection $productHasCollection): self
  490. {
  491. if (!$this->productHasCollections->contains($productHasCollection)) {
  492. $this->productHasCollections->add($productHasCollection);
  493. $productHasCollection->setProduct($this);
  494. }
  495. return $this;
  496. }
  497. public function removeProductHasCollection(ProductHasCollection $productHasCollection): self
  498. {
  499. if ($this->productHasCollections->removeElement($productHasCollection)) {
  500. // set the owning side to null (unless already changed)
  501. if ($productHasCollection->getProduct() === $this) {
  502. $productHasCollection->setProduct(null);
  503. }
  504. }
  505. return $this;
  506. }
  507. /**
  508. * @return Collection<int, ProductHasOccasion>
  509. */
  510. public function getProductHasOccasions(): Collection
  511. {
  512. return $this->productHasOccasions;
  513. }
  514. public function addProductHasOccasion(ProductHasOccasion $productHasOccasion): self
  515. {
  516. if (!$this->productHasOccasions->contains($productHasOccasion)) {
  517. $this->productHasOccasions->add($productHasOccasion);
  518. $productHasOccasion->setProduct($this);
  519. }
  520. return $this;
  521. }
  522. public function removeProductHasOccasion(ProductHasOccasion $productHasOccasion): self
  523. {
  524. if ($this->productHasOccasions->removeElement($productHasOccasion)) {
  525. // set the owning side to null (unless already changed)
  526. if ($productHasOccasion->getProduct() === $this) {
  527. $productHasOccasion->setProduct(null);
  528. }
  529. }
  530. return $this;
  531. }
  532. public function getMainImage(): ?Image
  533. {
  534. return $this->mainImage;
  535. }
  536. public function setMainImage(?Image $mainImage): self
  537. {
  538. $this->mainImage = $mainImage;
  539. return $this;
  540. }
  541. /**
  542. * @return Collection<int, CouponHasProduct>
  543. */
  544. public function getCouponHasProducts(): Collection
  545. {
  546. return $this->couponHasProducts;
  547. }
  548. public function addCouponHasProduct(CouponHasProduct $couponHasProduct): self
  549. {
  550. if (!$this->couponHasProducts->contains($couponHasProduct)) {
  551. $this->couponHasProducts->add($couponHasProduct);
  552. $couponHasProduct->setProduct($this);
  553. }
  554. return $this;
  555. }
  556. public function removeCouponHasProduct(CouponHasProduct $couponHasProduct): self
  557. {
  558. if ($this->couponHasProducts->removeElement($couponHasProduct)) {
  559. // set the owning side to null (unless already changed)
  560. if ($couponHasProduct->getProduct() === $this) {
  561. $couponHasProduct->setProduct(null);
  562. }
  563. }
  564. return $this;
  565. }
  566. public function isEnableVariants(): ?bool
  567. {
  568. return $this->enableVariants;
  569. }
  570. public function setEnableVariants(bool $enableVariants): self
  571. {
  572. $this->enableVariants = $enableVariants;
  573. return $this;
  574. }
  575. public function getBrand(): ?Brand
  576. {
  577. return $this->brand;
  578. }
  579. public function setBrand(?Brand $brand): static
  580. {
  581. $this->brand = $brand;
  582. return $this;
  583. }
  584. /**
  585. * @return Collection<int, ProductFavorite>
  586. */
  587. public function getFavorites(): Collection
  588. {
  589. return $this->favorites;
  590. }
  591. public function addFavorite(ProductFavorite $favorite): static
  592. {
  593. if (!$this->favorites->contains($favorite)) {
  594. $this->favorites->add($favorite);
  595. $favorite->setProduct($this);
  596. }
  597. return $this;
  598. }
  599. public function removeFavorite(ProductFavorite $favorite): static
  600. {
  601. if ($this->favorites->removeElement($favorite)) {
  602. // set the owning side to null (unless already changed)
  603. if ($favorite->getProduct() === $this) {
  604. $favorite->setProduct(null);
  605. }
  606. }
  607. return $this;
  608. }
  609. public function getVendor(): ?Vendor
  610. {
  611. return $this->vendor;
  612. }
  613. public function setVendor(?Vendor $vendor): static
  614. {
  615. $this->vendor = $vendor;
  616. return $this;
  617. }
  618. public function getStoreAddress(): ?StoreAddress
  619. {
  620. return $this->storeAddress;
  621. }
  622. public function setStoreAddress(?StoreAddress $storeAddress): static
  623. {
  624. $this->storeAddress = $storeAddress;
  625. return $this;
  626. }
  627. public function getThreeSixtyView(): ?ThreeSixtyView
  628. {
  629. return $this->threeSixtyView;
  630. }
  631. public function setThreeSixtyView(?ThreeSixtyView $threeSixtyView): static
  632. {
  633. $this->threeSixtyView = $threeSixtyView;
  634. return $this;
  635. }
  636. }