<?php
namespace JF\JuridicusBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
/**
* Pdf
*/
class Pdf extends InitModeEntity
{
const CLASSNAME = __CLASS__;
/**
* @var integer
*/
protected $id;
/**
* @var UploadedFile
* @Assert\File(maxSize="6000000")
*/
protected $file;
/**
* @var string
*/
protected $filename;
/**
* @var integer
*/
protected $anzahl_seiten;
/**
* @var string
*/
protected $path;
/**
* @var string
*/
protected $mimeType = 'application/pdf'; // Standardwert
/**
* @var float
*/
protected $size;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Get file
*
* @return UploadedFile
*/
public function getFile()
{
return $this->file;
}
/**
* Set file
*
* @return Pdf
*/
public function setFile($file)
{
$this->file = $file;
return $this;
}
/**
*
* @return string
*/
public function getFilename()
{
if (!$this->filename) {
$file = new File($this->getPath());
$this->filename = $file->getFileName();
}
return $this->filename;
}
/**
* Set anzahl_seiten
*
* @param integer $anzahlSeiten
* @return Pdf
*/
public function setAnzahlSeiten($anzahlSeiten)
{
$this->anzahl_seiten = $anzahlSeiten;
return $this;
}
/**
* Get anzahl_seiten
*
* @return integer
*/
public function getAnzahlSeiten()
{
return $this->anzahl_seiten;
}
/**
* Set path
*
* @param string $path
* @return Pdf
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
$this->path = str_replace('/data/webserv/sandbox_2/', __DIR__."/../../../../", $this->path);
$this->path = str_replace('/home/juridicus/', __DIR__."/../../../../".'app/files/', $this->path);
return $this->path; //__DIR__."/../../../../".
}
/**
* Set mimeType
*
* @param string $mimeType
* @return Pdf
*/
public function setMimeType($mimeType)
{
$this->mimeType = $mimeType;
return $this;
}
/**
* Get mimeType
*
* @return string
*/
public function getMimeType()
{
return $this->mimeType;
}
/**
* Set size
*
* @param float $size
* @return Pdf
*/
public function setSize($size)
{
$this->size = $size;
return $this;
}
/**
* Get size
*
* @return float
*/
public function getSize()
{
return $this->size;
}
/**
* Get dir
*
* @return string
*/
public function getDir()
{
return realpath(__DIR__ . '/../../../../app/files/pdf');
}
/**
*
* @return string
*/
public function getContent()
{
return file_get_contents($this->getPath());
}
/**
* createResponse - creates Response with Headers set for Download
*
* @param bool $inline
* @return Symfony\Component\HttpFoundation\Response
*/
public function createResponse($inline = false)
{
$response = new Response();
$disposition = $response->headers->makeDisposition(
$inline ? ResponseHeaderBag::DISPOSITION_INLINE : ResponseHeaderBag::DISPOSITION_ATTACHMENT,
$this->getFilename().".pdf",
iconv('utf-8', 'us-ascii//TRANSLIT', $this->getFilename())
);
$response->headers->set('Content-Disposition', $disposition);
$response->headers->set('Content-Type', $this->getMimeType());
$response->headers->set('Content-Transfer-Encoding', 'binary');
$response->headers->set('Content-Length', filesize($this->getPath()));
$response->setContent($this->getContent());
return $response;
}
/**
* createAttachment - creates Swift_Attachment for SwiftMailer
*
* @return Swift_Attachment
*/
public function createAttachment()
{
return \Swift_Attachment::newInstance()
->setFilename($this->getFilename())
->setContentType($this->getMimeType())
->setBody(file_get_contents($this->getPath()))
;
}
}