<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use App\Repository\MediaRepository;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass=MediaRepository::class)
* @Vich\Uploadable
*/
class Media
{
const ROLES = [
// label => value (first is default)
"document" => "document",
"miniature" => "thumbnail",
"diapositive" => "slide",
];
const TYPES = [
// label => value
"article" => "article",
"évènement" => "event",
"newsletter" => "newsletter",
"page" => "page",
"question-rep" => "questionAnswer",
"structure" => "structure",
"thème" => "theme",
];
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="integer", nullable=true)
*/
protected $tmpOldId;
/**
* NOTE: This is not a mapped field of entity metadata, just a simple property.
*
* @Vich\UploadableField(mapping="medias", fileNameProperty="name", size="size")
*
* @var File|null
*/
private $file;
/**
* @ORM\Column(type="string", length=200)
*
* @var string|null
*/
private $name;
/**
* @ORM\Column(type="string")
*
* @var string|null
*/
private $description = "";
/**
* @ORM\Column(type="integer")
*
* @var int|null
*/
private $size;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*
* @var string|null
*/
private $role;
/**
* @ORM\Column(type="array", length=255)
*
* @var array|null
*/
private $types;
/**
* @var \DateTime $createdAt
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime", nullable=true)
*/
private $createdAt;
/**
* @ORM\Column(type="datetime")
*
* @var \DateTimeInterface|null
*/
private $updatedAt;
/* relations */
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Article", inversedBy="medias", cascade={"persist"})
*/
private $articles;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Event", inversedBy="medias", cascade={"persist"})
*/
private $events;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Newsletter", inversedBy="medias", cascade={"persist"})
*/
private $newsletters;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Page", inversedBy="medias", cascade={"persist"})
*/
private $pages;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\QuestionAnswer", inversedBy="medias", cascade={"persist"})
*/
private $questionAnswers;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Structure", inversedBy="medias", cascade={"persist"})
*/
private $structures;
/**
* @ORM\OneToOne(targetEntity="App\Entity\Theme", inversedBy="picto", cascade={"persist"})
* @ORM\JoinColumn(name="picto_theme", referencedColumnName="id")
*/
private $theme;
public function __construct()
{
$this->updatedAt = new \DateTime();
$this->types = [];
$this->articles = new ArrayCollection();
$this->events = new ArrayCollection();
$this->newsletters = new ArrayCollection();
$this->pages = new ArrayCollection();
$this->questionAnswers = new ArrayCollection();
$this->structures = new ArrayCollection();
}
public function __toString()
{
return $this->name;
}
public static function getMimeType($ext){
$ext = strtolower($ext);
if (!(strpos($ext, '.') !== false)) {
$ext = '.' . $ext ;
}
switch ($ext) {
case '.aac': $mime ='audio/aac'; break; // AAC audio
case '.abw': $mime ='application/x-abiword'; break; // AbiWord document
case '.arc': $mime ='application/octet-stream'; break; // Archive document (multiple files embedded)
case '.avi': $mime ='video/x-msvideo'; break; // AVI: Audio Video Interleave
case '.azw': $mime ='application/vnd.amazon.ebook'; break; // Amazon Kindle eBook format
case '.bin': $mime ='application/octet-stream'; break; // Any kind of binary data
case '.bmp': $mime ='image/bmp'; break; // Windows OS/2 Bitmap Graphics
case '.bz': $mime ='application/x-bzip'; break; // BZip archive
case '.bz2': $mime ='application/x-bzip2'; break; // BZip2 archive
case '.csh': $mime ='application/x-csh'; break; // C-Shell script
case '.css': $mime ='text/css'; break; // Cascading Style Sheets (CSS)
case '.csv': $mime ='text/csv'; break; // Comma-separated values (CSV)
case '.doc': $mime ='application/msword'; break; // Microsoft Word
case '.docx': $mime ='application/vnd.openxmlformats-officedocument.wordprocessingml.document'; break; // Microsoft Word (OpenXML)
case '.eot': $mime ='application/vnd.ms-fontobject'; break; // MS Embedded OpenType fonts
case '.epub': $mime ='application/epub+zip'; break; // Electronic publication (EPUB)
case '.gif': $mime ='image/gif'; break; // Graphics Interchange Format (GIF)
case '.htm': $mime ='text/html'; break; // HyperText Markup Language (HTML)
case '.html': $mime ='text/html'; break; // HyperText Markup Language (HTML)
case '.ico': $mime ='image/x-icon'; break; // Icon format
case '.ics': $mime ='text/calendar'; break; // iCalendar format
case '.jar': $mime ='application/java-archive'; break; // Java Archive (JAR)
case '.jpeg': $mime ='image/jpeg'; break; // JPEG images
case '.jpg': $mime ='image/jpeg'; break; // JPEG images
case '.js': $mime ='application/javascript'; break; // JavaScript (IANA Specification) (RFC 4329 Section 8.2)
case '.json': $mime ='application/json'; break; // JSON format
case '.mid': $mime ='audio/midi audio/x-midi'; break; // Musical Instrument Digital Interface (MIDI)
case '.midi': $mime ='audio/midi audio/x-midi'; break; // Musical Instrument Digital Interface (MIDI)
case '.mpeg': $mime ='video/mpeg'; break; // MPEG Video
case '.mpkg': $mime ='application/vnd.apple.installer+xml'; break; // Apple Installer Package
case '.odp': $mime ='application/vnd.oasis.opendocument.presentation'; break; // OpenDocument presentation document
case '.ods': $mime ='application/vnd.oasis.opendocument.spreadsheet'; break; // OpenDocument spreadsheet document
case '.odt': $mime ='application/vnd.oasis.opendocument.text'; break; // OpenDocument text document
case '.oga': $mime ='audio/ogg'; break; // OGG audio
case '.ogv': $mime ='video/ogg'; break; // OGG video
case '.ogx': $mime ='application/ogg'; break; // OGG
case '.otf': $mime ='font/otf'; break; // OpenType font
case '.png': $mime ='image/png'; break; // Portable Network Graphics
case '.pdf': $mime ='application/pdf'; break; // Adobe Portable Document Format (PDF)
case '.ppt': $mime ='application/vnd.ms-powerpoint'; break; // Microsoft PowerPoint
case '.pptx': $mime ='application/vnd.openxmlformats-officedocument.presentationml.presentation'; break; // Microsoft PowerPoint (OpenXML)
case '.rar': $mime ='application/x-rar-compressed'; break; // RAR archive
case '.rtf': $mime ='application/rtf'; break; // Rich Text Format (RTF)
case '.sh': $mime ='application/x-sh'; break; // Bourne shell script
case '.svg': $mime ='image/svg+xml'; break; // Scalable Vector Graphics (SVG)
case '.swf': $mime ='application/x-shockwave-flash'; break; // Small web format (SWF) or Adobe Flash document
case '.tar': $mime ='application/x-tar'; break; // Tape Archive (TAR)
case '.tif': $mime ='image/tiff'; break; // Tagged Image File Format (TIFF)
case '.tiff': $mime ='image/tiff'; break; // Tagged Image File Format (TIFF)
case '.ts': $mime ='application/typescript'; break; // Typescript file
case '.ttf': $mime ='font/ttf'; break; // TrueType Font
case '.txt': $mime ='text/plain'; break; // Text, (generally ASCII or ISO 8859-n)
case '.vsd': $mime ='application/vnd.visio'; break; // Microsoft Visio
case '.wav': $mime ='audio/wav'; break; // Waveform Audio Format
case '.weba': $mime ='audio/webm'; break; // WEBM audio
case '.webm': $mime ='video/webm'; break; // WEBM video
case '.webp': $mime ='image/webp'; break; // WEBP image
case '.woff': $mime ='font/woff'; break; // Web Open Font Format (WOFF)
case '.woff2': $mime ='font/woff2'; break; // Web Open Font Format (WOFF)
case '.xhtml': $mime ='application/xhtml+xml'; break; // XHTML
case '.xls': $mime ='application/vnd.ms-excel'; break; // Microsoft Excel
case '.xlsx': $mime ='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; break; // Microsoft Excel (OpenXML)
case '.xml': $mime ='application/xml'; break; // XML
case '.xul': $mime ='application/vnd.mozilla.xul+xml'; break; // XUL
case '.zip': $mime ='application/zip'; break; // ZIP archive
case '.3gp': $mime ='video/3gpp'; break; // 3GPP audio/video container
case '.3g2': $mime ='video/3gpp2'; break; // 3GPP2 audio/video container
case '.7z': $mime ='application/x-7z-compressed'; break; // 7-zip archive
default: $mime = 'application/octet-stream' ; // general purpose MIME-type
}
return $mime ;
}
public function getId(): ?int
{
return $this->id;
}
public function setId($id)
{
$this->id = $id;
return $this;
}
public function getTmpOldId(): ?int
{
return $this->tmpOldId;
}
public function setTmpOldId(?int $tmpOldId): self
{
$this->tmpOldId = $tmpOldId;
return $this;
}
/**
* If manually uploading a file (i.e. not using Symfony Form) ensure an instance
* of 'UploadedFile' is injected into this setter to trigger the update. If this
* bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
* must be able to accept an instance of 'File' as the bundle will inject one here
* during Doctrine hydration.
*
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile|null $file
*/
public function setFile(?File $file = null): void
{
$this->file = $file;
if (null !== $file) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTimeImmutable();
}
}
public function getFile(): ?File
{
return $this->file;
}
public function setName(?string $name): void
{
$this->name = $name;
}
public function getName(): ?string
{
return $this->name;
}
public function setDescription(?string $description): void
{
$this->description = $description;
}
public function getDescription(): ?string
{
if($this->description !== "") {
return $this->description;
} else {
return $this->name;
}
}
public function setSize(?int $size): void
{
$this->size = $size;
}
public function getSize(): ?int
{
return $this->size;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(?\DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getRole(): ?string
{
return $this->role;
}
public function setRole(string $role): self
{
if(! in_array($role, self::ROLES) ) $role = array_values(self::ROLES)[0];
$this->role = $role;
return $this;
}
public function getTypes(): ?array
{
return $this->types;
}
public function setTypes(array $types): self
{
$this->types = $types;
return $this;
}
/**
* @return Collection|Article[]
*/
public function getArticles(): Collection
{
return $this->articles;
}
public function addArticle(Article $article): self
{
if (!$this->articles->contains($article)) {
$this->articles[] = $article;
}
$type = "article";
if (! in_array($type, $this->types) ) {
$this->types[] = $type;
}
return $this;
}
public function removeArticle(Article $article): self
{
if ($this->articles->contains($article)) {
$this->articles->removeElement($article);
}
return $this;
}
/**
* @return Collection|Event[]
*/
public function getEvents(): Collection
{
return $this->events;
}
public function addEvent(Event $event): self
{
if (!$this->events->contains($event)) {
$this->events[] = $event;
}
$type = "event";
if (! in_array($type, $this->types) ) {
$this->types[] = $type;
}
return $this;
}
public function removeEvent(Event $event): self
{
if ($this->events->contains($event)) {
$this->events->removeElement($event);
}
return $this;
}
/**
* @return Collection|Newsletter[]
*/
public function getNewsletters(): Collection
{
return $this->newsletters;
}
public function addNewsletter(Newsletter $newsletter): self
{
if (!$this->newsletters->contains($newsletter)) {
$this->newsletters[] = $newsletter;
}
$type = "newsletter";
if (! in_array($type, $this->types) ) {
$this->types[] = $type;
}
return $this;
}
public function removeNewsletter(Newsletter $newsletter): self
{
if ($this->newsletters->contains($newsletter)) {
$this->newsletters->removeElement($newsletter);
}
return $this;
}
/**
* @return Collection|Page[]
*/
public function getPages(): Collection
{
return $this->pages;
}
public function addPage(Page $page): self
{
if (!$this->pages->contains($page)) {
$this->pages[] = $page;
}
$type = "page";
if (! in_array($type, $this->types) ) {
$this->types[] = $type;
}
return $this;
}
public function removePage(Page $page): self
{
if ($this->pages->contains($page)) {
$this->pages->removeElement($page);
}
return $this;
}
/**
* @return Collection|QuestionAnswer[]
*/
public function getQuestionAnswers(): Collection
{
return $this->questionAnswers;
}
public function addQuestionAnswer(QuestionAnswer $questionAnswer): self
{
if (!$this->questionAnswers->contains($questionAnswer)) {
$this->questionAnswers[] = $questionAnswer;
}
$type = "questionAnswer";
if (! in_array($type, $this->types) ) {
$this->types[] = $type;
}
return $this;
}
public function removeQuestionAnswer(QuestionAnswer $questionAnswer): self
{
if ($this->questionAnswers->contains($questionAnswer)) {
$this->questionAnswers->removeElement($questionAnswer);
}
return $this;
}
/**
* @return Collection|Structure[]
*/
public function getStructures(): Collection
{
return $this->structures;
}
public function addStructure(Structure $structure): self
{
if (!$this->structures->contains($structure)) {
$this->structures[] = $structure;
}
$type = "structure";
if (! in_array($type, $this->types) ) {
$this->types[] = $type;
}
return $this;
}
public function removeStructure(Structure $structure): self
{
if ($this->structures->contains($structure)) {
$this->structures->removeElement($structure);
}
return $this;
}
public function getTheme(): ?Theme
{
return $this->theme;
}
public function setTheme(?Theme $theme): self
{
$this->theme = $theme;
$type = "theme";
if (! in_array($type, $this->types) ) {
$this->types[] = $type;
}
return $this;
}
}