src/Entity/Media.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use App\Repository\MediaRepository;
  7. use Gedmo\Mapping\Annotation as Gedmo;
  8. use Symfony\Component\Validator\Constraints as Assert;
  9. use Symfony\Component\HttpFoundation\File\File;
  10. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  11. /**
  12.  * @ORM\Entity(repositoryClass=MediaRepository::class)
  13.  * @Vich\Uploadable
  14.  */
  15. class Media
  16. {
  17.     const ROLES = [
  18.         // label => value (first is default)
  19.         "document" => "document",
  20.         "miniature" => "thumbnail",
  21.         "diapositive" => "slide",
  22.     ];
  23.     const TYPES = [
  24.         // label => value 
  25.         "article" => "article",
  26.         "évènement" => "event",
  27.         "newsletter" => "newsletter",
  28.         "page" => "page",
  29.         "question-rep" => "questionAnswer",
  30.         "structure" => "structure",
  31.         "thème" => "theme",
  32.     ];
  33.     
  34.     /**
  35.      * @ORM\Id()
  36.      * @ORM\GeneratedValue()
  37.      * @ORM\Column(type="integer")
  38.      */
  39.     private $id;
  40.     
  41.     /**
  42.      * @ORM\Column(type="integer", nullable=true)
  43.      */
  44.     protected $tmpOldId;
  45.     /**
  46.      * NOTE: This is not a mapped field of entity metadata, just a simple property.
  47.      * 
  48.      * @Vich\UploadableField(mapping="medias", fileNameProperty="name", size="size")
  49.      * 
  50.      * @var File|null
  51.      */
  52.     private $file;
  53.     /**
  54.      * @ORM\Column(type="string", length=200)
  55.      *
  56.      * @var string|null
  57.      */
  58.     private $name;
  59.     /**
  60.      * @ORM\Column(type="string")
  61.      *
  62.      * @var string|null
  63.      */
  64.     private $description "";
  65.     /**
  66.      * @ORM\Column(type="integer")
  67.      *
  68.      * @var int|null
  69.      */
  70.     private $size;
  71.     /**
  72.      * @ORM\Column(type="string", length=255, nullable=true)
  73.      *
  74.      * @var string|null
  75.      */
  76.     private $role;
  77.     /**
  78.      * @ORM\Column(type="array", length=255)
  79.      *
  80.      * @var array|null
  81.      */
  82.     private $types;
  83.             
  84.     /**
  85.      * @var \DateTime $createdAt
  86.      *
  87.      * @Gedmo\Timestampable(on="create")
  88.      * @ORM\Column(type="datetime", nullable=true)
  89.      */
  90.     private $createdAt;
  91.     /**
  92.      * @ORM\Column(type="datetime")
  93.      *
  94.      * @var \DateTimeInterface|null
  95.      */
  96.     private $updatedAt;
  97.     /* relations */
  98.     /**
  99.      * @ORM\ManyToMany(targetEntity="App\Entity\Article", inversedBy="medias", cascade={"persist"})
  100.      */
  101.     private $articles;
  102.     /**
  103.      * @ORM\ManyToMany(targetEntity="App\Entity\Event", inversedBy="medias", cascade={"persist"})
  104.      */
  105.     private $events;
  106.     /**
  107.      * @ORM\ManyToMany(targetEntity="App\Entity\Newsletter", inversedBy="medias", cascade={"persist"})
  108.      */
  109.     private $newsletters;
  110.     /**
  111.      * @ORM\ManyToMany(targetEntity="App\Entity\Page", inversedBy="medias", cascade={"persist"})
  112.      */
  113.     private $pages;
  114.     /**
  115.      * @ORM\ManyToMany(targetEntity="App\Entity\QuestionAnswer", inversedBy="medias", cascade={"persist"})
  116.      */
  117.     private $questionAnswers;
  118.     /**
  119.      * @ORM\ManyToMany(targetEntity="App\Entity\Structure", inversedBy="medias", cascade={"persist"})
  120.      */
  121.     private $structures;
  122.     /**
  123.      * @ORM\OneToOne(targetEntity="App\Entity\Theme", inversedBy="picto", cascade={"persist"})
  124.      * @ORM\JoinColumn(name="picto_theme", referencedColumnName="id")
  125.      */
  126.     private $theme;
  127.     public function __construct()
  128.     {
  129.         $this->updatedAt = new \DateTime();
  130.         $this->types = [];
  131.           $this->articles = new ArrayCollection();
  132.         $this->events = new ArrayCollection();
  133.         $this->newsletters = new ArrayCollection();
  134.         $this->pages = new ArrayCollection();
  135.         $this->questionAnswers = new ArrayCollection();
  136.         $this->structures = new ArrayCollection();
  137.     }
  138.     public function __toString()
  139.     {
  140.         return $this->name;
  141.     }
  142.     public static function getMimeType($ext){
  143.         $ext strtolower($ext);
  144.         if (!(strpos($ext'.') !== false)) {
  145.             $ext '.' $ext ;
  146.         }
  147.         switch ($ext) {
  148.             case '.aac'$mime ='audio/aac'; break; // AAC audio
  149.             case '.abw'$mime ='application/x-abiword'; break; // AbiWord document
  150.             case '.arc'$mime ='application/octet-stream'; break; // Archive document (multiple files embedded)
  151.             case '.avi'$mime ='video/x-msvideo'; break; // AVI: Audio Video Interleave
  152.             case '.azw'$mime ='application/vnd.amazon.ebook'; break; // Amazon Kindle eBook format
  153.             case '.bin'$mime ='application/octet-stream'; break; // Any kind of binary data
  154.             case '.bmp'$mime ='image/bmp'; break; // Windows OS/2 Bitmap Graphics
  155.             case '.bz'$mime ='application/x-bzip'; break; // BZip archive
  156.             case '.bz2'$mime ='application/x-bzip2'; break; // BZip2 archive
  157.             case '.csh'$mime ='application/x-csh'; break; // C-Shell script
  158.             case '.css'$mime ='text/css'; break; // Cascading Style Sheets (CSS)
  159.             case '.csv'$mime ='text/csv'; break; // Comma-separated values (CSV)
  160.             case '.doc'$mime ='application/msword'; break; // Microsoft Word
  161.             case '.docx'$mime ='application/vnd.openxmlformats-officedocument.wordprocessingml.document'; break; // Microsoft Word (OpenXML)
  162.             case '.eot'$mime ='application/vnd.ms-fontobject'; break; // MS Embedded OpenType fonts
  163.             case '.epub'$mime ='application/epub+zip'; break; // Electronic publication (EPUB)
  164.             case '.gif'$mime ='image/gif'; break; // Graphics Interchange Format (GIF)
  165.             case '.htm'$mime ='text/html'; break; // HyperText Markup Language (HTML)
  166.             case '.html'$mime ='text/html'; break; // HyperText Markup Language (HTML)
  167.             case '.ico'$mime ='image/x-icon'; break; // Icon format
  168.             case '.ics'$mime ='text/calendar'; break; // iCalendar format
  169.             case '.jar'$mime ='application/java-archive'; break; // Java Archive (JAR)
  170.             case '.jpeg'$mime ='image/jpeg'; break; // JPEG images
  171.             case '.jpg'$mime ='image/jpeg'; break; // JPEG images
  172.             case '.js'$mime ='application/javascript'; break; // JavaScript (IANA Specification) (RFC 4329 Section 8.2)
  173.             case '.json'$mime ='application/json'; break; // JSON format
  174.             case '.mid'$mime ='audio/midi audio/x-midi'; break; // Musical Instrument Digital Interface (MIDI)
  175.             case '.midi'$mime ='audio/midi audio/x-midi'; break; // Musical Instrument Digital Interface (MIDI)
  176.             case '.mpeg'$mime ='video/mpeg'; break; // MPEG Video
  177.             case '.mpkg'$mime ='application/vnd.apple.installer+xml'; break; // Apple Installer Package
  178.             case '.odp'$mime ='application/vnd.oasis.opendocument.presentation'; break; // OpenDocument presentation document
  179.             case '.ods'$mime ='application/vnd.oasis.opendocument.spreadsheet'; break; // OpenDocument spreadsheet document
  180.             case '.odt'$mime ='application/vnd.oasis.opendocument.text'; break; // OpenDocument text document
  181.             case '.oga'$mime ='audio/ogg'; break; // OGG audio
  182.             case '.ogv'$mime ='video/ogg'; break; // OGG video
  183.             case '.ogx'$mime ='application/ogg'; break; // OGG
  184.             case '.otf'$mime ='font/otf'; break; // OpenType font
  185.             case '.png'$mime ='image/png'; break; // Portable Network Graphics
  186.             case '.pdf'$mime ='application/pdf'; break; // Adobe Portable Document Format (PDF)
  187.             case '.ppt'$mime ='application/vnd.ms-powerpoint'; break; // Microsoft PowerPoint
  188.             case '.pptx'$mime ='application/vnd.openxmlformats-officedocument.presentationml.presentation'; break; // Microsoft PowerPoint (OpenXML)
  189.             case '.rar'$mime ='application/x-rar-compressed'; break; // RAR archive
  190.             case '.rtf'$mime ='application/rtf'; break; // Rich Text Format (RTF)
  191.             case '.sh'$mime ='application/x-sh'; break; // Bourne shell script
  192.             case '.svg'$mime ='image/svg+xml'; break; // Scalable Vector Graphics (SVG)
  193.             case '.swf'$mime ='application/x-shockwave-flash'; break; // Small web format (SWF) or Adobe Flash document
  194.             case '.tar'$mime ='application/x-tar'; break; // Tape Archive (TAR)
  195.             case '.tif'$mime ='image/tiff'; break; // Tagged Image File Format (TIFF)
  196.             case '.tiff'$mime ='image/tiff'; break; // Tagged Image File Format (TIFF)
  197.             case '.ts'$mime ='application/typescript'; break; // Typescript file
  198.             case '.ttf'$mime ='font/ttf'; break; // TrueType Font
  199.             case '.txt'$mime ='text/plain'; break; // Text, (generally ASCII or ISO 8859-n)
  200.             case '.vsd'$mime ='application/vnd.visio'; break; // Microsoft Visio
  201.             case '.wav'$mime ='audio/wav'; break; // Waveform Audio Format
  202.             case '.weba'$mime ='audio/webm'; break; // WEBM audio
  203.             case '.webm'$mime ='video/webm'; break; // WEBM video
  204.             case '.webp'$mime ='image/webp'; break; // WEBP image
  205.             case '.woff'$mime ='font/woff'; break; // Web Open Font Format (WOFF)
  206.             case '.woff2'$mime ='font/woff2'; break; // Web Open Font Format (WOFF)
  207.             case '.xhtml'$mime ='application/xhtml+xml'; break; // XHTML
  208.             case '.xls'$mime ='application/vnd.ms-excel'; break; // Microsoft Excel
  209.             case '.xlsx'$mime ='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; break; // Microsoft Excel (OpenXML)
  210.             case '.xml'$mime ='application/xml'; break; // XML
  211.             case '.xul'$mime ='application/vnd.mozilla.xul+xml'; break; // XUL
  212.             case '.zip'$mime ='application/zip'; break; // ZIP archive
  213.             case '.3gp'$mime ='video/3gpp'; break; // 3GPP audio/video container
  214.             case '.3g2'$mime ='video/3gpp2'; break; // 3GPP2 audio/video container
  215.             case '.7z'$mime ='application/x-7z-compressed'; break; // 7-zip archive
  216.             default: $mime 'application/octet-stream' // general purpose MIME-type
  217.         }
  218.         return $mime ;    
  219.     }
  220.     public function getId(): ?int
  221.     {
  222.         return $this->id;
  223.     }
  224.     public function setId($id)
  225.     {
  226.         $this->id $id;
  227.         return $this;
  228.     }
  229.     
  230.     public function getTmpOldId(): ?int
  231.     {
  232.         return $this->tmpOldId;
  233.     }
  234.     public function setTmpOldId(?int $tmpOldId): self
  235.     {
  236.         $this->tmpOldId $tmpOldId;
  237.         return $this;
  238.     }
  239.     /**
  240.      * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
  241.      * of 'UploadedFile' is injected into this setter to trigger the update. If this
  242.      * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
  243.      * must be able to accept an instance of 'File' as the bundle will inject one here
  244.      * during Doctrine hydration.
  245.      *
  246.      * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $file
  247.      */
  248.     public function setFile(?File $file null): void
  249.     {
  250.         $this->file $file;
  251.         
  252.         if (null !== $file) {
  253.             // It is required that at least one field changes if you are using doctrine
  254.             // otherwise the event listeners won't be called and the file is lost
  255.             $this->updatedAt = new \DateTimeImmutable();
  256.         }
  257.     }
  258.     public function getFile(): ?File
  259.     {
  260.         return $this->file;
  261.     }
  262.     public function setName(?string $name): void
  263.     {
  264.         $this->name $name;
  265.     }
  266.     public function getName(): ?string
  267.     {
  268.         return $this->name;
  269.     }
  270.     public function setDescription(?string $description): void
  271.     {
  272.         $this->description $description;
  273.     }
  274.     public function getDescription(): ?string
  275.     {
  276.         if($this->description !== "") {
  277.             return $this->description;
  278.         } else {
  279.             return $this->name;
  280.         }
  281.     }
  282.     
  283.     public function setSize(?int $size): void
  284.     {
  285.         $this->size $size;
  286.     }
  287.     public function getSize(): ?int
  288.     {
  289.         return $this->size;
  290.     }
  291.     public function getCreatedAt(): ?\DateTimeInterface
  292.     {
  293.         return $this->createdAt;
  294.     }
  295.     public function setCreatedAt(?\DateTimeInterface $createdAt): self
  296.     {
  297.         $this->createdAt $createdAt;
  298.         return $this;
  299.     }
  300.     public function getUpdatedAt(): ?\DateTimeInterface
  301.     {
  302.         return $this->updatedAt;
  303.     }
  304.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  305.     {
  306.         $this->updatedAt $updatedAt;
  307.         return $this;
  308.     }
  309.     public function getRole(): ?string
  310.     {
  311.         return $this->role;
  312.     }
  313.     public function setRole(string $role): self
  314.     {
  315.         if(! in_array($roleself::ROLES) ) $role array_values(self::ROLES)[0];
  316.         $this->role $role;
  317.         return $this;
  318.     }
  319.     public function getTypes(): ?array
  320.     {
  321.         return $this->types;
  322.     }
  323.     public function setTypes(array $types): self
  324.     {
  325.         $this->types $types;
  326.         return $this;
  327.     }
  328.     
  329.     /**
  330.      * @return Collection|Article[]
  331.      */
  332.     public function getArticles(): Collection
  333.     {
  334.         return $this->articles;
  335.     }
  336.     public function addArticle(Article $article): self
  337.     {
  338.         if (!$this->articles->contains($article)) {
  339.             $this->articles[] = $article;
  340.         }
  341.         $type "article";
  342.         if (! in_array($type$this->types) ) {
  343.             $this->types[] = $type;
  344.         }
  345.         return $this;
  346.     }
  347.     public function removeArticle(Article $article): self
  348.     {
  349.         if ($this->articles->contains($article)) {
  350.             $this->articles->removeElement($article);
  351.         }
  352.         return $this;
  353.     }
  354.     /**
  355.      * @return Collection|Event[]
  356.      */
  357.     public function getEvents(): Collection
  358.     {
  359.         return $this->events;
  360.     }
  361.     public function addEvent(Event $event): self
  362.     {
  363.         if (!$this->events->contains($event)) {
  364.             $this->events[] = $event;
  365.         }
  366.         $type "event";
  367.         if (! in_array($type$this->types) ) {
  368.             $this->types[] = $type;
  369.         }
  370.         return $this;
  371.     }
  372.     public function removeEvent(Event $event): self
  373.     {
  374.         if ($this->events->contains($event)) {
  375.             $this->events->removeElement($event);
  376.         }
  377.         return $this;
  378.     }
  379.     /**
  380.      * @return Collection|Newsletter[]
  381.      */
  382.     public function getNewsletters(): Collection
  383.     {
  384.         return $this->newsletters;
  385.     }
  386.     public function addNewsletter(Newsletter $newsletter): self
  387.     {
  388.         if (!$this->newsletters->contains($newsletter)) {
  389.             $this->newsletters[] = $newsletter;
  390.         }
  391.         $type "newsletter";
  392.         if (! in_array($type$this->types) ) {
  393.             $this->types[] = $type;
  394.         }
  395.         return $this;
  396.     }
  397.     public function removeNewsletter(Newsletter $newsletter): self
  398.     {
  399.         if ($this->newsletters->contains($newsletter)) {
  400.             $this->newsletters->removeElement($newsletter);
  401.         }
  402.         return $this;
  403.     }
  404.     /**
  405.      * @return Collection|Page[]
  406.      */
  407.     public function getPages(): Collection
  408.     {
  409.         return $this->pages;
  410.     }
  411.     public function addPage(Page $page): self
  412.     {
  413.         if (!$this->pages->contains($page)) {
  414.             $this->pages[] = $page;
  415.         }
  416.         $type "page";
  417.         if (! in_array($type$this->types) ) {
  418.             $this->types[] = $type;
  419.         }
  420.         return $this;
  421.     }
  422.     public function removePage(Page $page): self
  423.     {
  424.         if ($this->pages->contains($page)) {
  425.             $this->pages->removeElement($page);
  426.         }
  427.         return $this;
  428.     }
  429.     /**
  430.      * @return Collection|QuestionAnswer[]
  431.      */
  432.     public function getQuestionAnswers(): Collection
  433.     {
  434.         return $this->questionAnswers;
  435.     }
  436.     public function addQuestionAnswer(QuestionAnswer $questionAnswer): self
  437.     {
  438.         if (!$this->questionAnswers->contains($questionAnswer)) {
  439.             $this->questionAnswers[] = $questionAnswer;
  440.         }
  441.         $type "questionAnswer";
  442.         if (! in_array($type$this->types) ) {
  443.             $this->types[] = $type;
  444.         }
  445.         return $this;
  446.     }
  447.     public function removeQuestionAnswer(QuestionAnswer $questionAnswer): self
  448.     {
  449.         if ($this->questionAnswers->contains($questionAnswer)) {
  450.             $this->questionAnswers->removeElement($questionAnswer);
  451.         }
  452.         return $this;
  453.     }
  454.     /**
  455.      * @return Collection|Structure[]
  456.      */
  457.     public function getStructures(): Collection
  458.     {
  459.         return $this->structures;
  460.     }
  461.     public function addStructure(Structure $structure): self
  462.     {
  463.         if (!$this->structures->contains($structure)) {
  464.             $this->structures[] = $structure;
  465.         }
  466.         $type "structure";
  467.         if (! in_array($type$this->types) ) {
  468.             $this->types[] = $type;
  469.         }
  470.         return $this;
  471.     }
  472.     public function removeStructure(Structure $structure): self
  473.     {
  474.         if ($this->structures->contains($structure)) {
  475.             $this->structures->removeElement($structure);
  476.         }
  477.         return $this;
  478.     }
  479.     public function getTheme(): ?Theme
  480.     {
  481.         return $this->theme;
  482.     }
  483.     public function setTheme(?Theme $theme): self
  484.     {
  485.         $this->theme $theme;
  486.         $type "theme";
  487.         if (! in_array($type$this->types) ) {
  488.             $this->types[] = $type;
  489.         }
  490.         return $this;
  491.     }
  492. }