<?php
namespace App\Entity;
use App\Repository\ProductRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity(repositoryClass: ProductRepository::class)]
class Product
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
private ?int $id = null;
#[ORM\Column(length: 255)]
private ?string $reference = null;
#[ORM\Column(length: 13, nullable: true)]
private ?string $ean13 = null;
#[ORM\Column(length: 32, nullable: true)]
private ?string $isbn = null;
#[ORM\Column(length: 40, nullable: true)]
private ?string $mpn = null;
#[ORM\Column(type: Types::DECIMAL, precision: 26, scale: 6, nullable: true)]
private ?string $price = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $width = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $height = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $depth = null;
#[ORM\Column(type: Types::DECIMAL, precision: 10, scale: 2, nullable: true)]
private ?string $weight = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $date_add = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE)]
private ?\DateTimeInterface $date_upd = null;
#[ORM\OneToMany(mappedBy: 'product', targetEntity: ProductLang::class)]
private Collection $productLangs;
#[ORM\OneToMany(mappedBy: 'product', targetEntity: ProductAttribute::class)]
private Collection $productAttributes;
#[ORM\OneToMany(mappedBy: 'product', targetEntity: Image::class)]
private Collection $image;
#[ORM\ManyToMany(targetEntity: FeatureValue::class, inversedBy: 'products')]
private Collection $featureValues;
#[ORM\ManyToMany(targetEntity: Advantage::class, mappedBy: 'product')]
private Collection $advantages;
#[ORM\ManyToOne(inversedBy: 'products')]
private ?Brand $brand = null;
#[ORM\ManyToMany(targetEntity: File::class, mappedBy: 'products')]
private Collection $files;
#[ORM\OneToOne(mappedBy: 'product', targetEntity: ERPCommercialData::class)]
private ?ERPCommercialData $ERPCommercialData = null;
#[ORM\OneToOne(mappedBy: 'product', targetEntity: ERPLogisticData::class)]
private ?ERPLogisticData $ERPLogisticData = null;
#[ORM\OneToMany(mappedBy: 'product', targetEntity: ERPCommercialDataLang::class)]
private Collection $ERPCommercialDataLangs;
public function __construct()
{
$this->date_add = new \DateTime();
$this->date_upd = new \DateTime();
$this->productLangs = new ArrayCollection();
$this->productAttributes = new ArrayCollection();
$this->image = new ArrayCollection();
$this->featureValues = new ArrayCollection();
$this->advantages = new ArrayCollection();
$this->files = new ArrayCollection();
$this->ERPCommercialDataLangs = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getReference(): ?string
{
return $this->reference;
}
public function setReference(string $reference): self
{
$this->reference = $reference;
return $this;
}
public function getEan13(): ?string
{
return $this->ean13;
}
public function setEan13(string $ean13): self
{
$this->ean13 = $ean13;
return $this;
}
public function getIsbn(): ?string
{
return $this->isbn;
}
public function setIsbn(string $isbn): self
{
$this->isbn = $isbn;
return $this;
}
public function getMpn(): ?string
{
return $this->mpn;
}
public function setMpn(?string $mpn): self
{
$this->mpn = $mpn;
return $this;
}
public function getPrice(): ?string
{
return $this->price;
}
public function setPrice(?string $price): self
{
$this->price = $price;
return $this;
}
public function getWidth(): ?string
{
return $this->width;
}
public function setWidth(?string $width): self
{
$this->width = $width;
return $this;
}
public function getDateAdd(): ?\DateTimeInterface
{
return $this->date_add;
}
public function setDateAdd(\DateTimeInterface $date_add): self
{
$this->date_add = $date_add;
return $this;
}
public function getDateUpd(): ?\DateTimeInterface
{
return $this->date_upd;
}
public function setDateUpd(\DateTimeInterface $date_upd): self
{
$this->date_upd = $date_upd;
return $this;
}
/**
* Get the value of height
*
* @return ?string
*/
public function getHeight(): ?string
{
return $this->height;
}
/**
* Set the value of height
*
* @param ?string $height
*
* @return self
*/
public function setHeight(?string $height): self
{
$this->height = $height;
return $this;
}
/**
* Get the value of depth
*
* @return ?string
*/
public function getDepth(): ?string
{
return $this->depth;
}
/**
* Set the value of depth
*
* @param ?string $depth
*
* @return self
*/
public function setDepth(?string $depth): self
{
$this->depth = $depth;
return $this;
}
/**
* Get the value of weight
*
* @return ?string
*/
public function getWeight(): ?string
{
return $this->weight;
}
/**
* Set the value of weight
*
* @param ?string $weight
*
* @return self
*/
public function setWeight(?string $weight): self
{
$this->weight = $weight;
return $this;
}
/**
* @return Collection<int, ProductLang>
*/
public function getProductLangs(): Collection
{
return $this->productLangs;
}
public function addProductLang(ProductLang $productLang): self
{
if (!$this->productLangs->contains($productLang)) {
$this->productLangs->add($productLang);
$productLang->setProduct($this);
}
return $this;
}
public function removeProductLang(ProductLang $productLang): self
{
if ($this->productLangs->removeElement($productLang)) {
// set the owning side to null (unless already changed)
if ($productLang->getProduct() === $this) {
$productLang->setProduct(null);
}
}
return $this;
}
/**
* @return Collection<int, ProductAttribute>
*/
public function getProductAttributes(): Collection
{
return $this->productAttributes;
}
public function addProductAttribute(ProductAttribute $productAttribute): self
{
if (!$this->productAttributes->contains($productAttribute)) {
$this->productAttributes->add($productAttribute);
$productAttribute->setProduct($this);
}
return $this;
}
public function removeProductAttribute(ProductAttribute $productAttribute): self
{
if ($this->productAttributes->removeElement($productAttribute)) {
// set the owning side to null (unless already changed)
if ($productAttribute->getProduct() === $this) {
$productAttribute->setProduct(null);
}
}
return $this;
}
/**
* @return Collection<int, Image>
*/
public function getImage(): Collection
{
//sort images by position
$images = $this->image->toArray();
usort($images, function ($a, $b) {
return $a->getPosition() <=> $b->getPosition();
});
return new ArrayCollection($images);
}
public function addImage(Image $image): self
{
if (!$this->image->contains($image)) {
$this->image->add($image);
$image->setProduct($this);
}
return $this;
}
public function removeImage(Image $image): self
{
if ($this->image->removeElement($image)) {
// set the owning side to null (unless already changed)
if ($image->getProduct() === $this) {
$image->setProduct(null);
}
}
return $this;
}
/**
* @return Collection<int, FeatureValue>
*/
public function getFeatureValues(): Collection
{
return $this->featureValues;
}
public function addFeatureValue(FeatureValue $featureValue): self
{
if (!$this->featureValues->contains($featureValue)) {
$this->featureValues->add($featureValue);
}
return $this;
}
public function removeFeatureValue(FeatureValue $featureValue): self
{
$this->featureValues->removeElement($featureValue);
return $this;
}
/**
* @return Collection<int, Advantage>
*/
public function getAdvantages(): Collection
{
return $this->advantages;
}
public function addAdvantage(Advantage $advantage): self
{
if (!$this->advantages->contains($advantage)) {
$this->advantages->add($advantage);
$advantage->addProduct($this);
}
return $this;
}
public function removeAdvantage(Advantage $advantage): self
{
if ($this->advantages->removeElement($advantage)) {
$advantage->removeProduct($this);
}
return $this;
}
public function editorToHTML($editorContent){
$editorContent = json_decode($editorContent, true);
$editorHTML = "";
if(isset($editorContent['blocks'])) {
$editorBlocks = $editorContent['blocks'];
foreach ($editorBlocks as $keyBlock => $block) {
switch ($block['type']) {
case 'header':
$editorHTML .= "<h".$block['data']['level'].">". $block['data']['text'] . "</h" . $block['data']['level'] . ">";
break;
case 'paragraph':
$editorHTML .= "<p>".$block['data']['text']."</p>";
break;
case 'list':
$this->generateList($block['data']['items'], $editorHTML);
break;
case 'image':
$withBorder = $block['data']['withBorder'];
$stretched = $block['data']['stretched'];
$withBackground = $block['data']['withBackground'];
$editorHTML .= "<img";
$editorHTML .= " style='max-width:100%;";
if($withBorder || $stretched) {
if($withBorder) {
$editorHTML .= " border:1px solid black;";
}
if($stretched) {
$editorHTML .= " width:100%";
}
}
$editorHTML .= "' alt='".$block['data']['caption']."' src='".$_ENV['APP_URL'].$block['data']['file']['url']."'>";
break;
case 'html':
$editorHTML .= $block['data']['html'];
break;
case 'button':
$editorHTML .= "<div class='buttonContainer'><a href='".$block['data']['link']."'";
if($block['data']['blank']){
$editorHTML .= " target='_blank'";
}
$editorHTML .= " class='btn ";
if($block['data']['style'] == "full"){
$editorHTML .= "cta1'";
}
if($block['data']['style'] == "outline"){
$editorHTML .= "cta2'";
}
$editorHTML .= ">".$block['data']['text']."</a></div>";
break;
}
}
}
return $editorHTML;
}
public function generateDescription(Lang $lang){
$descriptionData = "";
foreach ($this->productLangs as $keyProductLang => $productLang) {
if($productLang->getLang() == $lang){
$descriptionData = $productLang->getDescription();
}
}
$descriptionData = json_decode($descriptionData, true);
$descriptionHTML = "";
$descriptionBlocks = $descriptionData['blocks'];
foreach ($descriptionBlocks as $keyBlock => $block) {
switch ($block['type']) {
case 'header':
$descriptionHTML .= "<h".$block['data']['level'].">". $block['data']['text'] . "</h" . $block['data']['level'] . ">";
break;
case 'paragraph':
$descriptionHTML .= "<p>".$block['data']['text']."</p>";
break;
case 'list':
$this->generateList($block['data']['items'], $descriptionHTML);
break;
case 'image':
$withBorder = $block['data']['withBorder'];
$stretched = $block['data']['stretched'];
$withBackground = $block['data']['withBackground'];
$descriptionHTML .= "<img";
if($withBorder || $stretched){
$descriptionHTML .= " style='";
if($withBorder){
$descriptionHTML .= " border:1px solid black;";
}
if($stretched){
$descriptionHTML .= " width:100%";
}
}
$descriptionHTML .= " alt='".$block['data']['caption']."' src='".$_ENV['APP_URL'].$block['data']['file']['url']."'>";
break;
}
}
return $descriptionHTML;
}
private function generateList($items, &$descriptionHTML){
$descriptionHTML .= "<ul class='list-tf'>";
foreach ($items as $keyItem => $item) {
$descriptionHTML .= "<li>".$item['content'];
if(count($item['items'])> 0){
$this->generateList($item['items'],$descriptionHTML);
}
$descriptionHTML .= "</li>";
}
$descriptionHTML .= "</ul>";
}
public function getBrand(): ?Brand
{
return $this->brand;
}
public function setBrand(?Brand $brand): self
{
$this->brand = $brand;
return $this;
}
/**
* @return Collection<int, File>
*/
public function getFiles(): Collection
{
return $this->files;
}
public function addFile(File $file): self
{
if (!$this->files->contains($file)) {
$this->files->add($file);
$file->addProduct($this);
}
return $this;
}
public function removeFile(File $file): self
{
if ($this->files->removeElement($file)) {
$file->removeProduct($this);
}
return $this;
}
public function getERPCommercialData(): ?ERPCommercialData
{
return $this->ERPCommercialData;
}
public function setERPCommercialData(?ERPCommercialData $ERPCommercialData): self
{
// unset the owning side of the relation if necessary
if ($ERPCommercialData === null && $this->ERPCommercialData !== null) {
$this->ERPCommercialData->setProduct(null);
}
// set the owning side of the relation if necessary
if ($ERPCommercialData !== null && $ERPCommercialData->getProduct() !== $this) {
$ERPCommercialData->setProduct($this);
}
$this->ERPCommercialData = $ERPCommercialData;
return $this;
}
public function getERPLogisticData(): ?ERPLogisticData
{
return $this->ERPLogisticData;
}
public function setERPLogisticData(?ERPLogisticData $ERPLogisticData): self
{
// unset the owning side of the relation if necessary
if ($ERPLogisticData === null && $this->ERPLogisticData !== null) {
$this->ERPLogisticData->setProduct(null);
}
// set the owning side of the relation if necessary
if ($ERPLogisticData !== null && $ERPLogisticData->getProduct() !== $this) {
$ERPLogisticData->setProduct($this);
}
$this->ERPLogisticData = $ERPLogisticData;
return $this;
}
/**
* @return Collection<int, ERPCommercialDataLang>
*/
public function getERPCommercialDataLangs(): Collection
{
return $this->ERPCommercialDataLangs;
}
public function addERPCommercialDataLang(ERPCommercialDataLang $eRPCommercialDataLang): self
{
if (!$this->ERPCommercialDataLangs->contains($eRPCommercialDataLang)) {
$this->ERPCommercialDataLangs->add($eRPCommercialDataLang);
$eRPCommercialDataLang->setProduct($this);
}
return $this;
}
public function removeERPCommercialDataLang(ERPCommercialDataLang $eRPCommercialDataLang): self
{
if ($this->ERPCommercialDataLangs->removeElement($eRPCommercialDataLang)) {
// set the owning side to null (unless already changed)
if ($eRPCommercialDataLang->getProduct() === $this) {
$eRPCommercialDataLang->setProduct(null);
}
}
return $this;
}
}