src/Entity/Day.php line 75

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\OrderFilter;
  7. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  8. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  9. use ApiPlatform\Core\Serializer\Filter\GroupFilter;
  10. use App\Repository\DayRepository;
  11. use Doctrine\Common\Collections\ArrayCollection;
  12. use Doctrine\Common\Collections\Collection;
  13. use Doctrine\ORM\Mapping as ORM;
  14. use Symfony\Component\Serializer\Annotation\Groups;
  15. /**
  16.  * @ORM\Entity(repositoryClass=DayRepository::class)
  17.  * @ApiResource(
  18.  *     attributes={
  19.  *              "order"={"rank": "ASC"}
  20.  *     },
  21.  *     normalizationContext={
  22.  *         "groups"={"day:read"}
  23.  *     },
  24.  *     denormalizationContext={
  25.  *          "groups"={"day:write"}
  26.  *     },
  27.  *     collectionOperations={
  28.  *              "get"={"security"="is_granted('ROLE_USER')"},
  29.  *              "post"={"security"="is_granted('ROLE_ADMIN')"}
  30.  *     },
  31.  *     itemOperations={
  32.  *              "get"={"security"="is_granted('ROLE_USER')"},
  33.  *              "delete"={"security"="is_granted('ROLE_ADMIN')"},
  34.  *              "patch"={"security"="is_granted('ROLE_ADMIN')"},
  35.  *              "activate_program"={
  36.  *                  "method"="GET",
  37.  *                  "controller"="App\Controller\Api\DayController::activate",
  38.  *                  "path"="/days/{id}/activate",
  39.  *                  "security"="is_granted('ROLE_ADMIN')",
  40.  *                  "openapi_context"={"summary"="Activates a day"},
  41.  *              },
  42.  *              "deactivate_program"={
  43.  *                  "method"="GET",
  44.  *                  "controller"="App\Controller\Api\DayController::deactivate",
  45.  *                  "path"="/days/{id}/deactivate",
  46.  *                  "security"="is_granted('ROLE_ADMIN')",
  47.  *                  "openapi_context"={"summary"="Deactivates a day"},
  48.  *              }
  49.  *     },
  50.  *     subresourceOperations={
  51.  *          "api_programs_day_get_subresource"= {
  52.  *              "security"="has_role('ROLE_USER')",
  53.  *          }
  54.  *      }
  55.  * )
  56.  * @ApiFilter(OrderFilter::class, properties={"id", "rank", "active", "name"})
  57.  * @ApiFilter(SearchFilter::class, properties={
  58.  *      "name": "partial", "active", "rank", "program.id", "status", "freeDay"
  59.  * })
  60.  * @ApiFilter(PropertyFilter::class, 
  61.  *      arguments={
  62.  *          "parameterName"="fields", 
  63.  *          "overrideDefaultProperties"=true
  64.  *     }
  65.  * )
  66.  * @ApiFilter(GroupFilter::class, arguments={
  67.  *      "parameterName": "groups", 
  68.  *      "overrideDefaultGroups": true, 
  69.  *      "whitelist": {"program_single_list", "edit_day"}
  70.  * })
  71.  */
  72. class Day
  73. {
  74.     /**
  75.      * @ORM\Id
  76.      * @ORM\GeneratedValue
  77.      * @ORM\Column(type="integer")
  78.      * @Groups({"day:read", "program_single_list", "edit_day", "program:read"})
  79.      */
  80.     private $id;
  81.     /**
  82.      * @ORM\Column(type="string", length=255)
  83.      * @Groups({"day:read", "day:write", "program:read"})
  84.      */
  85.     private $name;
  86.     /**
  87.      * @ORM\Column(type="text")
  88.      * @Groups({"day:read", "day:write", "program_single_list", "edit_day", "program:read"})
  89.      */
  90.     private $content;
  91.     /**
  92.      * @ORM\Column(name="`rank`", type="integer")
  93.      * @Groups({"day:read", "day:write", "program:read"})
  94.      */
  95.     private $rank;
  96.     /**
  97.      * @ORM\Column(type="datetime")
  98.      * @Groups({"day:read", "program:read"})
  99.      */
  100.     private $createdAt;
  101.     /**
  102.      * @ORM\OneToOne(targetEntity=Playlist::class, cascade={"persist", "refresh", "remove"})
  103.      * @Groups({"day:read", "program_single_list", "program:read"})
  104.      * @ApiSubresource(maxDepth=1)
  105.      */
  106.     private $playlist;
  107.     /**
  108.      * @ORM\ManyToOne(targetEntity=Program::class, inversedBy="days")
  109.      * @ORM\JoinColumn(nullable=false)
  110.      * @Groups({"day:read", "edit_day"})
  111.      * @ApiSubresource(maxDepth=1)
  112.      */
  113.     private $program;
  114.     /**
  115.      * Status are shared with Program
  116.      * @ORM\Column(type="string", length=255)
  117.      * @Groups({"day:read", "program:read"})
  118.      */
  119.     private $status Program::STATUS_NEW;
  120.     /**
  121.      * @ORM\Column(type="boolean", options={"default": "0"})
  122.      * @Groups({"day:read", "day:write", "program_single_list", "edit_day", "program:read"})
  123.      */
  124.     private $freeDay false;
  125.     /**
  126.      * @ORM\OneToMany(targetEntity=DayEvent::class, mappedBy="day", orphanRemoval=true)
  127.      */
  128.     private $dayEvents;
  129.     /**
  130.      * Custom fields for ROLE_CLIENT
  131.      * @var bool Indicates if a day has been done by the tvUser
  132.      * @Groups({"day:read", "day:read:dayCompleted", "program:read"})
  133.      */
  134.     public $dayCompleted false;
  135.     /**
  136.      * Custom fields for ROLE_CLIENT
  137.      * @var int|null
  138.      * @Groups({"day:read", "day:read:videosCompletedCount", "program:read"})
  139.      */
  140.     public $videosCompletedCount null;
  141.     /**
  142.      * Custom fields ROLE_CLIENT
  143.      * @var int|null
  144.      * @Groups({"day:read", "day:read:activeVideosCount", "program:read"})
  145.      */
  146.     public $videosCount null;
  147.     /**
  148.      * Custom fields for ROLE_CLIENT
  149.      * @var Video|null
  150.      * @Groups({"day:read", "day:read:currentVideo", "program:read"})
  151.      */
  152.     public $currentVideo null;
  153.     public function __construct()
  154.     {
  155.         $this->createdAt = new \DateTime();
  156.         $this->playlist = new Playlist();
  157.         $this->dayEvents = new ArrayCollection();
  158.     }
  159.     public function __clone()
  160.     {
  161.         $this->id null;
  162.         $this->status Program::STATUS_NEW;
  163.         $this->playlist = clone $this->playlist;
  164.         $this->dayEvents = new ArrayCollection();
  165.         $this->createdAt = new \DateTime();
  166.     }
  167.     public function getId(): ?int
  168.     {
  169.         return $this->id;
  170.     }
  171.     public function setId(?int $id): self
  172.     {
  173.         $this->id $id;
  174.         return $this;
  175.     }
  176.     public function getName(): ?string
  177.     {
  178.         return $this->name;
  179.     }
  180.     public function setName(string $name): self
  181.     {
  182.         $this->name $name;
  183.         return $this;
  184.     }
  185.     public function getContent(): ?string
  186.     {
  187.         return $this->content;
  188.     }
  189.     public function setContent(string $content): self
  190.     {
  191.         $this->content $content;
  192.         return $this;
  193.     }
  194.     public function getRank(): ?int
  195.     {
  196.         return $this->rank;
  197.     }
  198.     public function setRank(int $rank): self
  199.     {
  200.         $this->rank $rank;
  201.         return $this;
  202.     }
  203.     public function getCreatedAt(): ?\DateTimeInterface
  204.     {
  205.         return $this->createdAt;
  206.     }
  207.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  208.     {
  209.         $this->createdAt $createdAt;
  210.         return $this;
  211.     }
  212.     public function getPlaylist(): ?Playlist
  213.     {
  214.         return $this->playlist;
  215.     }
  216.     public function setPlaylist(?Playlist $playlist): self
  217.     {
  218.         $this->playlist $playlist;
  219.         return $this;
  220.     }
  221.     public function getProgram(): ?Program
  222.     {
  223.         return $this->program;
  224.     }
  225.     public function setProgram(?Program $program): self
  226.     {
  227.         $this->program $program;
  228.         return $this;
  229.     }
  230.     public function getStatus(): ?string
  231.     {
  232.         return $this->status;
  233.     }
  234.     public function setStatus(string $status): self
  235.     {
  236.         $this->status $status;
  237.         return $this;
  238.     }
  239.     /**
  240.      * @param Video $video
  241.      * @Groups({"day:write"})
  242.      */
  243.     public function setVideoAdd(Video $video)
  244.     {
  245.         $this->playlist->addVideo($video);
  246.     }
  247.     /**
  248.      * @param Video $video
  249.      * @Groups({"day:write"})
  250.      */
  251.     public function setVideoRemove(Video $video)
  252.     {
  253.         $this->playlist->removeVideo($video);
  254.     }
  255.     public function getFreeDay(): ?bool
  256.     {
  257.         return $this->freeDay;
  258.     }
  259.     public function setFreeDay(bool $freeDay): self
  260.     {
  261.         $this->freeDay $freeDay;
  262.         return $this;
  263.     }
  264.     /**
  265.      * @return Collection|DayEvent[]
  266.      */
  267.     public function getDayEvents(): Collection
  268.     {
  269.         return $this->dayEvents;
  270.     }
  271.     public function addDayEvent(DayEvent $dayEvent): self
  272.     {
  273.         if (!$this->dayEvents->contains($dayEvent)) {
  274.             $this->dayEvents[] = $dayEvent;
  275.             $dayEvent->setDay($this);
  276.         }
  277.         return $this;
  278.     }
  279.     public function removeDayEvent(DayEvent $dayEvent): self
  280.     {
  281.         if ($this->dayEvents->removeElement($dayEvent)) {
  282.             // set the owning side to null (unless already changed)
  283.             if ($dayEvent->getDay() === $this) {
  284.                 $dayEvent->setDay(null);
  285.             }
  286.         }
  287.         return $this;
  288.     }
  289. }