Initial commit
This commit is contained in:
273
local/modules/Front/Controller/AddressController.php
Normal file
273
local/modules/Front/Controller/AddressController.php
Normal file
@@ -0,0 +1,273 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Front\Controller;
|
||||
|
||||
use Front\Front;
|
||||
use Symfony\Component\Form\Form;
|
||||
use Thelia\Controller\Front\BaseFrontController;
|
||||
use Thelia\Core\Event\Address\AddressCreateOrUpdateEvent;
|
||||
use Thelia\Core\Event\Address\AddressEvent;
|
||||
use Thelia\Core\Event\TheliaEvents;
|
||||
use Thelia\Form\Definition\FrontForm;
|
||||
use Thelia\Form\Exception\FormValidationException;
|
||||
use Thelia\Log\Tlog;
|
||||
use Thelia\Model\AddressQuery;
|
||||
use Thelia\Model\Customer;
|
||||
|
||||
/**
|
||||
* Class AddressController
|
||||
* @package Thelia\Controller\Front
|
||||
* @author Manuel Raynaud <manu@raynaud.io>
|
||||
*/
|
||||
class AddressController extends BaseFrontController
|
||||
{
|
||||
|
||||
/**
|
||||
* Controller for generate modal containing update form
|
||||
* Check if request is a XmlHttpRequest and address owner is the current customer
|
||||
*
|
||||
* @param $address_id
|
||||
*/
|
||||
public function generateModalAction($address_id)
|
||||
{
|
||||
|
||||
$this->checkAuth();
|
||||
$this->checkXmlHttpRequest();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Create controller.
|
||||
* Check if customer is logged in
|
||||
*
|
||||
* Dispatch TheliaEvents::ADDRESS_CREATE event
|
||||
*/
|
||||
public function createAction()
|
||||
{
|
||||
$this->checkAuth();
|
||||
|
||||
$addressCreate = $this->createForm(FrontForm::ADDRESS_CREATE);
|
||||
|
||||
try {
|
||||
/** @var Customer $customer */
|
||||
$customer = $this->getSecurityContext()->getCustomerUser();
|
||||
|
||||
$form = $this->validateForm($addressCreate, "post");
|
||||
$event = $this->createAddressEvent($form);
|
||||
$event->setCustomer($customer);
|
||||
|
||||
$this->dispatch(TheliaEvents::ADDRESS_CREATE, $event);
|
||||
|
||||
return $this->generateSuccessRedirect($addressCreate);
|
||||
} catch (FormValidationException $e) {
|
||||
$message = $this->getTranslator()->trans("Please check your input: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN);
|
||||
} catch (\Exception $e) {
|
||||
$message = $this->getTranslator()->trans("Sorry, an error occured: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN);
|
||||
}
|
||||
|
||||
Tlog::getInstance()->error(sprintf("Error during address creation process : %s", $message));
|
||||
|
||||
$addressCreate->setErrorMessage($message);
|
||||
|
||||
$this->getParserContext()
|
||||
->addForm($addressCreate)
|
||||
->setGeneralError($message)
|
||||
;
|
||||
|
||||
// Redirect to error URL if defined
|
||||
if ($addressCreate->hasErrorUrl()) {
|
||||
return $this->generateErrorRedirect($addressCreate);
|
||||
}
|
||||
}
|
||||
|
||||
protected function createAddressEvent(Form $form)
|
||||
{
|
||||
return new AddressCreateOrUpdateEvent(
|
||||
$form->get("label")->getData(),
|
||||
$form->get("title")->getData(),
|
||||
$form->get("firstname")->getData(),
|
||||
$form->get("lastname")->getData(),
|
||||
$form->get("address1")->getData(),
|
||||
$form->get("address2")->getData(),
|
||||
$form->get("address3")->getData(),
|
||||
$form->get("zipcode")->getData(),
|
||||
$form->get("city")->getData(),
|
||||
$form->get("country")->getData(),
|
||||
$form->get("cellphone")->getData(),
|
||||
$form->get("phone")->getData(),
|
||||
$form->get("company")->getData(),
|
||||
$form->get("is_default")->getData(),
|
||||
$form->get("state")->getData()
|
||||
);
|
||||
}
|
||||
|
||||
public function updateViewAction($address_id)
|
||||
{
|
||||
$this->checkAuth();
|
||||
|
||||
$customer = $this->getSecurityContext()->getCustomerUser();
|
||||
$address = AddressQuery::create()->findPk($address_id);
|
||||
|
||||
if (!$address || $customer->getId() != $address->getCustomerId()) {
|
||||
return $this->generateRedirectFromRoute('default');
|
||||
}
|
||||
|
||||
$this->getParserContext()->set("address_id", $address_id);
|
||||
}
|
||||
|
||||
public function processUpdateAction($address_id)
|
||||
{
|
||||
$this->checkAuth();
|
||||
|
||||
$addressUpdate = $this->createForm(FrontForm::ADDRESS_UPDATE);
|
||||
|
||||
try {
|
||||
$customer = $this->getSecurityContext()->getCustomerUser();
|
||||
|
||||
$form = $this->validateForm($addressUpdate);
|
||||
|
||||
$address = AddressQuery::create()->findPk($address_id);
|
||||
|
||||
if (null === $address) {
|
||||
return $this->generateRedirectFromRoute('default');
|
||||
}
|
||||
|
||||
if ($address->getCustomer()->getId() != $customer->getId()) {
|
||||
return $this->generateRedirectFromRoute('default');
|
||||
}
|
||||
|
||||
$event = $this->createAddressEvent($form);
|
||||
$event->setAddress($address);
|
||||
|
||||
$this->dispatch(TheliaEvents::ADDRESS_UPDATE, $event);
|
||||
|
||||
return $this->generateSuccessRedirect($addressUpdate);
|
||||
} catch (FormValidationException $e) {
|
||||
$message = $this->getTranslator()->trans("Please check your input: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN);
|
||||
} catch (\Exception $e) {
|
||||
$message = $this->getTranslator()->trans("Sorry, an error occured: %s", ['%s' => $e->getMessage()], Front::MESSAGE_DOMAIN);
|
||||
}
|
||||
|
||||
$this->getParserContext()->set("address_id", $address_id);
|
||||
|
||||
Tlog::getInstance()->error(sprintf("Error during address creation process : %s", $message));
|
||||
|
||||
$addressUpdate->setErrorMessage($message);
|
||||
|
||||
$this->getParserContext()
|
||||
->addForm($addressUpdate)
|
||||
->setGeneralError($message)
|
||||
;
|
||||
|
||||
if ($addressUpdate->hasErrorUrl()) {
|
||||
return $this->generateErrorRedirect($addressUpdate);
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteAction($address_id)
|
||||
{
|
||||
$this->checkAuth();
|
||||
$error_message = false;
|
||||
|
||||
$customer = $this->getSecurityContext()->getCustomerUser();
|
||||
$address = AddressQuery::create()->findPk($address_id);
|
||||
|
||||
if (!$address || $customer->getId() != $address->getCustomerId()) {
|
||||
// If Ajax Request
|
||||
if ($this->getRequest()->isXmlHttpRequest()) {
|
||||
return $this->jsonResponse(
|
||||
json_encode(
|
||||
array(
|
||||
"success" => false,
|
||||
"message" => $this->getTranslator()->trans(
|
||||
"Error during address deletion process",
|
||||
[],
|
||||
Front::MESSAGE_DOMAIN
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
} else {
|
||||
return $this->generateRedirectFromRoute('default');
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$this->dispatch(TheliaEvents::ADDRESS_DELETE, new AddressEvent($address));
|
||||
} catch (\Exception $e) {
|
||||
$error_message = $e->getMessage();
|
||||
}
|
||||
|
||||
Tlog::getInstance()->error(sprintf('Error during address deletion : %s', $error_message));
|
||||
|
||||
// If Ajax Request
|
||||
if ($this->getRequest()->isXmlHttpRequest()) {
|
||||
if ($error_message) {
|
||||
$response = $this->jsonResponse(json_encode(array(
|
||||
"success" => false,
|
||||
"message" => $error_message
|
||||
)));
|
||||
} else {
|
||||
$response = $this->jsonResponse(
|
||||
json_encode([
|
||||
"success" => true,
|
||||
"message" => ""
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
return $response;
|
||||
|
||||
} else {
|
||||
return $this->generateRedirectFromRoute('default', array('view'=>'account'));
|
||||
}
|
||||
}
|
||||
|
||||
public function makeAddressDefaultAction($addressId)
|
||||
{
|
||||
$this->checkAuth();
|
||||
|
||||
$address = AddressQuery::create()
|
||||
->filterByCustomerId($this->getSecurityContext()->getCustomerUser()->getId())
|
||||
->findPk($addressId)
|
||||
;
|
||||
|
||||
if (null === $address) {
|
||||
$this->pageNotFound();
|
||||
}
|
||||
|
||||
try {
|
||||
$event = new AddressEvent($address);
|
||||
$this->dispatch(TheliaEvents::ADDRESS_DEFAULT, $event);
|
||||
} catch (\Exception $e) {
|
||||
$this->getParserContext()
|
||||
->setGeneralError($e->getMessage())
|
||||
;
|
||||
|
||||
return $this->render("account");
|
||||
}
|
||||
|
||||
return $this->generateRedirectFromRoute('default', array('view'=>'account'));
|
||||
}
|
||||
}
|
||||
237
local/modules/Front/Controller/CartController.php
Normal file
237
local/modules/Front/Controller/CartController.php
Normal file
@@ -0,0 +1,237 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
namespace Front\Controller;
|
||||
|
||||
use Front\Front;
|
||||
use Propel\Runtime\Exception\PropelException;
|
||||
use Symfony\Component\HttpFoundation\Cookie;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Thelia\Controller\Front\BaseFrontController;
|
||||
use Thelia\Core\Event\Cart\CartEvent;
|
||||
use Thelia\Core\Event\Order\OrderEvent;
|
||||
use Thelia\Core\Event\TheliaEvents;
|
||||
use Thelia\Form\CartAdd;
|
||||
use Thelia\Form\Definition\FrontForm;
|
||||
use Thelia\Form\Exception\FormValidationException;
|
||||
use Thelia\Log\Tlog;
|
||||
use Thelia\Model\AddressQuery;
|
||||
use Thelia\Model\ConfigQuery;
|
||||
use Thelia\Model\OrderPostage;
|
||||
use Thelia\Module\Exception\DeliveryException;
|
||||
use Thelia\Tools\URL;
|
||||
|
||||
class CartController extends BaseFrontController
|
||||
{
|
||||
public function addItem()
|
||||
{
|
||||
$request = $this->getRequest();
|
||||
|
||||
$cartAdd = $this->getAddCartForm($request);
|
||||
$message = null;
|
||||
|
||||
try {
|
||||
$form = $this->validateForm($cartAdd);
|
||||
|
||||
$cartEvent = $this->getCartEvent();
|
||||
|
||||
$cartEvent->bindForm($form);
|
||||
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::CART_ADDITEM, $cartEvent);
|
||||
|
||||
$this->afterModifyCart();
|
||||
|
||||
|
||||
if ($this->getRequest()->isXmlHttpRequest()) {
|
||||
$this->changeViewForAjax();
|
||||
} elseif (null !== $response = $this->generateSuccessRedirect($cartAdd)) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
} catch (PropelException $e) {
|
||||
Tlog::getInstance()->error(sprintf("Failed to add item to cart with message : %s", $e->getMessage()));
|
||||
$message = $this->getTranslator()->trans(
|
||||
"Failed to add this article to your cart, please try again",
|
||||
[],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
} catch (FormValidationException $e) {
|
||||
$message = $e->getMessage();
|
||||
}
|
||||
|
||||
if ($message) {
|
||||
$cartAdd->setErrorMessage($message);
|
||||
$this->getParserContext()->addForm($cartAdd);
|
||||
}
|
||||
}
|
||||
|
||||
public function changeItem()
|
||||
{
|
||||
$cartEvent = $this->getCartEvent();
|
||||
$cartEvent->setCartItemId($this->getRequest()->get("cart_item"));
|
||||
$cartEvent->setQuantity($this->getRequest()->get("quantity"));
|
||||
|
||||
try {
|
||||
$this->getTokenProvider()->checkToken(
|
||||
$this->getRequest()->query->get('_token')
|
||||
);
|
||||
|
||||
$this->dispatch(TheliaEvents::CART_UPDATEITEM, $cartEvent);
|
||||
|
||||
$this->afterModifyCart();
|
||||
|
||||
if ($this->getRequest()->isXmlHttpRequest()) {
|
||||
$this->changeViewForAjax();
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Tlog::getInstance()->error(sprintf("Failed to change cart item quantity: %s", $e->getMessage()));
|
||||
|
||||
$this->getParserContext()->setGeneralError($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteItem()
|
||||
{
|
||||
$cartEvent = $this->getCartEvent();
|
||||
$cartEvent->setCartItemId($this->getRequest()->get("cart_item"));
|
||||
|
||||
try {
|
||||
$this->getTokenProvider()->checkToken(
|
||||
$this->getRequest()->query->get('_token')
|
||||
);
|
||||
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::CART_DELETEITEM, $cartEvent);
|
||||
|
||||
$this->afterModifyCart();
|
||||
} catch (\Exception $e) {
|
||||
Tlog::getInstance()->error(sprintf("error during deleting cartItem with message : %s", $e->getMessage()));
|
||||
$this->getParserContext()->setGeneralError($e->getMessage());
|
||||
}
|
||||
|
||||
$this->changeViewForAjax();
|
||||
|
||||
if (null != $successUrl = $this->getRequest()->query->get('success_url')) {
|
||||
$response = $this->generateRedirect(
|
||||
URL::getInstance()->absoluteUrl($successUrl)
|
||||
);
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
protected function changeViewForAjax()
|
||||
{
|
||||
// If Ajax Request
|
||||
if ($this->getRequest()->isXmlHttpRequest()) {
|
||||
$request = $this->getRequest();
|
||||
|
||||
$view = $request->get('ajax-view', "includes/mini-cart");
|
||||
|
||||
$request->attributes->set('_view', $view);
|
||||
}
|
||||
}
|
||||
|
||||
public function changeCountry()
|
||||
{
|
||||
$redirectUrl = URL::getInstance()->absoluteUrl("/cart");
|
||||
$deliveryId = $this->getRequest()->get("country");
|
||||
$cookieName = ConfigQuery::read('front_cart_country_cookie_name', 'fcccn');
|
||||
$cookieExpires = ConfigQuery::read('front_cart_country_cookie_expires', 2592000);
|
||||
$cookieExpires = intval($cookieExpires) ?: 2592000;
|
||||
|
||||
$cookie = new Cookie($cookieName, $deliveryId, time() + $cookieExpires, '/');
|
||||
|
||||
$response = $this->generateRedirect($redirectUrl);
|
||||
$response->headers->setCookie($cookie);
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Thelia\Core\Event\Cart\CartEvent
|
||||
*/
|
||||
protected function getCartEvent()
|
||||
{
|
||||
$cart = $this->getSession()->getSessionCart($this->getDispatcher());
|
||||
|
||||
return new CartEvent($cart);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the good way to construct the cart form
|
||||
*
|
||||
* @param Request $request
|
||||
* @return CartAdd
|
||||
*/
|
||||
private function getAddCartForm(Request $request)
|
||||
{
|
||||
if ($request->isMethod("post")) {
|
||||
$cartAdd = $this->createForm(FrontForm::CART_ADD);
|
||||
} else {
|
||||
$cartAdd = $this->createForm(
|
||||
FrontForm::CART_ADD,
|
||||
"form",
|
||||
array(),
|
||||
array(
|
||||
'csrf_protection' => false,
|
||||
),
|
||||
$this->container
|
||||
);
|
||||
}
|
||||
|
||||
return $cartAdd;
|
||||
}
|
||||
|
||||
protected function afterModifyCart()
|
||||
{
|
||||
/* recalculate postage amount */
|
||||
$order = $this->getSession()->getOrder();
|
||||
if (null !== $order) {
|
||||
$deliveryModule = $order->getModuleRelatedByDeliveryModuleId();
|
||||
$deliveryAddress = AddressQuery::create()->findPk($order->getChoosenDeliveryAddress());
|
||||
|
||||
if (null !== $deliveryModule && null !== $deliveryAddress) {
|
||||
$moduleInstance = $deliveryModule->getDeliveryModuleInstance($this->container);
|
||||
|
||||
$orderEvent = new OrderEvent($order);
|
||||
|
||||
try {
|
||||
$postage = OrderPostage::loadFromPostage(
|
||||
$moduleInstance->getPostage($deliveryAddress->getCountry())
|
||||
);
|
||||
|
||||
$orderEvent->setPostage($postage->getAmount());
|
||||
$orderEvent->setPostageTax($postage->getAmountTax());
|
||||
$orderEvent->setPostageTaxRuleTitle($postage->getTaxRuleTitle());
|
||||
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent);
|
||||
} catch (DeliveryException $ex) {
|
||||
// The postage has been chosen, but changes in the cart causes an exception.
|
||||
// Reset the postage data in the order
|
||||
$orderEvent->setDeliveryModule(0);
|
||||
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_MODULE, $orderEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
84
local/modules/Front/Controller/ContactController.php
Normal file
84
local/modules/Front/Controller/ContactController.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Front\Controller;
|
||||
|
||||
use Thelia\Controller\Front\BaseFrontController;
|
||||
use Thelia\Form\Definition\FrontForm;
|
||||
use Thelia\Form\Exception\FormValidationException;
|
||||
use Thelia\Log\Tlog;
|
||||
use Thelia\Model\ConfigQuery;
|
||||
|
||||
/**
|
||||
* Class ContactController
|
||||
* @package Thelia\Controller\Front
|
||||
* @author Manuel Raynaud <manu@raynaud.io>
|
||||
*/
|
||||
class ContactController extends BaseFrontController
|
||||
{
|
||||
/**
|
||||
* send contact message
|
||||
*/
|
||||
public function sendAction()
|
||||
{
|
||||
$contactForm = $this->createForm(FrontForm::CONTACT);
|
||||
|
||||
try {
|
||||
$form = $this->validateForm($contactForm);
|
||||
|
||||
$this->getMailer()->sendSimpleEmailMessage(
|
||||
[ ConfigQuery::getStoreEmail() => $form->get('name')->getData() ],
|
||||
[ ConfigQuery::getStoreEmail() => ConfigQuery::getStoreName() ],
|
||||
$form->get('subject')->getData(),
|
||||
'',
|
||||
$form->get('message')->getData(),
|
||||
[],
|
||||
[],
|
||||
[ $form->get('email')->getData() => $form->get('name')->getData() ]
|
||||
);
|
||||
|
||||
if ($contactForm->hasSuccessUrl()) {
|
||||
return $this->generateSuccessRedirect($contactForm);
|
||||
}
|
||||
|
||||
return $this->generateRedirectFromRoute('contact.success');
|
||||
|
||||
} catch (FormValidationException $e) {
|
||||
$error_message = $e->getMessage();
|
||||
}
|
||||
|
||||
Tlog::getInstance()->error(sprintf('Error during sending contact mail : %s', $error_message));
|
||||
|
||||
$contactForm->setErrorMessage($error_message);
|
||||
|
||||
$this->getParserContext()
|
||||
->addForm($contactForm)
|
||||
->setGeneralError($error_message)
|
||||
;
|
||||
|
||||
// Redirect to error URL if defined
|
||||
if ($contactForm->hasErrorUrl()) {
|
||||
return $this->generateErrorRedirect($contactForm);
|
||||
}
|
||||
}
|
||||
}
|
||||
159
local/modules/Front/Controller/CouponController.php
Normal file
159
local/modules/Front/Controller/CouponController.php
Normal file
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
namespace Front\Controller;
|
||||
|
||||
use Front\Front;
|
||||
use Propel\Runtime\Exception\PropelException;
|
||||
use Thelia\Controller\Front\BaseFrontController;
|
||||
use Thelia\Core\Event\Coupon\CouponConsumeEvent;
|
||||
use Thelia\Core\Event\Order\OrderEvent;
|
||||
use Thelia\Core\Event\TheliaEvents;
|
||||
use Thelia\Exception\UnmatchableConditionException;
|
||||
use Thelia\Form\Definition\FrontForm;
|
||||
use Thelia\Form\Exception\FormValidationException;
|
||||
use Thelia\Log\Tlog;
|
||||
use Thelia\Model\AddressQuery;
|
||||
use Thelia\Model\OrderPostage;
|
||||
use Thelia\Module\Exception\DeliveryException;
|
||||
|
||||
/**
|
||||
* Class CouponController
|
||||
* @package Thelia\Controller\Front
|
||||
* @author Guillaume MOREL <gmorel@openstudio.fr>
|
||||
*/
|
||||
class CouponController extends BaseFrontController
|
||||
{
|
||||
/**
|
||||
* Clear all coupons.
|
||||
*/
|
||||
public function clearAllCouponsAction()
|
||||
{
|
||||
// Dispatch Event to the Action
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::COUPON_CLEAR_ALL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Coupon consuming
|
||||
*/
|
||||
public function consumeAction()
|
||||
{
|
||||
$this->checkCartNotEmpty();
|
||||
|
||||
$message = false;
|
||||
$couponCodeForm = $this->createForm(FrontForm::COUPON_CONSUME);
|
||||
|
||||
try {
|
||||
$form = $this->validateForm($couponCodeForm, 'post');
|
||||
|
||||
$couponCode = $form->get('coupon-code')->getData();
|
||||
|
||||
if (null === $couponCode || empty($couponCode)) {
|
||||
$message = true;
|
||||
throw new \Exception(
|
||||
$this->getTranslator()->trans(
|
||||
'Coupon code can\'t be empty',
|
||||
[],
|
||||
Front::MESSAGE_DOMAIN
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$couponConsumeEvent = new CouponConsumeEvent($couponCode);
|
||||
|
||||
// Dispatch Event to the Action
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::COUPON_CONSUME, $couponConsumeEvent);
|
||||
|
||||
/* recalculate postage amount */
|
||||
$order = $this->getSession()->getOrder();
|
||||
|
||||
if (null !== $order) {
|
||||
$deliveryModule = $order->getModuleRelatedByDeliveryModuleId();
|
||||
$deliveryAddress = AddressQuery::create()->findPk($order->getChoosenDeliveryAddress());
|
||||
|
||||
if (null !== $deliveryModule && null !== $deliveryAddress) {
|
||||
$moduleInstance = $deliveryModule->getDeliveryModuleInstance($this->container);
|
||||
|
||||
$orderEvent = new OrderEvent($order);
|
||||
|
||||
try {
|
||||
$postage = OrderPostage::loadFromPostage(
|
||||
$moduleInstance->getPostage($deliveryAddress->getCountry())
|
||||
);
|
||||
|
||||
$orderEvent->setPostage($postage->getAmount());
|
||||
$orderEvent->setPostageTax($postage->getAmountTax());
|
||||
$orderEvent->setPostageTaxRuleTitle($postage->getTaxRuleTitle());
|
||||
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent);
|
||||
} catch (DeliveryException $ex) {
|
||||
// The postage has been chosen, but changes dues to coupon causes an exception.
|
||||
// Reset the postage data in the order
|
||||
$orderEvent->setDeliveryModule(0);
|
||||
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_MODULE, $orderEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->generateSuccessRedirect($couponCodeForm);
|
||||
|
||||
} catch (FormValidationException $e) {
|
||||
$message = $this->getTranslator()->trans(
|
||||
'Please check your coupon code: %message',
|
||||
["%message" => $e->getMessage()],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
} catch (UnmatchableConditionException $e) {
|
||||
$message = $this->getTranslator()->trans(
|
||||
'You should <a href="%sign">sign in</a> or <a href="%register">register</a> to use this coupon',
|
||||
[
|
||||
'%sign' => $this->retrieveUrlFromRouteId('customer.login.view'),
|
||||
'%register' => $this->retrieveUrlFromRouteId('customer.create.view'),
|
||||
],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
} catch (PropelException $e) {
|
||||
$this->getParserContext()->setGeneralError($e->getMessage());
|
||||
} catch (\Exception $e) {
|
||||
$message = $this->getTranslator()->trans(
|
||||
'Sorry, an error occurred: %message',
|
||||
["%message" => $e->getMessage()],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
}
|
||||
|
||||
if ($message !== false) {
|
||||
Tlog::getInstance()->error(
|
||||
sprintf("Error during order delivery process : %s. Exception was %s", $message, $e->getMessage())
|
||||
);
|
||||
|
||||
$couponCodeForm->setErrorMessage($message);
|
||||
|
||||
$this->getParserContext()
|
||||
->addForm($couponCodeForm)
|
||||
->setGeneralError($message);
|
||||
}
|
||||
|
||||
return $this->generateErrorRedirect($couponCodeForm);
|
||||
}
|
||||
}
|
||||
610
local/modules/Front/Controller/CustomerController.php
Normal file
610
local/modules/Front/Controller/CustomerController.php
Normal file
@@ -0,0 +1,610 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
namespace Front\Controller;
|
||||
|
||||
use Front\Front;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Thelia\Controller\Front\BaseFrontController;
|
||||
use Thelia\Core\Event\Customer\CustomerCreateOrUpdateEvent;
|
||||
use Thelia\Core\Event\Customer\CustomerEvent;
|
||||
use Thelia\Core\Event\Customer\CustomerLoginEvent;
|
||||
use Thelia\Core\Event\LostPasswordEvent;
|
||||
use Thelia\Core\Event\Newsletter\NewsletterEvent;
|
||||
use Thelia\Core\Event\TheliaEvents;
|
||||
use Thelia\Core\Security\Authentication\CustomerUsernamePasswordFormAuthenticator;
|
||||
use Thelia\Core\Security\Exception\AuthenticationException;
|
||||
use Thelia\Core\Security\Exception\CustomerNotConfirmedException;
|
||||
use Thelia\Core\Security\Exception\UsernameNotFoundException;
|
||||
use Thelia\Core\Security\Exception\WrongPasswordException;
|
||||
use Thelia\Form\CustomerLogin;
|
||||
use Thelia\Form\Definition\FrontForm;
|
||||
use Thelia\Form\Exception\FormValidationException;
|
||||
use Thelia\Log\Tlog;
|
||||
use Thelia\Model\ConfigQuery;
|
||||
use Thelia\Model\Customer;
|
||||
use Thelia\Model\CustomerQuery;
|
||||
use Thelia\Model\NewsletterQuery;
|
||||
use Thelia\Tools\RememberMeTrait;
|
||||
use Thelia\Tools\URL;
|
||||
|
||||
/**
|
||||
* Class CustomerController
|
||||
* @package Thelia\Controller\Front
|
||||
* @author Manuel Raynaud <manu@raynaud.io>
|
||||
*/
|
||||
class CustomerController extends BaseFrontController
|
||||
{
|
||||
use RememberMeTrait;
|
||||
|
||||
/**
|
||||
* Display the register template if no customer logged
|
||||
*/
|
||||
public function viewLoginAction()
|
||||
{
|
||||
if ($this->getSecurityContext()->hasCustomerUser()) {
|
||||
// Redirect to home page
|
||||
return $this->generateRedirect(URL::getInstance()->getIndexPage());
|
||||
}
|
||||
|
||||
return $this->render("login");
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the register template if no customer logged
|
||||
*/
|
||||
public function viewRegisterAction()
|
||||
{
|
||||
if ($this->getSecurityContext()->hasCustomerUser()) {
|
||||
// Redirect to home page
|
||||
return $this->generateRedirect(URL::getInstance()->getIndexPage());
|
||||
}
|
||||
|
||||
return $this->render("register");
|
||||
}
|
||||
|
||||
public function newPasswordAction()
|
||||
{
|
||||
$passwordLost = $this->createForm(FrontForm::CUSTOMER_LOST_PASSWORD);
|
||||
|
||||
if (! $this->getSecurityContext()->hasCustomerUser()) {
|
||||
try {
|
||||
$form = $this->validateForm($passwordLost);
|
||||
|
||||
$event = new LostPasswordEvent($form->get("email")->getData());
|
||||
|
||||
$this->dispatch(TheliaEvents::LOST_PASSWORD, $event);
|
||||
|
||||
return $this->generateSuccessRedirect($passwordLost);
|
||||
|
||||
} catch (FormValidationException $e) {
|
||||
$message = $this->getTranslator()->trans(
|
||||
"Please check your input: %s",
|
||||
[
|
||||
'%s' => $e->getMessage()
|
||||
],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
$message = $this->getTranslator()->trans(
|
||||
"Sorry, an error occurred: %s",
|
||||
[
|
||||
'%s' => $e->getMessage()
|
||||
],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
}
|
||||
|
||||
if ($message !== false) {
|
||||
Tlog::getInstance()->error(
|
||||
sprintf(
|
||||
"Error during customer creation process : %s. Exception was %s",
|
||||
$message,
|
||||
$e->getMessage()
|
||||
)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$message = $this->getTranslator()->trans(
|
||||
"You're currently logged in. Please log out before requesting a new password.",
|
||||
[],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
}
|
||||
|
||||
$passwordLost->setErrorMessage($message);
|
||||
|
||||
$this->getParserContext()
|
||||
->addForm($passwordLost)
|
||||
->setGeneralError($message)
|
||||
;
|
||||
|
||||
// Redirect to error URL if defined
|
||||
if ($passwordLost->hasErrorUrl()) {
|
||||
return $this->generateErrorRedirect($passwordLost);
|
||||
}
|
||||
}
|
||||
|
||||
public function newPasswordSentAction()
|
||||
{
|
||||
$this->getParser()->assign('password_sent', true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new customer.
|
||||
* On success, redirect to success_url if exists, otherwise, display the same view again.
|
||||
*/
|
||||
public function createAction()
|
||||
{
|
||||
if (! $this->getSecurityContext()->hasCustomerUser()) {
|
||||
$customerCreation = $this->createForm(FrontForm::CUSTOMER_CREATE);
|
||||
|
||||
try {
|
||||
$form = $this->validateForm($customerCreation, "post");
|
||||
|
||||
$customerCreateEvent = $this->createEventInstance($form->getData());
|
||||
|
||||
$this->dispatch(TheliaEvents::CUSTOMER_CREATEACCOUNT, $customerCreateEvent);
|
||||
|
||||
$newCustomer = $customerCreateEvent->getCustomer();
|
||||
|
||||
// Newsletter
|
||||
if (true === $form->get('newsletter')->getData()) {
|
||||
$newsletterEmail = $newCustomer->getEmail();
|
||||
$nlEvent = new NewsletterEvent(
|
||||
$newsletterEmail,
|
||||
$this->getRequest()->getSession()->getLang()->getLocale()
|
||||
);
|
||||
$nlEvent->setFirstname($newCustomer->getFirstname());
|
||||
$nlEvent->setLastname($newCustomer->getLastname());
|
||||
|
||||
// Security : Check if this new Email address already exist
|
||||
if (null !== $newsletter = NewsletterQuery::create()->findOneByEmail($newsletterEmail)) {
|
||||
$nlEvent->setId($newsletter->getId());
|
||||
$this->dispatch(TheliaEvents::NEWSLETTER_UPDATE, $nlEvent);
|
||||
} else {
|
||||
$this->dispatch(TheliaEvents::NEWSLETTER_SUBSCRIBE, $nlEvent);
|
||||
}
|
||||
}
|
||||
|
||||
if (ConfigQuery::isCustomerEmailConfirmationEnable() && ! $newCustomer->getEnable()) {
|
||||
$response = $this->generateRedirectFromRoute('customer.login.view');
|
||||
} else {
|
||||
$this->processLogin($customerCreateEvent->getCustomer());
|
||||
|
||||
$cart = $this->getSession()->getSessionCart($this->getDispatcher());
|
||||
if ($cart->getCartItems()->count() > 0) {
|
||||
$response = $this->generateRedirectFromRoute('cart.view');
|
||||
} else {
|
||||
$response = $this->generateSuccessRedirect($customerCreation);
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
} catch (FormValidationException $e) {
|
||||
$message = $this->getTranslator()->trans(
|
||||
"Please check your input: %s",
|
||||
[
|
||||
'%s' => $e->getMessage()
|
||||
],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
$message = $this->getTranslator()->trans(
|
||||
"Sorry, an error occured: %s",
|
||||
[
|
||||
'%s' => $e->getMessage()
|
||||
],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
}
|
||||
|
||||
Tlog::getInstance()->error(
|
||||
sprintf(
|
||||
"Error during customer creation process : %s. Exception was %s",
|
||||
$message,
|
||||
$e->getMessage()
|
||||
)
|
||||
);
|
||||
|
||||
$customerCreation->setErrorMessage($message);
|
||||
|
||||
$this->getParserContext()
|
||||
->addForm($customerCreation)
|
||||
->setGeneralError($message)
|
||||
;
|
||||
|
||||
// Redirect to error URL if defined
|
||||
if ($customerCreation->hasErrorUrl()) {
|
||||
return $this->generateErrorRedirect($customerCreation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepare customer data update.
|
||||
*/
|
||||
public function viewAction()
|
||||
{
|
||||
$this->checkAuth();
|
||||
|
||||
/** @var Customer $customer */
|
||||
$customer = $this->getSecurityContext()->getCustomerUser();
|
||||
$data = array(
|
||||
'id' => $customer->getId(),
|
||||
'title' => $customer->getTitleId(),
|
||||
'firstname' => $customer->getFirstName(),
|
||||
'lastname' => $customer->getLastName(),
|
||||
'email' => $customer->getEmail(),
|
||||
'email_confirm' => $customer->getEmail(),
|
||||
'newsletter' => null !== NewsletterQuery::create()->findOneByEmail($customer->getEmail()),
|
||||
);
|
||||
|
||||
$customerProfileUpdateForm = $this->createForm(FrontForm::CUSTOMER_PROFILE_UPDATE, 'form', $data);
|
||||
|
||||
// Pass it to the parser
|
||||
$this->getParserContext()->addForm($customerProfileUpdateForm);
|
||||
}
|
||||
|
||||
public function updatePasswordAction()
|
||||
{
|
||||
if ($this->getSecurityContext()->hasCustomerUser()) {
|
||||
$customerPasswordUpdateForm = $this->createForm(FrontForm::CUSTOMER_PASSWORD_UPDATE);
|
||||
|
||||
try {
|
||||
/** @var Customer $customer */
|
||||
$customer = $this->getSecurityContext()->getCustomerUser();
|
||||
|
||||
$form = $this->validateForm($customerPasswordUpdateForm, "post");
|
||||
|
||||
$customerChangeEvent = $this->createEventInstance($form->getData());
|
||||
$customerChangeEvent->setCustomer($customer);
|
||||
$this->dispatch(TheliaEvents::CUSTOMER_UPDATEPROFILE, $customerChangeEvent);
|
||||
|
||||
return $this->generateSuccessRedirect($customerPasswordUpdateForm);
|
||||
|
||||
} catch (FormValidationException $e) {
|
||||
$message = $this->getTranslator()->trans(
|
||||
"Please check your input: %s",
|
||||
[
|
||||
'%s' => $e->getMessage()
|
||||
],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
$message = $this->getTranslator()->trans(
|
||||
"Sorry, an error occured: %s",
|
||||
[
|
||||
'%s' => $e->getMessage()
|
||||
],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
}
|
||||
|
||||
Tlog::getInstance()->error(
|
||||
sprintf(
|
||||
"Error during customer password modification process : %s.",
|
||||
$message
|
||||
)
|
||||
);
|
||||
|
||||
$customerPasswordUpdateForm->setErrorMessage($message);
|
||||
|
||||
$this->getParserContext()
|
||||
->addForm($customerPasswordUpdateForm)
|
||||
->setGeneralError($message)
|
||||
;
|
||||
|
||||
// Redirect to error URL if defined
|
||||
if ($customerPasswordUpdateForm->hasErrorUrl()) {
|
||||
return $this->generateErrorRedirect($customerPasswordUpdateForm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function updateAction()
|
||||
{
|
||||
if ($this->getSecurityContext()->hasCustomerUser()) {
|
||||
$customerProfileUpdateForm = $this->createForm(FrontForm::CUSTOMER_PROFILE_UPDATE);
|
||||
|
||||
try {
|
||||
/** @var Customer $customer */
|
||||
$customer = $this->getSecurityContext()->getCustomerUser();
|
||||
$newsletterOldEmail = $customer->getEmail();
|
||||
|
||||
$form = $this->validateForm($customerProfileUpdateForm, "post");
|
||||
|
||||
$customerChangeEvent = $this->createEventInstance($form->getData());
|
||||
$customerChangeEvent->setCustomer($customer);
|
||||
|
||||
$customerChangeEvent->setEmailUpdateAllowed(
|
||||
(intval(ConfigQuery::read('customer_change_email', 0))) ? true : false
|
||||
);
|
||||
|
||||
$this->dispatch(TheliaEvents::CUSTOMER_UPDATEPROFILE, $customerChangeEvent);
|
||||
|
||||
$updatedCustomer = $customerChangeEvent->getCustomer();
|
||||
|
||||
// Newsletter
|
||||
if (true === $form->get('newsletter')->getData()) {
|
||||
$nlEvent = new NewsletterEvent(
|
||||
$updatedCustomer->getEmail(),
|
||||
$this->getRequest()->getSession()->getLang()->getLocale()
|
||||
);
|
||||
$nlEvent->setFirstname($updatedCustomer->getFirstname());
|
||||
$nlEvent->setLastname($updatedCustomer->getLastname());
|
||||
|
||||
if (null !== $newsletter = NewsletterQuery::create()->findOneByEmail($newsletterOldEmail)) {
|
||||
$nlEvent->setId($newsletter->getId());
|
||||
$this->dispatch(TheliaEvents::NEWSLETTER_UPDATE, $nlEvent);
|
||||
} else {
|
||||
$this->dispatch(TheliaEvents::NEWSLETTER_SUBSCRIBE, $nlEvent);
|
||||
}
|
||||
} else {
|
||||
if (null !== $newsletter = NewsletterQuery::create()->findOneByEmail($newsletterOldEmail)) {
|
||||
$nlEvent = new NewsletterEvent(
|
||||
$updatedCustomer->getEmail(),
|
||||
$this->getRequest()->getSession()->getLang()->getLocale()
|
||||
);
|
||||
$nlEvent->setId($newsletter->getId());
|
||||
$this->dispatch(TheliaEvents::NEWSLETTER_UNSUBSCRIBE, $nlEvent);
|
||||
}
|
||||
}
|
||||
|
||||
$this->processLogin($updatedCustomer);
|
||||
|
||||
return $this->generateSuccessRedirect($customerProfileUpdateForm);
|
||||
|
||||
} catch (FormValidationException $e) {
|
||||
$message = $this->getTranslator()->trans(
|
||||
"Please check your input: %s",
|
||||
[
|
||||
'%s' => $e->getMessage()
|
||||
],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
$message = $this->getTranslator()->trans(
|
||||
"Sorry, an error occured: %s",
|
||||
[
|
||||
'%s' => $e->getMessage()
|
||||
],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
}
|
||||
|
||||
Tlog::getInstance()->error(sprintf("Error during customer modification process : %s.", $message));
|
||||
|
||||
$customerProfileUpdateForm->setErrorMessage($message);
|
||||
|
||||
$this->getParserContext()
|
||||
->addForm($customerProfileUpdateForm)
|
||||
->setGeneralError($message)
|
||||
;
|
||||
|
||||
// Redirect to error URL if defined
|
||||
if ($customerProfileUpdateForm->hasErrorUrl()) {
|
||||
return $this->generateErrorRedirect($customerProfileUpdateForm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform user login. On a successful login, the user is redirected to the URL
|
||||
* found in the success_url form parameter, or / if none was found.
|
||||
*
|
||||
* If login is not successfull, the same view is displayed again.
|
||||
*
|
||||
*/
|
||||
public function loginAction()
|
||||
{
|
||||
if (!$this->getSecurityContext()->hasCustomerUser()) {
|
||||
$request = $this->getRequest();
|
||||
$customerLoginForm = new CustomerLogin($request);
|
||||
|
||||
try {
|
||||
$form = $this->validateForm($customerLoginForm, "post");
|
||||
|
||||
// If User is a new customer
|
||||
if ($form->get('account')->getData() == 0 && $form->get("email")->getErrors()->count() == 0) {
|
||||
return $this->generateRedirectFromRoute(
|
||||
"customer.create.process",
|
||||
["email" => $form->get("email")->getData()]
|
||||
);
|
||||
} else {
|
||||
try {
|
||||
$authenticator = new CustomerUsernamePasswordFormAuthenticator($request, $customerLoginForm);
|
||||
|
||||
/** @var Customer $customer */
|
||||
$customer = $authenticator->getAuthentifiedUser();
|
||||
|
||||
$this->processLogin($customer);
|
||||
|
||||
if (intval($form->get('remember_me')->getData()) > 0) {
|
||||
// If a remember me field if present and set in the form, create
|
||||
// the cookie thant store "remember me" information
|
||||
$this->createRememberMeCookie(
|
||||
$customer,
|
||||
$this->getRememberMeCookieName(),
|
||||
$this->getRememberMeCookieExpiration()
|
||||
);
|
||||
}
|
||||
|
||||
return $this->generateSuccessRedirect($customerLoginForm);
|
||||
|
||||
} catch (UsernameNotFoundException $e) {
|
||||
$message = $this->getTranslator()->trans(
|
||||
"Wrong email or password. Please try again",
|
||||
[],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
} catch (WrongPasswordException $e) {
|
||||
$message = $this->getTranslator()->trans(
|
||||
"Wrong email or password. Please try again",
|
||||
[],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
} catch (CustomerNotConfirmedException $e) {
|
||||
if ($e->getUser() !== null) {
|
||||
// Send the confirmation email again
|
||||
$this->getDispatcher()->dispatch(
|
||||
TheliaEvents::SEND_ACCOUNT_CONFIRMATION_EMAIL,
|
||||
new CustomerEvent($e->getUser())
|
||||
);
|
||||
}
|
||||
$message = $this->getTranslator()->trans(
|
||||
"Your account is not yet confirmed. A confirmation email has been sent to your email address, please check your mailbox",
|
||||
[],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
} catch (AuthenticationException $e) {
|
||||
$message = $this->getTranslator()->trans(
|
||||
"Wrong email or password. Please try again",
|
||||
[],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
} catch (FormValidationException $e) {
|
||||
$message = $this->getTranslator()->trans(
|
||||
"Please check your input: %s",
|
||||
['%s' => $e->getMessage()],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
} catch (\Exception $e) {
|
||||
$message = $this->getTranslator()->trans(
|
||||
"Sorry, an error occured: %s",
|
||||
['%s' => $e->getMessage()],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
}
|
||||
|
||||
Tlog::getInstance()->error(
|
||||
sprintf(
|
||||
"Error during customer login process : %s. Exception was %s",
|
||||
$message,
|
||||
$e->getMessage()
|
||||
)
|
||||
);
|
||||
|
||||
$customerLoginForm->setErrorMessage($message);
|
||||
|
||||
$this->getParserContext()->addForm($customerLoginForm);
|
||||
|
||||
if ($customerLoginForm->hasErrorUrl()) {
|
||||
return $this->generateErrorRedirect($customerLoginForm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform customer logout.
|
||||
*/
|
||||
public function logoutAction()
|
||||
{
|
||||
if ($this->getSecurityContext()->hasCustomerUser()) {
|
||||
$this->dispatch(TheliaEvents::CUSTOMER_LOGOUT);
|
||||
}
|
||||
|
||||
$this->clearRememberMeCookie($this->getRememberMeCookieName());
|
||||
|
||||
// Redirect to home page
|
||||
return $this->generateRedirect(URL::getInstance()->getIndexPage());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $token
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
* @throws \Exception
|
||||
* @throws \Propel\Runtime\Exception\PropelException
|
||||
*/
|
||||
public function confirmCustomerAction($token)
|
||||
{
|
||||
/** @var Customer $customer */
|
||||
if (null === $customer = CustomerQuery::create()->findOneByConfirmationToken($token)) {
|
||||
throw new NotFoundHttpException();
|
||||
}
|
||||
|
||||
$customer
|
||||
->setEnable(true)
|
||||
->save()
|
||||
;
|
||||
|
||||
// Clear form error context
|
||||
|
||||
return $this->generateRedirectFromRoute('customer.login.view', [ 'validation_done' => 1 ]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Dispatch event for customer login action
|
||||
*
|
||||
* @param Customer $customer
|
||||
*/
|
||||
protected function processLogin(Customer $customer)
|
||||
{
|
||||
$this->dispatch(TheliaEvents::CUSTOMER_LOGIN, new CustomerLoginEvent($customer));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return \Thelia\Core\Event\Customer\CustomerCreateOrUpdateEvent
|
||||
*/
|
||||
private function createEventInstance($data)
|
||||
{
|
||||
$customerCreateEvent = new CustomerCreateOrUpdateEvent(
|
||||
isset($data["title"])?$data["title"]:null,
|
||||
isset($data["firstname"])?$data["firstname"]:null,
|
||||
isset($data["lastname"])?$data["lastname"]:null,
|
||||
isset($data["address1"])?$data["address1"]:null,
|
||||
isset($data["address2"])?$data["address2"]:null,
|
||||
isset($data["address3"])?$data["address3"]:null,
|
||||
isset($data["phone"])?$data["phone"]:null,
|
||||
isset($data["cellphone"])?$data["cellphone"]:null,
|
||||
isset($data["zipcode"])?$data["zipcode"]:null,
|
||||
isset($data["city"])?$data["city"]:null,
|
||||
isset($data["country"])?$data["country"]:null,
|
||||
isset($data["email"])?$data["email"]:null,
|
||||
isset($data["password"]) ? $data["password"]:null,
|
||||
$this->getRequest()->getSession()->getLang()->getId(),
|
||||
isset($data["reseller"])?$data["reseller"]:null,
|
||||
isset($data["sponsor"])?$data["sponsor"]:null,
|
||||
isset($data["discount"])?$data["discount"]:null,
|
||||
isset($data["company"])?$data["company"]:null,
|
||||
null,
|
||||
isset($data["state"])?$data["state"]:null
|
||||
);
|
||||
|
||||
return $customerCreateEvent;
|
||||
}
|
||||
|
||||
|
||||
protected function getRememberMeCookieName()
|
||||
{
|
||||
return ConfigQuery::read('customer_remember_me_cookie_name', 'crmcn');
|
||||
}
|
||||
|
||||
protected function getRememberMeCookieExpiration()
|
||||
{
|
||||
return ConfigQuery::read('customer_remember_me_cookie_expiration', 2592000 /* 1 month */);
|
||||
}
|
||||
}
|
||||
202
local/modules/Front/Controller/FeedController.php
Normal file
202
local/modules/Front/Controller/FeedController.php
Normal file
@@ -0,0 +1,202 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* This file is part of the Thelia package. */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : dev@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* For the full copyright and license information, please view the LICENSE.txt */
|
||||
/* file that was distributed with this source code. */
|
||||
/*************************************************************************************/
|
||||
|
||||
|
||||
namespace Front\Controller;
|
||||
|
||||
use Doctrine\Common\Cache\FilesystemCache;
|
||||
use Thelia\Controller\Front\BaseFrontController;
|
||||
use Thelia\Core\HttpFoundation\Request;
|
||||
use Thelia\Core\HttpFoundation\Response;
|
||||
use Thelia\Model\BrandQuery;
|
||||
use Thelia\Model\FolderQuery;
|
||||
use Thelia\Model\CategoryQuery;
|
||||
use Thelia\Model\ConfigQuery;
|
||||
use Thelia\Model\Lang;
|
||||
use Thelia\Model\LangQuery;
|
||||
|
||||
/**
|
||||
* Controller uses to generate RSS Feeds
|
||||
*
|
||||
* A default cache of 2 hours is used to avoid attack. You can flush cache if you have `ADMIN` role and pass flush=1 in
|
||||
* query string parameter.
|
||||
*
|
||||
* @package Front\Controller
|
||||
* @author Julien Chanséaume <jchanseaume@openstudio.fr>
|
||||
*/
|
||||
class FeedController extends BaseFrontController {
|
||||
|
||||
|
||||
/**
|
||||
* Folder name for feeds cache
|
||||
*/
|
||||
const FEED_CACHE_DIR = "feeds";
|
||||
|
||||
/**
|
||||
* Key prefix for feed cache
|
||||
*/
|
||||
const FEED_CACHE_KEY = "feed";
|
||||
|
||||
|
||||
/**
|
||||
* render the RSS feed
|
||||
*
|
||||
* @param $context string The context of the feed : catalog, content. default: catalog
|
||||
* @param $lang string The lang of the feed : fr_FR, en_US, ... default: default language of the site
|
||||
* @param $id string The id of the parent element. The id of the main parent category for catalog context.
|
||||
* The id of the content folder for content context
|
||||
* @return Response
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function generateAction($context, $lang, $id)
|
||||
{
|
||||
|
||||
/** @var Request $request */
|
||||
$request = $this->getRequest();
|
||||
|
||||
// context
|
||||
if ("" === $context){
|
||||
$context = "catalog";
|
||||
} else if (! in_array($context, array("catalog", "content", "brand")) ){
|
||||
$this->pageNotFound();
|
||||
}
|
||||
|
||||
// the locale : fr_FR, en_US,
|
||||
if ("" !== $lang) {
|
||||
if (! $this->checkLang($lang)){
|
||||
$this->pageNotFound();
|
||||
}
|
||||
} else {
|
||||
try{
|
||||
$lang = Lang::getDefaultLanguage();
|
||||
$lang = $lang->getLocale();
|
||||
} catch (\RuntimeException $ex){
|
||||
// @todo generate error page
|
||||
throw new \RuntimeException("No default language is defined. Please define one.");
|
||||
}
|
||||
}
|
||||
if (null === $lang = LangQuery::create()->findOneByLocale($lang)){
|
||||
$this->pageNotFound();
|
||||
}
|
||||
$lang = $lang->getId();
|
||||
|
||||
// check if element exists and is visible
|
||||
if ("" !== $id){
|
||||
if (false === $this->checkId($context, $id)){
|
||||
$this->pageNotFound();
|
||||
}
|
||||
}
|
||||
|
||||
$flush = $request->query->get("flush", "");
|
||||
|
||||
// check if feed already in cache
|
||||
$cacheContent = false;
|
||||
|
||||
$cacheDir = $this->getCacheDir();
|
||||
$cacheKey = self::FEED_CACHE_KEY . $lang . $context . $id;
|
||||
$cacheExpire = intval(ConfigQuery::read("feed_ttl", '7200')) ?: 7200;
|
||||
|
||||
$cacheDriver = new FilesystemCache($cacheDir);
|
||||
if (!($this->checkAdmin() && "" !== $flush)){
|
||||
$cacheContent = $cacheDriver->fetch($cacheKey);
|
||||
} else {
|
||||
$cacheDriver->delete($cacheKey);
|
||||
}
|
||||
|
||||
// if not in cache
|
||||
if (false === $cacheContent){
|
||||
// render the view
|
||||
$cacheContent = $this->renderRaw(
|
||||
"feed",
|
||||
array(
|
||||
"_context_" => $context,
|
||||
"_lang_" => $lang,
|
||||
"_id_" => $id
|
||||
)
|
||||
);
|
||||
// save cache
|
||||
$cacheDriver->save($cacheKey, $cacheContent, $cacheExpire);
|
||||
}
|
||||
|
||||
$response = new Response();
|
||||
$response->setContent($cacheContent);
|
||||
$response->headers->set('Content-Type', 'application/rss+xml');
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get the cache directory for feeds
|
||||
*
|
||||
* @return mixed|string
|
||||
*/
|
||||
protected function getCacheDir()
|
||||
{
|
||||
$cacheDir = $this->container->getParameter("kernel.cache_dir");
|
||||
$cacheDir = rtrim($cacheDir, '/');
|
||||
$cacheDir .= '/' . self::FEED_CACHE_DIR . '/';
|
||||
|
||||
return $cacheDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if current user has ADMIN role
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function checkAdmin(){
|
||||
return $this->getSecurityContext()->hasAdminUser();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if a lang is used
|
||||
*
|
||||
* @param $lang string The lang code. e.g.: fr
|
||||
* @return bool true if the language is used, otherwise false
|
||||
*/
|
||||
private function checkLang($lang)
|
||||
{
|
||||
// load locals
|
||||
$lang = LangQuery::create()
|
||||
->findOneByLocale($lang);
|
||||
|
||||
return (null !== $lang);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if the element exists and is visible
|
||||
*
|
||||
* @param $context string catalog or content
|
||||
* @param $id string id of the element
|
||||
* @return bool
|
||||
*/
|
||||
private function checkId($context, $id)
|
||||
{
|
||||
$ret = false;
|
||||
if (is_numeric($id)){
|
||||
if ("catalog" === $context){
|
||||
$cat = CategoryQuery::create()->findPk($id);
|
||||
$ret = (null !== $cat && $cat->getVisible());
|
||||
} elseif ("brand" === $context) {
|
||||
$brand = BrandQuery::create()->findPk($id);
|
||||
$ret = (null !== $brand && $brand->getVisible());
|
||||
} else {
|
||||
$folder = FolderQuery::create()->findPk($id);
|
||||
$ret = (null !== $folder && $folder->getVisible());
|
||||
}
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
164
local/modules/Front/Controller/NewsletterController.php
Normal file
164
local/modules/Front/Controller/NewsletterController.php
Normal file
@@ -0,0 +1,164 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Front\Controller;
|
||||
|
||||
use Front\Front;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Thelia\Controller\Front\BaseFrontController;
|
||||
use Thelia\Core\Event\Newsletter\NewsletterEvent;
|
||||
use Thelia\Core\Event\TheliaEvents;
|
||||
use Thelia\Form\Definition\FrontForm;
|
||||
use Thelia\Log\Tlog;
|
||||
use Thelia\Model\Customer;
|
||||
use Thelia\Model\NewsletterQuery;
|
||||
|
||||
/**
|
||||
* Class NewsletterController
|
||||
* @package Thelia\Controller\Front
|
||||
* @author Manuel Raynaud <manu@raynaud.io>, Franck Allimant <franck@cqfdev.fr>
|
||||
*/
|
||||
class NewsletterController extends BaseFrontController
|
||||
{
|
||||
/**
|
||||
* @since 2.3.0-alpha2
|
||||
*/
|
||||
public function unsubscribeAction()
|
||||
{
|
||||
$errorMessage = false;
|
||||
|
||||
$newsletterForm = $this->createForm(FrontForm::NEWSLETTER_UNSUBSCRIBE);
|
||||
|
||||
try {
|
||||
$form = $this->validateForm($newsletterForm);
|
||||
|
||||
$email = $form->get('email')->getData();
|
||||
|
||||
if (null !== $newsletter = NewsletterQuery::create()->findOneByEmail($email)) {
|
||||
$event = new NewsletterEvent(
|
||||
$email,
|
||||
$this->getRequest()->getSession()->getLang()->getLocale()
|
||||
);
|
||||
|
||||
$event->setId($newsletter->getId());
|
||||
|
||||
$this->dispatch(TheliaEvents::NEWSLETTER_UNSUBSCRIBE, $event);
|
||||
|
||||
// If a success URL is defined in the form, redirect to it, otherwise use the defaut view
|
||||
if ($newsletterForm->hasSuccessUrl() && !$this->getRequest()->isXmlHttpRequest()) {
|
||||
return $this->generateSuccessRedirect($newsletterForm);
|
||||
}
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$errorMessage = $e->getMessage();
|
||||
|
||||
Tlog::getInstance()->error(sprintf('Error during newsletter unsubscription : %s', $errorMessage));
|
||||
|
||||
$newsletterForm->setErrorMessage($errorMessage);
|
||||
}
|
||||
|
||||
// If Ajax Request
|
||||
if ($this->getRequest()->isXmlHttpRequest()) {
|
||||
return new JsonResponse([
|
||||
"success" => ($errorMessage) ? false : true,
|
||||
"message" => ($errorMessage) ? $errorMessage : $this->getTranslator()->trans(
|
||||
"Your subscription to our newsletter has been canceled.",
|
||||
[],
|
||||
Front::MESSAGE_DOMAIN
|
||||
)
|
||||
], ($errorMessage) ? 500 : 200);
|
||||
}
|
||||
|
||||
$this->getParserContext()
|
||||
->setGeneralError($errorMessage)
|
||||
->addForm($newsletterForm);
|
||||
|
||||
// If an error URL is defined in the form, redirect to it, otherwise use the defaut view
|
||||
if ($errorMessage && $newsletterForm->hasErrorUrl()) {
|
||||
return $this->generateErrorRedirect($newsletterForm);
|
||||
}
|
||||
}
|
||||
|
||||
public function subscribeAction()
|
||||
{
|
||||
$errorMessage = false;
|
||||
|
||||
$newsletterForm = $this->createForm(FrontForm::NEWSLETTER);
|
||||
|
||||
try {
|
||||
$form = $this->validateForm($newsletterForm);
|
||||
|
||||
$event = new NewsletterEvent(
|
||||
$form->get('email')->getData(),
|
||||
$this->getRequest()->getSession()->getLang()->getLocale()
|
||||
);
|
||||
|
||||
/** @var Customer $customer */
|
||||
if (null !== $customer = $this->getSecurityContext()->getCustomerUser()) {
|
||||
$event
|
||||
->setFirstname($customer->getFirstname())
|
||||
->setLastname($customer->getLastname())
|
||||
;
|
||||
} else {
|
||||
$event
|
||||
->setFirstname($form->get('firstname')->getData())
|
||||
->setLastname($form->get('lastname')->getData())
|
||||
;
|
||||
}
|
||||
|
||||
$this->dispatch(TheliaEvents::NEWSLETTER_SUBSCRIBE, $event);
|
||||
|
||||
// If a success URL is defined in the form, redirect to it, otherwise use the defaut view
|
||||
if ($newsletterForm->hasSuccessUrl() && ! $this->getRequest()->isXmlHttpRequest()) {
|
||||
return $this->generateSuccessRedirect($newsletterForm);
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$errorMessage = $e->getMessage();
|
||||
|
||||
Tlog::getInstance()->error(sprintf('Error during newsletter subscription : %s', $errorMessage));
|
||||
|
||||
$newsletterForm->setErrorMessage($errorMessage);
|
||||
}
|
||||
|
||||
// If Ajax Request
|
||||
if ($this->getRequest()->isXmlHttpRequest()) {
|
||||
return new JsonResponse([
|
||||
"success" => ($errorMessage) ? false : true,
|
||||
"message" => ($errorMessage) ? $errorMessage : $this->getTranslator()->trans(
|
||||
"Thanks for signing up! We'll keep you posted whenever we have any new updates.",
|
||||
[],
|
||||
Front::MESSAGE_DOMAIN
|
||||
)
|
||||
], ($errorMessage) ? 500 : 200);
|
||||
}
|
||||
|
||||
$this->getParserContext()
|
||||
->setGeneralError($errorMessage)
|
||||
->addForm($newsletterForm);
|
||||
|
||||
// If an error URL is defined in the form, redirect to it, otherwise use the defaut view
|
||||
if ($errorMessage && $newsletterForm->hasErrorUrl()) {
|
||||
return $this->generateErrorRedirect($newsletterForm);
|
||||
}
|
||||
}
|
||||
}
|
||||
604
local/modules/Front/Controller/OrderController.php
Normal file
604
local/modules/Front/Controller/OrderController.php
Normal file
@@ -0,0 +1,604 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* */
|
||||
/* Thelia */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : info@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* This program is free software; you can redistribute it and/or modify */
|
||||
/* it under the terms of the GNU General Public License as published by */
|
||||
/* the Free Software Foundation; either version 3 of the License */
|
||||
/* */
|
||||
/* This program is distributed in the hope that it will be useful, */
|
||||
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||
/* GNU General Public License for more details. */
|
||||
/* */
|
||||
/* You should have received a copy of the GNU General Public License */
|
||||
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||
/* */
|
||||
/*************************************************************************************/
|
||||
namespace Front\Controller;
|
||||
|
||||
use Front\Front;
|
||||
use Propel\Runtime\ActiveQuery\Criteria;
|
||||
use Propel\Runtime\Exception\PropelException;
|
||||
use Symfony\Component\HttpFoundation\Response as BaseResponse;
|
||||
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
|
||||
use Thelia\Controller\Front\BaseFrontController;
|
||||
use Thelia\Core\Event\Delivery\DeliveryPostageEvent;
|
||||
use Thelia\Core\Event\Order\OrderEvent;
|
||||
use Thelia\Core\Event\Product\VirtualProductOrderDownloadResponseEvent;
|
||||
use Thelia\Core\Event\TheliaEvents;
|
||||
use Thelia\Core\HttpFoundation\Request;
|
||||
use Thelia\Exception\TheliaProcessException;
|
||||
use Thelia\Form\Definition\FrontForm;
|
||||
use Thelia\Form\Exception\FormValidationException;
|
||||
use Thelia\Log\Tlog;
|
||||
use Thelia\Model\Address;
|
||||
use Thelia\Model\AddressQuery;
|
||||
use Thelia\Model\AreaDeliveryModuleQuery;
|
||||
use Thelia\Model\ConfigQuery;
|
||||
use Thelia\Model\ModuleQuery;
|
||||
use Thelia\Model\Order;
|
||||
use Thelia\Model\OrderProductQuery;
|
||||
use Thelia\Model\OrderQuery;
|
||||
use Thelia\Module\AbstractDeliveryModule;
|
||||
use Thelia\Module\Exception\DeliveryException;
|
||||
|
||||
/**
|
||||
* Class OrderController
|
||||
* @package Thelia\Controller\Front
|
||||
* @author Etienne Roudeix <eroudeix@openstudio.fr>
|
||||
*/
|
||||
class OrderController extends BaseFrontController
|
||||
{
|
||||
/**
|
||||
* Check if the cart contains only virtual products.
|
||||
*/
|
||||
public function deliverView()
|
||||
{
|
||||
$this->checkAuth();
|
||||
$this->checkCartNotEmpty();
|
||||
|
||||
// check if the cart contains only virtual products
|
||||
$cart = $this->getSession()->getSessionCart($this->getDispatcher());
|
||||
|
||||
$deliveryAddress = $this->getCustomerAddress();
|
||||
|
||||
if ($cart->isVirtual()) {
|
||||
if (null !== $deliveryAddress) {
|
||||
$deliveryModule = ModuleQuery::create()->retrieveVirtualProductDelivery($this->container);
|
||||
|
||||
if (false === $deliveryModule) {
|
||||
Tlog::getInstance()->error(
|
||||
$this->getTranslator()->trans(
|
||||
"To enable the virtual product feature, the VirtualProductDelivery module should be activated",
|
||||
[],
|
||||
Front::MESSAGE_DOMAIN
|
||||
)
|
||||
);
|
||||
} elseif (count($deliveryModule) == 1) {
|
||||
return $this->registerVirtualProductDelivery($deliveryModule[0], $deliveryAddress);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render(
|
||||
'order-delivery',
|
||||
[
|
||||
'delivery_address_id' => (null !== $deliveryAddress) ? $deliveryAddress->getId() : null
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param AbstractDeliveryModule $moduleInstance
|
||||
* @param Address $deliveryAddress
|
||||
* @return \Symfony\Component\HttpFoundation\Response
|
||||
*/
|
||||
private function registerVirtualProductDelivery($moduleInstance, $deliveryAddress)
|
||||
{
|
||||
/* get postage amount */
|
||||
$deliveryModule = $moduleInstance->getModuleModel();
|
||||
$cart = $this->getSession()->getSessionCart($this->getDispatcher());
|
||||
$deliveryPostageEvent = new DeliveryPostageEvent($moduleInstance, $cart, $deliveryAddress);
|
||||
|
||||
$this->getDispatcher()->dispatch(
|
||||
TheliaEvents::MODULE_DELIVERY_GET_POSTAGE,
|
||||
$deliveryPostageEvent
|
||||
);
|
||||
|
||||
$postage = $deliveryPostageEvent->getPostage();
|
||||
|
||||
$orderEvent = $this->getOrderEvent();
|
||||
$orderEvent->setDeliveryAddress($deliveryAddress->getId());
|
||||
$orderEvent->setDeliveryModule($deliveryModule->getId());
|
||||
$orderEvent->setPostage($postage->getAmount());
|
||||
$orderEvent->setPostageTax($postage->getAmountTax());
|
||||
$orderEvent->setPostageTaxRuleTitle($postage->getTaxRuleTitle());
|
||||
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_ADDRESS, $orderEvent);
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_MODULE, $orderEvent);
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent);
|
||||
|
||||
return $this->generateRedirectFromRoute("order.invoice");
|
||||
}
|
||||
|
||||
/**
|
||||
* set delivery address
|
||||
* set delivery module
|
||||
*/
|
||||
public function deliver()
|
||||
{
|
||||
$this->checkAuth();
|
||||
$this->checkCartNotEmpty();
|
||||
|
||||
$message = false;
|
||||
|
||||
$orderDelivery = $this->createForm(FrontForm::ORDER_DELIVER);
|
||||
|
||||
try {
|
||||
$form = $this->validateForm($orderDelivery, "post");
|
||||
|
||||
$deliveryAddressId = $form->get("delivery-address")->getData();
|
||||
$deliveryModuleId = $form->get("delivery-module")->getData();
|
||||
$deliveryAddress = AddressQuery::create()->findPk($deliveryAddressId);
|
||||
$deliveryModule = ModuleQuery::create()->findPk($deliveryModuleId);
|
||||
|
||||
/* check that the delivery address belongs to the current customer */
|
||||
if ($deliveryAddress->getCustomerId() !== $this->getSecurityContext()->getCustomerUser()->getId()) {
|
||||
throw new \Exception(
|
||||
$this->getTranslator()->trans(
|
||||
"Delivery address does not belong to the current customer",
|
||||
[],
|
||||
Front::MESSAGE_DOMAIN
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/* check that the delivery module fetches the delivery address area */
|
||||
if (null === AreaDeliveryModuleQuery::create()->findByCountryAndModule(
|
||||
$deliveryAddress->getCountry(),
|
||||
$deliveryModule
|
||||
)) {
|
||||
throw new \Exception(
|
||||
$this->getTranslator()->trans(
|
||||
"Delivery module cannot be use with selected delivery address",
|
||||
[],
|
||||
Front::MESSAGE_DOMAIN
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/* get postage amount */
|
||||
$moduleInstance = $deliveryModule->getDeliveryModuleInstance($this->container);
|
||||
|
||||
$cart = $this->getSession()->getSessionCart($this->getDispatcher());
|
||||
$deliveryPostageEvent = new DeliveryPostageEvent($moduleInstance, $cart, $deliveryAddress);
|
||||
|
||||
$this->getDispatcher()->dispatch(
|
||||
TheliaEvents::MODULE_DELIVERY_GET_POSTAGE,
|
||||
$deliveryPostageEvent
|
||||
);
|
||||
|
||||
if (!$deliveryPostageEvent->isValidModule() || null === $deliveryPostageEvent->getPostage()) {
|
||||
throw new DeliveryException(
|
||||
$this->getTranslator()->trans('The delivery module is not valid.', [], Front::MESSAGE_DOMAIN)
|
||||
);
|
||||
}
|
||||
|
||||
$postage = $deliveryPostageEvent->getPostage();
|
||||
|
||||
$orderEvent = $this->getOrderEvent();
|
||||
$orderEvent->setDeliveryAddress($deliveryAddressId);
|
||||
$orderEvent->setDeliveryModule($deliveryModuleId);
|
||||
$orderEvent->setPostage($postage->getAmount());
|
||||
$orderEvent->setPostageTax($postage->getAmountTax());
|
||||
$orderEvent->setPostageTaxRuleTitle($postage->getTaxRuleTitle());
|
||||
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_ADDRESS, $orderEvent);
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_DELIVERY_MODULE, $orderEvent);
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_POSTAGE, $orderEvent);
|
||||
|
||||
return $this->generateRedirectFromRoute("order.invoice");
|
||||
|
||||
} catch (FormValidationException $e) {
|
||||
$message = $this->getTranslator()->trans(
|
||||
"Please check your input: %s",
|
||||
['%s' => $e->getMessage()],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
} catch (PropelException $e) {
|
||||
$this->getParserContext()->setGeneralError($e->getMessage());
|
||||
} catch (\Exception $e) {
|
||||
$message = $this->getTranslator()->trans(
|
||||
"Sorry, an error occured: %s",
|
||||
['%s' => $e->getMessage()],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
}
|
||||
|
||||
if ($message !== false) {
|
||||
Tlog::getInstance()->error(
|
||||
sprintf("Error during order delivery process : %s. Exception was %s", $message, $e->getMessage())
|
||||
);
|
||||
|
||||
$orderDelivery->setErrorMessage($message);
|
||||
|
||||
$this->getParserContext()
|
||||
->addForm($orderDelivery)
|
||||
->setGeneralError($message)
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* set invoice address
|
||||
* set payment module
|
||||
*/
|
||||
public function invoice()
|
||||
{
|
||||
$this->checkAuth();
|
||||
$this->checkCartNotEmpty();
|
||||
$this->checkValidDelivery();
|
||||
|
||||
$message = false;
|
||||
|
||||
$orderPayment = $this->createForm(FrontForm::ORDER_PAYMENT);
|
||||
|
||||
try {
|
||||
$form = $this->validateForm($orderPayment, "post");
|
||||
|
||||
$invoiceAddressId = $form->get("invoice-address")->getData();
|
||||
$paymentModuleId = $form->get("payment-module")->getData();
|
||||
|
||||
/* check that the invoice address belongs to the current customer */
|
||||
$invoiceAddress = AddressQuery::create()->findPk($invoiceAddressId);
|
||||
if ($invoiceAddress->getCustomerId() !== $this->getSecurityContext()->getCustomerUser()->getId()) {
|
||||
throw new \Exception(
|
||||
$this->getTranslator()->trans(
|
||||
"Invoice address does not belong to the current customer",
|
||||
[],
|
||||
Front::MESSAGE_DOMAIN
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
$orderEvent = $this->getOrderEvent();
|
||||
$orderEvent->setInvoiceAddress($invoiceAddressId);
|
||||
$orderEvent->setPaymentModule($paymentModuleId);
|
||||
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_INVOICE_ADDRESS, $orderEvent);
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_SET_PAYMENT_MODULE, $orderEvent);
|
||||
|
||||
return $this->generateRedirectFromRoute("order.payment.process");
|
||||
|
||||
} catch (FormValidationException $e) {
|
||||
$message = $this->getTranslator()->trans(
|
||||
"Please check your input: %s",
|
||||
['%s' => $e->getMessage()],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
} catch (PropelException $e) {
|
||||
$this->getParserContext()->setGeneralError($e->getMessage());
|
||||
} catch (\Exception $e) {
|
||||
$message = $this->getTranslator()->trans(
|
||||
"Sorry, an error occured: %s",
|
||||
['%s' => $e->getMessage()],
|
||||
Front::MESSAGE_DOMAIN
|
||||
);
|
||||
}
|
||||
|
||||
if ($message !== false) {
|
||||
Tlog::getInstance()->error(
|
||||
sprintf("Error during order payment process : %s. Exception was %s", $message, $e->getMessage())
|
||||
);
|
||||
|
||||
$orderPayment->setErrorMessage($message);
|
||||
|
||||
$this->getParserContext()
|
||||
->addForm($orderPayment)
|
||||
->setGeneralError($message)
|
||||
;
|
||||
}
|
||||
|
||||
return $this->generateErrorRedirect($orderPayment);
|
||||
}
|
||||
|
||||
public function pay()
|
||||
{
|
||||
/* check customer */
|
||||
$this->checkAuth();
|
||||
|
||||
/* check cart count */
|
||||
$this->checkCartNotEmpty();
|
||||
|
||||
/* check stock not empty */
|
||||
if (true === ConfigQuery::checkAvailableStock()) {
|
||||
if (null !== $response = $this->checkStockNotEmpty()) {
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
/* check delivery address and module */
|
||||
$this->checkValidDelivery();
|
||||
|
||||
/* check invoice address and payment module */
|
||||
$this->checkValidInvoice();
|
||||
|
||||
$orderEvent = $this->getOrderEvent();
|
||||
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_PAY, $orderEvent);
|
||||
|
||||
$placedOrder = $orderEvent->getPlacedOrder();
|
||||
|
||||
if (null !== $placedOrder && null !== $placedOrder->getId()) {
|
||||
/* order has been placed */
|
||||
if ($orderEvent->hasResponse()) {
|
||||
return $orderEvent->getResponse();
|
||||
} else {
|
||||
return $this->generateRedirectFromRoute(
|
||||
'order.placed',
|
||||
[],
|
||||
['order_id' => $orderEvent->getPlacedOrder()->getId()]
|
||||
);
|
||||
}
|
||||
} else {
|
||||
/* order has not been placed */
|
||||
return $this->generateRedirectFromRoute('cart.view');
|
||||
}
|
||||
}
|
||||
|
||||
public function orderPlaced($order_id)
|
||||
{
|
||||
/* check if the placed order matched the customer */
|
||||
$placedOrder = OrderQuery::create()->findPk(
|
||||
$this->getRequest()->attributes->get('order_id')
|
||||
);
|
||||
|
||||
if (null === $placedOrder) {
|
||||
throw new TheliaProcessException(
|
||||
$this->getTranslator()->trans(
|
||||
"No placed order",
|
||||
[],
|
||||
Front::MESSAGE_DOMAIN
|
||||
),
|
||||
TheliaProcessException::NO_PLACED_ORDER,
|
||||
$placedOrder
|
||||
);
|
||||
}
|
||||
|
||||
$customer = $this->getSecurityContext()->getCustomerUser();
|
||||
|
||||
if (null === $customer || $placedOrder->getCustomerId() !== $customer->getId()) {
|
||||
throw new TheliaProcessException(
|
||||
$this->getTranslator()->trans(
|
||||
"Received placed order id does not belong to the current customer",
|
||||
[],
|
||||
Front::MESSAGE_DOMAIN
|
||||
),
|
||||
TheliaProcessException::PLACED_ORDER_ID_BAD_CURRENT_CUSTOMER,
|
||||
$placedOrder
|
||||
);
|
||||
}
|
||||
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::ORDER_CART_CLEAR, $this->getOrderEvent());
|
||||
|
||||
$this->getParserContext()->set("placed_order_id", $placedOrder->getId());
|
||||
}
|
||||
|
||||
|
||||
public function orderFailed($order_id, $message)
|
||||
{
|
||||
if (empty($order_id)) {
|
||||
// Fallback to request parameter if the method parameter is empty.
|
||||
$order_id = $this->getRequest()->get('order_id');
|
||||
}
|
||||
|
||||
$failedOrder = OrderQuery::create()->findPk($order_id);
|
||||
|
||||
if (null !== $failedOrder) {
|
||||
$customer = $this->getSecurityContext()->getCustomerUser();
|
||||
|
||||
if (null === $customer || $failedOrder->getCustomerId() !== $customer->getId()) {
|
||||
throw new TheliaProcessException(
|
||||
$this->getTranslator()->trans(
|
||||
"Received failed order id does not belong to the current customer",
|
||||
[],
|
||||
Front::MESSAGE_DOMAIN
|
||||
),
|
||||
TheliaProcessException::PLACED_ORDER_ID_BAD_CURRENT_CUSTOMER,
|
||||
$failedOrder
|
||||
);
|
||||
}
|
||||
} else {
|
||||
Tlog::getInstance()->warning("Failed order ID '$order_id' not found.");
|
||||
}
|
||||
|
||||
$this->getParserContext()
|
||||
->set("failed_order_id", $order_id)
|
||||
->set("failed_order_message", $message)
|
||||
;
|
||||
}
|
||||
|
||||
protected function getOrderEvent()
|
||||
{
|
||||
$order = $this->getOrder($this->getRequest());
|
||||
|
||||
return new OrderEvent($order);
|
||||
}
|
||||
|
||||
public function getOrder(Request $request)
|
||||
{
|
||||
$session = $request->getSession();
|
||||
|
||||
if (null !== $order = $session->getOrder()) {
|
||||
return $order;
|
||||
}
|
||||
|
||||
$order = new Order();
|
||||
|
||||
$session->setOrder($order);
|
||||
|
||||
return $order;
|
||||
}
|
||||
|
||||
|
||||
public function viewAction($order_id)
|
||||
{
|
||||
$this->checkOrderCustomer($order_id);
|
||||
|
||||
return $this->render('account-order', ['order_id' => $order_id]);
|
||||
}
|
||||
|
||||
public function generateInvoicePdf($order_id)
|
||||
{
|
||||
$this->checkOrderCustomer($order_id);
|
||||
|
||||
|
||||
return $this->generateOrderPdf($order_id, ConfigQuery::read('pdf_invoice_file', 'invoice'));
|
||||
}
|
||||
|
||||
public function generateDeliveryPdf($order_id)
|
||||
{
|
||||
$this->checkOrderCustomer($order_id);
|
||||
|
||||
return $this->generateOrderPdf($order_id, ConfigQuery::read('pdf_delivery_file', 'delivery'));
|
||||
}
|
||||
|
||||
public function downloadVirtualProduct($order_product_id)
|
||||
{
|
||||
if (null !== $orderProduct = OrderProductQuery::create()->findPk($order_product_id)) {
|
||||
$order = $orderProduct->getOrder();
|
||||
|
||||
if ($order->isPaid(false)) {
|
||||
// check customer
|
||||
$this->checkOrderCustomer($order->getId());
|
||||
|
||||
$virtualProductEvent = new VirtualProductOrderDownloadResponseEvent($orderProduct);
|
||||
$this->getDispatcher()->dispatch(
|
||||
TheliaEvents::VIRTUAL_PRODUCT_ORDER_DOWNLOAD_RESPONSE,
|
||||
$virtualProductEvent
|
||||
);
|
||||
|
||||
$response = $virtualProductEvent->getResponse();
|
||||
|
||||
if (!$response instanceof BaseResponse) {
|
||||
throw new \RuntimeException('A Response must be added in the event TheliaEvents::VIRTUAL_PRODUCT_ORDER_DOWNLOAD_RESPONSE');
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
|
||||
throw new AccessDeniedHttpException();
|
||||
|
||||
}
|
||||
|
||||
private function checkOrderCustomer($order_id)
|
||||
{
|
||||
$this->checkAuth();
|
||||
|
||||
$order = OrderQuery::create()->findPk($order_id);
|
||||
$valid = true;
|
||||
if ($order) {
|
||||
$customerOrder = $order->getCustomer();
|
||||
$customer = $this->getSecurityContext()->getCustomerUser();
|
||||
|
||||
if ($customerOrder->getId() != $customer->getId()) {
|
||||
$valid = false;
|
||||
}
|
||||
} else {
|
||||
$valid = false;
|
||||
}
|
||||
|
||||
if (false === $valid) {
|
||||
throw new AccessDeniedHttpException();
|
||||
}
|
||||
}
|
||||
|
||||
public function getDeliveryModuleListAjaxAction()
|
||||
{
|
||||
$this->checkXmlHttpRequest();
|
||||
|
||||
// Change the delivery address if customer has changed it
|
||||
$address = null;
|
||||
$session = $this->getSession();
|
||||
$addressId = $this->getRequest()->get('address_id', null);
|
||||
if (null !== $addressId && $addressId !== $session->getOrder()->getChoosenDeliveryAddress()) {
|
||||
$address = AddressQuery::create()->findPk($addressId);
|
||||
if (null !== $address && $address->getCustomerId() === $session->getCustomerUser()->getId()) {
|
||||
$session->getOrder()->setChoosenDeliveryAddress($addressId);
|
||||
}
|
||||
}
|
||||
|
||||
$address = AddressQuery::create()->findPk($session->getOrder()->getChoosenDeliveryAddress());
|
||||
|
||||
$countryId = $address->getCountryId();
|
||||
$stateId = $address->getStateId();
|
||||
|
||||
$args = array(
|
||||
'country' => $countryId,
|
||||
'state' => $stateId,
|
||||
'address' => $session->getOrder()->getChoosenDeliveryAddress()
|
||||
);
|
||||
|
||||
return $this->render('ajax/order-delivery-module-list', $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Redirect to cart view if at least one non product is out of stock
|
||||
*
|
||||
* @return null|BaseResponse
|
||||
*/
|
||||
private function checkStockNotEmpty()
|
||||
{
|
||||
$cart = $this->getSession()->getSessionCart($this->getDispatcher());
|
||||
|
||||
$cartItems = $cart->getCartItems();
|
||||
|
||||
foreach ($cartItems as $cartItem) {
|
||||
$pse = $cartItem->getProductSaleElements();
|
||||
|
||||
$product = $cartItem->getProduct();
|
||||
|
||||
if ($pse->getQuantity() <= 0 && $product->getVirtual() !== 1) {
|
||||
return $this->generateRedirectFromRoute('cart.view');
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the chosen delivery address for a cart or the default customer address if not exists
|
||||
*
|
||||
* @return null|Address
|
||||
*/
|
||||
protected function getCustomerAddress()
|
||||
{
|
||||
$deliveryAddress = null;
|
||||
$addressId = $this->getSession()->getOrder()->getChoosenDeliveryAddress();
|
||||
if (null === $addressId) {
|
||||
$customer = $this->getSecurityContext()->getCustomerUser();
|
||||
|
||||
$deliveryAddress = AddressQuery::create()
|
||||
->filterByCustomerId($customer->getId())
|
||||
->orderByIsDefault(Criteria::DESC)
|
||||
->findOne();
|
||||
|
||||
if (null !== $deliveryAddress) {
|
||||
$this->getSession()->getOrder()->setChoosenDeliveryAddress(
|
||||
$deliveryAddress->getId()
|
||||
);
|
||||
}
|
||||
} else {
|
||||
$deliveryAddress = AddressQuery::create()->findPk($addressId);
|
||||
}
|
||||
|
||||
return $deliveryAddress;
|
||||
}
|
||||
}
|
||||
150
local/modules/Front/Controller/SitemapController.php
Normal file
150
local/modules/Front/Controller/SitemapController.php
Normal file
@@ -0,0 +1,150 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* This file is part of the Thelia package. */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : dev@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* For the full copyright and license information, please view the LICENSE.txt */
|
||||
/* file that was distributed with this source code. */
|
||||
/*************************************************************************************/
|
||||
|
||||
|
||||
namespace Front\Controller;
|
||||
|
||||
use Doctrine\Common\Cache\FilesystemCache;
|
||||
use Thelia\Controller\Front\BaseFrontController;
|
||||
use Thelia\Core\HttpFoundation\Request;
|
||||
use Thelia\Core\HttpFoundation\Response;
|
||||
use Thelia\Model\ConfigQuery;
|
||||
use Thelia\Model\LangQuery;
|
||||
|
||||
/**
|
||||
* Controller uses to generate sitemap.xml
|
||||
*
|
||||
* A default cache of 2 hours is used to avoid attack. You can flush cache if you have `ADMIN` role and pass flush=1 in
|
||||
* query string parameter.
|
||||
*
|
||||
* You can generate sitemap according to specific language and/or specific context (catalog/content). You have to
|
||||
* use ```lang``` and ```context``` query string parameters to do so. If a language is not used in website or if the
|
||||
* context is not supported the page not found is displayed.
|
||||
*
|
||||
* {url}/sitemap?lang=fr&context=catalog will generate a sitemap for catalog (categories and products)
|
||||
* for french language.
|
||||
*
|
||||
* @package Front\Controller
|
||||
* @author Julien Chanséaume <jchanseaume@openstudio.fr>
|
||||
*/
|
||||
class SitemapController extends BaseFrontController {
|
||||
|
||||
|
||||
/**
|
||||
* Folder name for sitemap cache
|
||||
*/
|
||||
const SITEMAP_CACHE_DIR = "sitemap";
|
||||
|
||||
/**
|
||||
* Key prefix for sitemap cache
|
||||
*/
|
||||
const SITEMAP_CACHE_KEY = "sitemap";
|
||||
|
||||
/**
|
||||
* @return Response
|
||||
*/
|
||||
public function generateAction()
|
||||
{
|
||||
/** @var Request $request */
|
||||
$request = $this->getRequest();
|
||||
|
||||
// the locale : fr, en,
|
||||
$lang = $request->query->get("lang", "");
|
||||
if ("" !== $lang) {
|
||||
if (! $this->checkLang($lang)){
|
||||
$this->pageNotFound();
|
||||
}
|
||||
}
|
||||
// specific content : product, category, cms
|
||||
$context = $request->query->get("context", "");
|
||||
if (! in_array($context, array("", "catalog", "content")) ){
|
||||
$this->pageNotFound();
|
||||
}
|
||||
|
||||
$flush = $request->query->get("flush", "");
|
||||
|
||||
// check if sitemap already in cache
|
||||
$cacheContent = false;
|
||||
|
||||
$cacheDir = $this->getCacheDir();
|
||||
$cacheKey = self::SITEMAP_CACHE_KEY . $lang . $context;
|
||||
$cacheExpire = intval(ConfigQuery::read("sitemap_ttl", '7200')) ?: 7200;
|
||||
|
||||
$cacheDriver = new FilesystemCache($cacheDir);
|
||||
if (!($this->checkAdmin() && "" !== $flush)){
|
||||
$cacheContent = $cacheDriver->fetch($cacheKey);
|
||||
} else {
|
||||
$cacheDriver->delete($cacheKey);
|
||||
}
|
||||
|
||||
// if not in cache
|
||||
if (false === $cacheContent){
|
||||
// render the view
|
||||
$cacheContent = $this->renderRaw(
|
||||
"sitemap",
|
||||
array(
|
||||
"_lang_" => $lang,
|
||||
"_context_" => $context
|
||||
)
|
||||
);
|
||||
// save cache
|
||||
$cacheDriver->save($cacheKey, $cacheContent, $cacheExpire);
|
||||
}
|
||||
|
||||
$response = new Response();
|
||||
$response->setContent($cacheContent);
|
||||
$response->headers->set('Content-Type', 'application/xml');
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* get the cache directory for sitemap
|
||||
*
|
||||
* @return mixed|string
|
||||
*/
|
||||
protected function getCacheDir()
|
||||
{
|
||||
$cacheDir = $this->container->getParameter("kernel.cache_dir");
|
||||
$cacheDir = rtrim($cacheDir, '/');
|
||||
$cacheDir .= '/' . self::SITEMAP_CACHE_DIR . '/';
|
||||
|
||||
return $cacheDir;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if current user has ADMIN role
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function checkAdmin(){
|
||||
return $this->getSecurityContext()->hasAdminUser();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if a lang is used
|
||||
*
|
||||
* @param $lang The lang code. e.g.: fr
|
||||
* @return bool true if the language is used, otherwise false
|
||||
*/
|
||||
private function checkLang($lang)
|
||||
{
|
||||
// load locals
|
||||
$lang = LangQuery::create()
|
||||
->findOneByCode($lang);
|
||||
|
||||
return (null !== $lang);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user