src/Flexy/ShopBundle/Entity/Product/CategoryProduct.php line 34

  1. <?php
  2. namespace App\Flexy\ShopBundle\Entity\Product;
  3. use ApiPlatform\Core\Annotation\ApiResource;
  4. use App\Flexy\ShopBundle\Entity\Product\Product;
  5. use App\Flexy\ShopBundle\Entity\Promotion\Promotion;
  6. use App\Flexy\ShopBundle\Repository\Product\CategoryProductRepository;
  7. use App\Flexy\ShopBundle\Service\FlexyShopStatisticProvider;
  8. use Doctrine\Common\Collections\ArrayCollection;
  9. use Doctrine\Common\Collections\Collection;
  10. use Doctrine\ORM\Mapping as ORM;
  11. use Doctrine\ORM\Mapping\Table;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use Symfony\Component\Serializer\Annotation\Groups;
  14. use ApiPlatform\Core\Annotation\ApiFilter;
  15. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  16. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
  17. use Gedmo\Translatable\Translatable;
  18. use Gedmo\Mapping\Annotation as Gedmo;
  19. use Doctrine\Common\Collections\Criteria;
  20. #[ApiResource(
  21.     normalizationContext: ['groups' => ['read','readDeep']],
  22.     denormalizationContext: ['groups' => ['write']],
  23.     
  24.     )]
  25. #[ApiFilter(SearchFilter::class, properties: ['parentCategory'])]
  26. #[ApiFilter(ExistsFilter::class, properties: ['parentCategory'])]
  27. #[Table(name'flexy_categoryproduct')]
  28. #[ORM\Entity(repositoryClassCategoryProductRepository::class)]
  29. #[ORM\Cache(usage:"NONSTRICT_READ_WRITE",region:"append_depend")]
  30. #[Gedmo\SoftDeleteable(fieldName'deletedAt'timeAwarefalsehardDeletetrue)]
  31. class CategoryProduct implements \Stringable,Translatable
  32. {
  33.     #[Groups("read")]
  34.     #[ORM\Id]
  35.     #[ORM\GeneratedValue]
  36.     #[ORM\Column(type'integer')]
  37.     private $id;
  38.     #[Groups("read")]
  39.     #[ORM\Column(type'string'length255)]
  40.     #[Gedmo\Translatable]
  41.     private ?string $name null;
  42.     #[Groups("read")]
  43.     #[ORM\Column(type'text'nullabletrue)]
  44.     private ?string $description null;
  45.     #[Groups("read")]
  46.     #[ORM\Column(type'string'length255nullabletrue)]
  47.     private ?string $image null;
  48.     #[Groups("read")]
  49.     #[ORM\ManyToMany(targetEntityProduct::class, mappedBy'categoriesProduct'cascade: ['persist','remove'])]
  50.     private \Doctrine\Common\Collections\Collection|array $products;
  51.     #[Groups("readDeep")]
  52.     #[ORM\ManyToOne(targetEntityCategoryProduct::class, inversedBy'subCategories')]
  53.     private ?\App\Flexy\ShopBundle\Entity\Product\CategoryProduct $parentCategory null;
  54.     #[Groups("readDeep")]
  55.     #[ORM\OneToMany(targetEntityCategoryProduct::class, mappedBy'parentCategory')]
  56.     private $subCategories;
  57.     #[ORM\ManyToMany(targetEntityAttribut::class, inversedBy'categoriesProduct')]
  58.     private \Doctrine\Common\Collections\Collection|array $attributes;
  59.     /**
  60.      *   #ignoredForAi
  61.      */
  62.     #[ORM\Column(type'float'nullabletrue)]
  63.     private ?float $commission null;
  64.     #[ORM\OneToMany(targetEntityProduct::class, mappedBy'parentCategory')]
  65.     private \Doctrine\Common\Collections\Collection|array $productsChildrens;
  66.      /**
  67.      *   
  68.      * @example [simple|variable|pack|subscription] default: simple
  69.      */
  70.     #[ORM\Column(type'string'length255nullabletrue)]
  71.     private ?string $forProductType null;
  72.     /**
  73.      *   #ignoredForAi
  74.      */
  75.     #[ORM\Column(type'integer'nullabletrue)]
  76.     private ?int $oldId null;
  77.     #[Groups("read")]
  78.     #[ORM\Column(length255nullabletrue)]
  79.     private ?string $icon null;
  80.     #[ORM\Column(nullabletrue)]
  81.     private ?\DateTimeImmutable $deletedAt null;
  82.         /**
  83.      * @Gedmo\Slug(fields={"title"})
  84.      */
  85.     #[Gedmo\Slug(fields: ['name'],updatable:false)]
  86.     #[ORM\Column(type'string'length255nullabletrue)]
  87.     #[Groups(['read','write'])]
  88.     private ?string $slug null;
  89.     #[ORM\ManyToOne(inversedBy'categories')]
  90.     private ?Promotion $promotion null;
  91.     #[ORM\Column(type'integer'nullabletrue)]
  92.     private $position;
  93.    
  94.     public function __construct(
  95.         
  96.     )
  97.     {
  98.         
  99.         $this->products = new ArrayCollection();
  100.         $this->attributes = new ArrayCollection();
  101.         $this->subCategories = new ArrayCollection();
  102.         $this->productsChildrens = new ArrayCollection();
  103.         
  104.     }
  105.     public function __toString(): string
  106.     {
  107.         $name $this->name;
  108.         if($this->getParentCategory()){
  109.             $name $this->getParentCategory()->getName()." > ".$this->name;
  110.         }
  111.         return (string) $name;
  112.     }
  113.     public function getId(): ?int
  114.     {
  115.         return $this->id;
  116.     }
  117.     public function getName(): ?string
  118.     {
  119.         return $this->name;
  120.     }
  121.     public function setName(string $name): self
  122.     {
  123.         $this->name $name;
  124.         return $this;
  125.     }
  126.     public function getDescription(): ?string
  127.     {
  128.         return $this->description;
  129.     }
  130.     public function setDescription(?string $description): self
  131.     {
  132.         $this->description $description;
  133.         return $this;
  134.     }
  135.     public function getImage(): ?string
  136.     {
  137.         return $this->image;
  138.     }
  139.     public function setImage(?string $image): self
  140.     {
  141.         $this->image $image;
  142.         return $this;
  143.     }
  144.     public function getProducts()
  145.     {
  146.         $data = [];
  147.         foreach( $this->products as $singleProduct ){
  148.             if($singleProduct->getIsPublished()){
  149.                 $data[] = $singleProduct;
  150.             }
  151.             
  152.         }
  153.         
  154.         // Custom sorting by position
  155.         usort($data, function($a$b) {
  156.             return $a->getPosition() <=> $b->getPosition();
  157.         });
  158.         
  159.         
  160.      
  161.        return $data;
  162.     }
  163.     
  164.     public function addProduct(Product $product): self
  165.     {
  166.         if (!$this->products->contains($product)) {
  167.             $this->products[] = $product;
  168.         }
  169.         return $this;
  170.     }
  171.     public function removeProduct(Product $product): self
  172.     {
  173.         $this->products->removeElement($product);
  174.         return $this;
  175.     }
  176.     public function getParentCategory(): ?self
  177.     {
  178.         return $this->parentCategory;
  179.     }
  180.     public function setParentCategory(?self $parentCategory): self
  181.     {
  182.         $this->parentCategory $parentCategory;
  183.         return $this;
  184.     }
  185.     /**
  186.      * Get the value of subCategories
  187.      */ 
  188.     public function getSubCategories()
  189.     {
  190.         $data $this->subCategories->toArray();
  191.         // Custom sorting by position
  192.         usort($data, function($a$b) {
  193.             return $a->getPosition() <=> $b->getPosition();
  194.         });
  195.         return $data;
  196.     }
  197.     /**
  198.      * Set the value of subCategories
  199.      *
  200.      * @return  self
  201.      */ 
  202.     public function setSubCategories($subCategories)
  203.     {
  204.         $this->subCategories $subCategories;
  205.         return $this;
  206.     }
  207.     /**
  208.      * @return Collection|Attribut[]
  209.      */
  210.     public function getAttributes(): Collection
  211.     {
  212.         return $this->attributes;
  213.     }
  214.     public function addAttribute(Attribut $attribute): self
  215.     {
  216.         if (!$this->attributes->contains($attribute)) {
  217.             $this->attributes[] = $attribute;
  218.         }
  219.         return $this;
  220.     }
  221.     public function removeAttribute(Attribut $attribute): self
  222.     {
  223.         $this->attributes->removeElement($attribute);
  224.         return $this;
  225.     }
  226.     public function getCommission(): ?float
  227.     {
  228.         return $this->commission;
  229.     }
  230.     public function setCommission(?float $commission): self
  231.     {
  232.         $this->commission $commission;
  233.         return $this;
  234.     }
  235.     public function addSubCategory(CategoryProduct $subCategory): self
  236.     {
  237.         if (!$this->subCategories->contains($subCategory)) {
  238.             $this->subCategories[] = $subCategory;
  239.             $subCategory->setParentCategory($this);
  240.         }
  241.         return $this;
  242.     }
  243.     public function removeSubCategory(CategoryProduct $subCategory): self
  244.     {
  245.         if ($this->subCategories->removeElement($subCategory)) {
  246.             // set the owning side to null (unless already changed)
  247.             if ($subCategory->getParentCategory() === $this) {
  248.                 $subCategory->setParentCategory(null);
  249.             }
  250.         }
  251.         return $this;
  252.     }
  253.     /**
  254.      * @return Collection<int, Product>
  255.      */
  256.     public function getProductsChildrens(): Collection
  257.     {
  258.         return $this->productsChildrens;
  259.     }
  260.     public function addProductsChildren(Product $productsChildren): self
  261.     {
  262.         if (!$this->productsChildrens->contains($productsChildren)) {
  263.             $this->productsChildrens[] = $productsChildren;
  264.             $productsChildren->setParentCategory($this);
  265.         }
  266.         return $this;
  267.     }
  268.     public function removeProductsChildren(Product $productsChildren): self
  269.     {
  270.         if ($this->productsChildrens->removeElement($productsChildren)) {
  271.             // set the owning side to null (unless already changed)
  272.             if ($productsChildren->getParentCategory() === $this) {
  273.                 $productsChildren->setParentCategory(null);
  274.             }
  275.         }
  276.         return $this;
  277.     }
  278.     public function getForProductType(): ?string
  279.     {
  280.         return $this->forProductType;
  281.     }
  282.     public function setForProductType(?string $forProductType): self
  283.     {
  284.         $this->forProductType $forProductType;
  285.         return $this;
  286.     }
  287.     public function getOldId(): ?int
  288.     {
  289.         return $this->oldId;
  290.     }
  291.     public function setOldId(?int $oldId): self
  292.     {
  293.         $this->oldId $oldId;
  294.         return $this;
  295.     }
  296.     public function getIcon(): ?string
  297.     {
  298.         return $this->icon;
  299.     }
  300.     public function setIcon(?string $icon): static
  301.     {
  302.         $this->icon $icon;
  303.         return $this;
  304.     }
  305.     public function getSlug(): ?string
  306.     {
  307.         return $this->slug;
  308.     }
  309.     public function setSlug(string $slug): self
  310.     {
  311.         $this->slug $slug;
  312.         return $this;
  313.     }
  314.     public function getPromotion(): ?Promotion
  315.     {
  316.         return $this->promotion;
  317.     }
  318.     public function setPromotion(?Promotion $promotion): static
  319.     {
  320.         $this->promotion $promotion;
  321.         return $this;
  322.     }
  323.     /**
  324.      * Get the value of deletedAt
  325.      */ 
  326.     public function getDeletedAt()
  327.     {
  328.         return $this->deletedAt;
  329.     }
  330.     /**
  331.      * Set the value of deletedAt
  332.      *
  333.      * @return  self
  334.      */ 
  335.     public function setDeletedAt($deletedAt)
  336.     {
  337.         $this->deletedAt $deletedAt;
  338.         return $this;
  339.     }
  340.     /**
  341.      * Get the value of position
  342.      */ 
  343.     public function getPosition()
  344.     {
  345.         return $this->position;
  346.     }
  347.     /**
  348.      * Set the value of position
  349.      *
  350.      * @return  self
  351.      */ 
  352.     public function setPosition($position)
  353.     {
  354.         $this->position $position;
  355.         return $this;
  356.     }
  357. }