<?php
namespace App\Entity;
use App\Repository\CategoryRepository;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
use JMS\Serializer\Annotation\MaxDepth;
#[ORM\Entity(repositoryClass: CategoryRepository::class)]
#[ORM\HasLifecycleCallbacks]
class Category
{
#[ORM\Id]
#[ORM\GeneratedValue]
#[ORM\Column]
#[Serializer\Groups(["category","poi","tour"])]
private ?int $id = null;
#[ORM\OneToOne(cascade: ['persist', 'remove'])]
#[MaxDepth(1)]
#[Serializer\Groups(["category"])]
private ?AudioTextBlock $title = null;
#[ORM\OneToOne(cascade: ['persist', 'remove'])]
#[MaxDepth(1)]
#[Serializer\Groups(["category"])]
private ?AudioTextBlock $description = null;
#[ORM\Column(length: 255, nullable: false)]
#[Serializer\Groups(["category"])]
#[Serializer\SerializedName("previewImageUrl")]
private ?string $preview_image_url = null;
#[ORM\ManyToOne(cascade: ["persist"])]
#[MaxDepth(1)]
#[Serializer\Groups(["category"])]
#[Serializer\SerializedName("shortDescription")]
private ?AudioTextBlock $short_description = null;
#[ORM\Column(length: 255, nullable: true)]
#[Serializer\Groups(["category"])]
#[Serializer\SerializedName("itemPrensentationAspectratio")]
private ?string $item_prensentation_aspectratio = null;
#[ORM\ManyToMany(targetEntity: Tour::class, inversedBy: "categories")]
#[MaxDepth(1)]
private Collection $tours;
//#[ORM\ManyToMany(targetEntity: POI::class)]
//#[ORM\OrderBy(["sortorder" => "ASC"])]
// private Collection $pois;
#[ORM\OneToMany(targetEntity: CategoryPoi::class, mappedBy: "category",cascade:["persist","remove"])]
#[ORM\OrderBy(["sortorder" => "ASC"])]
#[MaxDepth(2)]
private Collection $pois;
#[ORM\ManyToMany(targetEntity: self::class)]
#[MaxDepth(2)]
private Collection $Subcategories;
#[ORM\Column(length: 255, nullable: true)]
#[Serializer\Groups(["category"])]
private ?string $titlevalue = null;
#[ORM\Column(type: Types::DATETIME_MUTABLE, nullable: true)]
#[Serializer\Groups(["category"])]
#[Serializer\SerializedName("updatedAt")]
private ?DateTime $updated_at = null;
#[ORM\Column(nullable: true)]
#[Serializer\Groups(["category"])]
private ?bool $live = null;
#[Serializer\Groups(["category"])]
#[Serializer\VirtualProperty()]
#[Serializer\SerializedName("Subcategories")]
public function getCategoryIDs(){
$categories = $this->getSubcategories();
$categoryIds = [];
foreach ($categories as $category) {
$categoryIds[]=$category->getId();
}
return $categoryIds ;
}
#[Serializer\Groups(["category"])]
#[Serializer\VirtualProperty()]
#[Serializer\SerializedName("pois")]
public function getPoiIDs(){
$pois = $this->getPois();
$poiIds = [];
foreach ($pois as $poi) {
$poiIds[]=$poi->getId();
}
return $poiIds ;
}
#[Serializer\Groups(["category"])]
#[Serializer\VirtualProperty()]
#[Serializer\SerializedName("tours")]
public function getTourIDs(){
$tours = $this->getTours();
$tourIDs = [];
foreach ($tours as $tour) {
$tourIDs[]=$tour->getId();
}
return $tourIDs ;
}
public function __construct()
{
$this->Subcategories = new ArrayCollection();
$this->pois = new ArrayCollection();
$this->tours = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function __toString()
{
return $this->title->getText();
}
public function getTitle(): ?AudioTextBlock
{
return $this->title;
}
public function setTitle(?AudioTextBlock $title): self
{
$this->title = $title;
return $this;
}
public function getDescription(): ?AudioTextBlock
{
return $this->description;
}
public function setDescription(?AudioTextBlock $description): self
{
$this->description = $description;
return $this;
}
public function getPreviewImageUrl(): ?string
{
return $this->preview_image_url;
}
public function setPreviewImageUrl(?string $preview_image_url): self
{
$this->preview_image_url = $preview_image_url;
return $this;
}
public function getShortDescription(): ?array
{
return [$this->short_description];
}
public function setShortDescription(?array $short_description): self
{
$this->short_description = $short_description[0];
return $this;
}
public function getItemPrensentationAspectratio(): ?string
{
return $this->item_prensentation_aspectratio;
}
public function setItemPrensentationAspectratio(?string $item_prensentation_aspectratio): self
{
$this->item_prensentation_aspectratio = $item_prensentation_aspectratio;
return $this;
}
public function getTours(): Collection
{
return $this->tours;
}
public function setTours(?Collection $tours): self
{
$this->tours = $tours;
return $this;
}
public function addTour(Tour $tour): self
{
if (!$this->tours->contains($tour)) {
$this->tours->add($tour);
$tour->addCategory($this);
}
return $this;
}
public function removeTour(Tour $tour): self
{
if ($this->tours->removeElement($tour)) {
$tour->removeCategory($this);
}
return $this;
}
/*
public function getPois(): Collection
{
return $this->pois;
}
*/
public function getPois(): Collection
{
$sortedCollection = [];
$criteria = Criteria::create()
->orderBy(['sortorder' => Criteria::ASC]);
$poicollection = $this->pois->matching($criteria);
$sortedCollection = $poicollection->map(function (CategoryPoi $poiCategory) {
$poiEntity = $poiCategory->getPoi();
$poiEntity->setTemporaryOrderValue($poiCategory->getSortorder());
if($poiEntity->getId()==7){
// return null;
}
return $poiEntity;
});
// dd($sortedCollection);
return $sortedCollection;
}
public function addPoi(POI $poi): self
{
$poicategoryrelation = new CategoryPoi;
$poicategoryrelation->setPoi($poi);
$poicategoryrelation->setCategory($this);
if (!$this->pois->contains($poicategoryrelation)) {
$this->pois->add($poicategoryrelation);
// $poi->setCategory($this);
}
return $this;
}
public function removePoi(POI $poi): self
{
$poiToRemove = new CategoryPoi;
foreach( $this->pois as $categoryPoi) {
if($categoryPoi->getPoi()->getId()==$poi->getId()){
$poiToRemove = $categoryPoi;
}
}
if ($this->pois->removeElement($poiToRemove)) {
// dd($poiToRemove);
$poiToRemove->setCategory(null);
$poi->removeCategoryPoi($poiToRemove);
}
return $this;
}
/**
* @return Collection<int, self>
*/
public function getSubcategories(): Collection
{
return $this->Subcategories;
}
public function addSubcategory(self $subcategory): self
{
if (!$this->Subcategories->contains($subcategory)) {
$this->Subcategories->add($subcategory);
}
return $this;
}
public function removeSubcategory(self $subcategory): self
{
$this->Subcategories->removeElement($subcategory);
return $this;
}
public function getTitlevalue(): ?string
{
return $this->titlevalue;
}
public function setTitlevalue(?string $titlevalue): self
{
$this->titlevalue = $titlevalue;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updated_at;
}
public function setUpdatedAt(?\DateTimeInterface $updated_at): self
{
$this->updated_at = $updated_at;
return $this;
}
#[ORM\PrePersist]
#[ORM\PreUpdate]
public function updatedTimestamps()
{
$this->setUpdatedAt(new \DateTime('now'));
}
public function isLive(): ?bool
{
return $this->live;
}
public function setLive(?bool $live): self
{
$this->live = $live;
return $this;
}
}