<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Annotation\ApiSubresource;
use ApiPlatform\Core\Serializer\Filter\GroupFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\BooleanFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use App\Repository\ObjectiveRepository;
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=ObjectiveRepository::class)
* @ApiResource(
* normalizationContext={
* "groups"={"objective:read"}
* },
* denormalizationContext={
* "groups"={"objective:write"}
* },
* collectionOperations={
* "get"={
* "security"="is_granted('ROLE_USER')"
* },
* "post"={
* "security"="is_granted('ROLE_ADMIN')"
* },
* "post_update"={
* "security"="is_granted('ROLE_ADMIN')",
* "path"="/objectives/{id}",
* "description"="Update an Objective with method POST (using content-type: 'multipart')",
* "method"="POST",
* "controller"="App\Controller\Api\ObjectiveController::update"
* }
* },
* itemOperations={
* "get"={
* "security"="is_granted('ROLE_USER')"
* },
* "delete"={
* "security"="is_granted('ROLE_ADMIN')"
* },
* "patch"={
* "security"="is_granted('ROLE_ADMIN')"
* }
* }
* )
* @ApiFilter(OrderFilter::class, properties={"id", "name", "active", "created_at", "updated_at"})
* @ApiFilter(GroupFilter::class,
* arguments={
* "parameterName": "groups",
* "overrideDefaultGroups": true
* }
* )
* @ApiFilter(SearchFilter::class, properties={"id":"exact", "name":"partial", "active":"exact", "users.id":"exact"})
* @ApiFilter(BooleanFilter::class, properties={"active", "isRecommendedObjective"})
* @ApiFilter(PropertyFilter::class,
* arguments={
* "parameterName"="fields",
* "overrideDefaultProperties"=true
* }
* )
*/
class Objective
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"objective:read", "homepage:read", "stats", "channel:read", "search_engine", "slide:read", "objective_list",
* "objective_form_complete", "survey:read", "user:read", "client:read"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"objective:read", "objective:write", "search_engine", "channel_list", "objective_list",
* "objective_form_complete", "survey:read", "user:read", "client:read"})
* @Assert\NotNull(message="objective.name")
* @Assert\NotBlank(message="objective.name")
*/
private $name;
/**
* @ORM\Column(type="text")
* @Groups({"objective:read", "objective:write", "objective_form_complete"})
*/
private $content = '';
/**
* @ORM\ManyToOne(targetEntity=MediaObject::class)
* @Groups({"objective:read", "objective:write", "stats", "objective_form_complete", "user:read", "client:read"})
*/
private $picture = null;
/**
* @ORM\ManyToOne(targetEntity=MediaObject::class)
* @Groups({"objective:read", "objective:write", "objective_form_complete", "user:read", "client:read"})
*/
private $illustration = null;
/**
* @ORM\Column(type="boolean", options={"default": "1"})
* @Groups({"objective:read", "objective:write", "objective_list", "objective_form_complete", "user:read", "client:read"})
*/
private $active = true;
/**
* @ORM\Column(type="datetime")
* @Gedmo\Timestampable(on="update")
* @Groups({"objective:read"})
*/
private $updatedAt;
/**
* @ORM\Column(type="datetime")
* @Gedmo\Timestampable(on="create")
* @Groups({"objective:read"})
*/
private $createdAt;
/**
* @Groups({"objective:read", "video:read"})
* @ApiSubresource(maxDepth=1)
*/
private $videos;
/**
* @ORM\ManyToMany(targetEntity=Program::class, inversedBy="objectives")
* @Groups({"objective:read", "objective_form_complete"})
* @ApiSubresource(maxDepth=1)
*/
private $programs;
/**
* @ORM\ManyToMany(targetEntity=Channel::class, inversedBy="objectives")
* @Groups({"objective:read", "objective_form_complete"})
* @ApiSubresource(maxDepth=1)
*/
private $channels;
/**
* @ORM\OneToOne(targetEntity=Playlist::class, cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=false)
* @Groups({"objective:read", "objective:write", "objective_form_complete"})
* @ApiSubresource(maxDepth=1)
*/
private $playlist;
/**
* @ORM\ManyToMany(targetEntity=TvUser::class, inversedBy="objectives")
*/
private $tvUsers;
/**
* @ORM\ManyToMany(targetEntity=User::class, inversedBy="objectives")
*/
private $users;
/**
* @ORM\Column(type="boolean", options={"default": "0"})
* @Groups({"objective:read", "objective:write", "user:read", "client:read"})
*/
private $isRecommendedObjective = false;
/**
* Cutsom field
* Si c'est un ROLE_CLIENT, permet de savoir si l'objectif a été selectionner par celui-ci
* @Groups({"objective:read", "user:read", "client:read"})
*/
public $isSelectedByUser = null;
public function __construct()
{
$this->programs = new ArrayCollection();
$this->channels = new ArrayCollection();
$this->videos = new ArrayCollection();
$this->playlist = new Playlist();
$this->tvUsers = new ArrayCollection();
$this->users = 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({"objective: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({"objective: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 getActive(): ?bool
{
return $this->active;
}
public function setActive(bool $active): self
{
$this->active = $active;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
/**
* @return Collection|Program[]
* @Groups({"objective_form_complete"})
*/
public function getPrograms(): Collection
{
return $this->programs;
}
/**
* @Groups({"objective:write"})
*/
public function setPrograms(Collection $programs): self
{
$this->programs = $programs;
return $this;
}
public function addProgram(Program $program): self
{
if (!$this->programs->contains($program)) {
$this->programs[] = $program;
$program->addObjective($this);
}
return $this;
}
public function removeProgram(Program $program): self
{
if ($this->programs->removeElement($program)) {
$program->removeObjective($this);
}
return $this;
}
/**
* @param Program $program
*/
public function setProgramAdd(Program $program): self
{
return $this->addProgram($program);
}
/**
* @param Program $program
*/
public function setProgramRemove(Program $program): self
{
return $this->removeProgram($program);
}
/**
* @var Collection|Program[]
* @return Collection|Program[]
*/
public function setProgramsRemove(array $programs): self
{
foreach ($programs as $program) {
if($program instanceof Program) {
$this->removeProgram($program);
}
}
return $this;
}
/**
* @return Collection|Channel[]
* @Groups({"objective_form_complete"})
*/
public function getChannels(): Collection
{
return $this->channels;
}
/**
* @Groups({"objective:write"})
*/
public function setChannels(Collection $channels): self
{
$this->channels = $channels;
return $this;
}
public function addChannel(Channel $channel): self
{
if (!$this->channels->contains($channel)) {
$this->channels[] = $channel;
$channel->addObjective($this);
}
return $this;
}
public function removeChannel(Channel $channel): self
{
if ($this->channels->removeElement($channel)) {
$channel->removeObjective($this);
}
return $this;
}
/**
* @param Channel $channel
*/
public function setChannelAdd(Channel $channel): self
{
return $this->addChannel($channel);
}
/**
* @var Collection|Channel[]
* @return Collection|Channel[]
*/
public function setChannelsAdd(array $channels): self
{
foreach ($channels as $channel) {
if($channel instanceof Channel) {
$this->addChannel($channel);
}
}
return $this;
}
/**
* @param Channel $channel
*/
public function setChannelRemove(Channel $channel): self
{
return $this->removeChannel($channel);
}
/**
* @var Collection|Channel[]
* @return Collection|Channel[]
*/
public function setChannelsRemove(array $channels): self
{
foreach ($channels as $channel) {
if($channel instanceof Channel) {
$this->removeChannel($channel);
}
}
return $this;
}
/**
* @return Collection|Video[]
*/
public function getVideos(): Collection
{
$this->videos = $this->playlist->getVideos();
return $this->videos;
}
public function setVideos(array $videos): self
{
$this->playlist->setVideos($videos);
return $this;
}
/**
* @param Video $video
* @Groups({"objective:write"})
*/
public function setVideoAdd(Video $video): self
{
if (!$this->playlist->getVideos()->contains($video)) {
$this->playlist->addVideo($video);
}
return $this;
}
/**
* @param Video $video
* @Groups({"objective:write"})
*/
public function setVideoRemove(Video $video): self
{
$this->playlist->removeVideo($video);
return $this;
}
/**
* @Groups({"objective_form_complete"})
*/
public function getPlaylist(): ?Playlist
{
return $this->playlist;
}
public function setPlaylist(Playlist $playlist): self
{
$this->playlist = $playlist;
return $this;
}
/**
* @return Collection|TvUser[]
*/
public function getTvUsers(): Collection
{
return $this->tvUsers;
}
public function addTvUser(TvUser $tvUser): self
{
if (!$this->tvUsers->contains($tvUser)) {
$this->tvUsers[] = $tvUser;
}
return $this;
}
public function removeTvUser(TvUser $tvUser): self
{
$this->tvUsers->removeElement($tvUser);
return $this;
}
/**
* @return Collection|User[]
*/
public function getUsers(): Collection
{
return $this->users;
}
public function addUser(User $user): self
{
if (!$this->users->contains($user)) {
$this->users[] = $user;
}
return $this;
}
public function removeUser(User $user): self
{
$this->users->removeElement($user);
return $this;
}
public function getIsRecommendedObjective(): ?bool
{
return $this->isRecommendedObjective;
}
public function setIsRecommendedObjective(bool $isRecommendedObjective): self
{
$this->isRecommendedObjective = $isRecommendedObjective;
return $this;
}
}