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