<?php
namespace Whater\UI\WebBundle\Controller\Whater;
use Endroid\QrCode\Builder\BuilderInterface;
use Whater\UI\WebBundle\Controller\AbstractBusController;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Whater\Application\UseCase\Blog\CommandRequest\GetBlogArticleForWhaterpointCommand;
use Whater\Application\UseCase\Product\CommandRequest\GetRecommendedProductsForWhaterpointCommand;
use Whater\Application\UseCase\Whater\CommandRequest\GetWhaterPointForShowCommand;
use Whater\Application\UseCase\Whater\CommandRequest\GetWhaterValorationForShowCommand;
use Whater\Application\UseCase\Whater\CommandRequest\GetAnalyticalByDistributionNetworkAndResponsableCommand;
use Whater\Application\UseCase\Whater\CommandRequest\GetWhaterPointForShowBySlugCommand;
use Whater\Domain\Blog\Model\Article;
/**
* @Route("public/whater-point")
*/
class PublicWhaterPointController extends AbstractBusController
{
/**
* @Route("/show/{whaterPointId}", name="web_public_whater_point_show_by_id_1", defaults={"_format" = "html"})
*/
public function showWhaterPointOldAction(Request $request, $whaterPointId)
{
return new RedirectResponse($this->router()->generate('web_public_whater_point_show', ['whaterPointId' => $whaterPointId]));
}
/**
* @Route("/show/id/{whaterPointId}", name="web_public_whater_point_show_by_id_2", defaults={"_format" = "html"})
*/
public function showWhaterPointAction(Request $request, $whaterPointId)
{
try {
$whaterPoint = $this->handle(new GetWhaterPointForShowCommand($whaterPointId));
if ($whaterPoint == null) {
return new RedirectResponse($this->router()->generate('web_init', array(), true));
}
return new RedirectResponse($this->router()->generate('web_public_whater_point_show_by_slug', ['whaterPointSlug' => $whaterPoint->slug()]));
} catch (\Exception $e) {
$this->setFlash('error', $this->translator()->trans($e->getMessage()));
return new RedirectResponse($this->router()->generate('web_init', array(), true));
}
}
/**
* @Route("/show/slug/{whaterPointSlug}", name="web_public_whater_point_show_by_slug", defaults={"_format" = "html"})
*/
public function showWhaterPointBySlugAction(Request $request, $whaterPointSlug)
{
$whaterPoint = null;
$colorValoration = null;
$tasteValoration = null;
$smellValoration = null;
$turbidityValoration = null;
$whaterPointAnalyticArray = [];
$recommendedProducts = [];
$whaterArticles = [];
try {
$whaterPoint = $this->handle(new GetWhaterPointForShowBySlugCommand($whaterPointSlug));
if ($whaterPoint == null) {
return new RedirectResponse($this->router()->generate('web_init', array(), true));
}
$whaterPointAnalyticArray = $this->handle(new GetAnalyticalByDistributionNetworkAndResponsableCommand(null, $whaterPoint));
$recommendedProducts = $this->handle(new GetRecommendedProductsForWhaterpointCommand($whaterPoint));
$grantUser = $this->getGrantedUser();
if ($grantUser != null) {
$whaterValoration = $this->handle(new GetWhaterValorationForShowCommand(
$whaterPoint->id(),
$grantUser
));
if ($whaterValoration != null) {
$colorValoration = $whaterValoration->colorValoration();
$tasteValoration = $whaterValoration->tasteValoration();
$smellValoration = $whaterValoration->smellValoration();
$turbidityValoration = $whaterValoration->turbidityValoration();
}
}
$whaterArticles = $whaterPoint->articles()->toArray();
if (count($whaterArticles) <= 5) {
if ($whaterPoint->distributionNetwork() != null) {
$distributionNetworkArticles = $whaterPoint->distributionNetwork()->articles()->toArray();
$whaterArticles = array_merge($whaterArticles, $distributionNetworkArticles);
}
}
$uniqueArticlesArray = [];
$ids = [];
// Recorrer el array y eliminar no publicados y duplicados
foreach ($whaterArticles as $wa) {
if ($wa->status() != Article::ARTICLE_PUBLIC) {
continue;
}
if (!in_array($wa->id(), $ids)) {
$uniqueArticlesArray[] = $wa;
$ids[] = $wa->id();
}
}
usort($uniqueArticlesArray, function ($a, $b) {
return $a->publishAt() < $b->publishAt();
});
$whaterArticles = $uniqueArticlesArray;
} catch (\Exception $e) {
$errorMessage = '<br/>' . $this->translator()->trans($e->getMessage());
$this->setFlash('error', $this->translator()->trans('admin.whaterpoint.show.exception') . $errorMessage);
}
return $this->render('Whater/public_whater_point_show.html.twig', array(
'whaterPoint' => $whaterPoint,
'colorValoration' => $colorValoration,
'tasteValoration' => $tasteValoration,
'smellValoration' => $smellValoration,
'turbidityValoration' => $turbidityValoration,
'analytics' => $whaterPointAnalyticArray,
'recommendedProducts' => $recommendedProducts,
'whaterArticles' => $whaterArticles
));
}
}