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

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