src/Controller/DefaultController.php line 89

Open in your IDE?
  1. <?php
  2.     namespace App\Controller;
  3.     use App\Form\ContactFormType;
  4.     use Devkitchen\DocumentIndexingBundle\Annotations\IndexingDocumentType;
  5.     use Devkitchen\DocumentIndexingBundle\Controller\DocumentTypeAwareController;
  6.     use Devkitchen\DocumentIndexingBundle\DataExtractorService\DataExtractorService;
  7.     use Devkitchen\DocumentIndexingBundle\DataExtractorService\DocumentTypeHandlerFactory;
  8.     use Devkitchen\DocumentIndexingBundle\IndexingService\IndexingService;
  9.     use Pimcore\Config;
  10.     use Pimcore\Log\ApplicationLogger;
  11.     use Pimcore\Mail;
  12.     use Pimcore\Model\Asset;
  13.     use Pimcore\Model\Document\Editable\Relations;
  14.     use Pimcore\Model\Document\Page;
  15.     use Pimcore\Translation\Translator;
  16.     use Symfony\Component\HttpFoundation\JsonResponse;
  17.     use Symfony\Component\HttpFoundation\RedirectResponse;
  18.     use Symfony\Component\HttpFoundation\Request;
  19.     use Symfony\Component\HttpFoundation\Response;
  20.     use Symfony\Component\HttpKernel\Exception\HttpException;
  21.     use Symfony\Component\HttpKernel\KernelInterface;
  22.     use Symfony\Component\Routing\Annotation\Route;
  23.     class DefaultController extends BaseController implements DocumentTypeAwareController {
  24.         /**
  25.          * @param Request $request
  26.          *
  27.          * @return Response
  28.          */
  29.         public function indexAction(Request $requestIndexingService $indexingService): Response {
  30.             $introItems = [];
  31.             /** @var Relations $documentRelations */
  32.             $documentRelations $this->getDocumentEditable'relations''intro-items');
  33.             if (!$documentRelations->isEmpty()) {
  34.                 $documentLinks $documentRelations->getData();
  35.                 /**
  36.                  * @var int $index
  37.                  * @var Page $documentLink
  38.                  */
  39.                 foreach($documentLinks as $index => $documentLink) {
  40.                     $documentData $indexingService->get$documentLink );
  41.                     if (!$documentData) continue;
  42.                     $imageId $documentData->teaserAssetBigId ?: $documentData->teaserAssetId;
  43.                     if ($imageId) {
  44.                         $asset Asset::getById$imageId );
  45.                     }
  46.                     else {
  47.                         $asset $this->document->getProperty'document-teaser-image-fallback' );
  48.                     }
  49.                     if ($asset instanceof Asset\Image) {
  50.                         $image $asset->getThumbnail('intro')->getHtml();
  51.                     }
  52.                     $introItems[] = [
  53.                         'title' => $documentData->teaserTitleBig ?: $documentData->teaserTitle,
  54.                         'content' => $documentData->teaserContentBig ?: $documentData->teaserContent,
  55.                         'image' => $image,
  56.                         'link' => $documentLink->getHref()
  57.                     ];
  58.                 }
  59.             }
  60.             return $this->render'default/index.html.twig', [
  61.                 'layoutClass' => 'layout--index',
  62.                 'introItems' => $introItems
  63.             ]);
  64.         }
  65.         /**
  66.          * @param Request $request
  67.          * @return Response
  68.          */
  69.         public function defaultAction(Request $request): Response {
  70.             return $this->render('default/default.html.twig');
  71.         }
  72.         /**
  73.          * @param Request $request
  74.          *
  75.          * @return Response
  76.          */
  77.         public function contactAction(Request $requestTranslator $translatorApplicationLogger $applicationLoggerKernelInterface $kernel): Response {
  78.             $formErrors = [];
  79.             $form $this->createFormContactFormType::class );
  80.             $form->handleRequest($request);
  81.             if ($request->isMethod'POST')) {
  82.                 if ($form->isSubmitted()) {
  83.                     if ($form->isValid()) {
  84.                         $formData $form->getData();
  85.                         $referer         $request->headers->get'referer' );
  86.                         $checkForReferer $kernel->getEnvironment() == 'prod' 'nuernberger-bratwuerste' 'cooking-pot';
  87.                         $honeypotMail    $request->get'email-repeat'false );
  88.                         if ( $honeypotMail != '' || strpos$referer$checkForReferer ) === false ) {
  89.                             $applicationLogger->info'Spam protection (contactForm), tried with mail: ' $formData['email'] );
  90.                             throw new HttpExceptionResponse::HTTP_NOT_ACCEPTABLE'Ehm no. EMR filled, ref not matching' );
  91.                         }
  92.                         //do stuff and send mail
  93.                         $body  'Name: '.$formData['name']."\r\n" .
  94.                                  'Email: '.$formData['email']."\r\n\n" .
  95.                                  'Nachricht: '.$formData['message'];
  96.                         try {
  97.                             $config Config::getSystemConfiguration('email');
  98.                             $mail = new Mail();
  99.                             $mail->to($config['sender']['email']);
  100.                             $mail->subject$translator->trans'mail.subject.contact-form' ) );
  101.                             $mail->text$body );
  102.                             $mail->send();
  103.                             return new RedirectResponse$this->document->getProperty'redirect-contactform-success') );
  104.                         }
  105.                         catch (\Exception $exception) {
  106.                             $applicationLogger->logException'Mailer error (contactForm)'$exception);
  107.                             return new RedirectResponse$this->document->getProperty'redirect-contactform-error') );
  108.                         }
  109.                     }
  110.                     else {
  111.                         foreach ($form->getErrors() as $error) {
  112.                             $formErrors[] = $error->getMessage();
  113.                         }
  114.                     }
  115.                 }
  116.             }
  117.             return $this->render'default/contact.html.twig', [
  118.                 'form' => $form->createView(),
  119.                 'errors' => $formErrors
  120.             ]);
  121.         }
  122.         /**
  123.          * @Route("/debug")
  124.          * @param Request $request
  125.          *
  126.          * @return JsonResponse
  127.          */
  128.         public function debugAction(Request $requestIndexingService $x): JsonResponse {
  129.             //$y = $x->create( 'recipe');
  130.             //$da = $y->getData();
  131.             //p_r($da);
  132.             //p_r( Page::getById( 35)->getProperty( 'document-teaser-image')->getId() );
  133.             //p_r($this->container);
  134.             //p_r($this->container->get('pimcore.email.sender.email'));
  135.             $config Config::getSystemConfiguration('email');
  136.             p_r($config);
  137.             //p_r($x->getDocumentTypes());
  138.             //
  139.             //p_r($x->getFieldAggregated( 'recipe', 'documentSubtype' ));
  140.             //p_r($x->listing( 'recipe', ['default'] ));
  141.             return new JsonResponse([]);
  142.         }
  143.     }