<?php
namespace MLDev\CatalogBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use MLDev\BaseBundle\Contract\SeoSite\SeoSiteInfoInterface;
use MLDev\BaseBundle\Entity\Page;
use MLDev\BaseBundle\Entity\Traits\Timestampable;
use MLDev\CatalogBundle\Repository\ProductRepository;
/**
* @ORM\Table(name="MLDev_Product")
* @ORM\Entity(repositoryClass=ProductRepository::class)
* @ORM\HasLifecycleCallbacks()
*/
class Product
{
use Timestampable;
const ORDER_BY_NAME = 'name';
const ORDER_BY_PRICE = 'price';
const ORDER_BY_PRIORITY = 'priority';
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(name="external_id", type="string", nullable=true)
*/
private $externalId;
/**
* @ORM\Column(name="name", type="string", nullable=false)
*/
private $name;
/**
* @ORM\Column(name="alter_name", type="string", nullable=true)
*/
private $alterName;
/**
* @ORM\Column(name="type_prefix", type="string", nullable=true)
*/
private $typePrefix;
/**
* @Gedmo\Slug(fields={"name"}, updatable=false, unique=true, separator="-")
* @ORM\Column(name="alias", type="string", length=255, nullable=false)
*/
private $alias;
/**
* @ORM\Column(name="unit", type="string", nullable=true)
*/
private $unit;
/**
* @ORM\Column(name="annotation", type="text", nullable=true)
*/
private $annotation;
/**
* @ORM\Column(name="description", type="text", nullable=true)
*/
private $description;
/**
* @ORM\Column(type="integer", name="priority", nullable=true)
*/
private $priority = 100;
/**
* @ORM\ManyToMany(targetEntity="MLDev\BaseBundle\Entity\Page", fetch="EXTRA_LAZY")
* @ORM\JoinTable(name="MLDev_Product_Page",
* joinColumns={
* @ORM\JoinColumn(name="product_id", referencedColumnName="id", onDelete="CASCADE")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="page_id", referencedColumnName="id", onDelete="CASCADE")
* }
* )
*/
private $pages;
/**
* @ORM\ManyToMany(targetEntity="MLDev\CatalogBundle\Entity\Product")
* @ORM\JoinTable(name="MLDev_Product_Related",
* joinColumns={
* @ORM\JoinColumn(name="main_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="linked_id", referencedColumnName="id")}
* )
*/
private $related;
/**
* @ORM\ManyToOne(targetEntity="MLDev\CatalogBundle\Entity\Brand", inversedBy="products")
* @ORM\JoinColumn(name="brand_id", referencedColumnName="id", onDelete="SET NULL")
*/
private $brand;
/**
* @ORM\ManyToMany(targetEntity="MLDev\CatalogBundle\Entity\Skin")
* @ORM\JoinTable(name="MLDev_Product_To_Skin",
* joinColumns={
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="skin_id", referencedColumnName="id")}
* )
*/
private $skin;
/**
* @ORM\ManyToMany(targetEntity="MLDev\CatalogBundle\Entity\SkinCondition")
* @ORM\JoinTable(name="MLDev_Product_To_SkinCondition",
* joinColumns={
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="skin_condition_id", referencedColumnName="id")}
* )
*/
private $skinCondition;
/**
* @ORM\ManyToMany(targetEntity="MLDev\CatalogBundle\Entity\Aroma")
* @ORM\JoinTable(name="MLDev_Product_To_Aroma",
* joinColumns={
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="aroma_id", referencedColumnName="id")}
* )
*/
private $aroma;
/**
* @ORM\ManyToMany(targetEntity="MLDev\CatalogBundle\Entity\Effect")
* @ORM\JoinTable(name="MLDev_Product_To_Effect",
* joinColumns={
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="aroma_id", referencedColumnName="id")}
* )
*/
private $effect;
/**
* @ORM\ManyToMany(targetEntity="MLDev\CatalogBundle\Entity\Composition")
* @ORM\JoinTable(name="MLDev_Product_To_Composition",
* joinColumns={
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="composition_id", referencedColumnName="id")}
* )
*/
private $composition;
/**
* @ORM\ManyToMany(targetEntity="MLDev\CatalogBundle\Entity\HairState")
* @ORM\JoinTable(name="MLDev_Product_To_HairState",
* joinColumns={
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="hair_state_id", referencedColumnName="id")}
* )
*/
private $hairState;
/**
* @ORM\ManyToMany(targetEntity="MLDev\CatalogBundle\Entity\HairType")
* @ORM\JoinTable(name="MLDev_Product_To_HairType",
* joinColumns={
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="hair_type_id", referencedColumnName="id")}
* )
*/
private $hairType;
/**
* @ORM\Column(name="is_active", type="boolean", nullable=false)
*/
private $isActive = true;
/**
* @ORM\OneToMany(targetEntity="MLDev\CatalogBundle\Entity\ProductItem", mappedBy="product", cascade={"persist", "remove"}, fetch="EXTRA_LAZY", orphanRemoval=true)
*/
private $items;
/**
* @ORM\OneToMany(targetEntity="MLDev\CatalogBundle\Entity\ProductImage", mappedBy="product", cascade={"persist", "remove"}, fetch="EXTRA_LAZY", orphanRemoval=true)
* @ORM\OrderBy({"priority": "ASC"})
*/
private $images;
/**
* @ORM\OneToMany(targetEntity="MLDev\CatalogBundle\Entity\ProductDocument", mappedBy="product", cascade={"persist", "remove"}, fetch="EXTRA_LAZY", orphanRemoval=true)
*/
private $documents;
/**
* @ORM\ManyToMany(targetEntity=SeoSiteInfoInterface::class, cascade={"persist"})
* @ORM\JoinTable(
* name="MLDev_Product_SeoInfo",
* joinColumns={
* @ORM\JoinColumn(name="product_id", referencedColumnName="id", onDelete="CASCADE")
* },
* inverseJoinColumns={
* @ORM\JoinColumn(name="seo_info_id", referencedColumnName="id", onDelete="CASCADE")
* }
* )
*/
private $seoInfo;
/**
* @ORM\Column(name="extra_fields", type="array", nullable=true)
*/
private $extraFields = [];
/**
* @ORM\Column(name="articles", type="json", nullable=true)
*/
private $articles = [];
/**
* @ORM\OneToMany(targetEntity=ProductContent::class, mappedBy="product", cascade={"persist"}, fetch="EXTRA_LAZY", orphanRemoval=true)
* @ORM\OrderBy({"priority" = "ASC", "id" = "ASC"})
*/
private $productContents;
/**
* @ORM\Column(name="rating_score", type="float", nullable=true)
*/
private $ratingScore;
/**
* @ORM\Column(name="reviews_count", type="integer", nullable=true)
*/
private $reviewsCount;
public function __construct($page = null)
{
$this->pages = new ArrayCollection();
$this->items = new ArrayCollection();
$this->images = new ArrayCollection();
$this->documents = new ArrayCollection();
$this->related = new ArrayCollection();
$this->skin = new ArrayCollection();
$this->skinCondition = new ArrayCollection();
$this->aroma = new ArrayCollection();
$this->effect = new ArrayCollection();
$this->composition = new ArrayCollection();
$this->hairState = new ArrayCollection();
$this->hairType = new ArrayCollection();
$this->seoInfo = new ArrayCollection();
$this->productContents = new ArrayCollection();
if ($page instanceof Page) {
$this->addPage($page);
}
$this->createdAt = new \DateTime();
}
public function getId(): ?int
{
return $this->id;
}
public function getExternalId(): ?string
{
return $this->externalId;
}
public function setExternalId(?string $externalId): void
{
$this->externalId = $externalId;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name)
{
$this->name = $name;
}
public function getAlterName(): ?string
{
return $this->alterName;
}
public function setAlterName(?string $alterName): void
{
$this->alterName = $alterName;
}
public function getTypePrefix()
{
return $this->typePrefix;
}
public function setTypePrefix($typePrefix): self
{
$this->typePrefix = $typePrefix;
return $this;
}
public function getAlias(): ?string
{
return $this->alias;
}
public function setAlias(?string $alias): void
{
$this->alias = $alias;
}
public function getUnit(): ?string
{
return $this->unit;
}
public function setUnit(?string $unit)
{
$this->unit = $unit;
}
public function getAnnotation(): ?string
{
return $this->annotation;
}
public function setAnnotation(?string $annotation)
{
$this->annotation = $annotation;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description)
{
$this->description = $description;
}
public function getPriority(): ?int
{
return $this->priority;
}
public function setPriority(?int $priority): void
{
$this->priority = $priority;
}
public function addPage(Page $page): self
{
$this->pages->add($page);
return $this;
}
public function removePage(Page $page)
{
$this->pages->removeElement($page);
}
public function getPages(): iterable
{
return $this->pages;
}
public function setPages(iterable $pages)
{
$this->pages = $pages;
}
public function addPages(iterable $pages)
{
foreach ($pages as $page) {
if (!$this->pages->contains($page)) {
$this->pages->add($page);
}
}
}
public function removePages(iterable $pages)
{
foreach ($pages as $page) {
$this->pages->removeElement($page);
}
}
public function getBrand(): ?Brand
{
return $this->brand;
}
public function setBrand(?Brand $brand): void
{
$this->brand = $brand;
}
public function getIsActive(): bool
{
return $this->isActive;
}
public function setIsActive(bool $isActive): void
{
$this->isActive = $isActive;
}
public function getItems(): iterable
{
return $this->items;
}
public function addItem(ProductItem $item)
{
$item->setProduct($this);
if (!$this->items->contains($item)) {
$this->items->add($item);
}
}
public function removeItem(ProductItem $item)
{
if ($this->items->contains($item)) {
$this->items->removeElement($item);
}
}
public function addItems(iterable $items)
{
foreach ($items as $item) {
$this->addItem($item);
}
}
public function removeItems(iterable $items)
{
foreach ($items as $item) {
$this->removeItem($item);
}
}
public function addImage(ProductImage $image)
{
$image->setProduct($this);
if (!$this->images->contains($image)) {
$this->images->add($image);
}
}
public function removeImage(ProductImage $image)
{
if ($this->items->contains($image)) {
$this->images->removeElement($image);
}
}
public function removeImages(iterable $images)
{
foreach ($images as $image) {
$this->removeImage($image);
}
}
public function addImages(iterable $images)
{
foreach ($images as $image) {
$this->addImage($image);
}
}
public function getImages(): iterable
{
return $this->images;
}
public function addDocument(ProductDocument $document)
{
$document->setProduct($this);
if (!$this->documents->contains($document)) {
$this->documents->add($document);
}
}
public function removeDocument(ProductDocument $document)
{
if ($this->documents->contains($document)) {
$this->documents->removeElement($document);
}
}
public function removeDocuments(iterable $documents)
{
foreach ($documents as $document) {
$this->removeDocument($document);
}
}
public function addDocuments(iterable $documents)
{
foreach ($documents as $document) {
$this->addDocument($document);
}
}
public function getDocuments(): iterable
{
return $this->documents;
}
public function getRelated(): iterable
{
return $this->related;
}
public function setRelated(iterable $related): void
{
$this->related = $related;
}
public function addRelated($related)
{
if (!$this->related->contains($related)) {
$this->related->add($related);
}
}
public function removeRelated($related): void
{
if ($this->related->contains($related)) {
$this->related->removeElement($related);
}
}
public function getSkin()
{
return $this->skin;
}
public function setSkin($skin): self
{
$this->skin = $skin;
return $this;
}
public function getComposition()
{
return $this->composition;
}
public function setComposition($composition): self
{
$this->composition = $composition;
return $this;
}
public function getSeoInfo(): iterable
{
return $this->seoInfo;
}
public function setSeoInfo(iterable $seoInfo): void
{
$this->seoInfo = $seoInfo;
}
public function getExtraFields(): ?array
{
return $this->extraFields;
}
public function setExtraFields(?array $extraFields): void
{
$this->extraFields = $extraFields;
}
public function getArticles(): ?array
{
return $this->articles;
}
public function setArticles(?array $articles): self
{
$this->articles = $articles;
return $this;
}
/**
* @return ArrayCollection
*/
public function getProductContents(): Collection
{
return $this->productContents;
}
/**
* @param $productContents
* @return $this
*/
public function addProductContents($productContents): self
{
foreach ($productContents as $productContent) {
if (!$this->productContents->contains($productContent)) {
$this->productContents->add($productContent);
}
}
return $this;
}
/**
* @param ProductContent $productContent
* @return Product
*/
public function addProductContent(ProductContent $productContent): self
{
$this->productContents[] = $productContent;
return $this;
}
/**
* @param $productContents
* @return Product
*/
public function removeProductContents($productContents): self
{
foreach ($productContents as $productContent) {
$this->productContents->removeElement($productContent);
}
return $this;
}
/**
* @param ProductContent $productContent
* @return Product
*/
public function removeProductContent(ProductContent $productContent): self
{
$this->productContents->removeElement($productContent);
return $this;
}
/**
* @return ArrayCollection|Collection
*/
public function getWidgets()
{
$criteria = Criteria::create()->where(
Criteria::expr()->eq('isActive', true)
);
$criteria->orderBy([
'priority' => Criteria::ASC,
'id' => Criteria::ASC,
]);
return $this->getProductContents()->matching($criteria);
}
public function getRatingScore()
{
return $this->ratingScore;
}
public function setRatingScore($ratingScore): self
{
$this->ratingScore = $ratingScore;
return $this;
}
public function getReviewsCount()
{
return $this->reviewsCount;
}
public function setReviewsCount($reviewsCount): self
{
$this->reviewsCount = $reviewsCount;
return $this;
}
public function getSkinCondition(): Collection
{
return $this->skinCondition;
}
public function setSkinCondition($skinCondition): self
{
$this->skinCondition = $skinCondition;
return $this;
}
public function getAroma(): Collection
{
return $this->aroma;
}
public function setAroma($aroma): self
{
$this->aroma = $aroma;
return $this;
}
public function getEffect(): Collection
{
return $this->effect;
}
public function setEffect($effect): self
{
$this->effect = $effect;
return $this;
}
public function getHairState(): Collection
{
return $this->hairState;
}
public function setHairState($hairState): self
{
$this->hairState = $hairState;
return $this;
}
public function getHairType(): Collection
{
return $this->hairType;
}
public function setHairType($hairType): self
{
$this->hairType = $hairType;
return $this;
}
}