<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\HttpFoundation\File\File as HttpFile;
/**
* @ORM\Entity(repositoryClass="App\Repository\MediaRepository")
* @Vich\Uploadable
*/
class Media
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="bigint")
*/
private $id;
/**
* @ORM\Version @ORM\Column(type="integer")
*/
private $version;
/**
* @ORM\Column(type="string", length=255)
*/
private $title;
/**
* @ORM\Column(type="text")
*/
private $description;
/**
* @ORM\Column(type="string", length=125)
*/
private $alt_tag;
/**
* @ORM\Column(type="string", length=16)
*/
private $extension;
/**
* @Vich\UploadableField(mapping="media_files", fileNameProperty="name")
* @var File
*/
private $data;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
/**
* @ORM\Column(type="string", length=50)
*/
private $type;
/**
* @ORM\Column(type="integer")
*/
private $status;
/**
* @ORM\Column(type="datetime")
*/
private $modified_at;
/**
* @ORM\Column(type="datetime")
*/
private $created_at;
/**
* @ORM\Column(type="bigint")
*/
private $old_id;
/**
* @ O R M\Column(type="string", length=255)
* /
private $sub_directory;
*/
public function __construct ()
{
$this->title = "";
$this->alt_tag = "";
$this->extension = "";
$this->name = "";
$this->type = "";
$this->status = "1";
$this->description = "";
$this->status = 1;
$this->old_id = 0;
// $this->sub_directory = "";
$this->modified_at = new \DateTime("now");
$this->created_at = new \DateTime("now");
}
public function getId()
{
return $this->id;
}
public function getVersion() {
return $this->version;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title = null): self
{
$this->title = $title ? $title : "";
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description = null): self
{
$this->description = $description ? $description : "";
return $this;
}
public function getAltTag(): ?string
{
return $this->alt_tag;
}
public function setAltTag(string $alt_tag = null): self
{
$this->alt_tag = $alt_tag ? $alt_tag : "";
return $this;
}
public function getAltTagText(): ?string
{
return $this->alt_tag ? $this->alt_tag : $this->title;
}
public function getExtension(): ?string
{
return $this->extension;
}
public function setExtension(string $extension = null): self
{
$this->extension = $extension ? $extension : "";
return $this;
}
public function getData(): ?HttpFile
{
return $this->data;
}
public function setData(HttpFile $data = null): self
{
$this->data = $data;
if ($data) {
$this->extension = $data->guessExtension();
// $this->extension = $data->getExtension();
$this->type = $data->getMimeType();
$this->modified_at = new \DateTime("now");
$nameArray = explode('/', $data);
$nameArray = array_slice($nameArray, -3, 3, true);
$this->name = implode('/', $nameArray);
if ($this->type == 'image/heif' || $this->type == 'application/octet-stream') {
//return null;
}
//Exif Rotation Fix
try {
$file = $data;
if (is_file($file) && exif_imagetype($file)) {
$type = exif_imagetype($file);
switch($type) {
case 1:
$image = imagecreatefromgif($file);
break;
case 2:
$image = imagecreatefromjpeg($file);
break;
case 3:
$image = imagecreatefrompng($file);
break;
default:
$image = 0;
}
if($image) {
$exif = exif_read_data($file);
if (!empty($exif['Orientation'])) {
switch ($exif['Orientation']) {
case 3:
$image = imagerotate($image, 180, 0);
break;
case 6:
$image = imagerotate($image, -90, 0);
break;
case 8:
$image = imagerotate($image, 90, 0);
break;
}
switch($type) {
case 1:
imagegif($image, $this->name);
break;
case 2:
imagejpeg($image, $this->name);
break;
case 3:
imagepng($image, $this->name);
break;
}
}
}
}
} catch (\Exception $e) {
//usually it's erroring on the exif_read_data function if the file is not supported
//do nothing, the image should be fine
}
}
return $this;
}
public function removeFile ()
{
if ($this->getName()) {
$file = dirname(dirname(dirname(__FILE__)))."/public/uploads/media/" . $this->getName();
if (is_file($file)) {
unlink ($file);
$this->name = null;
$this->data = null;
}
}
return $this;
}
public function getName(): ?string
{
/*
if ($this->name && $this->sub_directory &&
strpos($this->name, $this->sub_directory) === false
) {
return $this->sub_directory.$this->name;
}
*/
return $this->name;
}
public function setName(string $name = null): self
{
$this->name = $name ? $name : null;
return $this;
}
public function getURL ()
{
if(!$this->getName()) {
return "";
}
return "/uploads/media/{$this->name}";
}
public function getAbsoluteURL ()
{
if(!$this->getName()) {
return "";
}
return "https://" . $_SERVER['SERVER_NAME'] . "/uploads/media/{$this->name}";
}
//Todo: clean up getImageTag and getVideoTag
public function getImageTag(string $class = "", string $style = "", string $item_id = "", string $url = "", $lazy_load = true)
{
// for clicks:
// need to include link as well, but that's stored in the media group item e.g. <a href="{{ item.getExternalUrl() }}" target="_blank">
// so make a function in media group items to get the tag and call this function
// for impressions:
// insert record into database table
if(!$this->getURL()) {
return "";
}
if($this->getType() == "video/mp4") {
return "<video controls style='max-width:100%;'>
<source src='{$this->getURL()}' type='video/mp4'>
Your browser does not support the video tag.
</video>";
}
$return = "
<img src='{$this->getURL()}'
alt='{$this->getAltTagText()}'
title='{$this->getAltTagText()}'
class='{$class}'
style='{$style} ' ";
if($lazy_load) {
$return .= " loading='lazy' ";
}
if($item_id) {
$return .= "data-mitem-id='".$item_id."'";
}
if($url) {
$return .= "data-url='".$url."'";
}
$return .= "/>";
return $return;
}
public function getVideoTag(
string $class = "",
string $style = "",
string $item_id = "",
string $url = "",
$lazy_load = true,
$autoplay = false,
$controls = true,
$muted = false,
$thumbnailUrl = ""
) {
// for clicks:
// need to include link as well, but that's stored in the media group item e.g. <a href="{{ item.getExternalUrl() }}" target="_blank">
// so make a function in media group items to get the tag and call this function
// for impressions:
// insert record into database table
if(!$this->getURL()) {
return "";
}
if(in_array($this->getType(), ["video/mp4", "application/octe"])) {
$controlsText = $controls ? "controls" : "";
$autoplayText = $autoplay ? "autoplay" : "";
$mutedText = $muted ? "muted" : "";
//$thumbnailText = "";
$thumbnailText = "poster='{$thumbnailUrl}'";
$hoverPlay = false;
$hoverPlayText = $hoverPlay ? "onmouseover='this.play();' onmouseout='this.pause();this.currentTime=0;'" : "";
return "<video {$controlsText} {$autoplayText} {$mutedText} {$thumbnailText} {$hoverPlayText} class='{$class}' style='max-width:100%; {$style}' data-video-mid='{$this->getId()}'>
<source src='{$this->getURL()}' type='video/mp4'>
Your browser does not support the video tag.
</video>";
}
$return = "
<img src='{$this->getURL()}'
alt='{$this->getAltTagText()}'
title='{$this->getAltTagText()}'
class='{$class}'
style='{$style} ' ";
if($lazy_load) {
$return .= " loading='lazy' ";
}
if($item_id) {
$return .= "data-mitem-id='".$item_id."'";
}
if($url) {
$return .= "data-url='".$url."'";
}
$return .= "/>";
return $return;
}
public function getModifiedAt(): ?\DateTimeInterface
{
return $this->modifiedAt;
}
public function setModifiedAt(\DateTimeInterface $modified_at): self
{
$this->modifiedAt = $modified_at;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt ?? new \DateTime("now");
}
public function setCreatedAt(\DateTimeInterface $created_at): self
{
$this->createdAt = $created_at;
return $this;
}
public function getType ()
{
return $this->type;
}
public function setType (string $type = null): self
{
$this->type = $type ? $type : "";
return $this;
}
public function getStatus(): ?int
{
return $this->status;
}
public function setStatus(int $status): self
{
$this->status = $status;
return $this;
}
public function __toString ()
{
return "{$this->title} : {$this->type}";
}
public function getOldId(): ?int
{
return $this->old_id;
}
public function setOldId(int $old_id): self
{
$this->old_id = $old_id;
return $this;
}
public function isImage()
{
return (strpos($this->getType(), "image") !== false);
}
/*
public function getSubDirectory(): ?string
{
return $this->sub_directory;
}
public function setSubDirectory(string $sub_directory): self
{
$this->sub_directory = $sub_directory;
return $this;
}
*/
}