src/Entity/Category.php line 17

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\CategoryRepository;
  4. use DateTime;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Doctrine\Common\Collections\Criteria;
  8. use Doctrine\DBAL\Types\Types;
  9. use Doctrine\ORM\Mapping as ORM;
  10. use JMS\Serializer\Annotation as Serializer;
  11. use JMS\Serializer\Annotation\MaxDepth;
  12. #[ORM\Entity(repositoryClassCategoryRepository::class)]
  13. #[ORM\HasLifecycleCallbacks]
  14. class Category
  15. {
  16.     #[ORM\Id]
  17.     #[ORM\GeneratedValue]
  18.     #[ORM\Column]
  19.     #[Serializer\Groups(["category","poi","tour"])]
  20.     private ?int $id null;
  21.     #[ORM\OneToOne(cascade: ['persist''remove'])]
  22.     #[MaxDepth(1)]
  23.     #[Serializer\Groups(["category"])]
  24.     private ?AudioTextBlock $title null;
  25.     #[ORM\OneToOne(cascade: ['persist''remove'])]
  26.     #[MaxDepth(1)]
  27.     #[Serializer\Groups(["category"])]
  28.     private ?AudioTextBlock $description null;
  29.     #[ORM\Column(length255nullablefalse)]
  30.     #[Serializer\Groups(["category"])]
  31.     #[Serializer\SerializedName("previewImageUrl")]
  32.     private ?string $preview_image_url null;
  33.     #[ORM\ManyToOne(cascade: ["persist"])]
  34.     #[MaxDepth(1)]
  35.     #[Serializer\Groups(["category"])]
  36.     #[Serializer\SerializedName("shortDescription")]
  37.     private ?AudioTextBlock $short_description null;
  38.     #[ORM\Column(length255nullabletrue)]
  39.     #[Serializer\Groups(["category"])]
  40.     #[Serializer\SerializedName("itemPrensentationAspectratio")]
  41.     private ?string $item_prensentation_aspectratio null;
  42.     #[ORM\ManyToMany(targetEntityTour::class, inversedBy"categories")]
  43.     #[MaxDepth(1)]
  44.     private Collection $tours;
  45.     //#[ORM\ManyToMany(targetEntity: POI::class)]
  46.     //#[ORM\OrderBy(["sortorder" => "ASC"])]
  47.     // private Collection $pois;
  48.     #[ORM\OneToMany(targetEntityCategoryPoi::class, mappedBy"category",cascade:["persist","remove"])]
  49.     #[ORM\OrderBy(["sortorder" => "ASC"])]
  50.     #[MaxDepth(2)]
  51.     private Collection $pois;
  52.     #[ORM\ManyToMany(targetEntityself::class)]
  53.     #[MaxDepth(2)]
  54.     private Collection $Subcategories;
  55.     #[ORM\Column(length255nullabletrue)]
  56.     #[Serializer\Groups(["category"])]
  57.     private ?string $titlevalue null;
  58.     #[ORM\Column(typeTypes::DATETIME_MUTABLEnullabletrue)]
  59.     #[Serializer\Groups(["category"])]
  60.     #[Serializer\SerializedName("updatedAt")]
  61.     private ?DateTime $updated_at null;
  62.     #[ORM\Column(nullabletrue)]
  63.     #[Serializer\Groups(["category"])]
  64.     private ?bool $live null;
  65.     #[Serializer\Groups(["category"])]
  66.     #[Serializer\VirtualProperty()]
  67.     #[Serializer\SerializedName("Subcategories")]
  68.     public function getCategoryIDs(){
  69.         $categories $this->getSubcategories();
  70.         $categoryIds = [];
  71.         foreach ($categories as $category) {
  72.             $categoryIds[]=$category->getId();
  73.         }
  74.         return $categoryIds ;
  75.     }
  76.     #[Serializer\Groups(["category"])]
  77.     #[Serializer\VirtualProperty()]
  78.     #[Serializer\SerializedName("pois")]
  79.     public function getPoiIDs(){
  80.         $pois $this->getPois();
  81.         $poiIds = [];
  82.         foreach ($pois as $poi) {
  83.             $poiIds[]=$poi->getId();
  84.         }
  85.         return $poiIds ;
  86.     }
  87.     #[Serializer\Groups(["category"])]
  88.     #[Serializer\VirtualProperty()]
  89.     #[Serializer\SerializedName("tours")]
  90.     public function getTourIDs(){
  91.         $tours $this->getTours();
  92.         $tourIDs = [];
  93.         foreach ($tours as $tour) {
  94.             $tourIDs[]=$tour->getId();
  95.         }
  96.         return $tourIDs ;
  97.     }
  98.     public function __construct()
  99.     {
  100.         $this->Subcategories = new ArrayCollection();
  101.         $this->pois = new ArrayCollection();
  102.         $this->tours = new ArrayCollection();
  103.     }
  104.     public function getId(): ?int
  105.     {
  106.         return $this->id;
  107.     }
  108.     public function __toString()
  109.     {
  110.         return $this->title->getText();
  111.     }
  112.     public function getTitle(): ?AudioTextBlock
  113.     {
  114.         return $this->title;
  115.     }
  116.     public function setTitle(?AudioTextBlock $title): self
  117.     {
  118.         $this->title $title;
  119.         return $this;
  120.     }
  121.     public function getDescription(): ?AudioTextBlock
  122.     {
  123.         return $this->description;
  124.     }
  125.     public function setDescription(?AudioTextBlock $description): self
  126.     {
  127.         $this->description $description;
  128.         return $this;
  129.     }
  130.     public function getPreviewImageUrl(): ?string
  131.     {
  132.         return $this->preview_image_url;
  133.     }
  134.     public function setPreviewImageUrl(?string $preview_image_url): self
  135.     {
  136.         $this->preview_image_url $preview_image_url;
  137.         return $this;
  138.     }
  139.     public function getShortDescription(): ?array
  140.     {
  141.         return [$this->short_description];
  142.     }
  143.     public function setShortDescription(?array $short_description): self
  144.     {
  145.         $this->short_description $short_description[0];
  146.         return $this;
  147.     }
  148.     public function getItemPrensentationAspectratio(): ?string
  149.     {
  150.         return $this->item_prensentation_aspectratio;
  151.     }
  152.     public function setItemPrensentationAspectratio(?string $item_prensentation_aspectratio): self
  153.     {
  154.         $this->item_prensentation_aspectratio $item_prensentation_aspectratio;
  155.         return $this;
  156.     }
  157.     public function getTours(): Collection
  158.     {
  159.         return $this->tours;
  160.     }
  161.     public function setTours(?Collection $tours): self
  162.     {
  163.         $this->tours $tours;
  164.         return $this;
  165.     }
  166.     public function addTour(Tour $tour): self
  167.     {
  168.         if (!$this->tours->contains($tour)) {
  169.             $this->tours->add($tour);
  170.             $tour->addCategory($this);
  171.         }
  172.         return $this;
  173.     }
  174.     public function removeTour(Tour $tour): self
  175.     {
  176.         if ($this->tours->removeElement($tour)) {
  177.             $tour->removeCategory($this);
  178.         }
  179.         return $this;
  180.     }
  181.     /*
  182.     public function getPois(): Collection
  183.     {
  184.         return $this->pois;
  185.     }
  186. */
  187.     public function getPois(): Collection
  188.     {
  189.         $sortedCollection = [];
  190.         $criteria Criteria::create()
  191.             ->orderBy(['sortorder' => Criteria::ASC]);  
  192.         $poicollection $this->pois->matching($criteria);
  193.         $sortedCollection =  $poicollection->map(function (CategoryPoi $poiCategory) {
  194.             $poiEntity $poiCategory->getPoi();
  195.             $poiEntity->setTemporaryOrderValue($poiCategory->getSortorder());
  196.             if($poiEntity->getId()==7){
  197.              //   return null;
  198.             }
  199.             return $poiEntity;
  200.         });
  201.        // dd($sortedCollection);
  202.         return $sortedCollection;
  203.     }
  204.     public function addPoi(POI $poi): self
  205.     {
  206.         $poicategoryrelation = new CategoryPoi;
  207.         $poicategoryrelation->setPoi($poi);
  208.         $poicategoryrelation->setCategory($this);
  209.         if (!$this->pois->contains($poicategoryrelation)) {
  210.             $this->pois->add($poicategoryrelation);
  211.             //   $poi->setCategory($this);
  212.         }
  213.         return $this;
  214.     }
  215.     public function removePoi(POI $poi): self
  216.     {
  217. $poiToRemove = new CategoryPoi;
  218.         foreach( $this->pois as $categoryPoi) {
  219.             if($categoryPoi->getPoi()->getId()==$poi->getId()){
  220.                 $poiToRemove $categoryPoi;
  221.             }
  222.         }
  223.         if ($this->pois->removeElement($poiToRemove)) {
  224.           //  dd($poiToRemove);
  225.           $poiToRemove->setCategory(null);
  226.             $poi->removeCategoryPoi($poiToRemove);
  227.         }
  228.         return $this;
  229.     }
  230.     /**
  231.      * @return Collection<int, self>
  232.      */
  233.     public function getSubcategories(): Collection
  234.     {
  235.         return $this->Subcategories;
  236.     }
  237.     public function addSubcategory(self $subcategory): self
  238.     {
  239.         if (!$this->Subcategories->contains($subcategory)) {
  240.             $this->Subcategories->add($subcategory);
  241.         }
  242.         return $this;
  243.     }
  244.     public function removeSubcategory(self $subcategory): self
  245.     {
  246.         $this->Subcategories->removeElement($subcategory);
  247.         return $this;
  248.     }
  249.     public function getTitlevalue(): ?string
  250.     {
  251.         return $this->titlevalue;
  252.     }
  253.     public function setTitlevalue(?string $titlevalue): self
  254.     {
  255.         $this->titlevalue $titlevalue;
  256.         return $this;
  257.     }
  258.     public function getUpdatedAt(): ?\DateTimeInterface
  259.     {
  260.         return $this->updated_at;
  261.     }
  262.     public function setUpdatedAt(?\DateTimeInterface $updated_at): self
  263.     {
  264.         $this->updated_at $updated_at;
  265.         return $this;
  266.     }
  267.     #[ORM\PrePersist]
  268.     #[ORM\PreUpdate]
  269.     public function updatedTimestamps()
  270.     {
  271.         $this->setUpdatedAt(new \DateTime('now'));
  272.     }
  273.     public function isLive(): ?bool
  274.     {
  275.         return $this->live;
  276.     }
  277.     public function setLive(?bool $live): self
  278.     {
  279.         $this->live $live;
  280.         return $this;
  281.     }
  282. }