src/Entity/Objective.php line 74

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\Serializer\Filter\GroupFilter;
  7. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\OrderFilter;
  8. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
  9. use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\BooleanFilter;
  10. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  11. use App\Repository\ObjectiveRepository;
  12. use Doctrine\Common\Collections\ArrayCollection;
  13. use Doctrine\Common\Collections\Collection;
  14. use Doctrine\ORM\Mapping as ORM;
  15. use Gedmo\Mapping\Annotation as Gedmo;
  16. use Symfony\Component\HttpFoundation\File\UploadedFile;
  17. use Symfony\Component\Serializer\Annotation\Groups;
  18. use Symfony\Component\Validator\Constraints as Assert;
  19. /**
  20.  * @ORM\Entity(repositoryClass=ObjectiveRepository::class)
  21.  * @ApiResource(
  22.  *      normalizationContext={
  23.  *          "groups"={"objective:read"}
  24.  *      },
  25.  *      denormalizationContext={
  26.  *          "groups"={"objective:write"}
  27.  *      },
  28.  *      collectionOperations={
  29.  *          "get"={
  30.  *              "security"="is_granted('ROLE_USER')"
  31.  *          },
  32.  *          "post"={
  33.  *              "security"="is_granted('ROLE_ADMIN')"
  34.  *          },
  35.  *          "post_update"={
  36.  *              "security"="is_granted('ROLE_ADMIN')",
  37.  *              "path"="/objectives/{id}",
  38.  *              "description"="Update an Objective with method POST (using content-type: 'multipart')",
  39.  *              "method"="POST",
  40.  *              "controller"="App\Controller\Api\ObjectiveController::update"
  41.  *          }
  42.  *      },
  43.  *      itemOperations={
  44.  *          "get"={
  45.  *              "security"="is_granted('ROLE_USER')"
  46.  *          }, 
  47.  *          "delete"={
  48.  *              "security"="is_granted('ROLE_ADMIN')"
  49.  *          }, 
  50.  *          "patch"={
  51.  *              "security"="is_granted('ROLE_ADMIN')"
  52.  *          }
  53.  *      }
  54.  * )
  55.  * @ApiFilter(OrderFilter::class, properties={"id", "name", "active", "created_at", "updated_at"})
  56.  * @ApiFilter(GroupFilter::class, 
  57.  *      arguments={
  58.  *          "parameterName": "groups", 
  59.  *          "overrideDefaultGroups": true
  60.  *      }
  61.  * )
  62.  * @ApiFilter(SearchFilter::class, properties={"id":"exact", "name":"partial", "active":"exact", "users.id":"exact"})
  63.  * @ApiFilter(BooleanFilter::class, properties={"active", "isRecommendedObjective"})
  64.  * @ApiFilter(PropertyFilter::class, 
  65.  *      arguments={
  66.  *          "parameterName"="fields", 
  67.  *          "overrideDefaultProperties"=true
  68.  *     }
  69.  * )
  70.  */
  71. class Objective
  72. {
  73.     /**
  74.      * @ORM\Id
  75.      * @ORM\GeneratedValue
  76.      * @ORM\Column(type="integer")
  77.      * @Groups({"objective:read", "homepage:read", "stats", "channel:read", "search_engine", "slide:read", "objective_list", 
  78.      *      "objective_form_complete", "survey:read", "user:read", "client:read"})
  79.      */
  80.     private $id;
  81.     /**
  82.      * @ORM\Column(type="string", length=255)
  83.      * @Groups({"objective:read", "objective:write", "search_engine", "channel_list", "objective_list", 
  84.      *      "objective_form_complete", "survey:read", "user:read", "client:read"})
  85.      * @Assert\NotNull(message="objective.name")
  86.      * @Assert\NotBlank(message="objective.name")
  87.      */
  88.     private $name;
  89.     /**
  90.      * @ORM\Column(type="text")
  91.      * @Groups({"objective:read", "objective:write", "objective_form_complete"})
  92.      */
  93.     private $content '';
  94.     /**
  95.      * @ORM\ManyToOne(targetEntity=MediaObject::class)
  96.      * @Groups({"objective:read", "objective:write", "stats", "objective_form_complete", "user:read", "client:read"})
  97.      */
  98.     private $picture null;
  99.     /**
  100.      * @ORM\ManyToOne(targetEntity=MediaObject::class)
  101.      * @Groups({"objective:read", "objective:write", "objective_form_complete", "user:read", "client:read"})
  102.      */
  103.     private $illustration null;
  104.     /**
  105.      * @ORM\Column(type="boolean", options={"default": "1"})
  106.      * @Groups({"objective:read", "objective:write", "objective_list", "objective_form_complete", "user:read", "client:read"})
  107.      */
  108.     private $active true;
  109.     /**
  110.      * @ORM\Column(type="datetime")
  111.      * @Gedmo\Timestampable(on="update")
  112.      * @Groups({"objective:read"})
  113.      */
  114.     private $updatedAt;
  115.     /**
  116.      * @ORM\Column(type="datetime")
  117.      * @Gedmo\Timestampable(on="create")
  118.      * @Groups({"objective:read"})
  119.      */
  120.     private $createdAt;
  121.     /**
  122.      * @Groups({"objective:read", "video:read"})
  123.      * @ApiSubresource(maxDepth=1)
  124.      */
  125.     private $videos;
  126.     /**
  127.      * @ORM\ManyToMany(targetEntity=Program::class, inversedBy="objectives")
  128.      * @Groups({"objective:read", "objective_form_complete"})
  129.      * @ApiSubresource(maxDepth=1)
  130.      */
  131.     private $programs;
  132.     /**
  133.      * @ORM\ManyToMany(targetEntity=Channel::class, inversedBy="objectives")
  134.      * @Groups({"objective:read", "objective_form_complete"})
  135.      * @ApiSubresource(maxDepth=1)
  136.      */
  137.     private $channels;
  138.     /**
  139.      * @ORM\OneToOne(targetEntity=Playlist::class, cascade={"persist", "remove"})
  140.      * @ORM\JoinColumn(nullable=false)
  141.      * @Groups({"objective:read", "objective:write", "objective_form_complete"})
  142.      * @ApiSubresource(maxDepth=1)
  143.      */
  144.     private $playlist;
  145.     /**
  146.      * @ORM\ManyToMany(targetEntity=TvUser::class, inversedBy="objectives")
  147.      */
  148.     private $tvUsers;
  149.     /**
  150.      * @ORM\ManyToMany(targetEntity=User::class, inversedBy="objectives")
  151.      */
  152.     private $users;
  153.     /**
  154.      * @ORM\Column(type="boolean", options={"default": "0"})
  155.      * @Groups({"objective:read", "objective:write", "user:read", "client:read"})
  156.      */
  157.     private $isRecommendedObjective false;
  158.     /**
  159.      * Cutsom field
  160.      * Si c'est un ROLE_CLIENT, permet de savoir si l'objectif a été selectionner par celui-ci
  161.      * @Groups({"objective:read", "user:read", "client:read"})
  162.      */
  163.     public $isSelectedByUser null;
  164.     public function __construct()
  165.     {
  166.         $this->programs = new ArrayCollection();
  167.         $this->channels = new ArrayCollection();
  168.         $this->videos = new ArrayCollection();
  169.         $this->playlist = new Playlist();
  170.         $this->tvUsers = new ArrayCollection();
  171.         $this->users = new ArrayCollection();
  172.     }
  173.     public function getId(): ?int
  174.     {
  175.         return $this->id;
  176.     }
  177.     public function getName(): ?string
  178.     {
  179.         return $this->name;
  180.     }
  181.     public function setName(string $name): self
  182.     {
  183.         $this->name $name;
  184.         return $this;
  185.     }
  186.     public function getContent(): ?string
  187.     {
  188.         return $this->content;
  189.     }
  190.     public function setContent(string $content): self
  191.     {
  192.         $this->content $content;
  193.         return $this;
  194.     }
  195.     public function getPicture(): ?MediaObject
  196.     {
  197.         return $this->picture;
  198.     }
  199.     public function setPicture(?MediaObject $picture): self
  200.     {
  201.         $this->picture $picture;
  202.         return $this;
  203.     }
  204.     /**
  205.      * @Groups({"objective:write"})
  206.      */
  207.     public function setPictureFile($file null): self
  208.     {
  209.         if($file instanceof UploadedFile) {
  210.             $picture = empty($this->picture) ? new MediaObject $this->picture;
  211.             $picture->setFile($file);
  212.             $this->setPicture($picture);
  213.         }
  214.         return $this;
  215.     }
  216.     public function getIllustration(): ?MediaObject
  217.     {
  218.         return $this->illustration;
  219.     }
  220.     public function setIllustration(?MediaObject $illustration): self
  221.     {
  222.         $this->illustration $illustration;
  223.         return $this;
  224.     }
  225.     /**
  226.      * @Groups({"objective:write"})
  227.      */
  228.     public function setIllustrationFile($file null): self
  229.     {
  230.         if($file instanceof UploadedFile) {
  231.             $illustration = empty($this->illustration) ? new MediaObject $this->illustration;
  232.             $illustration->setFile($file);
  233.             $this->setIllustration($illustration);
  234.         }
  235.         return $this;
  236.     }
  237.     public function getActive(): ?bool
  238.     {
  239.         return $this->active;
  240.     }
  241.     public function setActive(bool $active): self
  242.     {
  243.         $this->active $active;
  244.         return $this;
  245.     }
  246.     public function getUpdatedAt(): ?\DateTimeInterface
  247.     {
  248.         return $this->updatedAt;
  249.     }
  250.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  251.     {
  252.         $this->updatedAt $updatedAt;
  253.         return $this;
  254.     }
  255.     public function getCreatedAt(): ?\DateTimeInterface
  256.     {
  257.         return $this->createdAt;
  258.     }
  259.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  260.     {
  261.         $this->createdAt $createdAt;
  262.         return $this;
  263.     }
  264.     /**
  265.      * @return Collection|Program[]
  266.      * @Groups({"objective_form_complete"})
  267.      */
  268.     public function getPrograms(): Collection
  269.     {
  270.         return $this->programs;
  271.     }
  272.     /**
  273.      * @Groups({"objective:write"})
  274.      */
  275.     public function setPrograms(Collection $programs): self
  276.     {
  277.         $this->programs $programs;
  278.         return $this;
  279.     }
  280.     public function addProgram(Program $program): self
  281.     {
  282.         if (!$this->programs->contains($program)) {
  283.             $this->programs[] = $program;
  284.             $program->addObjective($this);
  285.         }
  286.         return $this;
  287.     }
  288.     
  289.     public function removeProgram(Program $program): self
  290.     {
  291.         if ($this->programs->removeElement($program)) {
  292.             $program->removeObjective($this);
  293.         }
  294.         return $this;
  295.     }
  296.     /**
  297.      * @param Program $program
  298.      */
  299.     public function setProgramAdd(Program $program): self
  300.     {
  301.         return $this->addProgram($program);
  302.     }
  303.     /**
  304.      * @param Program $program
  305.      */
  306.     public function setProgramRemove(Program $program): self
  307.     {
  308.         return $this->removeProgram($program);
  309.     }
  310.     /**
  311.      * @var Collection|Program[]
  312.      * @return Collection|Program[]
  313.      */
  314.     public function setProgramsRemove(array $programs): self
  315.     {
  316.         foreach ($programs as $program) {
  317.             if($program instanceof Program) {
  318.                 $this->removeProgram($program);
  319.             }
  320.         }
  321.         return $this;
  322.     }
  323.     /**
  324.      * @return Collection|Channel[]
  325.      * @Groups({"objective_form_complete"})
  326.      */
  327.     public function getChannels(): Collection
  328.     {
  329.         return $this->channels;
  330.     }
  331.     /**
  332.      * @Groups({"objective:write"})
  333.      */
  334.     public function setChannels(Collection $channels): self
  335.     {
  336.         $this->channels $channels;
  337.         return $this;
  338.     }
  339.     public function addChannel(Channel $channel): self
  340.     {
  341.         if (!$this->channels->contains($channel)) {
  342.             $this->channels[] = $channel;
  343.             $channel->addObjective($this);
  344.         }
  345.         return $this;
  346.     }
  347.     
  348.     public function removeChannel(Channel $channel): self
  349.     {
  350.         if ($this->channels->removeElement($channel)) {
  351.             $channel->removeObjective($this);
  352.         }
  353.         return $this;
  354.     }
  355.     /**
  356.      * @param Channel $channel
  357.      */
  358.     public function setChannelAdd(Channel $channel): self
  359.     {
  360.         return $this->addChannel($channel);
  361.     }
  362.     /**
  363.      * @var Collection|Channel[]
  364.      * @return Collection|Channel[]
  365.      */
  366.     public function setChannelsAdd(array $channels): self
  367.     {
  368.         foreach ($channels as $channel) {
  369.             if($channel instanceof Channel) {
  370.                 $this->addChannel($channel);
  371.             }
  372.         }
  373.         return $this;
  374.     }
  375.     /**
  376.      * @param Channel $channel
  377.      */
  378.     public function setChannelRemove(Channel $channel): self
  379.     {
  380.         return $this->removeChannel($channel);
  381.     }
  382.     /**
  383.      * @var Collection|Channel[]
  384.      * @return Collection|Channel[]
  385.      */
  386.     public function setChannelsRemove(array $channels): self
  387.     {
  388.         foreach ($channels as $channel) {
  389.             if($channel instanceof Channel) {
  390.                 $this->removeChannel($channel);
  391.             }
  392.         }
  393.         return $this;
  394.     }
  395.     /**
  396.      * @return Collection|Video[]
  397.      */
  398.     public function getVideos(): Collection
  399.     {
  400.         $this->videos $this->playlist->getVideos();
  401.         
  402.         return $this->videos;
  403.     }
  404.     public function setVideos(array $videos): self
  405.     {
  406.         $this->playlist->setVideos($videos);
  407.         return $this;
  408.     }
  409.     /**
  410.      * @param Video $video
  411.      * @Groups({"objective:write"})
  412.      */
  413.     public function setVideoAdd(Video $video): self
  414.     {
  415.         if (!$this->playlist->getVideos()->contains($video)) {
  416.             $this->playlist->addVideo($video);
  417.         }
  418.         return $this;
  419.     }
  420.     /**
  421.      * @param Video $video
  422.      * @Groups({"objective:write"})
  423.      */
  424.     public function setVideoRemove(Video $video): self
  425.     {
  426.         $this->playlist->removeVideo($video);
  427.         return $this;
  428.     }
  429.     /**
  430.      * @Groups({"objective_form_complete"})
  431.      */
  432.     public function getPlaylist(): ?Playlist
  433.     {
  434.         return $this->playlist;
  435.     }
  436.     public function setPlaylist(Playlist $playlist): self
  437.     {
  438.         $this->playlist $playlist;
  439.         return $this;
  440.     }
  441.     /**
  442.      * @return Collection|TvUser[]
  443.      */
  444.     public function getTvUsers(): Collection
  445.     {
  446.         return $this->tvUsers;
  447.     }
  448.     public function addTvUser(TvUser $tvUser): self
  449.     {
  450.         if (!$this->tvUsers->contains($tvUser)) {
  451.             $this->tvUsers[] = $tvUser;
  452.         }
  453.         return $this;
  454.     }
  455.     public function removeTvUser(TvUser $tvUser): self
  456.     {
  457.         $this->tvUsers->removeElement($tvUser);
  458.         return $this;
  459.     }
  460.     /**
  461.      * @return Collection|User[]
  462.      */
  463.     public function getUsers(): Collection
  464.     {
  465.         return $this->users;
  466.     }
  467.     public function addUser(User $user): self
  468.     {
  469.         if (!$this->users->contains($user)) {
  470.             $this->users[] = $user;
  471.         }
  472.         return $this;
  473.     }
  474.     public function removeUser(User $user): self
  475.     {
  476.         $this->users->removeElement($user);
  477.         return $this;
  478.     }
  479.     public function getIsRecommendedObjective(): ?bool
  480.     {
  481.         return $this->isRecommendedObjective;
  482.     }
  483.     public function setIsRecommendedObjective(bool $isRecommendedObjective): self
  484.     {
  485.         $this->isRecommendedObjective $isRecommendedObjective;
  486.         return $this;
  487.     }
  488. }