From 555040fcabd3e170af38e5482c3302d60b96cfdb Mon Sep 17 00:00:00 2001 From: touffies Date: Thu, 7 Nov 2013 17:15:47 +0100 Subject: [PATCH] Add PDF for Order Details on the account page --- .../Thelia/Config/Resources/routing/front.xml | 5 ++ .../Controller/Front/BaseFrontController.php | 58 ++++++++++++++++++- .../Controller/Front/OrderController.php | 48 +++++++++++++++ templates/default/account.html | 2 +- 4 files changed, 110 insertions(+), 3 deletions(-) diff --git a/core/lib/Thelia/Config/Resources/routing/front.xml b/core/lib/Thelia/Config/Resources/routing/front.xml index 4d811599c..e3075ee02 100755 --- a/core/lib/Thelia/Config/Resources/routing/front.xml +++ b/core/lib/Thelia/Config/Resources/routing/front.xml @@ -58,6 +58,11 @@ Thelia\Controller\Front\DefaultController::noAction account-password + + + Thelia\Controller\Front\OrderController::generateDeliveryPdf + \d+ + diff --git a/core/lib/Thelia/Controller/Front/BaseFrontController.php b/core/lib/Thelia/Controller/Front/BaseFrontController.php index 60628bbc1..003db36c4 100755 --- a/core/lib/Thelia/Controller/Front/BaseFrontController.php +++ b/core/lib/Thelia/Controller/Front/BaseFrontController.php @@ -24,6 +24,7 @@ namespace Thelia\Controller\Front; use Symfony\Component\Routing\Router; use Thelia\Controller\BaseController; +use Thelia\Core\Template\TemplateHelper; use Thelia\Model\AddressQuery; use Thelia\Model\ConfigQuery; use Thelia\Model\ModuleQuery; @@ -88,12 +89,65 @@ class BaseFrontController extends BaseController /** * @return ParserInterface instance parser */ - protected function getParser() + protected function getParser($template = null) { $parser = $this->container->get("thelia.parser"); - $parser->setTemplate(ConfigQuery::getActiveTemplate()); + // Define the template that should be used + $parser->setTemplate($template ?: TemplateHelper::getInstance()->getActiveFrontTemplate()->getPath()); return $parser; } + + /** + * Render the given template, and returns the result as an Http Response. + * + * @param $templateName the complete template name, with extension + * @param array $args the template arguments + * @param int $status http code status + * @return \Thelia\Core\HttpFoundation\Response + */ + protected function render($templateName, $args = array(), $status = 200) + { + return Response::create($this->renderRaw($templateName, $args), $status); + } + + /** + * Render the given template, and returns the result as a string. + * + * @param $templateName the complete template name, with extension + * @param array $args the template arguments + * @param null $templateDir + * + * @return \Thelia\Core\HttpFoundation\Response + */ + protected function renderRaw($templateName, $args = array(), $templateDir = null) + { + + // Add the template standard extension + $templateName .= '.html'; + + $session = $this->getSession(); + + // Prepare common template variables + $args = array_merge($args, array( + 'locale' => $session->getLang()->getLocale(), + 'lang_code' => $session->getLang()->getCode(), + 'lang_id' => $session->getLang()->getId(), + 'current_url' => $this->getRequest()->getUri() + )); + + // Render the template. + try { + $data = $this->getParser($templateDir)->render($templateName, $args); + + return $data; + } catch (AuthenticationException $ex) { + // User is not authenticated, and templates requires authentication -> redirect to login page + Redirect::exec(URL::getInstance()->absoluteUrl($ex->getLoginTemplate())); + } catch (AuthorizationException $ex) { + // User is not allowed to perform the required action. Return the error page instead of the requested page. + return $this->errorPage($this->getTranslator()->trans("Sorry, you are not allowed to perform this action."), 403); + } + } } diff --git a/core/lib/Thelia/Controller/Front/OrderController.php b/core/lib/Thelia/Controller/Front/OrderController.php index bb0604bd1..38d95a1bd 100755 --- a/core/lib/Thelia/Controller/Front/OrderController.php +++ b/core/lib/Thelia/Controller/Front/OrderController.php @@ -23,6 +23,9 @@ namespace Thelia\Controller\Front; use Propel\Runtime\Exception\PropelException; +use Thelia\Core\Event\PdfEvent; +use Thelia\Core\HttpFoundation\Response; +use Thelia\Core\Template\TemplateHelper; use Thelia\Exception\TheliaProcessException; use Thelia\Form\Exception\FormValidationException; use Thelia\Core\Event\Order\OrderEvent; @@ -34,6 +37,7 @@ use Thelia\Log\Tlog; use Thelia\Model\AddressQuery; use Thelia\Model\AreaDeliveryModuleQuery; use Thelia\Model\Base\OrderQuery; +use Thelia\Model\ConfigQuery; use Thelia\Model\ModuleQuery; use Thelia\Model\Order; use Thelia\Tools\URL; @@ -242,4 +246,48 @@ class OrderController extends BaseFrontController return $order; } + + public function generateInvoicePdf($order_id) + { + return $this->generatePdf($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) + { + /* check customer */ + $this->checkAuth(); + + $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())); + + } + } } diff --git a/templates/default/account.html b/templates/default/account.html index 73da05a66..9904760f9 100644 --- a/templates/default/account.html +++ b/templates/default/account.html @@ -158,7 +158,7 @@ {format_date date=$CREATE_DATE} {format_number number=$TOTAL_TAXED_AMOUNT} {loop type="currency" name="order.currency" id={$CURRENCY}}{$SYMBOL}{/loop} {loop type="order-status" name="order.status" id={$STATUS}}{$TITLE}{/loop} - {intl l="Order details"} + {intl l="Order details"} {/loop}