<?php
/**
* Created by Elements.at New Media Solutions GmbH
*
*/
namespace App\Controller;
use App\Twig\ConfigHelper;
use Elements\Bundle\CmsToolsBundle\Tool\Helper\MailHelper;
use Elements\Bundle\HashCashBundle\Service\HashCashService;
use Pimcore\Log\ApplicationLogger;
use Pimcore\Mail;
use Pimcore\Model\DataObject\SiteConfig;
use Pimcore\Model\Document\Email;
use Pimcore\Model\Document\Page;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Translation\TranslatorInterface;
class FormsController extends AbstractController
{
public function __construct(private readonly HashCashService $hashCashService, private readonly TranslatorInterface $translator, private readonly ConfigHelper $configHelper)
{
}
/**
*
* @param Request $request
*
* @return Response
*/
public function contactAction(Request $request): Response
{
\Pimcore\Cache::disable();
$errors = [];
$success = null;
$siteConfig = $this->configHelper->getSiteConfig();
if ($request->isMethod('POST')) {
if ($siteConfig instanceof SiteConfig) {
$successPage = $siteConfig->getContactSuccessPage();
$resort = $this->document->getProperty('resort');
if ($resort == 'construction') {
$adminMail = $siteConfig->getContactConstructionAdminMail();
$userMail = $siteConfig->getContactConstructionUserMail();
$mailAddress = $siteConfig->getContactConstructionMailAddress();
} elseif ($resort == 'loader') {
$adminMail = $siteConfig->getContactLoaderAdminMail();
$userMail = $siteConfig->getContactLoaderUserMail();
$mailAddress = $siteConfig->getContactLoaderMailAddress();
} else {
$adminMail = $siteConfig->getContactGroupAdminMail();
$userMail = $siteConfig->getContactGroupUserMail();
$mailAddress = $siteConfig->getContactGroupMailAddress();
}
}
$required = ['firstname', 'lastname', 'email', 'message', 'gdpr'];
if (!$request->get('person', false)) {
$required[] = 'company';
}
$params = $this->checkForm($request, $required);
if (!empty($params['errors'])) {
$errors = $params['errors'];
$success = false;
} else {
$adminMailSuccess = false;
$userMailSuccess = false;
if (isset($adminMail) && isset($mailAddress) && $adminMail instanceof Email) {
$mail = new Mail();
try {
$mail->setParams($params);
$mail->setDocument($adminMail);
$mail->addTo($mailAddress);
$mail->send();
$adminMailSuccess = true;
} catch(\Exception $e) {
$errors[] = 'sending admin mail failed';
}
}
if (isset($userMail) && $userMail instanceof Email) {
$mail = new Mail();
try {
$mail->addTo($params['email']);
$mail->setDocument($userMail);
$mail->send();
$userMailSuccess = true;
} catch(\Exception $e) {
$errors[] = 'sending user mail failed';
}
}
if ($adminMailSuccess && $userMailSuccess && isset($successPage) && $successPage instanceof Page) {
return $this->redirect((string)$successPage);
}
}
}
return $this->renderTemplate('forms/contact.html.twig', [
'errors' => $errors,
'success' => $success,
]);
}
/**
*
* @param Request $request
* @param ApplicationLogger $logger
* @return Response
* @throws \Exception
*/
public function whistleBlowerContactAction(Request $request, ApplicationLogger $logger): Response
{
\Pimcore\Cache::disable();
$errors = [];
$success = null;
if ($request->isMethod('POST')) {
$adminMail = $this->getDocumentEditable('relation', 'email-admin')->getElement();
$adminMailAddress = $this->getDocumentEditable('input', 'admin-address')->getData();
$successPage = $this->getDocumentEditable('relation', 'success')->getElement();
$required = ['message'];
$params = $this->checkForm($request, $required);
if (!empty($params['errors'])) {
$errors = $params['errors'];
$success = false;
} else {
$adminMailSuccess = false;
$mailInfos = null;
if ($adminMail instanceof Email) {
$mail = new Mail();
try {
$mail->setParams($params);
$mail->addTo($adminMailAddress);
$mail->setDocument($adminMail);
$mailInfos = $mail->send();
$adminMailSuccess = true;
} catch(\Exception $e) {
$errors[] = 'sending admin mail failed';
}
}
if ($adminMailSuccess) {
$documentId = $mailInfos?->getDocumentId() ?? null;
$requestUri = $request->getRequestUri();
try {
$db = \Pimcore\Db::get();
$db->delete('email_log', [
'documentId' => $documentId,
'requestUri' => $requestUri,
'`to' => $adminMailAddress,
]);
} catch (\Exception $e) {
$logger->error("Could not delete whistleblower email: " . $e->getFile() . $e->getLine() . $e->getMessage());
}
if ($successPage instanceof Page) {
return $this->redirect((string)$successPage);
}
}
}
}
return $this->renderTemplate('forms/whistleBlowerContact.html.twig', [
'errors' => $errors,
'success' => $success,
]);
}
/**
*
* @param Request $request
*
* @return Response
*/
public function ePartsAction(Request $request): Response
{
\Pimcore\Cache::disable();
$errors = [];
$success = null;
if ($request->isMethod('POST')) {
$adminMail = $this->getDocumentEditable('relation', 'email-admin')->getElement();
$adminMailAddress = $this->getDocumentEditable('input', 'admin-address')->getData();
$successPage = $this->getDocumentEditable('relation', 'success')->getElement();
$required = ['company', 'street', 'zip', 'city', 'country', 'email', 'phone', 'contactPerson', 'gdpr'];
$params = $this->checkForm($request, $required);
if (!empty($params['errors'])) {
$errors = $params['errors'];
$success = false;
} else {
$adminMailSuccess = false;
if ($adminMail instanceof Email) {
$mail = new Mail();
try {
$mail->setParams($params);
$mail->addTo($adminMailAddress);
$mail->setDocument($adminMail);
$mail->send();
$adminMailSuccess = true;
$success = true;
} catch(\Exception $e) {
$errors[] = 'sending admin mail failed';
}
}
if ($adminMailSuccess && $successPage instanceof Page) {
return $this->redirect((string)$successPage);
}
}
}
return $this->renderTemplate('forms/eParts.html.twig', [
'errors' => $errors,
'success' => $success,
]);
}
/**
* @param Request $request
* @param mixed $required
*
* @return mixed
*/
private function checkForm(Request $request, mixed $required): mixed
{
$validHashCash = $this->hashCashService->validateProcessFrom();
if ($validHashCash) {
$params = $request->request->all();
unset($params['elhc_stamp'], $params['elhc_difficulty'], $params['elhc_nonce']);
$errors = [];
foreach ($required as $param) {
if ($request->get($param) == '') {
$errors[$param] = $param . ' is missing';
}
if ($param == 'email' && !MailHelper::isValidEmailAddress($request->get($param))) {
$errors[$param] = 'email invalid';
}
}
if (!empty($errors)) {
$params['errors'] = $errors;
}
if (isset($params['salutation'])) {
$params['salutation'] = $this->translator->trans('form.salutation.' . $params['salutation'], [], null, $request->getLocale());
}
$params['items'] = $params;
} else {
$params['errors'] = ['recaptcha' => 'invalid captcha'];
}
return $params;
}
}