src/Entity/Slide.php line 98

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\Bridge\Doctrine\Orm\Filter\SearchFilter;
  6. use ApiPlatform\Core\Serializer\Filter\PropertyFilter;
  7. use App\Annotation\CompanyCustomizable;
  8. use App\Repository\SlideRepository;
  9. use Doctrine\Common\Collections\ArrayCollection;
  10. use Doctrine\Common\Collections\Collection;
  11. use Doctrine\ORM\Mapping as ORM;
  12. use Gedmo\Mapping\Annotation as Gedmo;
  13. use Symfony\Component\HttpFoundation\File\UploadedFile;
  14. use Symfony\Component\Serializer\Annotation\Groups;
  15. use Symfony\Component\Validator\Constraints as Assert;
  16. /**
  17.  * @ORM\Entity(repositoryClass=SlideRepository::class)
  18.  * @ORM\HasLifecycleCallbacks
  19.  * @ApiResource(
  20.  *     normalizationContext={
  21.  *         "groups"={"slide:read"}
  22.  *     },
  23.  *     denormalizationContext={
  24.  *          "groups"={"slide:write"}
  25.  *     },
  26.  *     collectionOperations={
  27.  *          "get"={
  28.  *              "security"="is_granted('ROLE_ADMIN') or is_granted('ROLE_COMPANY')"
  29.  *          },
  30.  *          "post"={
  31.  *              "security"="is_granted('ROLE_ADMIN') or is_granted('ROLE_COMPANY')"
  32.  *          },
  33.  *          "get_channel_slides"={
  34.  *              "path"="/channels/{channel}/slides",
  35.  *              "method"="GET",
  36.  *              "controller"="App\Controller\Api\SlideController::getByChannel",
  37.  *              "security"="is_granted('ROLE_USER')"
  38.  *          },
  39.  *          "get_by_slug"={
  40.  *              "path"="/slides_page/{slug}",
  41.  *              "requirements"={"slug"=".*"},
  42.  *              "method"="GET",
  43.  *              "controller"="App\Controller\Api\SlideController::getBySlug",
  44.  *              "openapi_context"={
  45.  *                  "summary"="Fetches slides by slug (aka: page)",
  46.  *                  "parameters"={
  47.  *                      {
  48.  *                          "name"="slug",
  49.  *                          "in"="path",
  50.  *                          "description"="Slug to find",
  51.  *                          "required"=true,
  52.  *                          "schema"={
  53.  *                              "type"="string",
  54.  *                              "enum"=self::TYPES
  55.  *                          },
  56.  *                      },
  57.  *                  },
  58.  *              },
  59.  *          },
  60.  *          "get_slides_by_company"={
  61.  *              "path"="/companies/{company}/slides",
  62.  *              "method"="GET",
  63.  *              "controller"="App\Controller\Api\SlideController::getSlidesByCompany",
  64.  *              "security"="is_granted('ROLE_USER') or is_granted('ROLE_COMPANY') or is_granted('ROLE_ADMIN')"
  65.  *          },
  66.  *          "post_update"={
  67.  *              "security"="is_granted('ROLE_ADMIN') or is_granted('ROLE_COMPANY')",
  68.  *              "path"="/slides/{id}",
  69.  *              "description"="Update a Slide with method POST (using content-type: 'multipart')",
  70.  *              "method"="POST",
  71.  *              "controller"="App\Controller\Api\SlideController::update"
  72.  *          }
  73.  *     },
  74.  *     itemOperations={
  75.  *          "get"={
  76.  *              "security"="is_granted('ROLE_USER')"
  77.  *          },
  78.  *          "delete"={
  79.  *              "security"="is_granted('ROLE_ADMIN') or is_granted('ROLE_COMPANY')"
  80.  *          },
  81.  *          "patch"={
  82.  *              "security"="is_granted('ROLE_ADMIN') or is_granted('ROLE_COMPANY')"
  83.  *          }
  84.  *     }
  85.  * )
  86.  * @CompanyCustomizable
  87.  * @ApiFilter(SearchFilter::class, properties={"type"})
  88.  * @ApiFilter(PropertyFilter::class, 
  89.  *      arguments={
  90.  *          "parameterName"="fields", 
  91.  *          "overrideDefaultProperties"=true
  92.  *     }
  93.  * )
  94.  */
  95. class Slide
  96. {
  97.     const LOGIN_TYPE 'login_page';
  98.     const HOMEPAGE_TYPE 'homepage_slide';
  99.     const CHANNEL_TYPE 'channel_slide';
  100.     const TYPES = [
  101.         self::LOGIN_TYPE,
  102.         self::HOMEPAGE_TYPE,
  103.         self::CHANNEL_TYPE
  104.     ];
  105.     /**
  106.      * @ORM\Id
  107.      * @ORM\GeneratedValue
  108.      * @ORM\Column(type="integer")
  109.      * @Groups({"slide:read", "slide:read:id"})
  110.      */
  111.     private $id;
  112.     /**
  113.      * @ORM\Column(type="string", length=255)
  114.      * @Groups({"slide:read", "slide:write", "slide_slug:read", "slide:read:name"})
  115.      */
  116.     private $name;
  117.     /**
  118.      * @ORM\Column(type="boolean", options={"default": "1"})
  119.      * @Groups({"slide:read", "slide:write", "slide:read:active"})
  120.      */
  121.     private $active true;
  122.     /**
  123.      * @ORM\Column(type="datetime")
  124.      * @Gedmo\Timestampable(on="create")
  125.      * @Groups({"slide:read", "slide:read:createdAt"})
  126.      */
  127.     private $createdAt;
  128.     /**
  129.      * @ORM\Column(type="datetime", nullable=true)
  130.      * @Gedmo\Timestampable(on="update")
  131.      * @Groups({"slide:read", "slide:read:updatedAt"})
  132.      */
  133.     private $updatedAt;
  134.     /**
  135.      * Order on the slides, auto increment or any if the order is not important
  136.      * @Assert\NotBlank
  137.      * @ORM\Column(name="`rank`", type="integer")
  138.      * @Groups({"slide:read", "slide:write", "slide:read:rank"})
  139.      */
  140.     private $rank 1;
  141.     /**
  142.      * This is used for the customisation in the login page
  143.      * @ORM\ManyToOne(targetEntity=MediaObject::class)
  144.      * @Groups({"slide:read", "slide:write", "slide_slug:read", "slide:read:image"})
  145.      */
  146.     private $image;
  147.     /**
  148.      * Defines the origin of the slide in witch page will it be displayed
  149.      * (ex: a type 'homepage_slide' is a slide to be displayed in an HP)
  150.      * @ORM\Column(type="string", length=255, nullable=true)
  151.      * @Groups({"slide:read", "slide:write", "slide:read:type"})
  152.      * @Assert\Choice(choices=self::TYPES)
  153.      */
  154.     private $type;
  155.     /**
  156.      * This can be either the channel it is linked to if type is homepage
  157.      * (ex: one of the HP slides but it puts in the spot a particular channel),
  158.      * or the channel it belong to (example: this is the 5th slider in the "XXX" channel)
  159.      * @ORM\ManyToOne(targetEntity=Channel::class, inversedBy="slides")
  160.      * @Groups({"slide:read", "slide:write", "slide_slug:read", "slide:read:channel"})
  161.      */
  162.     private $channel;
  163.     /**
  164.      * This is a video to be put on the spot in this slide
  165.      * @ORM\ManyToOne(targetEntity=Video::class)
  166.      * @Groups({"slide:read", "slide:write", "slide_slug:read", "slide:read:video"})
  167.      */
  168.     private $video;
  169.     /**
  170.      * Label of the button to be displayed
  171.      * @ORM\Column(type="string", length=255, nullable=true)
  172.      * @Groups({"slide:read", "slide:write", "slide_slug:read", "slide:read:linkLabel"})
  173.      */
  174.     private $linkLabel;
  175.     /**
  176.      * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="slides")
  177.      * @Groups({"slide:read", "slide:write", "slide_slug:read", "slide:read:category"})
  178.      */
  179.     private $category;
  180.     /**
  181.      * @ORM\ManyToOne(targetEntity=Program::class, inversedBy="slides")
  182.      * @Groups({"slide:read", "slide:write", "slide_slug:read", "slide:read:program"})
  183.      */
  184.     private $program;
  185.     /**
  186.      * @ORM\ManyToOne(targetEntity=TvCompany::class, inversedBy="slides")
  187.      */
  188.     private $tvCompany;
  189.     /**
  190.      * @ORM\ManyToOne(targetEntity=Company::class, inversedBy="slides")
  191.      * @Groups({"slide:read", "slide:write", "slide_slug:read", "slide:read:company"})
  192.      */
  193.     private $company;
  194.     /**
  195.      * @ORM\ManyToOne(targetEntity=Slide::class, inversedBy="children")
  196.      */
  197.     private $parent;
  198.     /**
  199.      * @ORM\OneToMany(targetEntity=Slide::class, mappedBy="parent")
  200.      */
  201.     private $children;
  202.     /**
  203.      * @ORM\Column(type="string", length=255, nullable=true)
  204.      * @Groups({"slide:read", "slide:write", "slide_slug:read", "slide:read:linkUrl"})
  205.      */
  206.     private $linkUrl;
  207.     /**
  208.      * @ORM\Column(type="string", length=500, nullable=true)
  209.      * @Groups({"slide:read", "slide:write", "slide_slug:read", "slide:read:content"})
  210.      */
  211.     private $content;
  212.     public function __construct()
  213.     {
  214.         $this->children = new ArrayCollection();
  215.         $this->rank 1;
  216.     }
  217.     public function getId(): ?int
  218.     {
  219.         return $this->id;
  220.     }
  221.     public function setId(?int $id): self
  222.     {
  223.         $this->id $id;
  224.         return $this;
  225.     }
  226.     public function getName(): ?string
  227.     {
  228.         return $this->name;
  229.     }
  230.     public function setName(string $name): self
  231.     {
  232.         $this->name $name;
  233.         return $this;
  234.     }
  235.     public function getActive(): ?bool
  236.     {
  237.         return $this->active;
  238.     }
  239.     public function setActive(bool $active): self
  240.     {
  241.         $this->active $active;
  242.         return $this;
  243.     }
  244.     public function getCreatedAt(): ?\DateTimeInterface
  245.     {
  246.         return $this->createdAt;
  247.     }
  248.     public function setCreatedAt(\DateTimeInterface $createdAt): self
  249.     {
  250.         $this->createdAt $createdAt;
  251.         return $this;
  252.     }
  253.     public function getUpdatedAt(): ?\DateTimeInterface
  254.     {
  255.         return $this->updatedAt;
  256.     }
  257.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  258.     {
  259.         $this->updatedAt $updatedAt;
  260.         return $this;
  261.     }
  262.     public function getRank(): ?int
  263.     {
  264.         return $this->rank;
  265.     }
  266.     public function setRank(int $rank): self
  267.     {
  268.         $this->rank $rank;
  269.         return $this;
  270.     }
  271.     public function getImage(): ?MediaObject
  272.     {
  273.         return $this->image;
  274.     }
  275.     public function setImage(?MediaObject $image): self
  276.     {
  277.         $this->image $image;
  278.         return $this;
  279.     }
  280.     /**
  281.      * @Groups({"slide:write"})
  282.      */
  283.     public function setImageFile($file null): self
  284.     {
  285.         if($file instanceof UploadedFile) {
  286.             $image = empty($this->image) ? new MediaObject $this->image;
  287.             $image->setFile($file);
  288.             $this->setImage($image);
  289.         }
  290.         return $this;
  291.     }
  292.     public function getVideo(): ?Video
  293.     {
  294.         return $this->video;
  295.     }
  296.     public function setVideo(?Video $video): self
  297.     {
  298.         $this->video $video;
  299.         return $this;
  300.     }
  301.     public function getType(): ?string
  302.     {
  303.         return $this->type;
  304.     }
  305.     public function setType(?string $type): self
  306.     {
  307.         $this->type $type;
  308.         return $this;
  309.     }
  310.     public function getChannel(): ?Channel
  311.     {
  312.         return $this->channel;
  313.     }
  314.     public function setChannel(?Channel $channel): self
  315.     {
  316.         $this->channel $channel;
  317.         return $this;
  318.     }
  319.     public function getLinkLabel(): ?string
  320.     {
  321.         return $this->linkLabel;
  322.     }
  323.     public function setLinkLabel(?string $linkLabel): self
  324.     {
  325.         $this->linkLabel $linkLabel;
  326.         return $this;
  327.     }
  328.     public function getCategory(): ?Category
  329.     {
  330.         return $this->category;
  331.     }
  332.     public function setCategory(?Category $category): self
  333.     {
  334.         $this->category $category;
  335.         return $this;
  336.     }
  337.     public function getProgram(): ?Program
  338.     {
  339.         return $this->program;
  340.     }
  341.     public function setProgram(?Program $program): self
  342.     {
  343.         $this->program $program;
  344.         return $this;
  345.     }
  346.     public function getTvCompany(): ?TvCompany
  347.     {
  348.         return $this->tvCompany;
  349.     }
  350.     public function setTvCompany(?TvCompany $tvCompany): self
  351.     {
  352.         $this->tvCompany $tvCompany;
  353.         return $this;
  354.     }
  355.     public function getCompany(): ?Company
  356.     {
  357.         return $this->company;
  358.     }
  359.     public function setCompany(?Company $company): self
  360.     {
  361.         $this->company $company;
  362.         return $this;
  363.     }
  364.     public function getParent(): ?self
  365.     {
  366.         return $this->parent;
  367.     }
  368.     public function setParent(?self $parent): self
  369.     {
  370.         $this->parent $parent;
  371.         return $this;
  372.     }
  373.     /**
  374.      * @return Collection|self[]
  375.      */
  376.     public function getChildren(): Collection
  377.     {
  378.         return $this->children;
  379.     }
  380.     public function addChild(self $child): self
  381.     {
  382.         if (!$this->children->contains($child)) {
  383.             $this->children[] = $child;
  384.             $child->setParent($this);
  385.         }
  386.         return $this;
  387.     }
  388.     public function removeChild(self $child): self
  389.     {
  390.         if ($this->children->removeElement($child)) {
  391.             // set the owning side to null (unless already changed)
  392.             if ($child->getParent() === $this) {
  393.                 $child->setParent(null);
  394.             }
  395.         }
  396.         return $this;
  397.     }
  398.     public function getLinkUrl(): ?string
  399.     {
  400.         return $this->linkUrl;
  401.     }
  402.     public function setLinkUrl(?string $linkUrl): self
  403.     {
  404.         $this->linkUrl $linkUrl;
  405.         return $this;
  406.     }
  407.     public function getContent(): ?string
  408.     {
  409.         return $this->content;
  410.     }
  411.     public function setContent(?string $content): self
  412.     {
  413.         $this->content $content;
  414.         return $this;
  415.     }
  416. }