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.     /**
  191.      * Champ CUSTOM
  192.      * Nombre total de TeamUsers liés au Teamplay
  193.      * @Groups({"teamplay:read"})
  194.      */
  195.     private $totalTeamUsers = null;
  196.     public $isPlannable = null;
  197.     public function __construct()
  198.     {
  199.         $this->challenges = new ArrayCollection();
  200.         $this->teamplayLogs = new ArrayCollection();
  201.         $this->teams = new ArrayCollection();
  202.     }
  203.     public function getId(): ?int
  204.     {
  205.         return $this->id;
  206.     }
  207.     public function setId(?int $id): self
  208.     {
  209.         $this->id = $id;
  210.         return $this;
  211.     }
  212.     public function getName(): ?string
  213.     {
  214.         return $this->name;
  215.     }
  216.     public function setName(?string $name): self
  217.     {
  218.         $this->name = $name;
  219.         return $this;
  220.     }
  221.     public function getDuration(): ?int
  222.     {
  223.         return $this->duration;
  224.     }
  225.     public function setDuration(?int $duration): self
  226.     {
  227.         $this->duration = $duration;
  228.         return $this;
  229.     }
  230.     public function getPicture(): ?MediaObject
  231.     {
  232.         return $this->picture;
  233.     }
  234.     public function setPicture(?MediaObject $picture = null): self
  235.     {
  236.         $this->picture = $picture;
  237.         return $this;
  238.     }
  239.     /**
  240.      * @Groups({"teamplay:write"})
  241.      */
  242.     public function setPictureFile($file = null): self
  243.     {
  244.         if ($file instanceof UploadedFile) {
  245.             $picture = empty($this->picture) ? new MediaObject : $this->picture;
  246.             $picture->setFile($file);
  247.             $this->setPicture($picture);
  248.         }
  249.         return $this;
  250.     }
  251.     public function getDateStart(): ?\DateTimeInterface
  252.     {
  253.         return $this->dateStart;
  254.     }
  255.     public function setDateStart(?\DateTimeInterface $dateStart): self
  256.     {
  257.         $this->dateStart = $dateStart;
  258.         return $this;
  259.     }
  260.     public function getDateEnd(): ?\DateTimeInterface
  261.     {
  262.         return $this->dateEnd;
  263.     }
  264.     public function setDateEnd(?\DateTimeInterface $dateEnd): self
  265.     {
  266.         $this->dateEnd = $dateEnd;
  267.         return $this;
  268.     }
  269.     public function getTvCompany(): ?TvCompany
  270.     {
  271.         return $this->tvCompany;
  272.     }
  273.     public function setTvCompany(?TvCompany $tvCompany): self
  274.     {
  275.         $this->tvCompany = $tvCompany;
  276.         return $this;
  277.     }
  278.     public function getCompany(): ?Company
  279.     {
  280.         return $this->company;
  281.     }
  282.     public function setCompany(?Company $company): self
  283.     {
  284.         $this->company = $company;
  285.         return $this;
  286.     }
  287.     public function getCreatedAt(): ?\DateTimeInterface
  288.     {
  289.         return $this->createdAt;
  290.     }
  291.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  292.     {
  293.         $this->createdAt = $createdAt;
  294.         return $this;
  295.     }
  296.     public function getUpdatedAt(): ?\DateTimeInterface
  297.     {
  298.         return $this->updatedAt;
  299.     }
  300.     public function setUpdatedAt(?\DateTimeInterface $updatedAt): self
  301.     {
  302.         $this->updatedAt = $updatedAt;
  303.         return $this;
  304.     }
  305.     /**
  306.      * Permet de savoir si c'est une modèle
  307.      * Critères d'un modèle :
  308.      * - Ne pas être lié à une TvCompany 
  309.      * @return bool
  310.      */
  311.     public function isTemplate(): ?bool
  312.     {
  313.         return $this->getIsTemplate();
  314.     }
  315.     public function getIsTemplate(): ?bool
  316.     {
  317.         return $this->isTemplate;
  318.     }
  319.     public function setIsTemplate(?bool $isTemplate): self
  320.     {
  321.         $this->isTemplate = $isTemplate;
  322.         return $this;
  323.     }
  324.     /**
  325.      * @return Collection|TeamplayChallenge[]
  326.      */
  327.     public function getChallenges(): Collection
  328.     {
  329.         if (empty($this->challenges)) {
  330.             $this->challenges = new ArrayCollection;
  331.         }
  332.         return $this->challenges;
  333.     }
  334.     public function addChallenge(?TeamplayChallenge $challenge = null): self
  335.     {
  336.         if ($challenge instanceof TeamplayChallenge) {
  337.             if (!$this->challenges->contains($challenge)) {
  338.                 $this->challenges->add($challenge);
  339.                 $challenge->setTeamplay($this);
  340.             }
  341.         }
  342.         return $this;
  343.     }
  344.     /**
  345.      * @Groups({"teamplay:write"})
  346.      */
  347.     public function setAddChallenge(?TeamplayChallenge $challenge = null): self
  348.     {
  349.         if ($challenge instanceof TeamplayChallenge) {
  350.             $this->addChallenge($challenge);
  351.         }
  352.         return $this;
  353.     }
  354.     public function removeChallenge(TeamplayChallenge $challenge): self
  355.     {
  356.         if ($this->challenges->removeElement($challenge)) {
  357.             // set the owning side to null (unless already changed)
  358.             if ($challenge->getTeamplay() === $this) {
  359.                 $challenge->setTeamplay(null);
  360.             }
  361.         }
  362.         return $this;
  363.     }
  364.     /**
  365.      * @return Collection|Team[]
  366.      */
  367.     public function getTeams(): Collection
  368.     {
  369.         return $this->teams;
  370.     }
  371.     /**
  372.      * @Groups({"teamplay:write"})
  373.      */
  374.     public function setAddTeam(?Team $team = null): self
  375.     {
  376.         if ($team instanceof Team) {
  377.             if (!$this->teams->contains($team)) {
  378.                 $this->teams[] = $team;
  379.                 $team->setTeamplay($this);
  380.             }
  381.         }
  382.         return $this;
  383.     }
  384.     public function removeTeam(Team $team): self
  385.     {
  386.         if ($this->teams->removeElement($team)) {
  387.             // set the owning side to null (unless already changed)
  388.             if ($team->getTeamplay() === $this) {
  389.                 $team->setTeamplay(null);
  390.             }
  391.         }
  392.         return $this;
  393.     }
  394.     public function getActive(): ?bool
  395.     {
  396.         return $this->active;
  397.     }
  398.     public function isActive(): ?bool
  399.     {
  400.         return $this->active;
  401.     }
  402.     public function setActive(?bool $active): self
  403.     {
  404.         $this->active = $active;
  405.         return $this;
  406.     }
  407.     /**
  408.      * @Groups({"teamplay:read", "teamplay:read:inProgress"})
  409.      */
  410.     public function isInProgress(): ?bool
  411.     {
  412.         $today = new \DateTime("now");
  413.         if ($this->getDateStart() <= $today && $this->getDateEnd() > $today) {
  414.             return true;
  415.         }
  416.         return false;
  417.     }
  418.     /**
  419.      * @Groups({"teamplay:read", "teamplay:read:inComing"})
  420.      */
  421.     public function isInComing(): ?bool
  422.     {
  423.         $today = new \DateTime("now");
  424.         if ($this->getDateStart() > $today) {
  425.             return true;
  426.         }
  427.         return false;
  428.     }
  429.     /**
  430.      * @Groups({"teamplay:read", "teamplay:read:passed"})
  431.      */
  432.     public function isPassed(): ?bool
  433.     {
  434.         $today = new \DateTime("now");
  435.         if ($this->getDateEnd() < $today) {
  436.             return true;
  437.         }
  438.         return false;
  439.     }
  440.     public function getTemplate(): ?Teamplay
  441.     {
  442.         return $this->template;
  443.     }
  444.     /**
  445.      * @Groups({"teamplay:write"})
  446.      */
  447.     public function setTemplate(Teamplay $template): self
  448.     {
  449.         $this->template = $template;
  450.         return $this;
  451.     }
  452.     /**
  453.      * @return Collection|TeamplayLog[]
  454.      */
  455.     public function getTeamplayLogs(): Collection
  456.     {
  457.         return $this->teamplayLogs;
  458.     }
  459.     public function addTeamplayLog(TeamplayLog $teamplayLog): self
  460.     {
  461.         if (!$this->teamplayLogs->contains($teamplayLog)) {
  462.             $this->teamplayLogs[] = $teamplayLog;
  463.             $teamplayLog->setTeamplay($this);
  464.         }
  465.         return $this;
  466.     }
  467.     public function removeTeamplayLog(TeamplayLog $teamplayLog): self
  468.     {
  469.         if ($this->teamplayLogs->removeElement($teamplayLog)) {
  470.             // set the owning side to null (unless already changed)
  471.             if ($teamplayLog->getTeamplay() === $this) {
  472.                 $teamplayLog->setTeamplay(null);
  473.             }
  474.         }
  475.         return $this;
  476.     }
  477.     public function getAssociation(): ?Association
  478.     {
  479.         return $this->association;
  480.     }
  481.     public function setAssociation(?Association $association): self
  482.     {
  483.         $this->association = $association;
  484.         return $this;
  485.     }
  486.     public function getDescription(): ?string
  487.     {
  488.         return $this->description;
  489.     }
  490.     public function setDescription(?string $description): self
  491.     {
  492.         $this->description = $description;
  493.         return $this;
  494.     }
  495.     public function getActiveTeamsCount(): ?int
  496.     {
  497.         if ($this->teams->count() > 0) {
  498.             foreach ($this->teams as $team) {
  499.                 if ($team->isActive()) {
  500.                     $this->activeTeamsCount++;
  501.                 }
  502.             }
  503.         }
  504.         return $this->activeTeamsCount;
  505.     }
  506.     public function setActiveTeamsCount(?int $activeTeamsCount): self
  507.     {
  508.         $this->activeTeamsCount = $activeTeamsCount;
  509.         return $this;
  510.     }
  511.     public function getTeamsCount(): ?int
  512.     {
  513.         $this->teamsCount = $this->teams->count();
  514.         return $this->teamsCount;
  515.     }
  516.     public function setTeamsCount(?int $teamsCount): self
  517.     {
  518.         $this->teamsCount = $teamsCount;
  519.         return $this;
  520.     }
  521.     public function getTotalTeamUsers(): ?int
  522.     {
  523.         if ($this->totalTeamUsers !== null) {
  524.             return $this->totalTeamUsers;
  525.         }
  526.         $count = 0;
  527.         foreach ($this->teams as $team) {
  528.             $count += $team->getTeamUsers()->count();
  529.         }
  530.         return $count;
  531.     }
  532.     public function setTotalTeamUsers(?int $totalTeamUsers): self
  533.     {
  534.         $this->totalTeamUsers = $totalTeamUsers;
  535.         return $this;
  536.     }
  537. }