<?php
namespace App\Entity;
use App\Entity\Traits\CommonTrait;
use App\Utils\SlugGenerator;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Vich\UploaderBundle\Entity\File as EmbeddedFile;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* @Gedmo\Tree(type="nested")
* @ORM\Entity(repositoryClass="App\Repository\ItemCategoryRepository")
* @ORM\Table(name="itm_category", uniqueConstraints={@ORM\UniqueConstraint(columns={"slug", "parent_id"})})
* @ORM\HasLifecycleCallbacks()
* @UniqueEntity({"uuid"})
* @Vich\Uploadable
*/
#[Vich\Uploadable]
class ItemCategory
{
use CommonTrait;
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $slug;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Item", mappedBy="itemCategory")
*/
private $items;
/**
* @Gedmo\TreeLeft
* @ORM\Column(name="lft", type="integer")
*/
private $lft;
/**
* @Gedmo\TreeLevel
* @ORM\Column(name="lvl", type="integer")
*/
private $lvl;
/**
* @Gedmo\TreeRight
* @ORM\Column(name="rgt", type="integer")
*/
private $rgt;
/**
* @Gedmo\TreeRoot
* @ORM\ManyToOne(targetEntity="ItemCategory")
* @ORM\JoinColumn(name="tree_root", referencedColumnName="id", onDelete="CASCADE")
*/
private $root;
/**
* @Gedmo\TreeParent
* @ORM\ManyToOne(targetEntity="ItemCategory", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity="ItemCategory", mappedBy="parent")
* @ORM\OrderBy({"lft" = "ASC"})
*/
private $children;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $expiredDay;
/**
* @ORM\ManyToMany(targetEntity=ItemSpecCategory::class, mappedBy="category", cascade={"all"})
*/
private $itemSpecCategories;
/**
* @ORM\ManyToMany(targetEntity=ItemSpecTypes::class)
* @ORM\JoinTable(name="item_category_spec_required")
*/
private $requiredSpecs;
/**
* @ORM\ManyToMany(targetEntity=ItemSpecTypes::class)
* @ORM\JoinTable(name="item_category_spec_visible_list")
*/
private $specVisibleOnList;
/**
* @ORM\OneToMany(targetEntity=ItemCategoryPromotionPrice::class, mappedBy="itemCategory", orphanRemoval=true, cascade={"all"})
*/
private $itemCategoryPromotionPrices;
/**
* @ORM\ManyToMany(targetEntity=ItemSpecTypes::class)
*/
private $itemTitleSpec;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $hideState;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $hideFv;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $hideDescription;
/**
* @ORM\OneToOne(targetEntity=ItemSpecCategory::class, cascade={"persist", "remove"})
*/
private $priceLocation;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
*
* @var File|null
*/
#[Vich\UploadableField(mapping: 'category_icon', fileNameProperty: 'categoryIconImage.name', size: 'categoryIconImage.size', mimeType: 'categoryIconImage.mimeType', originalName: 'categoryIconImage.originalName', dimensions: 'categoryIconImage.dimensions')]
private $categoryIcon;
/**
* @ORM\Embedded(class="Vich\UploaderBundle\Entity\File")
*
* @var EmbeddedFile
*/
private $categoryIconImage;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
*
* @var File|null
*/
#[Vich\UploadableField(mapping: 'category_bg', fileNameProperty: 'categoryBgImage.name', size: 'categoryBgImage.size', mimeType: 'categoryBgImage.mimeType', originalName: 'categoryBgImage.originalName', dimensions: 'categoryBgImage.dimensions')]
private $categoryBg;
/**
* @ORM\Embedded(class="Vich\UploaderBundle\Entity\File")
*
* @var EmbeddedFile
*/
private $categoryBgImage;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $variantEnable;
/**
* @ORM\Column(type="json", nullable=true)
*/
private $sortTitle;
public function getRoot()
{
return $this->root;
}
public function setParent(ItemCategory $parent = null)
{
$this->parent = $parent;
}
public function getParent()
{
return $this->parent;
}
public function __construct()
{
$this->categoryBgImage = new EmbeddedFile();
$this->categoryIconImage = new EmbeddedFile();
$this->items = new ArrayCollection();
$this->itemSpecTypes = new ArrayCollection();
$this->itemSpecCategories = new ArrayCollection();
$this->requiredSpecs = new ArrayCollection();
$this->specVisibleOnList = new ArrayCollection();
$this->itemCategoryPromotionPrices = new ArrayCollection();
$this->itemTitleSpec = new ArrayCollection();
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getSlug(): ?string
{
return $this->slug;
}
public function setSlug(string $slug): self
{
$this->slug = $slug;
return $this;
}
public function getChildren() {
return $this->children;
}
/**
* @return Collection|Item[]
*/
public function getItems(): Collection
{
return $this->items;
}
public function addItem(Item $item): self
{
if (!$this->items->contains($item)) {
$this->items[] = $item;
$item->setItemCategory($this);
}
return $this;
}
public function removeItem(Item $item): self
{
if ($this->items->contains($item)) {
$this->items->removeElement($item);
// set the owning side to null (unless already changed)
if ($item->getItemCategory() === $this) {
$item->setItemCategory(null);
}
}
return $this;
}
public function __toString()
{
return $this->name;
}
/**
* @ORM\PrePersist()
*/
public function slugGen()
{
if(empty($this->slug))
{
$slugify = new SlugGenerator();
$this->slug = $slugify->slugifyString($this->name);
}
}
/**
* @ORM\PreUpdate()
*/
public function slugReGen()
{
$slugify = new SlugGenerator();
$this->slug = $slugify->slugifyString($this->name);
}
/**
* @return mixed
*/
public function getLft()
{
return $this->lft;
}
/**
* @param mixed $lft
*/
public function setLft($lft): void
{
$this->lft = $lft;
}
/**
* @return mixed
*/
public function getLvl()
{
return $this->lvl;
}
/**
* @param mixed $lvl
*/
public function setLvl($lvl): void
{
$this->lvl = $lvl;
}
/**
* @return mixed
*/
public function getRgt()
{
return $this->rgt;
}
/**
* @param mixed $rgt
*/
public function setRgt($rgt): void
{
$this->rgt = $rgt;
}
public function getExpiredDay(): ?int
{
return $this->expiredDay;
}
public function setExpiredDay(?int $expiredDay): self
{
$this->expiredDay = $expiredDay;
return $this;
}
/**
* @return Collection|ItemSpecCategory[]
*/
public function getItemSpecCategories(): Collection
{
return $this->itemSpecCategories;
}
public function addItemSpecCategory(ItemSpecCategory $itemSpecCategory): self
{
if (!$this->itemSpecCategories->contains($itemSpecCategory)) {
$this->itemSpecCategories[] = $itemSpecCategory;
$itemSpecCategory->addCategory($this);
}
return $this;
}
public function removeItemSpecCategory(ItemSpecCategory $itemSpecCategory): self
{
if ($this->itemSpecCategories->removeElement($itemSpecCategory)) {
$itemSpecCategory->removeCategory($this);
}
return $this;
}
/**
* @return Collection|ItemSpec[]
*/
public function getRequiredSpecs(): Collection
{
return $this->requiredSpecs;
}
public function addRequiredSpec(ItemSpecTypes $requiredSpec): self
{
if (!$this->requiredSpecs->contains($requiredSpec)) {
$this->requiredSpecs[] = $requiredSpec;
}
return $this;
}
public function removeRequiredSpec(ItemSpecTypes $requiredSpec): self
{
$this->requiredSpecs->removeElement($requiredSpec);
return $this;
}
/**
* @return Collection|ItemSpecTypes[]
*/
public function getSpecVisibleOnList(): Collection
{
return $this->specVisibleOnList;
}
public function addSpecVisibleOnList(ItemSpecTypes $specVisibleOnList): self
{
if (!$this->specVisibleOnList->contains($specVisibleOnList)) {
$this->specVisibleOnList[] = $specVisibleOnList;
}
return $this;
}
public function removeSpecVisibleOnList(ItemSpecTypes $specVisibleOnList): self
{
$this->specVisibleOnList->removeElement($specVisibleOnList);
return $this;
}
/**
* @return Collection|ItemCategoryPromotionPrice[]
*/
public function getItemCategoryPromotionPrices(): Collection
{
return $this->itemCategoryPromotionPrices;
}
public function addItemCategoryPromotionPrice(ItemCategoryPromotionPrice $itemCategoryPromotionPrice): self
{
if (!$this->itemCategoryPromotionPrices->contains($itemCategoryPromotionPrice)) {
$this->itemCategoryPromotionPrices[] = $itemCategoryPromotionPrice;
$itemCategoryPromotionPrice->setItemCategory($this);
}
return $this;
}
public function removeItemCategoryPromotionPrice(ItemCategoryPromotionPrice $itemCategoryPromotionPrice): self
{
if ($this->itemCategoryPromotionPrices->removeElement($itemCategoryPromotionPrice)) {
// set the owning side to null (unless already changed)
if ($itemCategoryPromotionPrice->getItemCategory() === $this) {
$itemCategoryPromotionPrice->setItemCategory(null);
}
}
return $this;
}
/**
* @return Collection|ItemSpecTypes[]
*/
public function getItemTitleSpec(): Collection
{
$v = [];
$vids = [];
$sort = $this->getSortTitle();
if($sort && is_countable($sort) && count($sort) > 1) {
foreach ($sort as $sortId) {
foreach ($this->itemTitleSpec as $spec) {
if($spec->getId() === $sortId) {
$v[] = $spec;
$vids[] = $spec->getId();
}
}
}
foreach ($this->itemTitleSpec as $spec) {
if(!in_array($spec->getId(), $vids)) {
$v[] = $spec;
}
}
return new ArrayCollection($v);
}
return $this->itemTitleSpec;
}
public function addItemTitleSpec(ItemSpecTypes $itemTitleSpec): self
{
if (!$this->itemTitleSpec->contains($itemTitleSpec)) {
$this->itemTitleSpec[] = $itemTitleSpec;
}
return $this;
}
public function removeItemTitleSpec(ItemSpecTypes $itemTitleSpec): self
{
$this->itemTitleSpec->removeElement($itemTitleSpec);
return $this;
}
public function getHideState(): ?bool
{
return $this->hideState;
}
public function setHideState(?bool $hideState): self
{
$this->hideState = $hideState;
return $this;
}
public function getHideFv(): ?bool
{
return $this->hideFv;
}
public function setHideFv(?bool $hideFv): self
{
$this->hideFv = $hideFv;
return $this;
}
public function getHideDescription(): ?bool
{
return $this->hideDescription;
}
public function setHideDescription(?bool $hideDescription): self
{
$this->hideDescription = $hideDescription;
return $this;
}
public function getPriceLocation(): ?ItemSpecCategory
{
return $this->priceLocation;
}
public function setPriceLocation(?ItemSpecCategory $priceLocation): self
{
$this->priceLocation = $priceLocation;
return $this;
}
public function setCategoryBg(?File $categoryBg = null)
{
$this->categoryBg = $categoryBg;
if (null !== $categoryBg) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTimeImmutable();
}
}
public function getCategoryBg(): ?File
{
return $this->categoryBg;
}
public function setCategoryBgImage(EmbeddedFile $image): void
{
$this->categoryBgImage = $image;
}
public function getCategoryBgImage(): ?EmbeddedFile
{
return $this->categoryBgImage;
}
public function setCategoryIcon(?File $categoryIcon = null)
{
$this->categoryIcon = $categoryIcon;
if (null !== $categoryIcon) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTimeImmutable();
}
}
public function getCategoryIcon(): ?File
{
return $this->categoryIcon;
}
public function setCategoryIconImage(EmbeddedFile $image): void
{
$this->categoryIconImage = $image;
}
public function getCategoryIconImage(): ?EmbeddedFile
{
return $this->categoryIconImage;
}
/**
* @return mixed
*/
public function getVariantEnable()
{
return $this->variantEnable;
}
/**
* @param mixed $variantEnable
*/
public function setVariantEnable($variantEnable): void
{
$this->variantEnable = $variantEnable;
}
public function setSortTitle($sortTitle)
{
$this->sortTitle = $sortTitle;
return $this;
}
public function getSortTitle()
{
if(!is_string($this->sortTitle)) {
return $this->sortTitle;
}
return json_decode($this->sortTitle, true);
}
}