<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use App\Annotation\CompanyCustomizable;
use App\Repository\SlideRepository;
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=SlideRepository::class)
* @ORM\HasLifecycleCallbacks
* @ApiResource(
* normalizationContext={
* "groups"={"slide:read"}
* },
* denormalizationContext={
* "groups"={"slide:write"}
* },
* collectionOperations={
* "get"={
* "security"="is_granted('ROLE_ADMIN') or is_granted('ROLE_COMPANY')"
* },
* "post"={
* "security"="is_granted('ROLE_ADMIN') or is_granted('ROLE_COMPANY')"
* },
* "get_channel_slides"={
* "path"="/channels/{channel}/slides",
* "method"="GET",
* "controller"="App\Controller\Api\SlideController::getByChannel",
* "security"="is_granted('ROLE_USER')"
* },
* "get_by_slug"={
* "path"="/slides_page/{slug}",
* "requirements"={"slug"=".*"},
* "method"="GET",
* "controller"="App\Controller\Api\SlideController::getBySlug",
* "openapi_context"={
* "summary"="Fetches slides by slug (aka: page)",
* "parameters"={
* {
* "name"="slug",
* "in"="path",
* "description"="Slug to find",
* "required"=true,
* "schema"={
* "type"="string",
* "enum"=self::TYPES
* },
* },
* },
* },
* },
* "get_slides_by_company"={
* "path"="/companies/{company}/slides",
* "method"="GET",
* "controller"="App\Controller\Api\SlideController::getSlidesByCompany",
* "security"="is_granted('ROLE_USER') or is_granted('ROLE_COMPANY') or is_granted('ROLE_ADMIN')"
* },
* "post_update"={
* "security"="is_granted('ROLE_ADMIN') or is_granted('ROLE_COMPANY')",
* "path"="/slides/{id}",
* "description"="Update a Slide with method POST (using content-type: 'multipart')",
* "method"="POST",
* "controller"="App\Controller\Api\SlideController::update"
* }
* },
* itemOperations={
* "get"={
* "security"="is_granted('ROLE_USER')"
* },
* "delete"={
* "security"="is_granted('ROLE_ADMIN') or is_granted('ROLE_COMPANY')"
* },
* "patch"={
* "security"="is_granted('ROLE_ADMIN') or is_granted('ROLE_COMPANY')"
* }
* }
* )
* @CompanyCustomizable
* @ApiFilter(SearchFilter::class, properties={"type"})
* @ApiFilter(PropertyFilter::class,
* arguments={
* "parameterName"="fields",
* "overrideDefaultProperties"=true
* }
* )
*/
class Slide
{
const LOGIN_TYPE = 'login_page';
const HOMEPAGE_TYPE = 'homepage_slide';
const CHANNEL_TYPE = 'channel_slide';
const TYPES = [
self::LOGIN_TYPE,
self::HOMEPAGE_TYPE,
self::CHANNEL_TYPE
];
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"slide:read", "slide:read:id"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"slide:read", "slide:write", "slide_slug:read", "slide:read:name"})
*/
private $name;
/**
* @ORM\Column(type="boolean", options={"default": "1"})
* @Groups({"slide:read", "slide:write", "slide:read:active"})
*/
private $active = true;
/**
* @ORM\Column(type="datetime")
* @Gedmo\Timestampable(on="create")
* @Groups({"slide:read", "slide:read:createdAt"})
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Gedmo\Timestampable(on="update")
* @Groups({"slide:read", "slide:read:updatedAt"})
*/
private $updatedAt;
/**
* Order on the slides, auto increment or any if the order is not important
* @Assert\NotBlank
* @ORM\Column(name="`rank`", type="integer")
* @Groups({"slide:read", "slide:write", "slide:read:rank"})
*/
private $rank = 1;
/**
* This is used for the customisation in the login page
* @ORM\ManyToOne(targetEntity=MediaObject::class)
* @Groups({"slide:read", "slide:write", "slide_slug:read", "slide:read:image"})
*/
private $image;
/**
* Defines the origin of the slide in witch page will it be displayed
* (ex: a type 'homepage_slide' is a slide to be displayed in an HP)
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"slide:read", "slide:write", "slide:read:type"})
* @Assert\Choice(choices=self::TYPES)
*/
private $type;
/**
* This can be either the channel it is linked to if type is homepage
* (ex: one of the HP slides but it puts in the spot a particular channel),
* or the channel it belong to (example: this is the 5th slider in the "XXX" channel)
* @ORM\ManyToOne(targetEntity=Channel::class, inversedBy="slides")
* @Groups({"slide:read", "slide:write", "slide_slug:read", "slide:read:channel"})
*/
private $channel;
/**
* This is a video to be put on the spot in this slide
* @ORM\ManyToOne(targetEntity=Video::class)
* @Groups({"slide:read", "slide:write", "slide_slug:read", "slide:read:video"})
*/
private $video;
/**
* Label of the button to be displayed
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"slide:read", "slide:write", "slide_slug:read", "slide:read:linkLabel"})
*/
private $linkLabel;
/**
* @ORM\ManyToOne(targetEntity=Category::class, inversedBy="slides")
* @Groups({"slide:read", "slide:write", "slide_slug:read", "slide:read:category"})
*/
private $category;
/**
* @ORM\ManyToOne(targetEntity=Program::class, inversedBy="slides")
* @Groups({"slide:read", "slide:write", "slide_slug:read", "slide:read:program"})
*/
private $program;
/**
* @ORM\ManyToOne(targetEntity=TvCompany::class, inversedBy="slides")
*/
private $tvCompany;
/**
* @ORM\ManyToOne(targetEntity=Company::class, inversedBy="slides")
* @Groups({"slide:read", "slide:write", "slide_slug:read", "slide:read:company"})
*/
private $company;
/**
* @ORM\ManyToOne(targetEntity=Slide::class, inversedBy="children")
*/
private $parent;
/**
* @ORM\OneToMany(targetEntity=Slide::class, mappedBy="parent")
*/
private $children;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"slide:read", "slide:write", "slide_slug:read", "slide:read:linkUrl"})
*/
private $linkUrl;
/**
* @ORM\Column(type="string", length=500, nullable=true)
* @Groups({"slide:read", "slide:write", "slide_slug:read", "slide:read:content"})
*/
private $content;
public function __construct()
{
$this->children = new ArrayCollection();
$this->rank = 1;
}
public function getId(): ?int
{
return $this->id;
}
public function setId(?int $id): self
{
$this->id = $id;
return $this;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getActive(): ?bool
{
return $this->active;
}
public function setActive(bool $active): self
{
$this->active = $active;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getRank(): ?int
{
return $this->rank;
}
public function setRank(int $rank): self
{
$this->rank = $rank;
return $this;
}
public function getImage(): ?MediaObject
{
return $this->image;
}
public function setImage(?MediaObject $image): self
{
$this->image = $image;
return $this;
}
/**
* @Groups({"slide:write"})
*/
public function setImageFile($file = null): self
{
if($file instanceof UploadedFile) {
$image = empty($this->image) ? new MediaObject : $this->image;
$image->setFile($file);
$this->setImage($image);
}
return $this;
}
public function getVideo(): ?Video
{
return $this->video;
}
public function setVideo(?Video $video): self
{
$this->video = $video;
return $this;
}
public function getType(): ?string
{
return $this->type;
}
public function setType(?string $type): self
{
$this->type = $type;
return $this;
}
public function getChannel(): ?Channel
{
return $this->channel;
}
public function setChannel(?Channel $channel): self
{
$this->channel = $channel;
return $this;
}
public function getLinkLabel(): ?string
{
return $this->linkLabel;
}
public function setLinkLabel(?string $linkLabel): self
{
$this->linkLabel = $linkLabel;
return $this;
}
public function getCategory(): ?Category
{
return $this->category;
}
public function setCategory(?Category $category): self
{
$this->category = $category;
return $this;
}
public function getProgram(): ?Program
{
return $this->program;
}
public function setProgram(?Program $program): self
{
$this->program = $program;
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;
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 getLinkUrl(): ?string
{
return $this->linkUrl;
}
public function setLinkUrl(?string $linkUrl): self
{
$this->linkUrl = $linkUrl;
return $this;
}
public function getContent(): ?string
{
return $this->content;
}
public function setContent(?string $content): self
{
$this->content = $content;
return $this;
}
}