src/UI/WebBundle/Controller/Whater/PublicWhaterPointController.php line 70

Open in your IDE?
  1. <?php
  2. namespace Whater\UI\WebBundle\Controller\Whater;
  3. use Endroid\QrCode\Builder\BuilderInterface;
  4. use Whater\UI\WebBundle\Controller\AbstractBusController;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use Symfony\Component\HttpFoundation\RedirectResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Whater\Application\UseCase\Blog\CommandRequest\GetBlogArticleForWhaterpointCommand;
  9. use Whater\Application\UseCase\Product\CommandRequest\GetRecommendedProductsForWhaterpointCommand;
  10. use Whater\Application\UseCase\Whater\CommandRequest\GetWhaterPointForShowCommand;
  11. use Whater\Application\UseCase\Whater\CommandRequest\GetWhaterValorationForShowCommand;
  12. use Whater\Application\UseCase\Whater\CommandRequest\GetAnalyticalByDistributionNetworkAndResponsableCommand;
  13. use Whater\Application\UseCase\Whater\CommandRequest\GetWhaterPointForShowBySlugCommand;
  14. use Whater\Domain\Blog\Model\Article;
  15. /**
  16. * @Route("public/whater-point")
  17. */
  18. class PublicWhaterPointController extends AbstractBusController
  19. {
  20. /**
  21. * @Route("/show/{whaterPointId}", name="web_public_whater_point_show_by_id_1", defaults={"_format" = "html"})
  22. */
  23. public function showWhaterPointOldAction(Request $request, $whaterPointId)
  24. {
  25. return new RedirectResponse($this->router()->generate('web_public_whater_point_show', ['whaterPointId' => $whaterPointId]));
  26. }
  27. /**
  28. * @Route("/show/id/{whaterPointId}", name="web_public_whater_point_show_by_id_2", defaults={"_format" = "html"})
  29. */
  30. public function showWhaterPointAction(Request $request, $whaterPointId)
  31. {
  32. try {
  33. $whaterPoint = $this->handle(new GetWhaterPointForShowCommand($whaterPointId));
  34. if ($whaterPoint == null) {
  35. return new RedirectResponse($this->router()->generate('web_init', array(), true));
  36. }
  37. return new RedirectResponse($this->router()->generate('web_public_whater_point_show_by_slug', ['whaterPointSlug' => $whaterPoint->slug()]));
  38. } catch (\Exception $e) {
  39. $this->setFlash('error', $this->translator()->trans($e->getMessage()));
  40. return new RedirectResponse($this->router()->generate('web_init', array(), true));
  41. }
  42. }
  43. /**
  44. * @Route("/show/slug/{whaterPointSlug}", name="web_public_whater_point_show_by_slug", defaults={"_format" = "html"})
  45. */
  46. public function showWhaterPointBySlugAction(Request $request, $whaterPointSlug)
  47. {
  48. $whaterPoint = null;
  49. $colorValoration = null;
  50. $tasteValoration = null;
  51. $smellValoration = null;
  52. $turbidityValoration = null;
  53. $whaterPointAnalyticArray = [];
  54. $recommendedProducts = [];
  55. $whaterArticles = [];
  56. try {
  57. $whaterPoint = $this->handle(new GetWhaterPointForShowBySlugCommand($whaterPointSlug));
  58. if ($whaterPoint == null) {
  59. return new RedirectResponse($this->router()->generate('web_init', array(), true));
  60. }
  61. $whaterPointAnalyticArray = $this->handle(new GetAnalyticalByDistributionNetworkAndResponsableCommand(null, $whaterPoint));
  62. $recommendedProducts = $this->handle(new GetRecommendedProductsForWhaterpointCommand($whaterPoint));
  63. $grantUser = $this->getGrantedUser();
  64. if ($grantUser != null) {
  65. $whaterValoration = $this->handle(new GetWhaterValorationForShowCommand(
  66. $whaterPoint->id(),
  67. $grantUser
  68. ));
  69. if ($whaterValoration != null) {
  70. $colorValoration = $whaterValoration->colorValoration();
  71. $tasteValoration = $whaterValoration->tasteValoration();
  72. $smellValoration = $whaterValoration->smellValoration();
  73. $turbidityValoration = $whaterValoration->turbidityValoration();
  74. }
  75. }
  76. $whaterArticles = $whaterPoint->articles()->toArray();
  77. if (count($whaterArticles) <= 5) {
  78. if ($whaterPoint->distributionNetwork() != null) {
  79. $distributionNetworkArticles = $whaterPoint->distributionNetwork()->articles()->toArray();
  80. $whaterArticles = array_merge($whaterArticles, $distributionNetworkArticles);
  81. }
  82. }
  83. $uniqueArticlesArray = [];
  84. $ids = [];
  85. // Recorrer el array y eliminar no publicados y duplicados
  86. foreach ($whaterArticles as $wa) {
  87. if ($wa->status() != Article::ARTICLE_PUBLIC) {
  88. continue;
  89. }
  90. if (!in_array($wa->id(), $ids)) {
  91. $uniqueArticlesArray[] = $wa;
  92. $ids[] = $wa->id();
  93. }
  94. }
  95. usort($uniqueArticlesArray, function ($a, $b) {
  96. return $a->publishAt() < $b->publishAt();
  97. });
  98. $whaterArticles = $uniqueArticlesArray;
  99. } catch (\Exception $e) {
  100. $errorMessage = '<br/>' . $this->translator()->trans($e->getMessage());
  101. $this->setFlash('error', $this->translator()->trans('admin.whaterpoint.show.exception') . $errorMessage);
  102. }
  103. return $this->render('Whater/public_whater_point_show.html.twig', array(
  104. 'whaterPoint' => $whaterPoint,
  105. 'colorValoration' => $colorValoration,
  106. 'tasteValoration' => $tasteValoration,
  107. 'smellValoration' => $smellValoration,
  108. 'turbidityValoration' => $turbidityValoration,
  109. 'analytics' => $whaterPointAnalyticArray,
  110. 'recommendedProducts' => $recommendedProducts,
  111. 'whaterArticles' => $whaterArticles
  112. ));
  113. }
  114. }