<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Enterprise License (PEL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PEL
*/
namespace App\Controller;
use App\Form\CarSubmitFormType;
use App\Form\ContactUsFormType;
use App\Model\Product\Car;
use App\Website\Tool\Text;
use Aws\Retry\RateLimiter;
use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
use Pimcore\Controller\Configuration\ResponseHeader;
use Pimcore\Model\DataObject\BodyStyle;
use Pimcore\Model\DataObject\Manufacturer;
use Pimcore\Model\DataObject\Service;
use Pimcore\Translation\Translator;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
use Symfony\Component\RateLimiter\RateLimit;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Config\Framework\RateLimiter\LimiterConfig;
class ContentController extends BaseController
{
/**
* @Template
*/
public function defaultAction()
{
return [];
}
/**
* The annotations below demonstrate the ResponseHeader annotation which can be
* used to set custom response headers on the auto-rendered response. At this point, the headers
* are not really set as we don't have a response yet, but they will be added to the final response
* by the ResponseHeaderListener.
*
*
* @return Response
*/
public function portalAction()
{
/* // you can also set the header via code
$this->addResponseHeader('X-Custom-Header3', ['foo', 'bar']);
return $this->render('content/portal.html.twig', [
'isPortal' => true
]); */
return $this->render('home/home.html.twig');
}
/**
* @return Response
*/
public function loggedHomeAction(UserInterface $loggedUser = null)
{
if (!$loggedUser) {
return new RedirectResponse('/');
}
return $this->render('content/loggedHome.html.twig');
}
/**
* @return Response
*/
public function editableRoundupAction()
{
return $this->render('content/editable_roundup.html.twig');
}
/**
* @return Response
*/
public function thumbnailsAction()
{
return $this->render('content/thumbnails.html.twig');
}
/**
* @param Request $request
* @param Translator $translator
*
* @return Response
*
* @throws \Exception
*/
public function carSubmitAction(Request $request, Translator $translator)
{
$form = $this->createForm(CarSubmitFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$formData = $form->getData();
$car = new Car();
$car->setParent(Service::createFolderByPath('/upload/new'));
$car->setKey(Text::toUrl($formData['name'] . '-' . time()));
$car->setName($formData['name']);
$car->setDescription($formData['description']);
$car->setManufacturer(Manufacturer::getById($formData['manufacturer']));
$car->setBodyStyle(BodyStyle::getById($formData['bodyStyle']));
$car->setCarClass($formData['carClass']);
$car->save();
$this->addFlash('success', $translator->trans('general.car-submitted'));
return $this->render('content/car_submit_success.html.twig', ['car' => $car]);
}
return $this->render('content/car_submit.html.twig', [
'form' => $form->createView()
]);
}
/**
* @param Request $request
* @param Factory $ecommerceFactory
*
* @return Response
*/
public function tenantSwitchesAction(Request $request, Factory $ecommerceFactory)
{
$environment = $ecommerceFactory->getEnvironment();
if ($request->get('change-checkout-tenant')) {
$checkoutTenant = $request->get('change-checkout-tenant');
$checkoutTenant = $checkoutTenant == 'default' ? '' : $checkoutTenant;
$environment->setCurrentCheckoutTenant(strip_tags($checkoutTenant));
$environment->save();
}
if ($request->get('change-assortment-tenant')) {
$assortmentTenant = $request->get('change-assortment-tenant');
$assortmentTenant = $assortmentTenant == 'default' ? '' : $assortmentTenant;
$environment->setCurrentAssortmentTenant(strip_tags($assortmentTenant));
$environment->save();
}
$paramsBag['checkoutTenants'] = ['default' => ''];
$paramsBag['currentCheckoutTenant'] = $environment->getCurrentCheckoutTenant() ? $environment->getCurrentCheckoutTenant() : 'default';
$paramsBag['assortmentTenants'] = ['default' => '', 'ElasticSearch' => 'needs to be configured and activated in configuration'];
$paramsBag['currentAssortmentTenant'] = $environment->getCurrentAssortmentTenant() ? $environment->getCurrentAssortmentTenant() : 'default';
return $this->render('content/tenant_switches.html.twig', $paramsBag);
}
/**
* Contact us
*
*/
public function contactUsAction(Request $request, RateLimiterFactory $anonymousApiLimiter)
{
$limiter = $anonymousApiLimiter->create($request->getClientIp());
$errors = [];
$success = null;
$form = $this->createForm(ContactUsFormType::class, null, [
'action' => '/contact-us',
]);
if (false === $limiter->consume(1)->isAccepted()) {
$errors = ["We're sorry, but you've sent too many messages recently. Please try again later."];
return $this->render('contact_us/contact_us.html.twig', [
'form' => $form->createView(),
'errors' => $errors,
'success' => $success
]);
} else {
$form->handleRequest($request);
$errors = [];
$success = null;
if ($form->isSubmitted() && $form->isValid()) {
$success = 'Thank you for your enquiry, a member of our team will be in touch shortly.';
$data = $form->getData();
$params = array(
'name' => $data['_name'],
'email' => $data['_email'],
'message' => $data['_message'],
);
//sending the email
$mail = new \Pimcore\Mail();
$mail->to('enquiries@osgo.co.uk');
$mail->setDocument('/en/mails/contact-us');
$mail->setParams($params);
$mail->send();
}
if ($form->isSubmitted() && !$form->isValid()) {
/* dump($form);
die(); */
foreach ($form->getErrors() as $error) {
$errors[] = $error->getMessage();
}
}
return $this->render('contact_us/contact_us.html.twig', [
'form' => $form->createView(),
'errors' => $errors,
'success' => $success
]);
}
}
}