src/Hitso/Bundle/ContentBundle/Controller/Front/ArticleController.php line 33

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Hitso\Bundle\ContentBundle\Controller\Front;
  4. use Doctrine\Common\Util\Debug;
  5. use Doctrine\ORM\Query;
  6. use Hitso\Bundle\CommonBundle\Controller\Controller;
  7. use Hitso\Bundle\ContentBundle\Entity\Article;
  8. use Hitso\Bundle\ContentBundle\Entity\Comment;
  9. use Hitso\Bundle\ContentBundle\Form\CommentFormType;
  10. use Hitso\Bundle\ContentBundle\Model\EditContext;
  11. use Hitso\Bundle\FileManagerBundle\Entity\File;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs;
  15. /**
  16.  * Class ArticleController
  17.  *
  18.  * @package Hitso\Bundle\ContentBundle\Controller\Front
  19.  */
  20. class ArticleController extends ContentController
  21. {
  22.     /**
  23.      * @param Request     $request
  24.      * @param Article     $article
  25.      * @param Breadcrumbs $breadcrumbs
  26.      *
  27.      * @return Response
  28.      */
  29.     public function showAction(Request $requestArticle $articleBreadcrumbs $breadcrumbs): Response
  30.     {
  31. //        @todo
  32. //        w przypadku usunięcia zdjęcia do artykułu (z poziomu FM)
  33. //        cała encja była pobierana z cache wraz z referencją do nieistniejącego już pliku
  34. //        dlatego artykuł pobieramy w ten sposób aby pominąć cache
  35.         $article $this->getManagerForClass(Article::class)
  36.             ->getRepository()
  37.             ->createQueryBuilder('q')
  38.             ->select()
  39.             ->where('q.id = :id')
  40.             ->setParameter('id'$article->getId())
  41.             ->setMaxResults(1)
  42.             ->getQuery()
  43.             ->setHint(Query::HINT_REFRESHtrue)
  44.             ->getOneOrNullResult();
  45.         $token $request->query->get('token'null);
  46.         if (false === $article->isAuthorized($token)) {
  47.             throw $this->createNotFoundException();
  48.         }
  49.         $category $article->getCategory();
  50.         $breadcrumbs->addRouteItem($category->getName(), 'dynamic_' $category->getRoute()->getId());
  51.         $breadcrumbs->addRouteItem($article->getTitle(), 'dynamic_' $article->getRoute()->getId());
  52.         $this->updateSeo($article);
  53.         $this->updateSeoPage($article);
  54.         $params = ['article' => $article];
  55.         $typeAcro        $article->getType();
  56.         $type            $this->get('hitso.content_types')->get($typeAcro);
  57.         $commentsSupport $type->isCommentsSupport();
  58.         if ($commentsSupport) {
  59.             $comment     = new Comment();
  60.             $commentForm $this->createForm(CommentFormType::class, $comment, [
  61.                 'commentIds' => $article->getComments()->getKeys(),
  62.             ]);
  63.             $commentForm->handleRequest($request);
  64.             $params['commentForm'] = $commentForm->createView();
  65.             if ($commentForm->isSubmitted() && $commentForm->isValid()) {
  66.                 $comment $commentForm->getData();
  67.                 $ip $request->getClientIp();
  68.                 $comment->setUserAgent($request->headers->get('User-Agent'));
  69.                 $comment->setIp($ip);
  70.                 $comment->setDomain(gethostbyaddr($ip));
  71.                 $em $this->getDoctrine()->getManager();
  72.                 $em->persist($comment);
  73.                 $em->flush();
  74.                 $article->addComment($comment);
  75.                 $em->persist($article);
  76.                 $em->flush();
  77.                 if ($request->isXmlHttpRequest()) {
  78.                     // blank comment
  79.                     $comment     = new Comment();
  80.                     $commentForm $this->createForm(CommentFormType::class, $comment, [
  81.                         'commentIds' => $article->getComments()->getKeys(),
  82.                     ]);
  83.                     $commentForm->handleRequest($request);
  84.                     $params['commentForm'] = $commentForm->createView();
  85.                     return $this->displayTemplate('Partials/comments'$params);
  86.                 } else {
  87.                     // reload
  88.                     $this->redirectToRoute('dynamic_' $article->getRoute()->getId());
  89.                 }
  90.             }
  91.             if ($request->isXmlHttpRequest()) {
  92.                 return $this->displayTemplate('Partials/comments'$params);
  93.             }
  94.         }
  95.         return $this->displayTemplate('show'$params);
  96.     }
  97.     /**
  98.      * @param Article     $article
  99.      * @param EditContext $context
  100.      *
  101.      * @return Response
  102.      */
  103.     public function editAction(Article $articleEditContext $context): Response
  104.     {
  105.         return $this->displayTemplate('edit', [
  106.             'cover'   => $this->get('serializer')->serialize($article->getPhoto(), 'json'),
  107.             'article' => $article,
  108.             'context' => $context,
  109.         ]);
  110.     }
  111.     /**
  112.      * @param Article $article
  113.      */
  114.     protected function updateSeo(Article $article)
  115.     {
  116.         $mediaUrlGenerator $this->get('hitso.media_url_generator');
  117.         $url     $this->getRouterHelper()->generateRouteUrl($article->getRoute(), [], 0);
  118.         $seoPage $this->getSeoPage();
  119.         $seoPage->setLinkCanonical($url);
  120.         $seoPage->addMeta('property''og:type''article');
  121.         $seoPage->addMeta('property''article:modified_time'$article->getUpdatedAt()->format('c'));
  122.         $seoPage->addMeta('property''article:published_time'$article->getPublishedAt()->format('c'));
  123.         $seoPage->addMeta('property''article:section'$article->getCategory()->getName());
  124.         $photo $article->getPhoto();
  125.         if ($mediaUrlGenerator->validUrl($photo) && $filePath $mediaUrlGenerator->fileUrl($photo)) {
  126.             $host $this->getRequestHelper()->getCurrentRequest()->getSchemeAndHttpHost();
  127.             $seoPage->addMeta('property''og:image:url'$host $filePath);
  128.             $file substr($filePath1);
  129.             if (file_exists($file)) {
  130.                 [$width$height] = getimagesize($file);
  131.                 $seoPage->addMeta('property''og:image:width', (string) $width);
  132.                 $seoPage->addMeta('property''og:image:height', (string) $height);
  133.             }
  134.         }
  135.     }
  136. }