<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiSubresource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Serializer\Filter\GroupFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use App\Annotation\CompanyCustomizable;
use App\Annotation\Exclusions;
use App\Repository\CategoryRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass=CategoryRepository::class)
* @ApiResource(
* normalizationContext={"groups"={"category:read"}},
* denormalizationContext={"groups"={"category:write"}},
* collectionOperations={
* "get"={
* "security"="is_granted('ROLE_USER')"
* },
* "homepage_categories"={
* "security"="is_granted('ROLE_USER')",
* "method"="GET",
* "path"="/categories/navigation",
* "controller"="App\Controller\Api\CategoryController::navigation",
* },
* "post"={
* "security"="is_granted('ROLE_USER')"
* },
* "post_update"={
* "security"="is_granted('ROLE_ADMIN')",
* "path"="/categories/{id}",
* "description"="Update an Category with method POST (using content-type: 'multipart')",
* "method"="POST",
* "controller"="App\Controller\Api\CategoryController::update"
* },
* "manager_categories"={
* "security"="is_granted('ROLE_USER')",
* "method"="GET",
* "path"="/manager/categories",
* "controller"="App\Controller\Api\CategoryController::getCategoriesForManager",
* }
* },
* itemOperations={
* "get"={
* "security"="is_granted('ROLE_USER')"
* },
* "delete"={
* "security"="is_granted('ROLE_ADMIN')"
* },
* "patch"={
* "security"="is_granted('ROLE_ADMIN')"
* }
* }
* )
* @ORM\HasLifecycleCallbacks
* @ApiFilter(OrderFilter::class, properties={"id", "name", "active", "activeMenu"})
* @ApiFilter(GroupFilter::class, arguments={
* "parameterName": "groups",
* "overrideDefaultGroups": true
* })
* @ApiFilter(SearchFilter::class, properties={"activeMenu", "company.id"})
* @ApiFilter(ExistsFilter::class, properties={"company", "parent"})
* @ApiFilter(PropertyFilter::class,
* arguments={
* "parameterName"="fields",
* "overrideDefaultProperties"=true
* }
* )
* @CompanyCustomizable
* @Exclusions(exclusionsClass="App\Entity\ExclusionsCategory", property="exclusions_categories")
*/
class Category
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"category:read", "category:read:id", "homepage:read", "stats", "channel:read", "search_engine", "slide:read", "category_list", "program_express_list"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"category:read", "category:read:name", "category:write", "stats", "channel:read", "homepage:read", "search_engine",
* "slide:read", "channel_list", "category_list", "program_express_list"})
* @Assert\NotNull(message="category.name")
* @Assert\NotBlank(message="category.name")
*/
private $name;
/**
* @ORM\Column(type="text")
* @Groups({"category:read", "category:read:content", "category:write"})
* @Assert\NotNull(message="category.content")
* @Assert\NotBlank(message="category.content")
*/
private $content;
/**
* @ORM\ManyToOne(targetEntity=MediaObject::class)
* @Groups({"category:read", "category:read:picture", "category:write", "stats"})
*/
private $picture;
/**
* @ORM\ManyToOne(targetEntity=MediaObject::class)
* @Groups({"category:read", "category:read:illustration", "category:write"})
*/
private $illustration;
/**
* @ORM\ManyToOne(targetEntity=MediaObject::class)
* @Groups({"category:read", "category:read:appPicture", "category:write"})
*/
private $appPicture;
/**
* @ORM\Column(type="datetime")
* @Gedmo\Timestampable(on="update")
* @Groups({"category:read", "category:read:updatedAt"})
*/
private $updatedAt;
/**
* @ORM\Column(type="datetime")
* @Gedmo\Timestampable(on="create")
* @Groups({"category:read", "category:read:createdAt"})
*/
private $createdAt;
/**
* @ORM\OneToMany(targetEntity=Channel::class, mappedBy="category", orphanRemoval=true)
* @Groups({"category:read", "category:read:channels", "homepage:read"})
* @ApiSubresource(maxDepth=1)
*/
private $channels;
/**
* @ORM\Column(type="boolean", options={"default": "1"})
* @Groups({"category:read", "category:read:active", "category:write", "category_list"})
*/
private $active = true;
/**
* @var int
* @Groups({"category:read", "category:read:channelsCount", "category_list"})
*/
private $channelsCount = 0;
/**
* Indicates to a manager if the category is excluded for them
* @var bool
* @Groups({"category:read", "category:read:excluded", "category_list"})
*/
private $excluded = false;
/**
* @ORM\ManyToOne(targetEntity=TvCompany::class, inversedBy="categories")
*/
private $tvCompany;
/**
* @ORM\OneToMany(targetEntity=Slide::class, mappedBy="category")
*/
private $slides;
/**
* @ORM\ManyToOne(targetEntity=Category::class, inversedBy="children")
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity=Category::class, mappedBy="parent")
*/
private $children;
/**
* @ORM\Column(type="boolean", options={"default": "0"})
* @Groups({"category:read", "category:read:activeMenu", "category:write", "stats", "channel:read", "homepage:read", "search_engine", "category_list"})
*/
private $activeMenu = false;
/**
* @ORM\OneToMany(targetEntity=ExclusionsCategory::class, mappedBy="category", orphanRemoval=true, cascade={"persist", "refresh", "remove"})
* @Groups({"category:read", "category:read:exclusionsCategories", "category:write"})
*/
private $exclusionsCategories;
/**
* @ORM\ManyToOne(targetEntity=Company::class, inversedBy="categories")
* @Groups({"category:read", "category:write"})
*/
private $company;
public function __construct()
{
$this->channels = new ArrayCollection();
$this->slides = new ArrayCollection();
$this->children = new ArrayCollection();
$this->activeMenu = false;
$this->exclusionsCategories = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(string $content): self
{
$this->content = $content;
return $this;
}
public function getPicture(): ?MediaObject
{
return $this->picture;
}
public function setPicture(?MediaObject $picture): self
{
$this->picture = $picture;
return $this;
}
/**
* @Groups({"category:write"})
*/
public function setPictureFile($file = null): self
{
if($file instanceof UploadedFile) {
$picture = empty($this->picture) ? new MediaObject : $this->picture;
$picture->setFile($file);
$this->setPicture($picture);
}
return $this;
}
public function getIllustration(): ?MediaObject
{
return $this->illustration;
}
public function setIllustration(?MediaObject $illustration): self
{
$this->illustration = $illustration;
return $this;
}
/**
* @Groups({"category:write"})
*/
public function setIllustrationFile($file = null): self
{
if($file instanceof UploadedFile) {
$illustration = empty($this->illustration) ? new MediaObject : $this->illustration;
$illustration->setFile($file);
$this->setIllustration($illustration);
}
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
/**
* @return Collection|Channel[]
*/
public function getChannels(): Collection
{
return $this->channels;
}
public function addChannel(Channel $channel): self
{
if (!$this->channels->contains($channel)) {
$this->channels[] = $channel;
$channel->setCategory($this);
}
return $this;
}
public function removeChannel(Channel $channel): self
{
if ($this->channels->removeElement($channel)) {
// set the owning side to null (unless already changed)
if ($channel->getCategory() === $this) {
$channel->setCategory(null);
}
}
return $this;
}
public function getActive(): ?bool
{
return $this->active;
}
public function setActive(bool $active): self
{
$this->active = $active;
return $this;
}
public function getTvCompany(): ?TvCompany
{
return $this->tvCompany;
}
public function setTvCompany(?TvCompany $tvCompany): self
{
$this->tvCompany = $tvCompany;
return $this;
}
public function getCompany(): ?Company
{
return $this->company;
}
public function setCompany(?Company $company): self
{
$this->company = $company;
foreach ($this->getChannels() as $channel) {
$channel->setCompany($company);
}
return $this;
}
/**
* @return Collection|Slide[]
*/
public function getSlides(): Collection
{
return $this->slides;
}
public function addSlide(Slide $slide): self
{
if (!$this->slides->contains($slide)) {
$this->slides[] = $slide;
$slide->setCategory($this);
}
return $this;
}
public function removeSlide(Slide $slide): self
{
if ($this->slides->removeElement($slide)) {
// set the owning side to null (unless already changed)
if ($slide->getCategory() === $this) {
$slide->setCategory(null);
}
}
return $this;
}
public function getParent(): ?self
{
return $this->parent;
}
public function setParent(?self $parent): self
{
$this->parent = $parent;
return $this;
}
/**
* @return Collection|self[]
*/
public function getChildren(): Collection
{
return $this->children;
}
public function addChild(self $child): self
{
if (!$this->children->contains($child)) {
$this->children[] = $child;
$child->setParent($this);
}
return $this;
}
public function removeChild(self $child): self
{
if ($this->children->removeElement($child)) {
// set the owning side to null (unless already changed)
if ($child->getParent() === $this) {
$child->setParent(null);
}
}
return $this;
}
public function getActiveMenu(): ?bool
{
return $this->activeMenu;
}
public function setActiveMenu(bool $activeMenu): self
{
$this->activeMenu = $activeMenu;
return $this;
}
/**
* @return Collection|ExclusionsCategory[]
*/
public function getExclusionsCategories(): Collection
{
return $this->exclusionsCategories;
}
public function addExclusionsCategory(ExclusionsCategory $exclusionsCategory): self
{
if (!$this->exclusionsCategories->contains($exclusionsCategory)) {
$this->exclusionsCategories[] = $exclusionsCategory;
$exclusionsCategory->setCategory($this);
// Ajout des Exclusions de toutes les chaines liées a cette Category
foreach($this->getChannels()->toArray() as $channel) {
// Gestion des ExclusionsChannel
$exclusionsChannel = new ExclusionsChannel;
$exclusionsChannel
->setChannel($channel)
->setCompany($exclusionsCategory->getCompany())
->setCreatedAt(new \DateTime());
// Vérifier si il en existe pas deja un
$channel->addExclusionsChannel($exclusionsChannel);
}
}
return $this;
}
public function removeExclusionsCategory(ExclusionsCategory $exclusionsCategory): self
{
if ($this->exclusionsCategories->removeElement($exclusionsCategory)) {
// set the owning side to null (unless already changed)
if ($exclusionsCategory->getCategory() === $this) {
$exclusionsCategory->setCategory(null);
// Retrait des Exclusion de toutes les chaines liées a cette Category
foreach($this->getChannels()->toArray() as $channel) {
// Gestion des ExclusionsChannel
foreach ($channel->getExclusionsChannels()->toArray() as $exclusionsChannel) {
if($exclusionsChannel->getCompany() === $exclusionsCategory->getCompany()) {
$channel->removeExclusionsChannel($exclusionsChannel);
}
}
}
}
}
return $this;
}
/**
* @return int
*/
public function getChannelsCount(): int
{
return $this->channelsCount;
}
/**
* @param int $channelsCount
* @return Category
*/
public function setChannelsCount(int $channelsCount): Category
{
$this->channelsCount = $channelsCount;
return $this;
}
/**
* @param bool $excluded
* @return Category
*/
public function setExcluded(bool $excluded): Category
{
$this->excluded = $excluded;
return $this;
}
/**
* @return bool
*/
public function isExcluded(): bool
{
return $this->excluded;
}
public function getAppPicture(): ?MediaObject
{
return $this->appPicture;
}
public function setAppPicture(?MediaObject $appPicture): self
{
$this->appPicture = $appPicture;
return $this;
}
/**
* @Groups({"category:write"})
*/
public function setAppPictureFile($file = null): self
{
if($file instanceof UploadedFile) {
$appPicture = empty($this->appPicture) ? new MediaObject : $this->appPicture;
$appPicture->setFile($file);
$this->setAppPicture($appPicture);
}
return $this;
}
}