From 555040fcabd3e170af38e5482c3302d60b96cfdb Mon Sep 17 00:00:00 2001 From: touffies Date: Thu, 7 Nov 2013 17:15:47 +0100 Subject: [PATCH 01/20] 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} From 07b88266e9c753825ebe9c68a4dc2011d5392e8d Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 8 Nov 2013 09:41:20 +0100 Subject: [PATCH 02/20] refactor pdf generator --- .../Controller/Admin/OrderController.php | 48 ++++++------------ core/lib/Thelia/Controller/BaseController.php | 50 +++++++++++++++++++ .../Controller/Front/OrderController.php | 6 +-- 3 files changed, 67 insertions(+), 37 deletions(-) diff --git a/core/lib/Thelia/Controller/Admin/OrderController.php b/core/lib/Thelia/Controller/Admin/OrderController.php index 54dcc3059..54ff428ea 100644 --- a/core/lib/Thelia/Controller/Admin/OrderController.php +++ b/core/lib/Thelia/Controller/Admin/OrderController.php @@ -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( + 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 - ), - 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( - 'order_id' => $order_id - )))); + return $response; } + + } diff --git a/core/lib/Thelia/Controller/BaseController.php b/core/lib/Thelia/Controller/BaseController.php index 450f98e74..43dfaca8c 100755 --- a/core/lib/Thelia/Controller/BaseController.php +++ b/core/lib/Thelia/Controller/BaseController.php @@ -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 diff --git a/core/lib/Thelia/Controller/Front/OrderController.php b/core/lib/Thelia/Controller/Front/OrderController.php index 38d95a1bd..e4b1ec3a9 100755 --- a/core/lib/Thelia/Controller/Front/OrderController.php +++ b/core/lib/Thelia/Controller/Front/OrderController.php @@ -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(); From d80bf97b33a44bbb220267be867e56680fa965cc Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 8 Nov 2013 09:59:46 +0100 Subject: [PATCH 03/20] refactor pdf generator in front part --- core/lib/Thelia/Controller/BaseController.php | 2 -- .../Controller/Front/OrderController.php | 36 +++---------------- 2 files changed, 4 insertions(+), 34 deletions(-) diff --git a/core/lib/Thelia/Controller/BaseController.php b/core/lib/Thelia/Controller/BaseController.php index 43dfaca8c..b4da346bf 100755 --- a/core/lib/Thelia/Controller/BaseController.php +++ b/core/lib/Thelia/Controller/BaseController.php @@ -228,8 +228,6 @@ class BaseController extends ContainerAware protected function generateOrderPdf($order_id, $fileName) { - - $html = $this->renderRaw( $fileName, array( diff --git a/core/lib/Thelia/Controller/Front/OrderController.php b/core/lib/Thelia/Controller/Front/OrderController.php index e4b1ec3a9..997d1e2f6 100755 --- a/core/lib/Thelia/Controller/Front/OrderController.php +++ b/core/lib/Thelia/Controller/Front/OrderController.php @@ -249,45 +249,17 @@ class OrderController extends BaseFrontController public function generateInvoicePdf($order_id) { + /* check customer */ + $this->checkAuth(); return $this->generateOrderPdf($order_id, ConfigQuery::read('pdf_invoice_file', 'invoice')); } public function generateDeliveryPdf($order_id) { + /* check customer */ + $this->checkAuth(); return $this->generateOrderPdf($order_id, ConfigQuery::read('pdf_delivery_file', 'delivery')); } - protected function generateOrderPdf($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())); - - } - } } From 0f1e071967db72b43df86d28d2c8ec4a12db75a8 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 8 Nov 2013 10:04:33 +0100 Subject: [PATCH 04/20] remove unused controller --- core/lib/Thelia/Controller/BaseController.php | 5 -- core/lib/Thelia/Controller/Front/Mail.php | 48 ------------------- 2 files changed, 53 deletions(-) delete mode 100644 core/lib/Thelia/Controller/Front/Mail.php diff --git a/core/lib/Thelia/Controller/BaseController.php b/core/lib/Thelia/Controller/BaseController.php index b4da346bf..fcfb0b910 100755 --- a/core/lib/Thelia/Controller/BaseController.php +++ b/core/lib/Thelia/Controller/BaseController.php @@ -370,9 +370,4 @@ class BaseController extends ContainerAware { return $this->container->get("thelia.parser"); } - - protected function render($inline) - { - return $this->getParser()->fetch(sprintf("string:%s", $inline)); - } } diff --git a/core/lib/Thelia/Controller/Front/Mail.php b/core/lib/Thelia/Controller/Front/Mail.php deleted file mode 100644 index f25024f99..000000000 --- a/core/lib/Thelia/Controller/Front/Mail.php +++ /dev/null @@ -1,48 +0,0 @@ -. */ -/* */ -/*************************************************************************************/ - -namespace Thelia\Controller\Front; - -/** - * Class Mail - * @package Thelia\Controller\Front - * @author Manuel Raynaud - */ -class Mail extends BaseFrontController -{ - /** - * This is a demo how to send a mail using swiftmailer + smarty - */ - public function test() - { - $message = \Swift_Message::newInstance('Wonderful Subject') - ->setFrom(array('john@doe.com' => 'John Doe')) - ->setTo(array('mraynaud@openstudio.fr' => 'name')) - ->setBody($this->render('Here is the message itself')) - ; - - $this->getMailer()->send($message); - - exit; - } -} From 41d8fce23b1d4b29852e9ae1e5f53e4db2b216c9 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 8 Nov 2013 10:04:53 +0100 Subject: [PATCH 05/20] remove test route --- core/lib/Thelia/Config/Resources/routing/front.xml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/core/lib/Thelia/Config/Resources/routing/front.xml b/core/lib/Thelia/Config/Resources/routing/front.xml index e3075ee02..702c050ed 100755 --- a/core/lib/Thelia/Config/Resources/routing/front.xml +++ b/core/lib/Thelia/Config/Resources/routing/front.xml @@ -157,10 +157,6 @@ - - Thelia\Controller\Front\Mail::test - - Thelia\Controller\Front\ContactController::sendAction From ac76a206c10560a8c722740fb6b6919eb3c569c9 Mon Sep 17 00:00:00 2001 From: touffies Date: Fri, 8 Nov 2013 10:54:03 +0100 Subject: [PATCH 06/20] Remove role="form" on a form element. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Role Form should be use on a region of the document that represents a collection of form-associated elements.
GOOD
BAD --- templates/default/account-password.html | 2 +- templates/default/account-update.html | 2 +- templates/default/address-update.html | 2 +- templates/default/address.html | 2 +- templates/default/cart.html | 2 +- templates/default/contact.html | 2 +- templates/default/includes/categories-filters.html | 2 +- templates/default/includes/mini-cart.html | 2 +- templates/default/includes/single-product.html | 2 +- templates/default/layout.tpl | 11 ++++++----- templates/default/login.html | 2 +- templates/default/newsletter.html | 2 +- templates/default/order-delivery.html | 2 +- templates/default/order-invoice.html | 4 ++-- templates/default/password.html | 2 +- templates/default/product.html | 2 +- templates/default/register.html | 2 +- 17 files changed, 23 insertions(+), 22 deletions(-) diff --git a/templates/default/account-password.html b/templates/default/account-password.html index 1e67d4520..653ed9eef 100644 --- a/templates/default/account-password.html +++ b/templates/default/account-password.html @@ -25,7 +25,7 @@

{intl l="Change Password"}

{form name="thelia.front.customer.password.update"} -
+ {form_field form=$form field='success_url'} {/form_field} diff --git a/templates/default/account-update.html b/templates/default/account-update.html index d7b38e773..d4ba0fadb 100644 --- a/templates/default/account-update.html +++ b/templates/default/account-update.html @@ -24,7 +24,7 @@

{intl l="Update Profile"}

{form name="thelia.front.customer.profile.update"} - + {form_field form=$form field='success_url'} {/form_field} diff --git a/templates/default/address-update.html b/templates/default/address-update.html index 8ab66107d..f31d3eaaa 100644 --- a/templates/default/address-update.html +++ b/templates/default/address-update.html @@ -24,7 +24,7 @@

{intl l="Address Update"}

{form name="thelia.front.address.update"} {loop name="customer.update" type="address" customer="current" id="{$address_id}"} - + {form_field form=$form field='success_url'} {/form_field} diff --git a/templates/default/address.html b/templates/default/address.html index ac625af2b..771d4aad5 100644 --- a/templates/default/address.html +++ b/templates/default/address.html @@ -23,7 +23,7 @@

{intl l="Create New Address"}

{form name="thelia.front.address.create"} - + {form_field form=$form field='success_url'} {/form_field} diff --git a/templates/default/cart.html b/templates/default/cart.html index 592b5f34c..22d0af420 100644 --- a/templates/default/cart.html +++ b/templates/default/cart.html @@ -98,7 +98,7 @@
- + diff --git a/templates/default/layout.tpl b/templates/default/layout.tpl index d89694ab5..588461306 100644 --- a/templates/default/layout.tpl +++ b/templates/default/layout.tpl @@ -34,12 +34,13 @@ GNU General Public License : http://www.gnu.org/licenses/ {block name="page-title"}{strip}{if $breadcrumbs}{foreach from=$breadcrumbs|array_reverse item=breadcrumb}{$breadcrumb.title} - {/foreach}{/if}{config key="company_name"}{/strip}{/block} {* Meta Tags *} - - - {block name="meta"}{/block} + {block name="meta"} + + + {/block} {* Stylesheets *} {stylesheets file='assets/less/styles.less' filters='less,cssembed'} @@ -100,7 +101,7 @@ GNU General Public License : http://www.gnu.org/licenses/