<?php
namespace App\Controller;
use Symfony\Contracts\Translation\TranslatorInterface;
use Pimcore\Controller\FrontendController;
use Pimcore\Model\DataObject\Customer;
use Pimcore\Model\DataObject\Faq\Listing;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use FrontendPermissionToolkitBundle\Service;
class DefaultController extends FrontendController
{
/** @var TranslatorInterface */
protected $translator;
/**
* @var Service
*/
private $permissionService;
public function __construct(TranslatorInterface $translator, Service $permissionService )
{
$this->translator = $translator;
$this->permissionService = $permissionService;
}
/**
* @Template
*
* @Route("/", name="home")
* @param Request $request
*
* @return array
*/
public function defaultAction(Request $request)
{
$params = [];
$user = $this->getUser();
if(!$user){
return $this->redirectToRoute('account-login');
}
$this->setUserParams($params);
return $params;
}
/**
* @Template
* @param Request $request
*
* @return array
*/
public function faqAction(Request $request){
$params = [];
$user = $this->getUser();
if(!$user){
return $this->redirectToRoute('account-login');
}
$this->setUserParams($params);
$faqList = new Listing();
$faqList->filterByEnabled("1");
$faqs = $faqList->load();
if($faqs){
$params['faqs'] = $faqs;
} else{
$params['faqs'] = [];
}
return $params;
}
/**
* @param Request $request
* @return array
*/
public function sentPublicationNewsletterEmailAction(Request $request){
$params = [
"customerName" => $request->get('customerName'),
"materials" => $request->get('materials'),
];
return $this->render("default/publicationNewsletterEmail.html.twig", $params);
}
/**
* Undocumented function
*
* @param array $params
* @return void
*/
protected function setUserParams(array &$params){
$user = $this->getUser();
if ($user && $user instanceof Customer) {
$customerLanguages = $user->getLanguages();
if($customerLanguages){
$languages = [];
foreach($customerLanguages as $customerLanguage){
$language['code'] = $customerLanguage;
$language['lang'] = locale_get_display_language($customerLanguage, 'en');
array_push($languages,$language);
}
$params['customerLanguages'] = $languages;
}
$params['educationPageAllowed'] = false;
if($this->permissionService && $this->permissionService->isAllowed($user,"education")){
$params['educationPageAllowed'] = true;
}
$params['everythingAllowed'] = false;
if($this->permissionService && $this->permissionService->isAllowed($user,"everything")){
$params['everythingAllowed'] = true;
}
$params['previewAllowed'] = false;
if($this->permissionService && $this->permissionService->isAllowed($user,"preview")){
$params['previewAllowed'] = true;
}
$params['openAssetAllowed'] = false;
if($this->permissionService && $this->permissionService->isAllowed($user,"openAssets")){
$params['openAssetAllowed'] = true;
}
$userRole = $user->getCustomerRole();
$params['includedBrands'] = $userRole->getIncludedBrands();
$params['excludedBrands'] = $userRole->getExcludedBrands();
$params['includedSublines'] = $userRole->getIncludedSublines();
$params['excludedSublines'] = $userRole->getExcludedSublines();
$params['logged'] = true;
$params['mail'] = $user->getEmail();
}else{
$params['logged'] = false;
}
return $params;
}
/**
* @Route("/confirm-registration/{guid}", methods={"GET"})
*/
public function confirmRegistration(Request $request)
{
$guid = $request->get('guid');
if (!(empty($guid))) {
$customer = Customer::getByConfirmRegistrationToken($guid, 1);
if($customer){
$customer->setActive(true);
$customer->setConfirmRegistrationToken(null);
$language = "en";
$languages = $customer->getLanguages();
$guid = uuid_create();
$customer->setPasswordRecoveryToken($guid);
$customer->save();
return $this->render('account/passwordRestore.html.twig', array(
'errors' => [],
'title' => $this->translator->trans("password_creation_title", [], 'messages', $language),
'psw_criter_upper' => $this->translator->trans("error_password_uppercase", [], 'messages', $language),
'psw_criter_lower' => $this->translator->trans("error_password_lowercase", [], 'messages', $language),
'psw_criter_special' => $this->translator->trans("error_password_special", [], 'messages', $language),
'psw_criter_twelve' => $this->translator->trans("error_password_number", [], 'messages', $language),
'psw_criter_number' => $this->translator->trans("error_password_twelve", [], 'messages', $language),
'locale' => $language,
'guid' => $guid,
));
}else{
return $this->redirectToRoute('account-login');
}
} else {
return $this->redirectToRoute('home');
}
}
/**
*
* @Route("/password-restore/{guid}", methods={"GET"})
*
* @param Request $request
* @return void
*/
public function passwordRestoreAction(Request $request, string $guid)
{
$customerByGuid = Customer::getByPasswordRecoveryToken($guid, 1);
if(!$customerByGuid){
return $this->redirectToRoute('account-login');
}
$locale = $this->document->getProperty("language");
if (($customerByGuid instanceof Customer)) {
$language = "en";
return $this->render('account/passwordRestore.html.twig', array(
'guid' => $guid,
'psw_criter_upper' => $this->translator->trans("error_password_uppercase", [], 'messages', $language),
'psw_criter_lower' => $this->translator->trans("error_password_lowercase", [], 'messages', $language),
'psw_criter_special' => $this->translator->trans("error_password_special", [], 'messages', $language),
'psw_criter_twelve' => $this->translator->trans("error_password_number", [], 'messages', $language),
'psw_criter_number' => $this->translator->trans("error_password_twelve", [], 'messages', $language),
'title' => $this->translator->trans("password_restore_title", [], 'messages', $language),
'locale' => $locale
));
} else {
return $this->redirectToRoute('home');
}
return $this->json(["success" => true]);
}
/**
* @Route("/password-recovery", methods={"GET"})
*/
public function passwordRecoveryAction(Request $request)
{
return $this->render('account/passwordRecovery.html.twig', array('success' => "", "error" => "","locale"=>"en"));
}
}