src/Entity/Teamplay.php line 86

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use ApiPlatform\Core\Annotation\ApiFilter;
  4. use ApiPlatform\Core\Annotation\ApiResource;
  5. use ApiPlatform\Core\Annotation\ApiSubresource;
  6. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\BooleanFilter;
  7. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\DateFilter;
  8. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  9. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  10. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\ExistsFilter;
  11. use ApiPlatform\Core\Serializer\Filter\GroupFilter;
  12. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  13. use App\Repository\TeamplayRepository;
  14. use Doctrine\Common\Collections\ArrayCollection;
  15. use Doctrine\Common\Collections\Collection;
  16. use Doctrine\ORM\Mapping as ORM;
  17. use Gedmo\Mapping\Annotation as Gedmo;
  18. use Symfony\Component\HttpFoundation\File\UploadedFile;
  19. use Symfony\Component\Serializer\Annotation\Groups;
  20. use Symfony\Component\Validator\Constraints as Assert;
  21. /**
  22.  * @ORM\Entity(repositoryClass=TeamplayRepository::class)
  23.  * @ApiResource(
  24.  *      normalizationContext={
  25.  *          "groups"={"teamplay:read"}
  26.  *      },
  27.  *      denormalizationContext={
  28.  *          "groups"={"teamplay:write"}
  29.  *      },
  30.  *      itemOperations={
  31.  *          "get"={
  32.  *              "security"="is_granted('ROLE_USER')"
  33.  *          }, 
  34.  *          "patch"={
  35.  *              "security"="is_granted('ROLE_ADMIN')"
  36.  *          },
  37.  *          "delete"={
  38.  *              "security"="is_granted('ROLE_ADMIN')"
  39.  *          }
  40.  *      },
  41.  *      collectionOperations={
  42.  *          "get"={
  43.  *              "security"="is_granted('ROLE_USER')"
  44.  *          }, 
  45.  *          "post"={
  46.  *             "security"="is_granted('ROLE_ADMIN')",
  47.  *          },
  48.  *          "post_update"={
  49.  *              "security"="is_granted('ROLE_ADMIN')",
  50.  *              "path"="/teamplays/{id}",
  51.  *              "description"="Update a Teamplay with method POST (using content-type: 'multipart')",
  52.  *              "method"="POST",
  53.  *              "controller"="App\Controller\Api\TeamplayController::update"
  54.  *          },
  55.  *          "get_teamplay_in_progress_by_company"={
  56.  *              "security"="is_granted('ROLE_USER')",
  57.  *              "path"="/companies/{id}/teamplay-in-progress",
  58.  *              "method"="GET",
  59.  *              "description"="Get  the teamplay in progress for this company.",
  60.  *              "controller"="App\Controller\Api\TeamplayController::getTeamplayInProgress"
  61.  *          }
  62.  *      }
  63.  * )
  64.  * @ApiFilter(SearchFilter::class, strategy="exact",
  65.  *      properties={"active", "name": "partial", "company.id", "isTemplate", "association.id"}
  66.  * )
  67.  * @ApiFilter(BooleanFilter::class, properties={"active", "isTemplate"})
  68.  * @ApiFilter(DateFilter::class, properties={"dateStart", "dateEnd"})
  69.  * @ApiFilter(OrderFilter::class, properties={"id", "name", "company.name", "dateStart", "dateEnd", "duration"})
  70.  * @ApiFilter(ExistsFilter::class, properties={"company", "association"})
  71.  * @ApiFilter(GroupFilter::class, arguments={
  72.  *      "parameterName": "groups", 
  73.  *      "overrideDefaultGroups": true
  74.  * })
  75.  * @ApiFilter(PropertyFilter::class, 
  76.  *      arguments={
  77.  *          "parameterName"="fields", 
  78.  *          "overrideDefaultProperties"=true
  79.  *     }
  80.  * )
  81.  */
  82. class Teamplay
  83. {
  84.     /**
  85.      * @ORM\Id
  86.      * @ORM\GeneratedValue
  87.      * @ORM\Column(type="integer")
  88.      * @Groups({"teamplay:read", "teamplay_form", "teamplay:read:id"})
  89.      */
  90.     private $id;
  91.     /**
  92.      * @ORM\Column(type="string", length=255, nullable=true)
  93.      * @Groups({"teamplay:read", "teamplay:write", "teamplay_form", "teamplay:read:name"})
  94.      */
  95.     private $name;
  96.     /**
  97.      * @ORM\Column(type="integer", nullable=true)
  98.      * @Groups({"teamplay:read", "teamplay:write", "company_planning_form", "teamplay:read:duration"})
  99.      */
  100.     private $duration;
  101.     /**
  102.      * @ORM\ManyToOne(targetEntity=MediaObject::class, cascade={"persist", "refresh", "remove"})
  103.      * @Groups({"teamplay:read", "teamplay:write", "teamplay_form", "teamplay:read:picture"})
  104.      */
  105.     private $picture;
  106.     /**
  107.      * @ORM\Column(type="datetime", nullable=true)
  108.      * @Groups({"teamplay:read", "teamplay:write", "teamplay:read:dateStart"})
  109.      */
  110.     private $dateStart;
  111.     /**
  112.      * @ORM\Column(type="datetime", nullable=true)
  113.      * @Groups({"teamplay:read", "teamplay:write", "teamplay:read:dateEnd"})
  114.      */
  115.     private $dateEnd;
  116.     /**
  117.      * @ORM\Column(type="boolean", nullable=true, options={"default": "1"})
  118.      * @Assert\NotNull
  119.      * @Groups({"teamplay:read", "teamplay:write", "teamplay:read:isTemplate"})
  120.      */
  121.     private $isTemplate true;
  122.     /**
  123.      * @ORM\ManyToOne(targetEntity=TvCompany::class, inversedBy="teamplays")
  124.      */
  125.     private $tvCompany;
  126.     /**
  127.      * @ORM\ManyToOne(targetEntity=Company::class, inversedBy="teamplays")
  128.      * @Groups({"teamplay:read", "teamplay:write", "teamplay:read:company"})
  129.      */
  130.     private $company;
  131.     /**
  132.      * @ORM\OneToMany(targetEntity=TeamplayChallenge::class, mappedBy="teamplay", orphanRemoval=true, cascade={"persist", "refresh", "remove"})
  133.      * @Groups({"teamplay:read", "teamplay:write", "teamplay:read:challenges"})
  134.      * @ApiSubresource(maxDepth=1)
  135.      */
  136.     private $challenges;
  137.     /**
  138.      * @ORM\Column(type="boolean", nullable=true, options={"default": "1"})
  139.      * @Assert\NotNull
  140.      * @Groups({"teamplay:read", "teamplay:write", "teamplay:read:active"})
  141.      */
  142.     private $active true;
  143.     /**
  144.      * @ORM\Column(type="datetime", options={"default": "CURRENT_TIMESTAMP"})
  145.      * @Gedmo\Timestampable(on="create")
  146.      * @Groups({"teamplay:read", "teamplay:read:createdAt"})
  147.      */
  148.     private $createdAt;
  149.     /**
  150.      * @ORM\Column(type="datetime", nullable=true)
  151.      * @Gedmo\Timestampable(on="update")
  152.      * @Groups({"teamplay:read", "teamplay:read:updatedAt"})
  153.      */
  154.     private $updatedAt;
  155.     /**
  156.      * @Groups({"teamplay:write", "teamplay:read:template"})
  157.      */
  158.     private $template null;
  159.     /**
  160.      * @ORM\OneToMany(targetEntity=TeamplayLog::class, mappedBy="teamplay", orphanRemoval=true, cascade={"persist", "refresh", "remove"})
  161.      */
  162.     private $teamplayLogs;
  163.     /**
  164.      * @ORM\ManyToOne(targetEntity=Association::class, inversedBy="teamplays")
  165.      * @Groups({"teamplay:read", "teamplay:write", "teamplay:read:association"})
  166.      */
  167.     private $association;
  168.     /**
  169.      * @ORM\Column(type="text", nullable=true)
  170.      * @Groups({"teamplay:read", "teamplay:write", "teamplay_form", "teamplay:read:description"})
  171.      */
  172.     private $description;
  173.     /**
  174.      * @ORM\OneToMany(targetEntity=Team::class, mappedBy="teamplay", orphanRemoval=true, cascade={"persist", "refresh", "remove"})
  175.      * @Groups({"teamplay:read", "teamplay:read:teams"})
  176.      */
  177.     private $teams;
  178.     /**
  179.      * Champ CUSTOM
  180.      * Permet de récupérer le nombre total d'équipe active
  181.      * @Groups({"teamplay:read", "teamplay:read:activeTeamsCount"})
  182.      */
  183.     private $activeTeamsCount null;
  184.     /**
  185.      * Champ CUSTOM
  186.      * Permet de récupérer le nombre total d'équipe active ou non
  187.      * @Groups({"teamplay:read", "teamplay:read:teamsCount"})
  188.      */
  189.     private $teamsCount null;
  190.     public $isPlannable null;
  191.     public function __construct()
  192.     {
  193.         $this->challenges = new ArrayCollection();
  194.         $this->teamplayLogs = new ArrayCollection();
  195.         $this->teams = new ArrayCollection();
  196.     }
  197.     public function getId(): ?int
  198.     {
  199.         return $this->id;
  200.     }
  201.     public function setId(?int $id): self
  202.     {
  203.         $this->id $id;
  204.         return $this;
  205.     }
  206.     public function getName(): ?string
  207.     {
  208.         return $this->name;
  209.     }
  210.     public function setName(?string $name): self
  211.     {
  212.         $this->name $name;
  213.         return $this;
  214.     }
  215.     public function getDuration(): ?int
  216.     {
  217.         return $this->duration;
  218.     }
  219.     public function setDuration(?int $duration): self
  220.     {
  221.         $this->duration $duration;
  222.         return $this;
  223.     }
  224.     public function getPicture(): ?MediaObject
  225.     {
  226.         return $this->picture;
  227.     }
  228.     public function setPicture(?MediaObject $picture null): self
  229.     {
  230.         $this->picture $picture;
  231.         return $this;
  232.     }
  233.     /**
  234.      * @Groups({"teamplay:write"})
  235.      */
  236.     public function setPictureFile($file null): self
  237.     {
  238.         if ($file instanceof UploadedFile) {
  239.             $picture = empty($this->picture) ? new MediaObject $this->picture;
  240.             $picture->setFile($file);
  241.             $this->setPicture($picture);
  242.         }
  243.         return $this;
  244.     }
  245.     public function getDateStart(): ?\DateTimeInterface
  246.     {
  247.         return $this->dateStart;
  248.     }
  249.     public function setDateStart(?\DateTimeInterface $dateStart): self
  250.     {
  251.         $this->dateStart $dateStart;
  252.         return $this;
  253.     }
  254.     public function getDateEnd(): ?\DateTimeInterface
  255.     {
  256.         return $this->dateEnd;
  257.     }
  258.     public function setDateEnd(?\DateTimeInterface $dateEnd): self
  259.     {
  260.         $this->dateEnd $dateEnd;
  261.         return $this;
  262.     }
  263.     public function getTvCompany(): ?TvCompany
  264.     {
  265.         return $this->tvCompany;
  266.     }
  267.     public function setTvCompany(?TvCompany $tvCompany): self
  268.     {
  269.         $this->tvCompany $tvCompany;
  270.         return $this;
  271.     }
  272.     public function getCompany(): ?Company
  273.     {
  274.         return $this->company;
  275.     }
  276.     public function setCompany(?Company $company): self
  277.     {
  278.         $this->company $company;
  279.         return $this;
  280.     }
  281.     public function getCreatedAt(): ?\DateTimeInterface
  282.     {
  283.         return $this->createdAt;
  284.     }
  285.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  286.     {
  287.         $this->createdAt $createdAt;
  288.         return $this;
  289.     }
  290.     public function getUpdatedAt(): ?\DateTimeInterface
  291.     {
  292.         return $this->updatedAt;
  293.     }
  294.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
  295.     {
  296.         $this->updatedAt $updatedAt;
  297.         return $this;
  298.     }
  299.     /**
  300.      * Permet de savoir si c'est une modèle
  301.      * Critères d'un modèle :
  302.      * - Ne pas être lié à une TvCompany 
  303.      * @return bool
  304.      */
  305.     public function isTemplate(): ?bool
  306.     {
  307.         return $this->getIsTemplate();
  308.     }
  309.     public function getIsTemplate(): ?bool
  310.     {
  311.         return $this->isTemplate;
  312.     }
  313.     public function setIsTemplate(?bool $isTemplate): self
  314.     {
  315.         $this->isTemplate $isTemplate;
  316.         return $this;
  317.     }
  318.     /**
  319.      * @return Collection|TeamplayChallenge[]
  320.      */
  321.     public function getChallenges(): Collection
  322.     {
  323.         if (empty($this->challenges)) {
  324.             $this->challenges = new ArrayCollection;
  325.         }
  326.         return $this->challenges;
  327.     }
  328.     public function addChallenge(?TeamplayChallenge $challenge null): self
  329.     {
  330.         if ($challenge instanceof TeamplayChallenge) {
  331.             if (!$this->challenges->contains($challenge)) {
  332.                 $this->challenges->add($challenge);
  333.                 $challenge->setTeamplay($this);
  334.             }
  335.         }
  336.         return $this;
  337.     }
  338.     /**
  339.      * @Groups({"teamplay:write"})
  340.      */
  341.     public function setAddChallenge(?TeamplayChallenge $challenge null): self
  342.     {
  343.         if ($challenge instanceof TeamplayChallenge) {
  344.             $this->addChallenge($challenge);
  345.         }
  346.         return $this;
  347.     }
  348.     public function removeChallenge(TeamplayChallenge $challenge): self
  349.     {
  350.         if ($this->challenges->removeElement($challenge)) {
  351.             // set the owning side to null (unless already changed)
  352.             if ($challenge->getTeamplay() === $this) {
  353.                 $challenge->setTeamplay(null);
  354.             }
  355.         }
  356.         return $this;
  357.     }
  358.     /**
  359.      * @return Collection|Team[]
  360.      */
  361.     public function getTeams(): Collection
  362.     {
  363.         return $this->teams;
  364.     }
  365.     /**
  366.      * @Groups({"teamplay:write"})
  367.      */
  368.     public function setAddTeam(?Team $team null): self
  369.     {
  370.         if ($team instanceof Team) {
  371.             if (!$this->teams->contains($team)) {
  372.                 $this->teams[] = $team;
  373.                 $team->setTeamplay($this);
  374.             }
  375.         }
  376.         return $this;
  377.     }
  378.     public function removeTeam(Team $team): self
  379.     {
  380.         if ($this->teams->removeElement($team)) {
  381.             // set the owning side to null (unless already changed)
  382.             if ($team->getTeamplay() === $this) {
  383.                 $team->setTeamplay(null);
  384.             }
  385.         }
  386.         return $this;
  387.     }
  388.     public function getActive(): ?bool
  389.     {
  390.         return $this->active;
  391.     }
  392.     public function isActive(): ?bool
  393.     {
  394.         return $this->active;
  395.     }
  396.     public function setActive(?bool $active): self
  397.     {
  398.         $this->active $active;
  399.         return $this;
  400.     }
  401.     /**
  402.      * @Groups({"teamplay:read", "teamplay:read:inProgress"})
  403.      */
  404.     public function isInProgress(): ?bool
  405.     {
  406.         $today = new \DateTime("now");
  407.         if ($this->getDateStart() <= $today && $this->getDateEnd() > $today) {
  408.             return true;
  409.         }
  410.         return false;
  411.     }
  412.     /**
  413.      * @Groups({"teamplay:read", "teamplay:read:inComing"})
  414.      */
  415.     public function isInComing(): ?bool
  416.     {
  417.         $today = new \DateTime("now");
  418.         if ($this->getDateStart() > $today) {
  419.             return true;
  420.         }
  421.         return false;
  422.     }
  423.     /**
  424.      * @Groups({"teamplay:read", "teamplay:read:passed"})
  425.      */
  426.     public function isPassed(): ?bool
  427.     {
  428.         $today = new \DateTime("now");
  429.         if ($this->getDateEnd() < $today) {
  430.             return true;
  431.         }
  432.         return false;
  433.     }
  434.     public function getTemplate(): ?Teamplay
  435.     {
  436.         return $this->template;
  437.     }
  438.     /**
  439.      * @Groups({"teamplay:write"})
  440.      */
  441.     public function setTemplate(Teamplay $template): self
  442.     {
  443.         $this->template $template;
  444.         return $this;
  445.     }
  446.     /**
  447.      * @return Collection|TeamplayLog[]
  448.      */
  449.     public function getTeamplayLogs(): Collection
  450.     {
  451.         return $this->teamplayLogs;
  452.     }
  453.     public function addTeamplayLog(TeamplayLog $teamplayLog): self
  454.     {
  455.         if (!$this->teamplayLogs->contains($teamplayLog)) {
  456.             $this->teamplayLogs[] = $teamplayLog;
  457.             $teamplayLog->setTeamplay($this);
  458.         }
  459.         return $this;
  460.     }
  461.     public function removeTeamplayLog(TeamplayLog $teamplayLog): self
  462.     {
  463.         if ($this->teamplayLogs->removeElement($teamplayLog)) {
  464.             // set the owning side to null (unless already changed)
  465.             if ($teamplayLog->getTeamplay() === $this) {
  466.                 $teamplayLog->setTeamplay(null);
  467.             }
  468.         }
  469.         return $this;
  470.     }
  471.     public function getAssociation(): ?Association
  472.     {
  473.         return $this->association;
  474.     }
  475.     public function setAssociation(?Association $association): self
  476.     {
  477.         $this->association $association;
  478.         return $this;
  479.     }
  480.     public function getDescription(): ?string
  481.     {
  482.         return $this->description;
  483.     }
  484.     public function setDescription(?string $description): self
  485.     {
  486.         $this->description $description;
  487.         return $this;
  488.     }
  489.     public function getActiveTeamsCount(): ?int
  490.     {
  491.         if ($this->teams->count() > 0) {
  492.             foreach ($this->teams as $team) {
  493.                 if ($team->isActive()) {
  494.                     $this->activeTeamsCount++;
  495.                 }
  496.             }
  497.         }
  498.         return $this->activeTeamsCount;
  499.     }
  500.     public function setActiveTeamsCount(?int $activeTeamsCount): self
  501.     {
  502.         $this->activeTeamsCount $activeTeamsCount;
  503.         return $this;
  504.     }
  505.     public function getTeamsCount(): ?int
  506.     {
  507.         $this->teamsCount $this->teams->count();
  508.         return $this->teamsCount;
  509.     }
  510.     public function setTeamsCount(?int $teamsCount): self
  511.     {
  512.         $this->teamsCount $teamsCount;
  513.         return $this;
  514.     }
  515. }