src/Controller/Frontend/InseratController.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Frontend;
  3. use App\Application\Content\ContentFrontendData;
  4. use App\Application\Content\ContentService;
  5. use App\Application\InseratFormular\InseratFormularService;
  6. use App\Entity\InseratFormular\Anschlagbrett;
  7. use App\Entity\InseratFormular\Kleininserat;
  8. use App\Form\InserateFormular\AnschlagbrettType;
  9. use App\Form\InserateFormular\KleinInseratType;
  10. use Psr\Log\LoggerInterface;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\Filesystem\Filesystem;
  13. use Symfony\Component\Finder\Finder;
  14. use Symfony\Component\HttpFoundation\File\UploadedFile;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\KernelInterface;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. use Symfony\Component\Validator\Constraints\Image;
  21. use Symfony\Component\Validator\Validator\ValidatorInterface;
  22. class InseratController extends AbstractController
  23. {
  24. #[Route(path: '/aschlagbraett', name: 'fe.page_aschlagbraett')]
  25. public function aschlagbraett(ContentService $contentService): Response
  26. {
  27. $content = $contentService->getContentByCode('ASCHLAGBRAETT', new ContentFrontendData(), true);
  28. return $this->render('frontend/page/page-template.html.twig', [
  29. 'h1_title' => 'Aschlagbrätt',
  30. 'content' => $content,
  31. ]);
  32. }
  33. #[Route(path: '/aschlagbraett/formular', name: 'fe.formular_anschlagbrett')]
  34. public function anschlagbrettFormular(
  35. Request $request,
  36. InseratFormularService $inseratFormularService
  37. ): Response {
  38. $anschlagbrett = new Anschlagbrett();
  39. $form = $this->createForm(AnschlagbrettType::class, $anschlagbrett);
  40. $form->handleRequest($request);
  41. if ($form->isSubmitted() && $form->isValid()) {
  42. $inseratFormularService->sendAnschlagbrett($anschlagbrett);
  43. return $this->redirectToRoute('fe.formular_anschlagbrett_gebucht');
  44. }
  45. return $this->render('frontend/inserat-formular/index.html.twig', [
  46. 'entity' => $anschlagbrett,
  47. 'form' => $form->createView(),
  48. 'template' => 'anschlagbrett.html.twig',
  49. ]);
  50. }
  51. #[Route(path: '/aschlagbraett/formular/gebucht', name: 'fe.formular_anschlagbrett_gebucht')]
  52. public function anschlagbrettGebucht(): Response
  53. {
  54. return $this->render('frontend/inserat-formular/anschlagbrett-gebucht.html.twig');
  55. }
  56. #[Route(path: '/kleininserat', name: 'fe.page_kleininserat')]
  57. public function kleininserat(ContentService $contentService): Response
  58. {
  59. $content = $contentService->getContentByCode('KLEININSERAT', new ContentFrontendData(), true);
  60. return $this->render('frontend/page/page-template.html.twig', [
  61. 'h1_title' => 'Kleininserat',
  62. 'content' => $content,
  63. ]);
  64. }
  65. #[Route(path: '/kleininserat/formular', name: 'fe.formular_kleininserat')]
  66. public function kleininseratFormular(
  67. Request $request,
  68. InseratFormularService $inseratFormularService
  69. ): Response {
  70. $kleininserat = new Kleininserat();
  71. $form = $this->createForm(KleinInseratType::class, $kleininserat);
  72. $form->handleRequest($request);
  73. if ($form->isSubmitted() && $form->isValid()) {
  74. $inseratFormularService->sendKleininserat($kleininserat);
  75. return $this->redirectToRoute('fe.formular_kleininserat_gebucht');
  76. }
  77. return $this->render('frontend/inserat-formular/index.html.twig', [
  78. 'entity' => $kleininserat,
  79. 'form' => $form->createView(),
  80. 'template' => 'kleininserat.html.twig',
  81. ]);
  82. }
  83. #[Route(path: '/kleininserat/formular/gebucht', name: 'fe.formular_kleininserat_gebucht')]
  84. public function kleininseratGebucht(): Response
  85. {
  86. return $this->render('frontend/inserat-formular/kleininserat-gebucht.html.twig');
  87. }
  88. #[Route(path: '/inserat-formular/fileupload/{action}', name: 'fe.inserat_formular_jqueryfileupload')]
  89. public function jqueryFileUpload(Request $request, $action, LoggerInterface $logger, ValidatorInterface $validator, KernelInterface $kernel): JsonResponse
  90. {
  91. if (!$action) {
  92. throw new \InvalidArgumentException('Action nicht definiert! Benötigt zur korrekten Ablage des hochgeladenen Files auf dem Server.');
  93. }
  94. $return_ = [
  95. 'success' => null,
  96. 'msg' => '',
  97. 'uploadedFileWebPath' => null,
  98. 'requestQueryParams' => $request->query->all(), // Alle URL Übergabeparameter zurückgeben zur möglichen Hilfe und Weiterverwendung in Frontend
  99. ];
  100. try {
  101. /**
  102. * @var UploadedFile $uploadedFile
  103. */
  104. foreach ($request->files as $uploadedFile) {
  105. switch ($action) {
  106. case 'IMAGE_FRONTEND':
  107. $uuid = $request->query->get('uuid');
  108. $formGroupId = $request->query->get('formGroupId');
  109. if (!$uuid) {
  110. throw new \InvalidArgumentException('URL Parameter "uuid" leer oder nicht vorhanden!');
  111. }
  112. // Validierung Bildupload
  113. $violations = $validator->validate($uploadedFile, [
  114. new Image(['maxSize' => '5Mi', 'maxWidth' => 5120, 'mimeTypes' => ['image/jpeg', 'image/png', 'image/gif']]), // 5120 als Schutz
  115. ]);
  116. if (0 !== count($violations)) {
  117. $return_['success'] = false;
  118. foreach ($violations as $violation) {
  119. $return_['msg'] .= $violation->getMessage();
  120. }
  121. } else {
  122. $saveDirPath = $kernel->getProjectDir().'/var/data/public/inserat-formular/images';
  123. // directory protection/security for overloading: delete all images bevor
  124. $fs = new Filesystem();
  125. if ($fs->exists($saveDirPath)) {
  126. // Lösche Dateien mit derselben UID. Gibt Probleme wenn mehrere Files
  127. // $finder = new Finder();
  128. // $finder->files()->in($saveDirPath)->name('*'.$uuid.'*');
  129. // foreach ($finder as $file) {
  130. // $fs->remove($file->getPathname());
  131. // }
  132. // Lösche alle Dateien die älter als 1 Stunde
  133. $finder = new Finder();
  134. $finder->files()->in($saveDirPath)->date('before 1 hour ago');
  135. foreach ($finder as $file) {
  136. $fs->remove($file->getPathname());
  137. }
  138. }
  139. // end
  140. // Erstelle neuen "sauberen", fixen Filename
  141. $newFileName = $uuid.'-'.mt_rand(1000, 9999).'.'.$uploadedFile->getClientOriginalExtension();
  142. $webPathToFile = $request->getBasePath().'/data/inserat-formular/images/'.$newFileName;
  143. // Datei in Projektverzeichnis speichern
  144. $uploadedFile->move($saveDirPath, $newFileName);
  145. // Webpath zum hochgeladenen File
  146. $return_['filename'] = $newFileName;
  147. $return_['success'] = true;
  148. $return_['uploadedFileWebPath'] = $webPathToFile;
  149. }
  150. break;
  151. default:
  152. throw new \InvalidArgumentException('File-Action "'.$action.'" nicht definiert oder ungültig!');
  153. }
  154. }
  155. } catch (\InvalidArgumentException $e) {
  156. $return_['success'] = false;
  157. $return_['msg'] = $e->getMessage();
  158. } catch (\Exception $e) {
  159. $logger->critical($e->getMessage());
  160. $return_['success'] = false;
  161. $return_['msg'] = 'Es ist ein Fehler beim Dateiupload passiert. Bitte versuchen Sie es noch einmal.';
  162. }
  163. return new JsonResponse($return_);
  164. }
  165. }