<?php
namespace App\Controller;
use Pimcore\Model\DataObject;
use Pimcore\Controller\FrontendController;
use Symfony\Component\HttpFoundation\Request;
use Pimcore\Bundle\EcommerceFrameworkBundle\CheckoutManager\ICheckoutStep;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
use Symfony\Component\Routing\Annotation\Route;
// class CheckoutController extends \Website\Controller\CartAware
/**
* @Route("/{_locale}/checkout")
*/
class CheckoutController extends CartAware
{
/**
* @inheritDoc
*/
public function onKernelController(FilterControllerEvent $event)
{
parent::onKernelController($event);
//navigation and breadcrumbs
$this->view->hideNav = true;
$translator = $this->get('translator');
/** @var callable $placeholder */
$placeholder = $this->get('pimcore.templating.view_helper.placeholder');
$placeholder('addBreadcrumb')->append([
'parentId' => $this->document->getId(),
'id' => 'cart',
'label' => $translator->trans('general.mycart'),
'url' => $this->generateUrl('cart', ['action' => 'list'])
]);
$placeholder('addBreadcrumb')->append([
'parentId' => 'cart',
'id' => 'checkout',
'label' => $translator->trans('general.checkout')
]);
$cart = $this->getCart();
$this->view->cart = $cart;
$checkoutManager = Factory::getInstance()->getCheckoutManager($cart);
//check, if currently active payment is available
if ($checkoutManager->hasActivePayment()) {
$url = $this->generateUrl('action', [
'controller' => 'payment',
'action' => 'payment',
'prefix' => strtolower($this->locale)
]);
$event->setController(function () use ($url) {
return new RedirectResponse($url);
});
}
}
/**
* init controller
*/
/*public function init()
{
parent::init();
//navigation and breadcrumbs
$this->view->hideNav = true;
$translator = new \Pimcore\Translate\Website(Zend_Registry::get("Zend_Locale"));
$this->view->placeholder('addBreadcrumb')->append([
'parentId' => $this->document->getId()
, 'id' => "cart"
, 'label' => $translator->translate("general.mycart")
, 'url' => $this->view->url(["prefix" => $this->locale, "action" => "list"], "cart", true)
]);
$this->view->placeholder('addBreadcrumb')->append([
'parentId' => "cart"
, 'id' => "checkout"
, 'label' => $translator->translate("general.checkout")
]);
$cart = $this->getCart();
$this->view->cart = $cart;
$checkoutManager = \Pimcore\Bundle\EcommerceFrameworkBundle\Factory::getInstance()->getCheckoutManager($cart);
//check, if currently active payment is available
if($checkoutManager->hasActivePayment()) {
return $this->redirect($this->view->url(["controller" => "payment", "action" => "payment", "prefix" => $this->locale], "action", true));
}
}*/
/**
* @Route("/delivery", name="checkout")
*
* @param Request $request
* @param Factory $factory
*/
public function deliveryAction(Request $request,Factory $factory)
{
// init
//$this->layout()->setLayout('cart');
//$this->enableLayout();
$params = [];
$cart = $this->getCart();
$params['currentStep'] = 'delivery';
$params['locale'] = strtolower($request->getLocale());
$params['params'] = $request->request->all();
$params['preview'] = $request->get('pimcore_preview',null);
// Set Us as default country
$defaultCountry = DataObject\AvailabilityCountry::getByPath('/availability-countries/us');
if(isset($this->country)){
$availableCountry = $this->country;
}else{
$availableCountry = $defaultCountry;
$params['country'] = $defaultCountry;
}
//$authNamespace = new Zend_Session_Namespace('Zend_Auth');
//todo
$session = $request->getSession();
$authNamespace = $session->getBag('espirit');
if ($authNamespace->get('customeraccount')) {
$params['otheremailaddress'] = $authNamespace->get('customeraccount')->getEmail();
}
//if cart empty, redirect to cart list
if(count($cart->getItems()) == 0) {
//$urlHelper = \Pimcore::getContainer()->get('pimcore.templating.view_helper.pimcore_url');
//return $this->redirect($urlHelper(array('action' => 'list', 'prefix' => strtolower($this->locale)), 'cart', true));
return $this->redirectToRoute('shop-cart-detail');
}
//$checkoutManager = Factory::getInstance()->getCheckoutManager( $cart );
$checkoutManager = $factory->getCheckoutManager($cart);
$deliveryAddress = $checkoutManager->getCheckoutStep('deliveryaddress');
//$trackingManager = \Pimcore\Bundle\EcommerceFrameworkBundle\Factory::getInstance()->getTrackingManager();
//$trackingManager->trackCheckoutStep($deliveryAddress, $cart, 1);
$params['deliveryAddress'] = $deliveryAddress;
// check errors
//$params['errors'] = $this->getParam('error') ? explode(',', $this->getParam('error')) : array();
$params['errors'] = 0;
// update delivery address
if($request->getMethod() == 'POST')
{
$address = new \stdClass();
// save address if we have no errors
//TODO
//if(@count($params['errors']) === 0)
//{
// Set the dataformat items
//$authNamespace = new Zend_Session_Namespace('Zend_Auth');
//TODO
if($request->get('otheremailcheck'))
{
$authNamespace->set('otheremail',$request->get("otheremail"));
}
$authNamespace->set('datafileformat',$request->get("datafileformat"));
$authNamespace->set('photographyfileformat',$request->get("photographyfileformat"));
$authNamespace->set('photographyfiledimension',$request->get("photographyfiledimension"));
$authNamespace->set('lifestyleassets',$request->get("lifestyleAssets"));
$authNamespace->set('fileDownloadName',$request->get("file_download_name"));
// commit step
$checkoutManager->commitStep($deliveryAddress, $address);
// goto next step
//$urlHelper = \Pimcore::getContainer()->get('pimcore.templating.view_helper.pimcore_url');
//return $this->redirect($urlHelper(["action" => "confirm", "prefix" => strtolower($this->locale)], "checkout", true));
return $this->redirectToRoute('checkout-confirm');
//}
}
$params['cart'] = $cart;
$params['deviceDetector'] = \Pimcore\Tool\DeviceDetector::getInstance();
return $this->render('Checkout/delivery.html.twig',$params);
}
/**
* @Route("/confirm", name="checkout-confirm")
*
* @param Request $request
*
*/
public function confirmAction(Request $request)
{
// init
// $authNamespace = new Zend_Session_Namespace('Zend_Auth');
$params = [];
$params['locale'] = strtolower($request->getLocale());
$params['params'] = $request->request->all();
// Set Us as default country
$defaultCountry = DataObject\AvailabilityCountry::getByPath('/availability-countries/us');
if(isset($this->country)){
$availableCountry = $this->country;
}else{
$availableCountry = $defaultCountry;
$params['country'] = $defaultCountry;
}
//TODO
$session = $request->getSession();
$authNamespace = $session->getBag('espirit');
$params['authNamespace'] = $authNamespace;
// END TODO
//$this->layout()->setLayout('cart');
//$this->enableLayout();
$cart = $this->getCart();
$params['currentStep'] = 'confirm';
$params['showSummary'] = true;
//if cart empty, redirect to cart list
//TODo
if(count($cart->getItems()) == 0) {
//$urlHelper = \Pimcore::getContainer()->get('pimcore.templating.view_helper.pimcore_url');
//return $this->redirect($urlHelper(array('action' => 'list', 'prefix' => strtolower($this->locale)), 'cart', true));
$url = $this->generateUrl('shop-cart-detail');
return $this->redirect($url);
}
$checkoutManager = Factory::getInstance()->getCheckoutManager( $cart );
//needed for sidebar
$deliveryAddress = $checkoutManager->getCheckoutStep('deliveryaddress');
$params['deliveryAddress'] = $deliveryAddress;
$payment = $checkoutManager->getPayment();
// check errors
$params['errors'] = array();
if($request->get('error') != '') {
$params['errors'] = explode(',', $request->get('error'));
}
$confirmStep = $checkoutManager->getCheckoutStep('confirm');
// go to payment
if($request->getMethod() == 'POST')
{
//TODO
/*if (!$authNamespace->get('customeraccount')) {
$urlHelper = \Pimcore::getContainer()->get('pimcore.templating.view_helper.pimcore_url');
return $this->redirect($urlHelper(["controller" => "account", "action" => "sign-in", "prefix" => strtolower($this->locale), "ns" => "confirm"], "action"));
}*/
if($request->get('agb-accepted'))
{
$checkoutManager->commitStep($confirmStep, true);
//$this->downloadAction();
//$urlHelper = \Pimcore::getContainer()->get('pimcore.templating.view_helper.pimcore_url');
//return $this->redirect($urlHelper(["controller" => "cart", "action" => "download", "prefix" => strtolower($this->locale), true], "action"));
//if($payment) {
// $this->redirect($this->view->url(["controller" => "payment", "action" => "payment", "prefix" => $this->language, true], "action"));
//} else {
// $order = $checkoutManager->commitOrder();
//}
$url = strtolower($this->generateUrl('cart-download'));
return $this->redirect($url);
}
else
{
$params['errors'][] = 'terms';
}
}
$params['cart'] = $cart;
$params['deviceDetector'] = \Pimcore\Tool\DeviceDetector::getInstance();
return $this->render('Checkout/confirm.html.twig',$params);
}
/**
* order completed
*/
public function completedAction(Request $request)
{
// init
//$this->enableLayout();
$this->view->order = \Pimcore\Model\DataObject\OnlineShopOrder::getById( $request->get('id') );
$itemKeyUniversal = \Pimcore\Bundle\EcommerceFrameworkBundle\CheckoutManager\CheckoutManager::TRACK_ECOMMERCE_UNIVERSAL . "_" . $this->view->order->getOrdernumber();
$environment = \Pimcore\Bundle\EcommerceFrameworkBundle\Factory::getInstance()->getEnvironment();
$codeUniversal = $environment->getCustomItem($itemKeyUniversal);
if(!empty($codeUniversal)) {
$this->view->gacodeUniversal = $codeUniversal;
$environment->removeCustomItem($itemKeyUniversal);
$environment->save();
}
}
public function downloadAction(Request $request)
{
//$this->enableLayout();
$this->view->hideNav = true;
$cart = $this->getCart();
$this->view->cart = $cart;
//if cart empty, redirect to cart list
if(count($cart->getItems()) == 0) {
$urlHelper = \Pimcore::getContainer()->get('pimcore.templating.view_helper.pimcore_url');
return $this->redirect($urlHelper(array('action' => 'list', 'prefix' => strtolower($this->locale)), 'cart', true));
}
$checkoutManager = \Pimcore\Bundle\EcommerceFrameworkBundle\Factory::getInstance()->getCheckoutManager( $cart );
$order = $checkoutManager->commitOrder();
//$authNamespace = new Zend_Session_Namespace('Zend_Auth');
$session = $this->get('session');
$authNamespace = $session->getBag('espirit');
$order->setcustomer($authNamespace->get('customeraccount'));
$order->setdatafileformat($authNamespace->get('datafileformat'));
$order->setotheremail($authNamespace->get('otheremail'));
$order->setphotographyfileformat($authNamespace->get('photographyfileformat'));
$order->setphotographyfiledimension($authNamespace->get('photographyfiledimension'));
$order->save();
//Send the Email
$params = array();
$params["order"] = $order;
$params["customer"] = $order->getCustomer();
$params["ordernumber"] = $order->getOrdernumber();
$mail = new \Pimcore\Mail(array("document" => "/en/emails/download-confirmation", "params" => $params));
if($order->getCustomer()) {
$mail->setIgnoreDebugMode(true);
//$mail->setDefaultFrom("contact@beamsuntory.com","Product Download");
$mail->addTo($order->getCustomer()->getEmail());
if ($authNamespace->get('otheremail')) {
$toemails = explode(",", $authNamespace->get('otheremail'));
foreach ($toemails as $toemail) {
if (!empty( $toemail )) $mail->addTo( $toemail );
}
}
$mail->send();
} else {
\Logger::err("No Customer found!");
}
// init
$this->view->order = \Pimcore\Model\DataObject\OnlineShopOrder::getById( $order->getId() );
$itemKeyUniversal = \Pimcore\Bundle\EcommerceFrameworkBundle\CheckoutManager\CheckoutManager::TRACK_ECOMMERCE_UNIVERSAL . "_" . $order->getOrdernumber();
$environment = \Pimcore\Bundle\EcommerceFrameworkBundle\Factory::getInstance()->getEnvironment();
$codeUniversal = $environment->getCustomItem($itemKeyUniversal);
if(!empty($codeUniversal)) {
$this->view->gacodeUniversal = $codeUniversal;
$environment->removeCustomItem($itemKeyUniversal);
$environment->save();
}
}
}