<?php
namespace Aviatur\EditionBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\Validator\Constraints as Assert;
/**
* HomePromo.
*
* @ORM\Table(name="home_promo", indexes={@ORM\Index(name="home_promo_list_id", columns={"home_promo_list_id"})})
* @ORM\Entity(repositoryClass="Aviatur\EditionBundle\Entity\HomePromoRepository")
* @ORM\HasLifecycleCallbacks
*/
class HomePromo
{
private $em;
private $temp;
private $tempSwitch;
public function __contruct($em)
{
$this->em = $em;
}
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="size", type="string", length=255)
*/
private $size;
/**
* @var bool
*
* @ORM\Column(name = "switch", type = "boolean")
*/
private $switch;
/**
* @var bool
*
* @ORM\Column(name = "mobile", type = "boolean")
*/
private $mobile;
/**
* @var bool
*
* @ORM\Column(name = "toUpdate", type = "boolean", nullable=true)
*/
private $toUpdate;
/**
* @var string
*
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* @var string
*
* @ORM\Column(name="titleSwitch", type="string", length=255, nullable=true)
*/
private $titleSwitch;
/**
* @var string
*
* @ORM\Column(name="price", type="string", length=255)
*/
private $price;
/**
* @var string
*
* @ORM\Column(name="content", type="text")
*/
private $content;
/**
* @var string
*
* @ORM\Column(name="details", type="string", length=255, nullable=true)
*/
private $details;
/**
* @var string
*
* @ORM\Column(name="duration", type="string", length=255, nullable=true)
*/
private $duration;
/**
* @var string
*
* @ORM\Column(name="path", type="string", length=255)
*/
private $path;
/**
* @Assert\File(maxSize="6000000")
*/
private ?\Symfony\Component\HttpFoundation\File\UploadedFile $file = null;
/**
* @Assert\File(maxSize="6000000")
*/
private ?\Symfony\Component\HttpFoundation\File\UploadedFile $fileSwitch = null;
/**
* @var string
*
* @ORM\Column(name="link", type="string", length=255)
*/
private $link;
/**
* @var string
*
* @ORM\Column(name="alt", type="string", length=255)
*/
private $alt;
/**
* @var string
*
* @ORM\Column(name="linkSwitch", type="string", length=255)
*/
private $linkSwitch;
/**
* @var int
*
* @ORM\Column(name="promoSize", type="integer")
*/
private $promoSize;
/**
* @var \DateTime
* @ORM\Column(name="date", type="datetime", nullable=true)
*/
private $date;
/**
*
* @ORM\ManyToOne(targetEntity="HomePromoList", inversedBy="homePromo")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="home_promo_list_id", referencedColumnName="id")
* })
*/
private ?\Aviatur\EditionBundle\Entity\HomePromoList $homePromoList = null;
/**
* Get id.
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set size.
*
* @param string $size
*
* @return HomePromo
*/
public function setSize($size)
{
$this->size = $size;
return $this;
}
/**
* Get size.
*
* @return string
*/
public function getSize()
{
return $this->size;
}
/**
* Set switch.
*
* @param bool $switch
*
* @return HomePromo
*/
public function setSwitch($switch)
{
$this->switch = $switch;
return $this;
}
/**
* Get switch.
*
* @return bool
*/
public function getSwitch()
{
return $this->switch;
}
/**
* Set mobile.
*
* @param bool $mobile
*
* @return HomePromo
*/
public function setMobile($mobile)
{
$this->mobile = $mobile;
return $this;
}
/**
* Get mobile.
*
* @return bool
*/
public function getMobile()
{
return $this->mobile;
}
/**
* Set title.
*
* @param string $title
*
* @return HomePromo
*/
public function setTitle($title)
{
$this->title = $title;
return $this;
}
/**
* Get title.
*
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Set price.
*
* @param int $price
*
* @return HomePromo
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price.
*
* @return int
*/
public function getPrice()
{
return $this->price;
}
/**
* Set content.
*
* @param string $content
*
* @return HomePromo
*/
public function setContent($content)
{
$this->content = $content;
return $this;
}
/**
* Get content.
*
* @return string
*/
public function getContent()
{
return $this->content;
}
/**
* Set link.
*
* @param string $link
*
* @return HomePromo
*/
public function setLink($link)
{
$this->link = $link;
return $this;
}
/**
* Get link.
*
* @return string
*/
public function getLink()
{
return $this->link;
}
/**
* Set link.
*
* @param string $alt
*
* @return HomePromo
*/
public function setAlt($alt)
{
$this->alt = $alt;
return $this;
}
/**
* Get alt.
*
* @return string
*/
public function getAlt()
{
return $this->alt;
}
/**
* Set promoSize.
*
* @param string $promoSize
*
* @return HomePromo
*/
public function setPromoSize($promoSize)
{
$this->promoSize = $promoSize;
return $this;
}
/**
* Get officeId.
*
* @return string
*/
public function getPromoSize()
{
return $this->promoSize;
}
/**
* Set linkSwitch.
*
* @param stringSwitch $linkSwitch
*
* @return HomePromo
*/
public function setLinkSwitch($linkSwitch)
{
$this->linkSwitch = $linkSwitch;
return $this;
}
/**
* Get linkSwitch.
*
* @return string
*/
public function getLinkSwitch()
{
return $this->linkSwitch;
}
/**
* Set path.
*
* @param string $path
*
* @return HomePromo
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path.
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* Set date.
*
* @param \DateTime $date
*
* @return HomePromo
*/
public function setDate($date)
{
$this->date = $date;
return $this;
}
/**
* Get date.
*
* @return \DateTime
*/
public function getDate()
{
return $this->date;
}
/**
* Set homePromoList.
*
* @param \Aviatur\EditionBundle\Entity\HomePromoList $homePromoList
*
* @return HomePromo
*/
public function setHomePromoList(\Aviatur\EditionBundle\Entity\HomePromoList $homePromoList = null)
{
$this->homePromoList = $homePromoList;
return $this;
}
/**
* Get homePromoList.
*
* @return \Aviatur\EditionBundle\Entity\HomePromoList
*/
public function getHomePromoList()
{
return $this->homePromoList;
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* Get fileSwitch.
*
* @return UploadedFile
*/
public function getFileSwitch()
{
return $this->fileSwitch;
}
public function getWebPath()
{
return null === $this->path ? null : $this->getUploadDir().'/'.$this->path;
}
protected function getUploadRootDir()
{
// the absolute directory path where uploaded
// documents should be saved
return __DIR__.'/../../../../web/'.$this->getUploadDir();
}
protected function getUploadDir()
{
// get rid of the __DIR__ so it doesn't screw up
// when displaying uploaded doc/image in the view.
return 'assets/'.$this->getHomePromoList()->getAgency()->getAssetsFolder().'_assets/img/promo/box';
}
protected function getFileName($link)
{
$textTemp = explode('/', strtok($link, '?'));
$text = is_array($textTemp) ? $textTemp[sizeof($textTemp) - 1] : $textTemp;
$text = ('' != $text) ? $text : null;
if (preg_match('/.\..[^\.jpeg]/', $text)) {
$text = null;
}
$finalName = null != $text ? $text.'_'.$this->id : $this->id;
return $finalName;
}
/**
* Sets file.
*
* @param UploadedFile $file
*/
public function setFile(UploadedFile $file = null)
{
$this->file = $file;
// check if we have an old image path
if (is_file($this->getAbsolutePath())) {
// store the old name to delete after the update
$this->temp = $this->getAbsolutePath();
} else {
$this->path = 'initial';
}
}
/**
* Sets fileSwitch.
*
* @param UploadedFile $fileSwitch
*/
public function setFileSwitch(UploadedFile $fileSwitch = null)
{
$this->fileSwitch = $fileSwitch;
// check if we have an old image path
if (is_file($this->getAbsolutePathSwitch())) {
// store the old name to delete after the update
$this->tempSwitch = $this->getAbsolutePathSwitch();
} else {
$this->path = 'initial';
}
}
/**
* @ORM\PrePersist()
* @ORM\PreUpdate()
*/
public function preUpload()
{
if (null !== $this->getFile()) {
$this->path = $this->getFile()->guessExtension();
}
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function upload()
{
if (null === $this->getFile()) {
return;
}
// check if we have an old image
if (isset($this->temp)) {
echo 'entro temp: ';
// delete the old image
unlink($this->temp);
// clear the temp image path
$this->temp = null;
}
// you must throw an exception here if the file cannot be moved
// so that the entity is not persisted to the database
// which the UploadedFile move() method does
$this->getFile()->move(
$this->getUploadRootDir(),
$this->getFileName($this->link).'.'.$this->getFile()->guessExtension()
);
$this->setFile(null);
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function uploadName()
{
$pathCopy = $this->getUploadRootDir().'/'.$this->getFileName($this->link).'.'.$this->path;
return $pathCopy;
}
/**
* @ORM\PostPersist()
* @ORM\PostUpdate()
*/
public function uploadSwitch()
{
if (null === $this->getFileSwitch()) {
return;
}
// check if we have an old image
if (isset($this->tempSwitch)) {
// delete the old image
unlink($this->tempSwitch);
// clear the temp image path
$this->tempSwitch = null;
}
// you must throw an exception here if the file cannot be moved
// so that the entity is not persisted to the database
// which the UploadedFile move() method does
$this->getFileSwitch()->move(
$this->getUploadRootDir(),
$this->getFileName($this->link).'-2.'.$this->getFileSwitch()->guessExtension()
);
$this->setFileSwitch(null);
}
/**
* @ORM\PreRemove()
*/
public function storeFilenameForRemove()
{
$this->temp = $this->getAbsolutePath();
$this->tempSwitch = $this->getAbsolutePathSwitch();
}
/**
* @ORM\PostRemove()
*/
public function removeUpload()
{
if (isset($this->temp)) {
unlink($this->temp);
}
if (isset($this->tempSwitch)) {
unlink($this->tempSwitch);
}
}
public function getAbsolutePath()
{
$return = null === $this->path ? null : $this->getUploadRootDir().'/'.$this->getFileName($this->link).'.'.$this->path;
if (!file_exists($return)) {
$return = null === $this->path ? null : $this->getUploadRootDir().'/'.$this->id.'.'.$this->path;
if (!file_exists($return)) {
$return = null;
}
}
return $return;
}
public function getAbsolutePathSwitch()
{
if (true == $this->switch) {
$return = $this->getUploadRootDir().'/'.$this->getFileName($this->link).'-2.'.$this->path;
if (!file_exists($return)) {
$return = $this->getUploadRootDir().'/'.$this->id.'-2.'.$this->path;
if (!file_exists($return)) {
$return = null;
}
}
return $return;
} else {
return null;
}
}
/**
* Set toUpdate.
*
* @param bool $toUpdate
*
* @return HomePromo
*/
public function setToUpdate($toUpdate)
{
$this->toUpdate = $toUpdate;
return $this;
}
/**
* Get toUpdate.
*
* @return bool
*/
public function getToUpdate()
{
return $this->toUpdate;
}
/**
* Set titleSwitch.
*
* @param string $titleSwitch
*
* @return HomePromo
*/
public function setTitleSwitch($titleSwitch)
{
$this->titleSwitch = $titleSwitch;
return $this;
}
/**
* Get titleSwitch.
*
* @return string
*/
public function getTitleSwitch()
{
return $this->titleSwitch;
}
/**
* Set details.
*
* @param string $details
*
* @return HomePromo
*/
public function setDetails($details)
{
$this->details = $details;
return $this;
}
/**
* Get details.
*
* @return string
*/
public function getDetails()
{
return $this->details;
}
/**
* Set duration.
*
* @param string $duration
*
* @return HomePromo
*/
public function setDuration($duration)
{
$this->duration = $duration;
return $this;
}
/**
* Get duration.
*
* @return string
*/
public function getDuration()
{
return $this->duration;
}
}