refactor pdf generator
This commit is contained in:
@@ -202,48 +202,28 @@ class OrderController extends BaseAdminController
|
||||
|
||||
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)
|
||||
{
|
||||
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;
|
||||
|
||||
$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 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()));
|
||||
|
||||
return $this->generateBackOfficeOrderPdf($order_id, ConfigQuery::read('pdf_delivery_file', 'delivery'));
|
||||
}
|
||||
|
||||
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
|
||||
))));
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -22,6 +22,8 @@
|
||||
/*************************************************************************************/
|
||||
namespace Thelia\Controller;
|
||||
|
||||
use Thelia\Core\Event\PdfEvent;
|
||||
use Thelia\Core\Event\TheliaEvents;
|
||||
use Thelia\Core\HttpFoundation\Response;
|
||||
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\Router;
|
||||
use Thelia\Core\Security\SecurityContext;
|
||||
use Thelia\Core\Template\TemplateHelper;
|
||||
use Thelia\Core\Translation\Translator;
|
||||
use Thelia\Model\OrderQuery;
|
||||
use Thelia\Tools\URL;
|
||||
use Thelia\Tools\Redirect;
|
||||
use Thelia\Core\Template\ParserContext;
|
||||
@@ -73,6 +77,21 @@ class BaseController extends ContainerAware
|
||||
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
|
||||
*
|
||||
@@ -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
|
||||
|
||||
@@ -249,15 +249,15 @@ class OrderController extends BaseFrontController
|
||||
|
||||
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)
|
||||
{
|
||||
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 */
|
||||
$this->checkAuth();
|
||||
|
||||
Reference in New Issue
Block a user