<?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\BooleanFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
use ApiPlatform\Core\Serializer\Filter\GroupFilter;
use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
use App\Repository\TeamplayRepository;
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=TeamplayRepository::class)
* @ApiResource(
* normalizationContext={
* "groups"={"teamplay:read"}
* },
* denormalizationContext={
* "groups"={"teamplay:write"}
* },
* itemOperations={
* "get"={
* "security"="is_granted('ROLE_USER')"
* },
* "patch"={
* "security"="is_granted('ROLE_ADMIN')"
* },
* "delete"={
* "security"="is_granted('ROLE_ADMIN')"
* }
* },
* collectionOperations={
* "get"={
* "security"="is_granted('ROLE_USER')"
* },
* "post"={
* "security"="is_granted('ROLE_ADMIN')",
* },
* "post_update"={
* "security"="is_granted('ROLE_ADMIN')",
* "path"="/teamplays/{id}",
* "description"="Update a Teamplay with method POST (using content-type: 'multipart')",
* "method"="POST",
* "controller"="App\Controller\Api\TeamplayController::update"
* },
* "get_teamplay_in_progress_by_company"={
* "security"="is_granted('ROLE_USER')",
* "path"="/companies/{id}/teamplay-in-progress",
* "method"="GET",
* "description"="Get the teamplay in progress for this company.",
* "controller"="App\Controller\Api\TeamplayController::getTeamplayInProgress"
* }
* }
* )
* @ApiFilter(SearchFilter::class, strategy="exact",
* properties={"active", "name": "partial", "company.id", "isTemplate", "association.id"}
* )
* @ApiFilter(BooleanFilter::class, properties={"active", "isTemplate"})
* @ApiFilter(DateFilter::class, properties={"dateStart", "dateEnd"})
* @ApiFilter(OrderFilter::class, properties={"id", "name", "company.name", "dateStart", "dateEnd", "duration"})
* @ApiFilter(ExistsFilter::class, properties={"company", "association"})
* @ApiFilter(GroupFilter::class, arguments={
* "parameterName": "groups",
* "overrideDefaultGroups": true
* })
* @ApiFilter(PropertyFilter::class,
* arguments={
* "parameterName"="fields",
* "overrideDefaultProperties"=true
* }
* )
*/
class Teamplay
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"teamplay:read", "teamplay_form", "teamplay:read:id"})
*/
private $id;
/**
* @ORM\Column(type="string", length=255, nullable=true)
* @Groups({"teamplay:read", "teamplay:write", "teamplay_form", "teamplay:read:name"})
*/
private $name;
/**
* @ORM\Column(type="integer", nullable=true)
* @Groups({"teamplay:read", "teamplay:write", "company_planning_form", "teamplay:read:duration"})
*/
private $duration;
/**
* @ORM\ManyToOne(targetEntity=MediaObject::class, cascade={"persist", "refresh", "remove"})
* @Groups({"teamplay:read", "teamplay:write", "teamplay_form", "teamplay:read:picture"})
*/
private $picture;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Groups({"teamplay:read", "teamplay:write", "teamplay:read:dateStart"})
*/
private $dateStart;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Groups({"teamplay:read", "teamplay:write", "teamplay:read:dateEnd"})
*/
private $dateEnd;
/**
* @ORM\Column(type="boolean", nullable=true, options={"default": "1"})
* @Assert\NotNull
* @Groups({"teamplay:read", "teamplay:write", "teamplay:read:isTemplate"})
*/
private $isTemplate = true;
/**
* @ORM\ManyToOne(targetEntity=TvCompany::class, inversedBy="teamplays")
*/
private $tvCompany;
/**
* @ORM\ManyToOne(targetEntity=Company::class, inversedBy="teamplays")
* @Groups({"teamplay:read", "teamplay:write", "teamplay:read:company"})
*/
private $company;
/**
* @ORM\OneToMany(targetEntity=TeamplayChallenge::class, mappedBy="teamplay", orphanRemoval=true, cascade={"persist", "refresh", "remove"})
* @Groups({"teamplay:read", "teamplay:write", "teamplay:read:challenges"})
* @ApiSubresource(maxDepth=1)
*/
private $challenges;
/**
* @ORM\Column(type="boolean", nullable=true, options={"default": "1"})
* @Assert\NotNull
* @Groups({"teamplay:read", "teamplay:write", "teamplay:read:active"})
*/
private $active = true;
/**
* @ORM\Column(type="datetime", options={"default": "CURRENT_TIMESTAMP"})
* @Gedmo\Timestampable(on="create")
* @Groups({"teamplay:read", "teamplay:read:createdAt"})
*/
private $createdAt;
/**
* @ORM\Column(type="datetime", nullable=true)
* @Gedmo\Timestampable(on="update")
* @Groups({"teamplay:read", "teamplay:read:updatedAt"})
*/
private $updatedAt;
/**
* @Groups({"teamplay:write", "teamplay:read:template"})
*/
private $template = null;
/**
* @ORM\OneToMany(targetEntity=TeamplayLog::class, mappedBy="teamplay", orphanRemoval=true, cascade={"persist", "refresh", "remove"})
*/
private $teamplayLogs;
/**
* @ORM\ManyToOne(targetEntity=Association::class, inversedBy="teamplays")
* @Groups({"teamplay:read", "teamplay:write", "teamplay:read:association"})
*/
private $association;
/**
* @ORM\Column(type="text", nullable=true)
* @Groups({"teamplay:read", "teamplay:write", "teamplay_form", "teamplay:read:description"})
*/
private $description;
/**
* @ORM\OneToMany(targetEntity=Team::class, mappedBy="teamplay", orphanRemoval=true, cascade={"persist", "refresh", "remove"})
* @Groups({"teamplay:read", "teamplay:read:teams"})
*/
private $teams;
/**
* Champ CUSTOM
* Permet de récupérer le nombre total d'équipe active
* @Groups({"teamplay:read", "teamplay:read:activeTeamsCount"})
*/
private $activeTeamsCount = null;
/**
* Champ CUSTOM
* Permet de récupérer le nombre total d'équipe active ou non
* @Groups({"teamplay:read", "teamplay:read:teamsCount"})
*/
private $teamsCount = null;
public $isPlannable = null;
public function __construct()
{
$this->challenges = new ArrayCollection();
$this->teamplayLogs = new ArrayCollection();
$this->teams = new ArrayCollection();
}
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 getDuration(): ?int
{
return $this->duration;
}
public function setDuration(?int $duration): self
{
$this->duration = $duration;
return $this;
}
public function getPicture(): ?MediaObject
{
return $this->picture;
}
public function setPicture(?MediaObject $picture = null): self
{
$this->picture = $picture;
return $this;
}
/**
* @Groups({"teamplay: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 getDateStart(): ?\DateTimeInterface
{
return $this->dateStart;
}
public function setDateStart(?\DateTimeInterface $dateStart): self
{
$this->dateStart = $dateStart;
return $this;
}
public function getDateEnd(): ?\DateTimeInterface
{
return $this->dateEnd;
}
public function setDateEnd(?\DateTimeInterface $dateEnd): self
{
$this->dateEnd = $dateEnd;
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 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;
}
/**
* Permet de savoir si c'est une modèle
* Critères d'un modèle :
* - Ne pas être lié à une TvCompany
* @return bool
*/
public function isTemplate(): ?bool
{
return $this->getIsTemplate();
}
public function getIsTemplate(): ?bool
{
return $this->isTemplate;
}
public function setIsTemplate(?bool $isTemplate): self
{
$this->isTemplate = $isTemplate;
return $this;
}
/**
* @return Collection|TeamplayChallenge[]
*/
public function getChallenges(): Collection
{
if (empty($this->challenges)) {
$this->challenges = new ArrayCollection;
}
return $this->challenges;
}
public function addChallenge(?TeamplayChallenge $challenge = null): self
{
if ($challenge instanceof TeamplayChallenge) {
if (!$this->challenges->contains($challenge)) {
$this->challenges->add($challenge);
$challenge->setTeamplay($this);
}
}
return $this;
}
/**
* @Groups({"teamplay:write"})
*/
public function setAddChallenge(?TeamplayChallenge $challenge = null): self
{
if ($challenge instanceof TeamplayChallenge) {
$this->addChallenge($challenge);
}
return $this;
}
public function removeChallenge(TeamplayChallenge $challenge): self
{
if ($this->challenges->removeElement($challenge)) {
// set the owning side to null (unless already changed)
if ($challenge->getTeamplay() === $this) {
$challenge->setTeamplay(null);
}
}
return $this;
}
/**
* @return Collection|Team[]
*/
public function getTeams(): Collection
{
return $this->teams;
}
/**
* @Groups({"teamplay:write"})
*/
public function setAddTeam(?Team $team = null): self
{
if ($team instanceof Team) {
if (!$this->teams->contains($team)) {
$this->teams[] = $team;
$team->setTeamplay($this);
}
}
return $this;
}
public function removeTeam(Team $team): self
{
if ($this->teams->removeElement($team)) {
// set the owning side to null (unless already changed)
if ($team->getTeamplay() === $this) {
$team->setTeamplay(null);
}
}
return $this;
}
public function getActive(): ?bool
{
return $this->active;
}
public function isActive(): ?bool
{
return $this->active;
}
public function setActive(?bool $active): self
{
$this->active = $active;
return $this;
}
/**
* @Groups({"teamplay:read", "teamplay:read:inProgress"})
*/
public function isInProgress(): ?bool
{
$today = new \DateTime("now");
if ($this->getDateStart() <= $today && $this->getDateEnd() > $today) {
return true;
}
return false;
}
/**
* @Groups({"teamplay:read", "teamplay:read:inComing"})
*/
public function isInComing(): ?bool
{
$today = new \DateTime("now");
if ($this->getDateStart() > $today) {
return true;
}
return false;
}
/**
* @Groups({"teamplay:read", "teamplay:read:passed"})
*/
public function isPassed(): ?bool
{
$today = new \DateTime("now");
if ($this->getDateEnd() < $today) {
return true;
}
return false;
}
public function getTemplate(): ?Teamplay
{
return $this->template;
}
/**
* @Groups({"teamplay:write"})
*/
public function setTemplate(Teamplay $template): self
{
$this->template = $template;
return $this;
}
/**
* @return Collection|TeamplayLog[]
*/
public function getTeamplayLogs(): Collection
{
return $this->teamplayLogs;
}
public function addTeamplayLog(TeamplayLog $teamplayLog): self
{
if (!$this->teamplayLogs->contains($teamplayLog)) {
$this->teamplayLogs[] = $teamplayLog;
$teamplayLog->setTeamplay($this);
}
return $this;
}
public function removeTeamplayLog(TeamplayLog $teamplayLog): self
{
if ($this->teamplayLogs->removeElement($teamplayLog)) {
// set the owning side to null (unless already changed)
if ($teamplayLog->getTeamplay() === $this) {
$teamplayLog->setTeamplay(null);
}
}
return $this;
}
public function getAssociation(): ?Association
{
return $this->association;
}
public function setAssociation(?Association $association): self
{
$this->association = $association;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getActiveTeamsCount(): ?int
{
if ($this->teams->count() > 0) {
foreach ($this->teams as $team) {
if ($team->isActive()) {
$this->activeTeamsCount++;
}
}
}
return $this->activeTeamsCount;
}
public function setActiveTeamsCount(?int $activeTeamsCount): self
{
$this->activeTeamsCount = $activeTeamsCount;
return $this;
}
public function getTeamsCount(): ?int
{
$this->teamsCount = $this->teams->count();
return $this->teamsCount;
}
public function setTeamsCount(?int $teamsCount): self
{
$this->teamsCount = $teamsCount;
return $this;
}
}