<?php
declare(strict_types=1);
namespace Hitso\Bundle\ContentBundle\Controller\Front;
use Doctrine\Common\Util\Debug;
use Doctrine\ORM\Query;
use Hitso\Bundle\CommonBundle\Controller\Controller;
use Hitso\Bundle\ContentBundle\Entity\Article;
use Hitso\Bundle\ContentBundle\Entity\Comment;
use Hitso\Bundle\ContentBundle\Form\CommentFormType;
use Hitso\Bundle\ContentBundle\Model\EditContext;
use Hitso\Bundle\FileManagerBundle\Entity\File;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs;
/**
* Class ArticleController
*
* @package Hitso\Bundle\ContentBundle\Controller\Front
*/
class ArticleController extends ContentController
{
/**
* @param Request $request
* @param Article $article
* @param Breadcrumbs $breadcrumbs
*
* @return Response
*/
public function showAction(Request $request, Article $article, Breadcrumbs $breadcrumbs): Response
{
// @todo
// w przypadku usunięcia zdjęcia do artykułu (z poziomu FM)
// cała encja była pobierana z cache wraz z referencją do nieistniejącego już pliku
// dlatego artykuł pobieramy w ten sposób aby pominąć cache
$article = $this->getManagerForClass(Article::class)
->getRepository()
->createQueryBuilder('q')
->select()
->where('q.id = :id')
->setParameter('id', $article->getId())
->setMaxResults(1)
->getQuery()
->setHint(Query::HINT_REFRESH, true)
->getOneOrNullResult();
$token = $request->query->get('token', null);
if (false === $article->isAuthorized($token)) {
throw $this->createNotFoundException();
}
$category = $article->getCategory();
$breadcrumbs->addRouteItem($category->getName(), 'dynamic_' . $category->getRoute()->getId());
$breadcrumbs->addRouteItem($article->getTitle(), 'dynamic_' . $article->getRoute()->getId());
$this->updateSeo($article);
$this->updateSeoPage($article);
$params = ['article' => $article];
$typeAcro = $article->getType();
$type = $this->get('hitso.content_types')->get($typeAcro);
$commentsSupport = $type->isCommentsSupport();
if ($commentsSupport) {
$comment = new Comment();
$commentForm = $this->createForm(CommentFormType::class, $comment, [
'commentIds' => $article->getComments()->getKeys(),
]);
$commentForm->handleRequest($request);
$params['commentForm'] = $commentForm->createView();
if ($commentForm->isSubmitted() && $commentForm->isValid()) {
$comment = $commentForm->getData();
$ip = $request->getClientIp();
$comment->setUserAgent($request->headers->get('User-Agent'));
$comment->setIp($ip);
$comment->setDomain(gethostbyaddr($ip));
$em = $this->getDoctrine()->getManager();
$em->persist($comment);
$em->flush();
$article->addComment($comment);
$em->persist($article);
$em->flush();
if ($request->isXmlHttpRequest()) {
// blank comment
$comment = new Comment();
$commentForm = $this->createForm(CommentFormType::class, $comment, [
'commentIds' => $article->getComments()->getKeys(),
]);
$commentForm->handleRequest($request);
$params['commentForm'] = $commentForm->createView();
return $this->displayTemplate('Partials/comments', $params);
} else {
// reload
$this->redirectToRoute('dynamic_' . $article->getRoute()->getId());
}
}
if ($request->isXmlHttpRequest()) {
return $this->displayTemplate('Partials/comments', $params);
}
}
return $this->displayTemplate('show', $params);
}
/**
* @param Article $article
* @param EditContext $context
*
* @return Response
*/
public function editAction(Article $article, EditContext $context): Response
{
return $this->displayTemplate('edit', [
'cover' => $this->get('serializer')->serialize($article->getPhoto(), 'json'),
'article' => $article,
'context' => $context,
]);
}
/**
* @param Article $article
*/
protected function updateSeo(Article $article)
{
$mediaUrlGenerator = $this->get('hitso.media_url_generator');
$url = $this->getRouterHelper()->generateRouteUrl($article->getRoute(), [], 0);
$seoPage = $this->getSeoPage();
$seoPage->setLinkCanonical($url);
$seoPage->addMeta('property', 'og:type', 'article');
$seoPage->addMeta('property', 'article:modified_time', $article->getUpdatedAt()->format('c'));
$seoPage->addMeta('property', 'article:published_time', $article->getPublishedAt()->format('c'));
$seoPage->addMeta('property', 'article:section', $article->getCategory()->getName());
$photo = $article->getPhoto();
if ($mediaUrlGenerator->validUrl($photo) && $filePath = $mediaUrlGenerator->fileUrl($photo)) {
$host = $this->getRequestHelper()->getCurrentRequest()->getSchemeAndHttpHost();
$seoPage->addMeta('property', 'og:image:url', $host . $filePath);
$file = substr($filePath, 1);
if (file_exists($file)) {
[$width, $height] = getimagesize($file);
$seoPage->addMeta('property', 'og:image:width', (string) $width);
$seoPage->addMeta('property', 'og:image:height', (string) $height);
}
}
}
}