refactor pdf generator

This commit is contained in:
Manuel Raynaud
2013-11-08 09:41:20 +01:00
parent 555040fcab
commit 07b88266e9
3 changed files with 67 additions and 37 deletions

View File

@@ -202,48 +202,28 @@ class OrderController extends BaseAdminController
public function generateInvoicePdf($order_id) public function generateInvoicePdf($order_id)
{ {
return $this->generatePdf($order_id, ConfigQuery::read('pdf_invoice_file', 'invoice')); if (null !== $response = $this->checkAuth(AdminResources::ORDER, AccessManager::UPDATE)) return $response;
return $this->generateBackOfficeOrderPdf($order_id, ConfigQuery::read('pdf_invoice_file', 'invoice'));
} }
public function generateDeliveryPdf($order_id) public function generateDeliveryPdf($order_id)
{
return $this->generatePdf($order_id, ConfigQuery::read('pdf_delivery_file', 'delivery'));
}
protected function generatePdf($order_id, $fileName)
{ {
if (null !== $response = $this->checkAuth(AdminResources::ORDER, AccessManager::UPDATE)) return $response; if (null !== $response = $this->checkAuth(AdminResources::ORDER, AccessManager::UPDATE)) return $response;
$html = $this->renderRaw( return $this->generateBackOfficeOrderPdf($order_id, ConfigQuery::read('pdf_delivery_file', 'delivery'));
$fileName, }
array(
private function generateBackOfficeOrderPdf($order_id, $fileName)
{
if(null === $response = $this->generateOrderPdf($order_id, $fileName)){
$this->redirect(URL::getInstance()->absoluteUrl($this->getRoute("admin.order.update.view", array(
'order_id' => $order_id 'order_id' => $order_id
), ))));
TemplateHelper::getInstance()->getActivePdfTemplate()->getPath()
);
$order = OrderQuery::create()->findPk($order_id);
try {
$pdfEvent = new PdfEvent($html);
$this->dispatch(TheliaEvents::GENERATE_PDF, $pdfEvent);
if ($pdfEvent->hasPdf()) {
return Response::create($pdfEvent->getPdf(), 200,
array(
'Content-type' => "application/pdf",
'Content-Disposition' => sprintf('Attachment;filename=%s.pdf', $order->getRef()),
));
}
} catch (\Exception $e) {
\Thelia\Log\Tlog::getInstance()->error(sprintf('error during generating invoice pdf for order id : %d with message "%s"', $order_id, $e->getMessage()));
} }
$this->redirect(URL::getInstance()->absoluteUrl($this->getRoute("admin.order.update.view", array( return $response;
'order_id' => $order_id
))));
} }
} }

View File

@@ -22,6 +22,8 @@
/*************************************************************************************/ /*************************************************************************************/
namespace Thelia\Controller; namespace Thelia\Controller;
use Thelia\Core\Event\PdfEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\HttpFoundation\Response; use Thelia\Core\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerAware; use Symfony\Component\DependencyInjection\ContainerAware;
@@ -31,7 +33,9 @@ use Symfony\Component\Routing\Exception\MissingMandatoryParametersException;
use Symfony\Component\Routing\Exception\RouteNotFoundException; use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\Router; use Symfony\Component\Routing\Router;
use Thelia\Core\Security\SecurityContext; use Thelia\Core\Security\SecurityContext;
use Thelia\Core\Template\TemplateHelper;
use Thelia\Core\Translation\Translator; use Thelia\Core\Translation\Translator;
use Thelia\Model\OrderQuery;
use Thelia\Tools\URL; use Thelia\Tools\URL;
use Thelia\Tools\Redirect; use Thelia\Tools\Redirect;
use Thelia\Core\Template\ParserContext; use Thelia\Core\Template\ParserContext;
@@ -73,6 +77,21 @@ class BaseController extends ContainerAware
return new Response($json_data, $status, array('content-type' => 'application/json')); return new Response($json_data, $status, array('content-type' => 'application/json'));
} }
/**
* @param $pdf
* @param $fileName
* @param $status
* @return \Symfony\Component\HttpFoundation\Response
*/
protected function pdfResponse($pdf, $fileName, $status = 200)
{
return Response::create($pdf, $status,
array(
'Content-type' => "application/pdf",
'Content-Disposition' => sprintf('Attachment;filename=%s.pdf', $fileName),
));
}
/** /**
* Dispatch a Thelia event * Dispatch a Thelia event
* *
@@ -207,6 +226,37 @@ class BaseController extends ContainerAware
} }
} }
protected function generateOrderPdf($order_id, $fileName)
{
$html = $this->renderRaw(
$fileName,
array(
'order_id' => $order_id
),
TemplateHelper::getInstance()->getActivePdfTemplate()->getPath()
);
$order = OrderQuery::create()->findPk($order_id);
try {
$pdfEvent = new PdfEvent($html);
$this->dispatch(TheliaEvents::GENERATE_PDF, $pdfEvent);
if ($pdfEvent->hasPdf()) {
return $this->pdfResponse($pdfEvent->getPdf(), $order->getRef());
}
} catch (\Exception $e) {
\Thelia\Log\Tlog::getInstance()->error(sprintf('error during generating invoice pdf for order id : %d with message "%s"', $order_id, $e->getMessage()));
}
}
/** /**
* *
* redirect request to the specified url * redirect request to the specified url

View File

@@ -249,15 +249,15 @@ class OrderController extends BaseFrontController
public function generateInvoicePdf($order_id) public function generateInvoicePdf($order_id)
{ {
return $this->generatePdf($order_id, ConfigQuery::read('pdf_invoice_file', 'invoice')); return $this->generateOrderPdf($order_id, ConfigQuery::read('pdf_invoice_file', 'invoice'));
} }
public function generateDeliveryPdf($order_id) public function generateDeliveryPdf($order_id)
{ {
return $this->generatePdf($order_id, ConfigQuery::read('pdf_delivery_file', 'delivery')); return $this->generateOrderPdf($order_id, ConfigQuery::read('pdf_delivery_file', 'delivery'));
} }
protected function generatePdf($order_id, $fileName) protected function generateOrderPdf($order_id, $fileName)
{ {
/* check customer */ /* check customer */
$this->checkAuth(); $this->checkAuth();