<?php
namespace App\Entity;
use App\Annotation\EsUploadable;
use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiFilter;
use App\Entity\Traits\TimestampableEntity;
use Doctrine\Common\Collections\Collection;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\CommonCategoryRepository;
use App\Entity\Interfaces\OrderMappedInterface;
use App\Entity\Interfaces\ClientMappedInterface;
use App\Entity\Interfaces\UploadMappedInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use Locastic\ApiPlatformTranslationBundle\Model\AbstractTranslatable;
use Locastic\ApiPlatformTranslationBundle\Model\TranslationInterface;
/**
* @ORM\Entity(repositoryClass=CommonCategoryRepository::class)
*/
/**
* @ApiResource(
* attributes={"security"="is_granted('ROLE_OPERATOR')", "filters"={"translation.groups"}},
* normalizationContext={"groups"={"CommonCategory:Read"}, "skip_null_values"=false},
* denormalizationContext={"groups"={"CommonCategory:Write"}},
* collectionOperations={
* "get"={"security"="is_granted('IS_AUTHENTICATED_FULLY')"},
* "get_for_public_page"={
* "route_name"="api_pub_common_categories_tags_get_for_public_page_collection",
* "method"="GET",
* "security"="is_granted('PUBLIC_ACCESS')",
* "normalization_context"={"groups"={"CommonCategory:PRead"}, "skip_null_values"=false}
* },
* "post"
* },
* itemOperations={
* "get"={"security"="is_granted('IS_CLIENT_OPR', object)"},
* "put"={"security"="is_granted('IS_CLIENT_OPR', object)"},
* "patch"={"security"="is_granted('IS_CLIENT_OPR', object)"},
* "patch_change_ord"={
* "path"="/common_categories/{id}/change-ord",
* "method"="PATCH",
* "denormalization_context"={"groups"={"CommonCategory:ChangeOrd"}},
* "security"="is_granted('IS_CLIENT_OPR', object)"
* },
* "delete"={"security"="is_granted('IS_CLIENT_OPR', object)"}
* }
* )
* @ApiFilter(SearchFilter::class, properties={"id": "exact", "client.id": "exact", "type": "exact", "translations.name": "partial", "parent.id": "exact"})
* @ApiFilter(OrderFilter::class, properties={"ord": "ASC", "translations.name", "id", "parent.id"})
* @ApiFilter(ExistsFilter::class, properties={"parent"})
* @ORM\Entity(repositoryClass=CommonCategoryRepository::class)
* @ORM\Table(
* indexes={
* @ORM\Index(name="common_category_type_idx", columns={"type"})
* }
* )
*/
class CommonCategory extends AbstractTranslatable implements ClientMappedInterface, OrderMappedInterface, UploadMappedInterface
{
/**
* Hook timestampable behavior
* updates createdAt, updatedAt fields
*/
use TimestampableEntity;
public const CGR = [
"CommonCategory:Read", "CommonCategory:PRead",
"Container:ReadSecure", "Container:Storage", "Container:PRead", "Container:Overview",
"Conference:Read", "Conference:PRead",
"Session:Read", "Session:GetItemDetail", "Session:PRead", "Session:Agenda", "Session:Read-Watched", "Session:VideoGallery",
"ElCourse:Read", "ElCourse:PRead",
"ElLession:Detail",
"ElTask:Detail",
"Newsfeed:Read",
"VideoGallery:Read", "VideoGallery:PRead", "VideoGallery:Read-Public",
"User:Read", "User:Write", "User:Me", "User:Attendee-List", "User:Attendee-List-O", "User:Change-Profile", "User:Get-Limited", "User:PRead", "User:Exp",
"DocFile:Read",
"InfoPage:Read",
"Company:Read", "Company:PRead",
];
public const CG = [
...self::CGR,
"CommonCategory:Write",
"User:Write"
];
public const ECG = [
...self::CG,
"Conference:EL", "Session:EL", "VideoGallery:EL", "ElCourse:EL", "Newsfeed:EL", "User:EL"
];
/**
* @ORM\OneToMany(targetEntity="CommonCategoryTranslation", mappedBy="translatable", fetch="EXTRA_LAZY", indexBy="locale", cascade={"PERSIST"}, orphanRemoval=true)
*
* @Groups({"Conference:EL", "Session:EL", "VideoGallery:EL", "ElCourse:EL", "Newsfeed:EL", "User:EL", "CommonCategory:Write", "CommonCategoryTranslationGroup", "CommonCategory:PRead"})
* @Assert\Valid()
*/
protected $translations;
/**
* @Groups(self::CG)
*/
private $name;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups(self::CGR)
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=Client::class)
* @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
* @Groups({"CommonCategory:Write"})
*/
private $client;
/**
* @ORM\Column(type="string", length=16, nullable=true)
* @Groups(self::ECG)
*/
private $color;
/**
* @ORM\Column(type="string", length=16, nullable=true)
* @Groups(self::ECG)
*/
private $textColor;
/**
* @ORM\Column(type="integer", nullable=true)
* @Groups(self::CG)
*/
private $ord;
/**
* @Groups({"CommonCategory:ChangeOrd"})
*/
private int $newOrd;
private $ordChangeDirection;
/**
* @ORM\Column(type="string", length=32)
* @Groups(self::CG)
*/
private $type;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups(self::ECG)
*
* @Assert\Length(max=255, maxMessage="validation.commonCategory:imageName.max")
* @EsUploadable()
*/
private $imageName;
/**
* @ORM\ManyToOne(targetEntity=CommonCategory::class, inversedBy="childrens")
* @ORM\JoinColumn(nullable=true, onDelete="CASCADE")
* @Groups(self::CG)
* @ApiProperty(readableLink=false, writableLink=false)
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity=CommonCategory::class, mappedBy="parent")
* @Groups(self::CGR)
*/
private $childrens;
/**
* @ORM\OneToMany(targetEntity=SpecialTitle::class, mappedBy="newsfeedCategory")
*/
private $specialTitles;
/**
* @ORM\ManyToMany(targetEntity=User::class, mappedBy="newsfeedSubscriptions")
*/
private $newsfeedSubscribedUsers;
public function __construct()
{
parent::__construct();
$this->childrens = new ArrayCollection();
$this->specialTitles = new ArrayCollection();
$this->newsfeedSubscribedUsers = new ArrayCollection();
}
public const CATEGORYTYPE_CONTAINER = "CONTAINER";
public const CATEGORYTYPE_CONFERENCE = "CONFERENCE";
public const CATEGORYTYPE_SESSION = "SESSION";
public const CATEGORYTYPE_VIDEOGALLERY = "VIDEOGALLERY";
public const CATEGORYTYPE_COURSE = "COURSE";
public const CATEGORYTYPE_NEWSFEED = "NEWSFEED";
public const CATEGORYTYPE_DOCFILE = "DOCFILE";
public const CATEGORYTYPE_INFOPAGE = "INFOPAGE";
public const CATEGORYTYPE_USER = "USER";
public const CATEGORYTYPE_COMPANY = "COMPANY";
protected function createTranslation(): TranslationInterface
{
return new CommonCategoryTranslation();
}
public function addTranslation(TranslationInterface $translation): void
{
$this->setUpdatedAt(new \DateTime());
parent::addTranslation($translation);
}
public function getName(): ?string
{
return $this->getTranslation()->getName();
}
public function setName(string $name): self
{
$this->getTranslation()->setName($name);
return $this;
}
public function getId(): ?int
{
return $this->id;
}
public function getClient(): ?Client
{
return $this->client;
}
public function setClient(?Client $client): self
{
$this->client = $client;
return $this;
}
public function getColor(): ?string
{
return $this->color;
}
public function setColor(?string $color): self
{
$this->color = $color;
return $this;
}
public function getTextColor(): ?string
{
return $this->textColor;
}
public function setTextColor(?string $textColor): self
{
$this->textColor = $textColor;
return $this;
}
public function getOrd(): ?int
{
return $this->ord;
}
public function setOrd(?int $ord): self
{
$this->ord = $ord;
return $this;
}
public function getOrdParents(): array
{
return [
'client' => $this->getClient(),
'type' => $this->getType()
];
}
public function getOrdChangeDirection()
{
return $this->ordChangeDirection;
}
public function getNewOrd(): int
{
return $this->newOrd ?? 0;
}
public function setNewOrd(int $newOrd): self
{
$oldOrd = $this->ord;
$this->newOrd = $newOrd;
$this->ord = $newOrd;
$this->ordChangeDirection = ($this->newOrd < $oldOrd) ? 'desc' : 'asc';
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(string $type): self
{
$this->type = $type;
return $this;
}
public function getImageName(): ?string
{
return $this->imageName;
}
public function setImageName(?string $imageName): self
{
$this->imageName = $imageName;
return $this;
}
public function getAllTranslation()
{
$translations = [];
foreach ($this->getTranslations() as $translation) {
$translations[] = [
$translation->getLocale() => [
'name' => $translation->getName()
]
];
}
return $translations;
}
public function getMeta()
{
$meta = [
'translations' => $this->getAllTranslation(),
'color' => $this->getColor(),
'textColor' => $this->getTextColor()
];
return $meta;
}
public function getQueueInfo(): array
{
return [
'type' => 'CommonCategory',
'id' => $this->getId(),
'categoryType' => $this->getType()
];
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* @return Collection<int, self>
*/
public function getChildrens(): Collection
{
return $this->childrens;
}
public function addChildren(self $children): self
{
if (!$this->childrens->contains($children)) {
$this->childrens[] = $children;
$children->setParent($this);
}
return $this;
}
public function removeChildren(self $children): self
{
if ($this->childrens->removeElement($children)) {
// set the owning side to null (unless already changed)
if ($children->getParent() === $this) {
$children->setParent(null);
}
}
return $this;
}
/**
* @return Collection<int, SpecialTitle>
*/
public function getSpecialTitles(): Collection
{
return $this->specialTitles;
}
public function addSpecialTitle(SpecialTitle $specialTitle): self
{
if (!$this->specialTitles->contains($specialTitle)) {
$this->specialTitles[] = $specialTitle;
$specialTitle->setNewsfeedCategory($this);
}
return $this;
}
public function removeSpecialTitle(SpecialTitle $specialTitle): self
{
if ($this->specialTitles->removeElement($specialTitle)) {
// set the owning side to null (unless already changed)
if ($specialTitle->getNewsfeedCategory() === $this) {
$specialTitle->setNewsfeedCategory(null);
}
}
return $this;
}
/**
* @return Collection<int, User>
*/
public function getNewsfeedSubscribedUsers(): Collection
{
return $this->newsfeedSubscribedUsers;
}
public function addNewsfeedSubscribedUser(User $newsfeedSubscribedUser): self
{
if (!$this->newsfeedSubscribedUsers->contains($newsfeedSubscribedUser)) {
$this->newsfeedSubscribedUsers[] = $newsfeedSubscribedUser;
$newsfeedSubscribedUser->addNewsfeedSubscription($this);
}
return $this;
}
public function removeNewsfeedSubscribedUser(User $newsfeedSubscribedUser): self
{
if ($this->newsfeedSubscribedUsers->removeElement($newsfeedSubscribedUser)) {
$newsfeedSubscribedUser->removeNewsfeedSubscription($this);
}
return $this;
}
}