src/Entity/ItemCategory.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Entity\Traits\CommonTrait;
  4. use App\Utils\SlugGenerator;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\ORM\Mapping as ORM;
  8. use Gedmo\Mapping\Annotation as Gedmo;
  9. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  10. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  11. use Vich\UploaderBundle\Entity\File as EmbeddedFile;
  12. use Symfony\Component\HttpFoundation\File\File;
  13. use Symfony\Component\HttpFoundation\File\UploadedFile;
  14. /**
  15.  * @Gedmo\Tree(type="nested")
  16.  * @ORM\Entity(repositoryClass="App\Repository\ItemCategoryRepository")
  17.  * @ORM\Table(name="itm_category", uniqueConstraints={@ORM\UniqueConstraint(columns={"slug", "parent_id"})})
  18.  * @ORM\HasLifecycleCallbacks()
  19.  * @UniqueEntity({"uuid"})
  20.  * @Vich\Uploadable
  21.  */
  22. #[Vich\Uploadable]
  23. class ItemCategory
  24. {
  25.     use CommonTrait;
  26.     /**
  27.      * @ORM\Id()
  28.      * @ORM\GeneratedValue()
  29.      * @ORM\Column(type="integer")
  30.      */
  31.     private $id;
  32.     /**
  33.      * @ORM\Column(type="string", length=255)
  34.      */
  35.     private $name;
  36.     /**
  37.      * @ORM\Column(type="string", length=255, nullable=true)
  38.      */
  39.     private $slug;
  40.     /**
  41.      * @ORM\OneToMany(targetEntity="App\Entity\Item", mappedBy="itemCategory")
  42.      */
  43.     private $items;
  44.     /**
  45.      * @Gedmo\TreeLeft
  46.      * @ORM\Column(name="lft", type="integer")
  47.      */
  48.     private $lft;
  49.     /**
  50.      * @Gedmo\TreeLevel
  51.      * @ORM\Column(name="lvl", type="integer")
  52.      */
  53.     private $lvl;
  54.     /**
  55.      * @Gedmo\TreeRight
  56.      * @ORM\Column(name="rgt", type="integer")
  57.      */
  58.     private $rgt;
  59.     /**
  60.      * @Gedmo\TreeRoot
  61.      * @ORM\ManyToOne(targetEntity="ItemCategory")
  62.      * @ORM\JoinColumn(name="tree_root", referencedColumnName="id", onDelete="CASCADE")
  63.      */
  64.     private $root;
  65.     /**
  66.      * @Gedmo\TreeParent
  67.      * @ORM\ManyToOne(targetEntity="ItemCategory", inversedBy="children")
  68.      * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
  69.      */
  70.     private $parent;
  71.     /**
  72.      * @ORM\OneToMany(targetEntity="ItemCategory", mappedBy="parent")
  73.      * @ORM\OrderBy({"lft" = "ASC"})
  74.      */
  75.     private $children;
  76.     /**
  77.      * @ORM\Column(type="integer", nullable=true)
  78.      */
  79.     private $expiredDay;
  80.     /**
  81.      * @ORM\ManyToMany(targetEntity=ItemSpecCategory::class, mappedBy="category", cascade={"all"})
  82.      */
  83.     private $itemSpecCategories;
  84.     /**
  85.      * @ORM\ManyToMany(targetEntity=ItemSpecTypes::class)
  86.      * @ORM\JoinTable(name="item_category_spec_required")
  87.      */
  88.     private $requiredSpecs;
  89.     /**
  90.      * @ORM\ManyToMany(targetEntity=ItemSpecTypes::class)
  91.      * @ORM\JoinTable(name="item_category_spec_visible_list")
  92.      */
  93.     private $specVisibleOnList;
  94.     /**
  95.      * @ORM\OneToMany(targetEntity=ItemCategoryPromotionPrice::class, mappedBy="itemCategory", orphanRemoval=true, cascade={"all"})
  96.      */
  97.     private $itemCategoryPromotionPrices;
  98.     /**
  99.      * @ORM\ManyToMany(targetEntity=ItemSpecTypes::class)
  100.      */
  101.     private $itemTitleSpec;
  102.     /**
  103.      * @ORM\Column(type="boolean", nullable=true)
  104.      */
  105.     private $hideState;
  106.     /**
  107.      * @ORM\Column(type="boolean", nullable=true)
  108.      */
  109.     private $hideFv;
  110.     /**
  111.      * @ORM\Column(type="boolean", nullable=true)
  112.      */
  113.     private $hideDescription;
  114.     /**
  115.      * @ORM\OneToOne(targetEntity=ItemSpecCategory::class, cascade={"persist", "remove"})
  116.      */
  117.     private $priceLocation;
  118.     /**
  119.      * NOTE: This is not a mapped field of entity metadata, just a simple property.
  120.      *
  121.      *
  122.      * @var File|null
  123.      */
  124.     #[Vich\UploadableField(mapping'category_icon'fileNameProperty'categoryIconImage.name'size'categoryIconImage.size'mimeType'categoryIconImage.mimeType'originalName'categoryIconImage.originalName'dimensions'categoryIconImage.dimensions')]
  125.     private $categoryIcon;
  126.     /**
  127.      * @ORM\Embedded(class="Vich\UploaderBundle\Entity\File")
  128.      *
  129.      * @var EmbeddedFile
  130.      */
  131.     private $categoryIconImage;
  132.     /**
  133.      * NOTE: This is not a mapped field of entity metadata, just a simple property.
  134.      *
  135.      *
  136.      * @var File|null
  137.      */
  138.     #[Vich\UploadableField(mapping'category_bg'fileNameProperty'categoryBgImage.name'size'categoryBgImage.size'mimeType'categoryBgImage.mimeType'originalName'categoryBgImage.originalName'dimensions'categoryBgImage.dimensions')]
  139.     private $categoryBg;
  140.     /**
  141.      * @ORM\Embedded(class="Vich\UploaderBundle\Entity\File")
  142.      *
  143.      * @var EmbeddedFile
  144.      */
  145.     private $categoryBgImage;
  146.     /**
  147.      * @ORM\Column(type="boolean", nullable=true)
  148.      */
  149.     private $variantEnable;
  150.     /**
  151.      * @ORM\Column(type="json", nullable=true)
  152.      */
  153.     private $sortTitle;
  154.     public function getRoot()
  155.     {
  156.         return $this->root;
  157.     }
  158.     public function setParent(ItemCategory $parent null)
  159.     {
  160.         $this->parent $parent;
  161.     }
  162.     public function getParent()
  163.     {
  164.         return $this->parent;
  165.     }
  166.     public function __construct()
  167.     {
  168.         $this->categoryBgImage = new EmbeddedFile();
  169.         $this->categoryIconImage = new EmbeddedFile();
  170.         $this->items = new ArrayCollection();
  171.         $this->itemSpecTypes = new ArrayCollection();
  172.         $this->itemSpecCategories = new ArrayCollection();
  173.         $this->requiredSpecs = new ArrayCollection();
  174.         $this->specVisibleOnList = new ArrayCollection();
  175.         $this->itemCategoryPromotionPrices = new ArrayCollection();
  176.         $this->itemTitleSpec = new ArrayCollection();
  177.     }
  178.     public function getName(): ?string
  179.     {
  180.         return $this->name;
  181.     }
  182.     public function setName(string $name): self
  183.     {
  184.         $this->name $name;
  185.         return $this;
  186.     }
  187.     public function getSlug(): ?string
  188.     {
  189.         return $this->slug;
  190.     }
  191.     public function setSlug(string $slug): self
  192.     {
  193.         $this->slug $slug;
  194.         return $this;
  195.     }
  196.     public function getChildren() {
  197.         return $this->children;
  198.     }
  199.     /**
  200.      * @return Collection|Item[]
  201.      */
  202.     public function getItems(): Collection
  203.     {
  204.         return $this->items;
  205.     }
  206.     public function addItem(Item $item): self
  207.     {
  208.         if (!$this->items->contains($item)) {
  209.             $this->items[] = $item;
  210.             $item->setItemCategory($this);
  211.         }
  212.         return $this;
  213.     }
  214.     public function removeItem(Item $item): self
  215.     {
  216.         if ($this->items->contains($item)) {
  217.             $this->items->removeElement($item);
  218.             // set the owning side to null (unless already changed)
  219.             if ($item->getItemCategory() === $this) {
  220.                 $item->setItemCategory(null);
  221.             }
  222.         }
  223.         return $this;
  224.     }
  225.     public function __toString()
  226.     {
  227.         return $this->name;
  228.     }
  229.     /**
  230.      * @ORM\PrePersist()
  231.      */
  232.     public function slugGen()
  233.     {
  234.         if(empty($this->slug))
  235.         {
  236.             $slugify = new SlugGenerator();
  237.             $this->slug $slugify->slugifyString($this->name);
  238.         }
  239.     }
  240.     /**
  241.      * @ORM\PreUpdate()
  242.      */
  243.     public function slugReGen()
  244.     {
  245.         $slugify = new SlugGenerator();
  246.         $this->slug $slugify->slugifyString($this->name);
  247.     }
  248.     /**
  249.      * @return mixed
  250.      */
  251.     public function getLft()
  252.     {
  253.         return $this->lft;
  254.     }
  255.     /**
  256.      * @param mixed $lft
  257.      */
  258.     public function setLft($lft): void
  259.     {
  260.         $this->lft $lft;
  261.     }
  262.     /**
  263.      * @return mixed
  264.      */
  265.     public function getLvl()
  266.     {
  267.         return $this->lvl;
  268.     }
  269.     /**
  270.      * @param mixed $lvl
  271.      */
  272.     public function setLvl($lvl): void
  273.     {
  274.         $this->lvl $lvl;
  275.     }
  276.     /**
  277.      * @return mixed
  278.      */
  279.     public function getRgt()
  280.     {
  281.         return $this->rgt;
  282.     }
  283.     /**
  284.      * @param mixed $rgt
  285.      */
  286.     public function setRgt($rgt): void
  287.     {
  288.         $this->rgt $rgt;
  289.     }
  290.     public function getExpiredDay(): ?int
  291.     {
  292.         return $this->expiredDay;
  293.     }
  294.     public function setExpiredDay(?int $expiredDay): self
  295.     {
  296.         $this->expiredDay $expiredDay;
  297.         return $this;
  298.     }
  299.     /**
  300.      * @return Collection|ItemSpecCategory[]
  301.      */
  302.     public function getItemSpecCategories(): Collection
  303.     {
  304.         return $this->itemSpecCategories;
  305.     }
  306.     public function addItemSpecCategory(ItemSpecCategory $itemSpecCategory): self
  307.     {
  308.         if (!$this->itemSpecCategories->contains($itemSpecCategory)) {
  309.             $this->itemSpecCategories[] = $itemSpecCategory;
  310.             $itemSpecCategory->addCategory($this);
  311.         }
  312.         return $this;
  313.     }
  314.     public function removeItemSpecCategory(ItemSpecCategory $itemSpecCategory): self
  315.     {
  316.         if ($this->itemSpecCategories->removeElement($itemSpecCategory)) {
  317.             $itemSpecCategory->removeCategory($this);
  318.         }
  319.         return $this;
  320.     }
  321.     /**
  322.      * @return Collection|ItemSpec[]
  323.      */
  324.     public function getRequiredSpecs(): Collection
  325.     {
  326.         return $this->requiredSpecs;
  327.     }
  328.     public function addRequiredSpec(ItemSpecTypes $requiredSpec): self
  329.     {
  330.         if (!$this->requiredSpecs->contains($requiredSpec)) {
  331.             $this->requiredSpecs[] = $requiredSpec;
  332.         }
  333.         return $this;
  334.     }
  335.     public function removeRequiredSpec(ItemSpecTypes $requiredSpec): self
  336.     {
  337.         $this->requiredSpecs->removeElement($requiredSpec);
  338.         return $this;
  339.     }
  340.     /**
  341.      * @return Collection|ItemSpecTypes[]
  342.      */
  343.     public function getSpecVisibleOnList(): Collection
  344.     {
  345.         return $this->specVisibleOnList;
  346.     }
  347.     public function addSpecVisibleOnList(ItemSpecTypes $specVisibleOnList): self
  348.     {
  349.         if (!$this->specVisibleOnList->contains($specVisibleOnList)) {
  350.             $this->specVisibleOnList[] = $specVisibleOnList;
  351.         }
  352.         return $this;
  353.     }
  354.     public function removeSpecVisibleOnList(ItemSpecTypes $specVisibleOnList): self
  355.     {
  356.         $this->specVisibleOnList->removeElement($specVisibleOnList);
  357.         return $this;
  358.     }
  359.     /**
  360.      * @return Collection|ItemCategoryPromotionPrice[]
  361.      */
  362.     public function getItemCategoryPromotionPrices(): Collection
  363.     {
  364.         return $this->itemCategoryPromotionPrices;
  365.     }
  366.     public function addItemCategoryPromotionPrice(ItemCategoryPromotionPrice $itemCategoryPromotionPrice): self
  367.     {
  368.         if (!$this->itemCategoryPromotionPrices->contains($itemCategoryPromotionPrice)) {
  369.             $this->itemCategoryPromotionPrices[] = $itemCategoryPromotionPrice;
  370.             $itemCategoryPromotionPrice->setItemCategory($this);
  371.         }
  372.         return $this;
  373.     }
  374.     public function removeItemCategoryPromotionPrice(ItemCategoryPromotionPrice $itemCategoryPromotionPrice): self
  375.     {
  376.         if ($this->itemCategoryPromotionPrices->removeElement($itemCategoryPromotionPrice)) {
  377.             // set the owning side to null (unless already changed)
  378.             if ($itemCategoryPromotionPrice->getItemCategory() === $this) {
  379.                 $itemCategoryPromotionPrice->setItemCategory(null);
  380.             }
  381.         }
  382.         return $this;
  383.     }
  384.     /**
  385.      * @return Collection|ItemSpecTypes[]
  386.      */
  387.     public function getItemTitleSpec(): Collection
  388.     {
  389.         $v = [];
  390.         $vids = [];
  391.         $sort $this->getSortTitle();
  392.         if($sort && is_countable($sort) && count($sort) > 1) {
  393.             foreach ($sort as $sortId) {
  394.                 foreach ($this->itemTitleSpec as $spec) {
  395.                     if($spec->getId() === $sortId) {
  396.                         $v[] = $spec;
  397.                         $vids[] = $spec->getId();
  398.                     }
  399.                 }
  400.             }
  401.             foreach ($this->itemTitleSpec as $spec) {
  402.                 if(!in_array($spec->getId(), $vids)) {
  403.                     $v[] = $spec;
  404.                 }
  405.             }
  406.             return new ArrayCollection($v);
  407.         }
  408.         return $this->itemTitleSpec;
  409.     }
  410.     public function addItemTitleSpec(ItemSpecTypes $itemTitleSpec): self
  411.     {
  412.         if (!$this->itemTitleSpec->contains($itemTitleSpec)) {
  413.             $this->itemTitleSpec[] = $itemTitleSpec;
  414.         }
  415.         return $this;
  416.     }
  417.     public function removeItemTitleSpec(ItemSpecTypes $itemTitleSpec): self
  418.     {
  419.         $this->itemTitleSpec->removeElement($itemTitleSpec);
  420.         return $this;
  421.     }
  422.     public function getHideState(): ?bool
  423.     {
  424.         return $this->hideState;
  425.     }
  426.     public function setHideState(?bool $hideState): self
  427.     {
  428.         $this->hideState $hideState;
  429.         return $this;
  430.     }
  431.     public function getHideFv(): ?bool
  432.     {
  433.         return $this->hideFv;
  434.     }
  435.     public function setHideFv(?bool $hideFv): self
  436.     {
  437.         $this->hideFv $hideFv;
  438.         return $this;
  439.     }
  440.     public function getHideDescription(): ?bool
  441.     {
  442.         return $this->hideDescription;
  443.     }
  444.     public function setHideDescription(?bool $hideDescription): self
  445.     {
  446.         $this->hideDescription $hideDescription;
  447.         return $this;
  448.     }
  449.     public function getPriceLocation(): ?ItemSpecCategory
  450.     {
  451.         return $this->priceLocation;
  452.     }
  453.     public function setPriceLocation(?ItemSpecCategory $priceLocation): self
  454.     {
  455.         $this->priceLocation $priceLocation;
  456.         return $this;
  457.     }
  458.     public function setCategoryBg(?File $categoryBg null)
  459.     {
  460.         $this->categoryBg $categoryBg;
  461.         if (null !== $categoryBg) {
  462.             // It is required that at least one field changes if you are using doctrine
  463.             // otherwise the event listeners won't be called and the file is lost
  464.             $this->updatedAt = new \DateTimeImmutable();
  465.         }
  466.     }
  467.     public function getCategoryBg(): ?File
  468.     {
  469.         return $this->categoryBg;
  470.     }
  471.     public function setCategoryBgImage(EmbeddedFile $image): void
  472.     {
  473.         $this->categoryBgImage $image;
  474.     }
  475.     public function getCategoryBgImage(): ?EmbeddedFile
  476.     {
  477.         return $this->categoryBgImage;
  478.     }
  479.     public function setCategoryIcon(?File $categoryIcon null)
  480.     {
  481.         $this->categoryIcon $categoryIcon;
  482.         if (null !== $categoryIcon) {
  483.             // It is required that at least one field changes if you are using doctrine
  484.             // otherwise the event listeners won't be called and the file is lost
  485.             $this->updatedAt = new \DateTimeImmutable();
  486.         }
  487.     }
  488.     public function getCategoryIcon(): ?File
  489.     {
  490.         return $this->categoryIcon;
  491.     }
  492.     public function setCategoryIconImage(EmbeddedFile $image): void
  493.     {
  494.         $this->categoryIconImage $image;
  495.     }
  496.     public function getCategoryIconImage(): ?EmbeddedFile
  497.     {
  498.         return $this->categoryIconImage;
  499.     }
  500.     /**
  501.      * @return mixed
  502.      */
  503.     public function getVariantEnable()
  504.     {
  505.         return $this->variantEnable;
  506.     }
  507.     /**
  508.      * @param mixed $variantEnable
  509.      */
  510.     public function setVariantEnable($variantEnable): void
  511.     {
  512.         $this->variantEnable $variantEnable;
  513.     }
  514.     public function setSortTitle($sortTitle)
  515.     {
  516.         $this->sortTitle $sortTitle;
  517.         return $this;
  518.     }
  519.     public function getSortTitle()
  520.     {
  521.         if(!is_string($this->sortTitle)) {
  522.             return $this->sortTitle;
  523.         }
  524.         return json_decode($this->sortTitletrue);
  525.     }
  526. }