<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* ModuleTextItem
*
* @ORM\Table(name="module_carousel_items")
* @ORM\Entity(repositoryClass="App\Repository\ModuleCarouselItemRepository")
* @ORM\HasLifecycleCallbacks
*/
class ModuleCarouselItem {
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="img_fr", type="text", nullable=true)
*/
private $imgFr;
/**
* @var string
*
* @ORM\Column(name="img_mobile_fr", type="text", nullable=true)
*/
private $imgMobileFr;
/**
* @var string
*
* @ORM\Column(name="url_fr", type="text", nullable=true)
*/
private $urlFr;
/**
* @var string
*
* @ORM\Column(name="img_en", type="text", nullable=true)
*/
private $imgEn;
/**
* @var string
*
* @ORM\Column(name="img_mobile_en", type="text", nullable=true)
*/
private $imgMobileEn;
/**
* @var string
*
* @ORM\Column(name="url_en", type="text", nullable=true)
*/
private $urlEn;
/**
* @var integer
*
* @ORM\Column(name="order_num", type="integer")
*/
private $orderNum;
/**
* @var integer
*
* @ORM\ManyToOne(targetEntity="ModuleCarousel", inversedBy="items", cascade={"persist"})
*/
private $moduleCarousel;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\PriceGroup")
* @ORM\JoinColumn(name="price_group_id", referencedColumnName="id", nullable=true, onDelete="CASCADE")
*/
private $priceContext;
public $fileFr;
public $fileMobileFr;
public $fileEn;
public $fileMobileEn;
private $tempEn;
private $tempMobileEn;
private $tempFr;
private $tempMobileFr;
protected $widthFr;
protected $heightFr;
protected $mobileFrWidth;
protected $mobileFrHeight;
protected $widthEn;
protected $heightEn;
protected $mobileEnWidth;
protected $mobileEnHeight;
public function getId() {
return $this->id;
}
public function getImgFr(): ?string {
return $this->imgFr;
}
public function getImgMobileFr(): ?string {
return $this->imgMobileFr;
}
public function getUrlFr(): ?string {
return $this->urlFr;
}
public function getImgEn(): ?string {
return $this->imgEn;
}
public function getImgMobileEn(): ?string {
return $this->imgMobileEn;
}
public function getUrlEn(): ?string {
return $this->urlEn;
}
public function getOrderNum() {
return $this->orderNum;
}
public function getModuleCarousel() {
return $this->moduleCarousel;
}
public function getFile() {
return $this->file;
}
public function getFileMobile() {
return $this->fileMobile;
}
public function getTemp() {
return $this->temp;
}
public function getTempMobile() {
return $this->tempMobile;
}
public function getWidth() {
return $this->width;
}
public function getHeight() {
return $this->height;
}
public function getMobileWidth() {
return $this->mobileWidth;
}
public function getMobileHeight() {
return $this->mobileHeight;
}
public function setId($id): void {
$this->id = $id;
}
public function getPriceContext() {
return $this->priceContext;
}
public function setImgFr(string $imgFr): void {
$this->imgFr = $imgFr;
}
public function setImgMobileFr(string $imgMobileFr): void {
$this->imgMobileFr = $imgMobileFr;
}
public function setUrlFr(string $urlFr): void {
$this->urlFr = $urlFr;
}
public function setImgEn(string $imgEn): void {
$this->imgEn = $imgEn;
}
public function setImgMobileEn(string $imgMobileEn): void {
$this->imgMobileEn = $imgMobileEn;
}
public function setUrlEn(string $urlEn): void {
$this->urlEn = $urlEn;
}
public function setOrderNum($orderNum): void {
$this->orderNum = $orderNum;
}
public function setModuleCarousel($moduleCarousel): void {
$this->moduleCarousel = $moduleCarousel;
}
public function setFile($file): void {
$this->file = $file;
}
public function setFileMobile($fileMobile): void {
$this->fileMobile = $fileMobile;
}
public function setTemp($temp): void {
$this->temp = $temp;
}
public function setTempMobile($tempMobile): void {
$this->tempMobile = $tempMobile;
}
public function setWidth($width): void {
$this->width = $width;
}
public function setHeight($height): void {
$this->height = $height;
}
public function setMobileWidth($mobileWidth): void {
$this->mobileWidth = $mobileWidth;
}
public function setMobileHeight($mobileHeight): void {
$this->mobileHeight = $mobileHeight;
}
public function setPriceContext(PriceGroup $priceContext): void {
$this->priceContext = $priceContext;
}
public function matchPriceContext(?PriceGroup $priceContext) {
return empty($this->priceContext) || ($priceContext == $this->priceContext);
}
public function getUpdatedAt($type = 'desktop') {
$dir = __DIR__ . '/../../../public/';
if($type == 'desktop'){
return filemtime($dir.$this->imgFr);
}
if($type == 'mobile'){
return filemtime($dir.$this->imgMobileFr);
}
return '';
}
public function getUploadDir() {
return 'assets/modules/carousel/';
}
public function getUploadRootDir() {
// le chemin absolu du répertoire où les documents uploadés doivent être sauvegardés
return __DIR__ . '/../../public/' . $this->getUploadDir();
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload() {
if (!empty($this->fileFr)) {
$this->imgFr = $this->generateFilename($this->fileFr);
}
if (!empty($this->fileMobileFr)) {
$this->imgMobileFr = $this->generateFilename($this->fileMobileFr,'-mobile');
}
if (!empty($this->fileEn)) {
$this->imgEn = $this->generateFilename($this->fileEn);
}
if (!empty($this->fileMobileEn)) {
$this->imgMobileEn = $this->generateFilename($this->fileMobileEn,'-mobile');
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null !== $this->fileFr) {
if (isset($this->tempFr)) {
$this->tempFr = null;
}
$this->fileFr->move($this->getUploadRootDir(), $this->imgFr);
unset($this->fileFr);
}
if (null !== $this->fileMobileFr) {
if (isset($this->tempMobileFr)) {
$this->tempMobileFr = null;
}
$this->fileMobileFr->move($this->getUploadRootDir(), $this->imgMobileFr);
unset($this->fileMobileFr);
}
if (null !== $this->fileEn) {
if (isset($this->tempEn)) {
$this->tempEn = null;
}
$this->fileEn->move($this->getUploadRootDir(), $this->imgEn);
unset($this->fileEn);
}
if (null !== $this->fileMobileEn) {
if (isset($this->tempMobileEn)) {
$this->tempMobileEn = null;
}
$this->fileMobileEn->move($this->getUploadRootDir(), $this->imgMobileEn);
unset($this->fileMobileEn);
}
}
/**
* @ORM\PostLoad()
*/
public function postLoad()
{
if(is_file($this->imgFr)){
$infos = getimagesize($this->imgFr);
if(is_array($infos)){
$this->widthFr = isset($infos[0])?$infos[0]:0;
$this->heightFr = isset($infos[1])?$infos[1]:0;
}
}
if(is_file($this->imgMobileFr)){
$infos = getimagesize($this->imgMobileFr);
if(is_array($infos)){
$this->mobileFrWidth = isset($infos[0])?$infos[0]:0;
$this->mobileFrHeight = isset($infos[1])?$infos[1]:0;
}
}
if(is_file($this->imgEn)){
$infos = getimagesize($this->imgEn);
if(is_array($infos)){
$this->widthEn = isset($infos[0])?$infos[0]:0;
$this->heightEn = isset($infos[1])?$infos[1]:0;
}
}
if(is_file($this->imgMobileEn)){
$infos = getimagesize($this->imgMobileEn);
if(is_array($infos)){
$this->mobileEnWidth = isset($infos[0])?$infos[0]:0;
$this->mobileEnHeight = isset($infos[1])?$infos[1]:0;
}
}
}
private function generateFilename($file,$suffix='') {
// $title = $this->title;
// if (!empty($title)) {
// $tmp = $this->getUploadDir() . \App\Helpers\CustomEncoder::formatPictureUrl($title);
// } else {
$tmp = $this->getUploadDir() . sha1(uniqid(mt_rand(), true));
// }
$cpt = 0;
//$suffix = '-v2';
$f_name = $tmp . $suffix . '.' . $file->guessExtension();
while (file_exists($this->getUploadRootDir() . $f_name)) {
$cpt++;
$f_name = $tmp . ($cpt ? ('-' . $cpt) : '') . $suffix . '.' . $file->guessExtension();
}
return $f_name;
}
public function isGif() {
$ext = strtolower(substr($this->getImg(), -3));
return $ext == 'gif';
}
}