src/Controller/UploadController.php line 122

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Dictionary\PageDictionary;
  4. use Doctrine\Common\Collections\ArrayCollection;
  5. use Doctrine\ORM\EntityManager;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use Imagick;
  8. use App\Dictionary\UploaderDirectory;
  9. use App\Entity\Offer;
  10. use App\Entity\Review;
  11. use App\Entity\ReviewAverageRate;
  12. use App\Entity\ReviewComment;
  13. use App\Entity\ReviewCommentAuthor;
  14. use App\Entity\ReviewCommentAuthorDictionary;
  15. use App\Entity\ReviewCommentRate;
  16. use App\Entity\ReviewCommentService;
  17. use App\Entity\ReviewRateDictionary;
  18. use App\Entity\ReviewServiceArea;
  19. use App\Entity\ReviewServiceDictionary;
  20. use App\Entity\ReviewStatusDictionary;
  21. use App\Entity\ReviewVerificationDictionary;
  22. use App\Entity\ReviewVerificationStatusDictionary;
  23. use App\Entity\ReviewVeryfication;
  24. use App\Form\ReviewType;
  25. use App\Repository\OfferRepository;
  26. use App\Service\DirectoryManager;
  27. use App\Uploader\Document;
  28. use App\Entity\ReviewCommentPhoto;
  29. use App\Uploader\FileSaver;
  30. use App\Uploader\HddSaver;
  31. use App\Uploader\ImageResizer;
  32. use App\Validator\ReviewDescription;
  33. use Psr\Log\LoggerInterface;
  34. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  35. use Symfony\Component\HttpFoundation\File\UploadedFile;
  36. use Symfony\Component\HttpFoundation\RedirectResponse;
  37. use Symfony\Component\HttpFoundation\Request;
  38. use Symfony\Component\HttpFoundation\JsonResponse;
  39. use Symfony\Component\HttpFoundation\RequestStack;
  40. use Symfony\Component\HttpFoundation\Response;
  41. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  42. /**
  43.  * https://www.wanadev.fr/44-tuto-symfony3-et-dropzonejs/
  44.  * https://stackoverflow.com/questions/30813413/dropzone-js-and-symfony-formbuilder
  45.  *
  46.  * Class UploadController
  47.  * @package App\Controller
  48.  */
  49. class UploadController extends AbstractController
  50. {
  51.     /**
  52.      * @var EntityManager|object
  53.      */
  54.     private $em;
  55.     /**
  56.      * @var RequestStack
  57.      */
  58.     private $requestStack;
  59.     /**
  60.      * @var ReviewDescription
  61.      */
  62.     private $validator;
  63.     /**
  64.      * @var int ID ogłoszenia
  65.      */
  66.     private $offerId;
  67.     /**
  68.      * @var LoggerInterface
  69.      */
  70.     private $logger;
  71.     /**
  72.      * @var bool Własność ustawiana na true w momencie gdy opinia nie przeszła walidacji i musi być sprawdzona pod kątem, np. wulgaryzmów
  73.      */
  74.     private $validateChecker false;
  75.     /**
  76.      * @var DirectoryManager
  77.      */
  78.     private $directoryManager;
  79.     /**
  80.      * @var ReviewMailerController
  81.      */
  82.     private $offerReviewMailer;
  83.     /**
  84.      * UploadController constructor.
  85.      * @param RequestStack $requestStack
  86.      * @param EntityManagerInterface $em
  87.      * @param ReviewDescription $validator
  88.      * @param LoggerInterface $logger
  89.      * @param DirectoryManager $directoryManager
  90.      * @param ReviewMailerController $offerReviewMailer
  91.      */
  92.     public function __construct(RequestStack $requestStackEntityManagerInterface $emReviewDescription $validatorLoggerInterface $loggerDirectoryManager $directoryManagerReviewMailerController $offerReviewMailer)
  93.     {
  94.         $this->requestStack $requestStack;
  95.         $this->em $em;
  96.         $this->validator $validator;
  97.         $this->logger $logger;
  98.         $this->directoryManager $directoryManager;
  99. //        $url = explode('/', $this->requestStack->getCurrentRequest()->getPathInfo());
  100. //        $this->offerId = (int)end($url);
  101.         $this->offerReviewMailer $offerReviewMailer;
  102.     }
  103.     /**
  104.      * @param Request $request
  105.      * @param int $offerId ID ogłoszenia
  106.      *
  107.      * @return RedirectResponse|Response
  108.      */
  109.     public function indexAction(Request $requestint $offerId)
  110.     {
  111.         $offerBasicData $this->em->getRepository(Offer::class)->getBasicData($offerId);
  112.         /** Jeżeli dane do ogłoszenia nie zostaną odnalezione, ozncza to że nie istnieje */
  113.         if (!$offerBasicData) {
  114.             throw new NotFoundHttpException('Podane ogłoszenie nie istnieje');
  115.         }
  116.         $this->setUploadDirectory($offerId);
  117.         $this->get('session')->set('offer_id'$offerId);
  118.         $form $this->createForm(ReviewType::class, null, [
  119.             'data' => [
  120.                 'offerId' => $offerId,
  121.                 'areaId' => $offerBasicData['kategoria_id']
  122.             ]
  123.         ]);
  124.         $form->handleRequest($request);
  125.         if ($form->isSubmitted() && $form->isValid()) {
  126.             /** @var array Tablica z dodanymi zdjęciami $images */
  127.             $images $this->directoryManager->getFileList(UploaderDirectory::ROOT_DIR UploaderDirectory::UPLOAD_DIR DIRECTORY_SEPARATOR $this->get('session')->get('upload_directory_' $offerId));
  128.             /**
  129.              * Zapis danych z formularza do bazy
  130.              */
  131.             $offerId $form->getData()['offerId'];
  132.             $offer $this->em->getRepository(Offer::class)->find($offerId);
  133.             $this->saveCommentToDb($offer$form->getData(), $images);
  134.             /**
  135.              * Zapis zdjęć na dysku w docelowej lokalizacji
  136.              */
  137.             $this->directoryManager->createOfferImageDirectory($offerId);
  138.             /** Po udanym zapisie i zwalidowaniu formularza kopiujemy zdjęcia do docelowej lokalizacji */
  139.             $this->directoryManager->copyFiles(
  140.                 $offerId,
  141.                 $images
  142.             );
  143.             $this->directoryManager->removeFiles($offerIdtrue);
  144.             /** Wysyłka e-maila po zapisie do bazy jeżeli w opinii nie znaleziono wulgaryzmów lub linków */
  145.             if ($this->validateChecker === false) {
  146.                 $notification $this->em->getRepository(Offer::class)->checkUserNotification((int)$offerBasicData['uzytkownik_id']);
  147.                 if ($notification != false) {
  148.                     $this->offerReviewMailer->sendMailToExhibitorAction($offerBasicData);
  149.                 }
  150.             } else {
  151.                 $this->offerReviewMailer->sendMailValidatedComment($offerBasicData$form->getData()['email']);
  152.                 $this->offerReviewMailer->sendMailToAdmin($offerBasicData);
  153.             }
  154.             /** Przekierowanie na stronę z widokiem ogłoszenia */
  155.             return $this->redirect(PageDictionary::ADRES_STRONY_WWW '/' $offerBasicData['link_txt'] . '-' $offerId '#opinie');
  156.         }
  157.         return $this->render('uploader.html.twig', array(
  158.             'form' => $form->createView(),
  159.             'offer_id' => $offerId,
  160.             'offer_basic_data' => $offerBasicData
  161.         ));
  162.     }
  163.     /**
  164.      * @param Request $request
  165.      * @return JsonResponse
  166.      */
  167.     public function ajaxSnippetImageSendAction(Request $request)
  168.     {
  169.         try {
  170.             /** @var UploadedFile $media */
  171.             $media $request->files->get('file');
  172.             $uploadDir UploaderDirectory::UPLOAD_DIR DIRECTORY_SEPARATOR $this->get('session')->get('upload_directory_' $this->get('session')->get('offer_id'));
  173.             $hddSaver = new HddSaver($media$this->get('session')->get('offer_id'), $uploadDir);
  174.             $hddSaver->save();
  175.             /** Tworzenie miniaturki obrazka */
  176.             $imageName $hddSaver->getDocument()->getName();
  177.             $hddSaver->resizeImage(
  178.                 $uploadDir DIRECTORY_SEPARATOR $imageName,
  179.                 $uploadDir DIRECTORY_SEPARATOR 'thumb_' $imageName
  180.             );
  181.             return new JsonResponse(array('success' => true));
  182.         } catch (\Exception $e) {
  183.             $this->logger->error($e->getMessage());
  184.             return new JsonResponse(array('success' => false));
  185.         }
  186.     }
  187.     /**
  188.      * @param int $offerId ID ogłoszenia
  189.      */
  190.     private function setUploadDirectory(int $offerId)
  191.     {
  192.         /**
  193.          * Jeżeli został ustawion folder tymczasowy do zapisu musi być on cały czas taki sam
  194.          * nawet po przeładowaniu strony
  195.          */
  196.         if (!is_null($this->get('session')->get('upload_directory_' $offerId))) {
  197.             return;
  198.         }
  199.         $date = new \DateTime();
  200.         $uploadDir 'temp_' $offerId '_' $date->format('YmdHis') . '_' $this->get('session')->getId();
  201.         $this->get('session')->set('upload_directory_' $offerId$uploadDir);
  202.     }
  203.     /**
  204.      * Zapis do bazy danych z formularza
  205.      *
  206.      * @param Offer $offer
  207.      * @param array $formData Dane pochodzące z formularza
  208.      * @param array $images Tablica ze zdjęciami
  209.      */
  210.     private function saveCommentToDb(Offer $offer, array $formData, array $images)
  211.     {
  212.         $reviewStatusDictionary $this->em->getRepository(ReviewStatusDictionary::class)->find(1);
  213.         $reviewAuthorType $this->em->getRepository(ReviewCommentAuthorDictionary::class)->find($formData['person']);
  214.         $review = new Review();
  215.         $review->setOffer($offer);
  216.         $review->setStatus($reviewStatusDictionary);
  217.         $review->setBrowser($_SERVER['HTTP_USER_AGENT']);
  218.         $review->setIp($_SERVER['REMOTE_ADDR']);
  219.         $this->em->persist($review);
  220.         $reviewComment = new ReviewComment();
  221.         $reviewComment->setReview($review);
  222.         $reviewComment->setCategory($offer->getCategoryId());
  223.         $reviewComment->setDescription($formData['description']);
  224.         (!is_null($formData['price'])) ? $reviewComment->setPrice(str_replace(',''.'$formData['price'])) : $formData['price'];
  225.         $reviewComment->setSource($formData['portal']);
  226.         $this->em->persist($reviewComment);
  227.         $reviewAuthor = new ReviewCommentAuthor();
  228.         $reviewAuthor->setReviewComment($reviewComment);
  229.         $reviewAuthor->setSignature($formData['signature']);
  230.         $reviewAuthor->setEmail($formData['email']);
  231.         $reviewAuthor->setType($reviewAuthorType);
  232.         $this->em->persist($reviewAuthor);
  233.         /**
  234.          * Dodanie ocen z gwiazdek
  235.          */
  236.         if (is_int($formData['stars_quality'])) {
  237.             $this->addStarRate($offer$reviewComment2$formData['stars_quality']);
  238.         }
  239.         if (is_int($formData['stars_professional'])) {
  240.             $this->addStarRate($offer$reviewComment3$formData['stars_professional']);
  241.         }
  242.         if (is_int($formData['stars_flexibility'])) {
  243.             $this->addStarRate($offer$reviewComment4$formData['stars_flexibility']);
  244.         }
  245.         if (is_int($formData['stars_price'])) {
  246.             $this->addStarRate($offer$reviewComment5$formData['stars_price']);
  247.         }
  248.         if (is_int($formData['stars_cooperation'])) {
  249.             $this->addStarRate($offer$reviewComment6$formData['stars_cooperation']);
  250.         }
  251.         /** Ocena średnia */
  252.         $average $this->countMainAverageRate($formData);
  253.         if ($average) {
  254.             $this->addStarRate($offer$reviewComment1$average);
  255.         }
  256.         /**
  257.          * Zapisanie usług
  258.          */
  259.         /** @var ArrayCollection $formData ['service'] */
  260.         $services $formData['service']->toArray();
  261.         /** @var ReviewServiceArea $service */
  262.         foreach ($services as $service) {
  263.             $serviceArea = new ReviewCommentService();
  264.             $serviceArea->setReviewComment($reviewComment);
  265.             $serviceArea->setService($service->getService());
  266.             $this->em->persist($serviceArea);
  267.         }
  268.         /** Zapis zdjęć do bazy */
  269.         $this->savePhoto($images$reviewComment$formData['photo_author']);
  270.         /** Sprawdzenie treści komentarza pod kątem m.in. wystepowania wulgaryzmów */
  271.         if ($this->validator->checkUrlInString($formData['description']) == true || $this->validator->checkBadWords($formData['description']) == true) {
  272.             $reviewStatusDictionary $this->em->getRepository(ReviewStatusDictionary::class)->find(3);
  273.             $review->setStatus($reviewStatusDictionary);
  274.             $this->em->persist($review);
  275.             $reviewVerification = new ReviewVeryfication();
  276.             $declarantDictionary $this->em->getRepository(ReviewVerificationDictionary::class)->find(1);
  277.             $verificationStatus $this->em->getRepository(ReviewVerificationStatusDictionary::class)->find(3);
  278.             $reviewVerification->setReview($review);
  279.             $reviewVerification->setDeclarant($declarantDictionary);
  280.             $reviewVerification->setAddDate(new \DateTime());
  281.             $reviewVerification->setDescription('Znaleziono wulgaryzm lub link');
  282.             $reviewVerification->setStatus($verificationStatus);
  283.             $this->em->persist($reviewVerification);
  284.             $this->validateChecker true;
  285.         }
  286.         $this->em->flush();
  287.     }
  288.     /**
  289.      * @param Offer $offer
  290.      * @param ReviewComment $reviewComment
  291.      * @param int $rateDictionaryId ID typu gwiazdki
  292.      * @param double $rateValue Ocena
  293.      */
  294.     private function addStarRate(Offer $offerReviewComment $reviewCommentint $rateDictionaryId$rateValue)
  295.     {
  296.         $rateQualityType $this->em->getRepository(ReviewRateDictionary::class)->find($rateDictionaryId);
  297.         $rateQuality = new ReviewCommentRate();
  298.         $rateQuality->setReviewComment($reviewComment);
  299.         $rateQuality->setType($rateQualityType);
  300.         $rateQuality->setRate($rateValue);
  301.         $this->em->persist($rateQuality);
  302.         /** Dodanie średniej liczby głosów */
  303.         /** Sprawdzenie czy istnieje już średnia ocena dla tej oferty i tego typu, jeżeli tak to zostanie pobrana w celu aktualizacji */
  304.         $rateQualityAverage $this->em->getRepository(ReviewAverageRate::class)->findOneBy([
  305.             "type" => $rateQualityType,
  306.             "offer" => $offer
  307.         ]);
  308.         /** Jeżeli średnia ocena jeszcze nie istnieje dla tego typu oceny to wówczas dodajemy pierwszy wpis */
  309.         if (is_null($rateQualityAverage)) {
  310.             $rateQualityAverage = new ReviewAverageRate();
  311.         }
  312.         $voteNumbers $rateQualityAverage->getVotesNumber() + 1;
  313.         $averageRateCount round($rateQualityAverage->getAverage() * $rateQualityAverage->getVotesNumber(), 2);
  314.         $rateQualityAverage->setOffer($offer);
  315.         $rateQualityAverage->setType($rateQualityType);
  316.         $rateQualityAverage->setAverage(round(($averageRateCount $rateValue) / $voteNumbers2));
  317.         $rateQualityAverage->setVotesNumber($voteNumbers);
  318.         $this->em->persist($rateQualityAverage);
  319.     }
  320.     /**
  321.      * Metoda wylicza całkowitą średnią z 5 ocen z formularza
  322.      *
  323.      * @param array $formData
  324.      *
  325.      * @return bool|float|int
  326.      */
  327.     private function countMainAverageRate(array $formData)
  328.     {
  329.         /**
  330.          * W PHP 7.1 jeżeli mamy pusty string to samo zsumowanie spowoduje warning, dlatego trzeba sprawdzić przed dodaniem czy zmienna jest typem numerycznym
  331.          */
  332.         $average 0;
  333.         if (is_numeric($formData['stars_quality'])) {
  334.             $average += $formData['stars_quality'];
  335.         }
  336.         if (is_numeric($formData['stars_professional'])) {
  337.             $average += $formData['stars_professional'];
  338.         }
  339.         if (is_numeric($formData['stars_flexibility'])) {
  340.             $average += $formData['stars_flexibility'];
  341.         }
  342.         if (is_numeric($formData['stars_price'])) {
  343.             $average += $formData['stars_price'];
  344.         }
  345.         if (is_numeric($formData['stars_cooperation'])) {
  346.             $average += $formData['stars_cooperation'];
  347.         }
  348.         if (trim($average) !== 0) {
  349.             return round($average 52);
  350.         }
  351.         return false;
  352.     }
  353.     /**
  354.      * Przygotowanie dodanych zdjęć do zapisu w bazie
  355.      *
  356.      * @param array $images
  357.      * @param ReviewComment $reviewComment
  358.      * @param string $photoAuthor
  359.      */
  360.     private function savePhoto(array $imagesReviewComment $reviewComment$photoAuthor)
  361.     {
  362.         foreach ($images as $image) {
  363.             /** Miniatruek nie zapisujemy do bazy, posdiadają one zawsze przedrostke thumb */
  364.             if (strpos($image'thumb_') === false) {
  365.                 $photo = new ReviewCommentPhoto();
  366.                 $photo->setReviewComment($reviewComment);
  367.                 $photo->setPhotoName($image);
  368.                 $photo->setAuthor($photoAuthor);
  369.                 $this->em->persist($photo);
  370.             }
  371.         }
  372.     }
  373. }