src/Controller/ContentController.php line 115

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Form\ContactFormType;
  4. use App\Form\WhitepaperFormType;
  5. use Pimcore\Model\DataObject;
  6. use Pimcore\Controller\FrontendController;
  7. use Pimcore\Log\ApplicationLogger;
  8. use Pimcore\Config\Config;
  9. use Pimcore\Model\WebsiteSetting;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Contracts\Translation\TranslatorInterface;
  12. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  13. class ContentController extends FrontendController
  14. {
  15.     // --------------------------------
  16.     // DEFAULT
  17.     // --------------------------------
  18.     public function defaultAction(
  19.         Request $request
  20.         ApplicationLogger $logger,
  21.         TranslatorInterface $translator
  22.     ) {
  23.         // contact form
  24.         $contactFormConfirmation WebsiteSetting::getByName('contactConfirmation'null$request->getLocale());
  25.         $contactForm $this->contactForm($request$logger$translator);
  26.         if ($contactForm == 'submitted') {
  27.             return $this->redirect($contactFormConfirmation->getData()->getPath() . $contactFormConfirmation->getData()->getKey());
  28.         }
  29.         return $this->render('content/default.html.twig', [
  30.             'contactForm' => $contactForm
  31.         ]);
  32.     }
  33.     // --------------------------------
  34.     // CONTACT FORM
  35.     // --------------------------------
  36.     public function contactForm(
  37.         Request $request
  38.         ApplicationLogger $logger,
  39.         TranslatorInterface $translator
  40.     ) {
  41.         // website settings:
  42.         // contactMail (email)
  43.         // contactConfirmation (document)
  44.         // build form
  45.         $form $this->createForm(ContactFormType::class);
  46.         // get form request
  47.         $form->handleRequest($request);
  48.         
  49.         // success
  50.         if ($form->isSubmitted() && $form->isValid()) {
  51.             // get form fields
  52.             $data $form->getData();
  53.             // spam
  54.             if ($data['hidden'] !== NULL || $data['filled'] !== 'jsfilled') {
  55.                 $logger->debug('Contact mail spam');
  56.                 return 'submitted';
  57.             
  58.             // no spam
  59.             } else {
  60.                 //$logger->debug('Contact mail valid');
  61.                 // mail document
  62.                 $mailDocPath WebsiteSetting::getByName('contactMail'null$request->getLocale());
  63.                 $mailDoc $mailDocPath->getData();
  64.                 // send mail
  65.                 try {
  66.                     $mail = new \Pimcore\Mail();
  67.                     $mail->addTo($data['email']);
  68.                     $mail->setDocument($mailDoc);
  69.                     $mail->setParams([
  70.                         'headtitle' => $translator->trans('email.contact.headtitle'),
  71.                         'preheader' => $translator->trans('email.contact.preheader'),
  72.                         'firstname' => $data['firstname'],
  73.                         'lastname'  => $data['lastname'],
  74.                         'phone'     => $data['phone'],
  75.                         'email'     => $data['email'],
  76.                         'message'   => nl2br($data['message'])
  77.                     ]);
  78.                     $mail->send();
  79.                 } catch (\Exception $e) {
  80.                     $logger->debug('Contact mail failed: ' $e);
  81.                 }
  82.                 return 'submitted';
  83.             }
  84.         } else {
  85.             return $form->createView();
  86.         }
  87.     }
  88.     // -----------------------------------
  89.     // FOOTER
  90.     // -----------------------------------
  91.     public function footerAction(Request $request)
  92.     {
  93.         return $this->render('content/footer.html.twig');
  94.     }
  95.     // -----------------------------------
  96.     // STYLE GUIDE
  97.     // -----------------------------------
  98.     public function styleguideAction(Request $request)
  99.     {
  100.         return $this->render('content/styleguide.html.twig');
  101.     }
  102. }