<?php
namespace Whater\UI\WebBundle\Controller\Zones;
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\User\CommandRequest\AppRegisterUbicationOwnershipRequestCommand;
use Whater\Application\UseCase\Zones\CommandRequest\GetUbicationByIdCommand;
use Whater\Application\UseCase\Zones\CommandRequest\GetEstablishmentByIdCommand;
use Whater\Application\UseCase\Zones\CommandRequest\GetUbicationBySlugCommand;
use Whater\Infrastructure\UserBundle\Form\Type\AppRegisterUbicationOwnershipRequestType;
/**
* @Route("public/ubication")
*/
class PublicUbicationController extends AbstractBusController
{
/**
* @Route("/show/id/{ubicationId}", name="web_public_ubication_show", defaults={"_format" = "html"})
*/
public function showLocationAction(Request $request, $ubicationId)
{
try {
$ubication = $this->handle(new GetUbicationByIdCommand($ubicationId));
if ($ubication == null) {
return new RedirectResponse($this->router()->generate('web_init', array(), true));
}
return new RedirectResponse($this->router()->generate('web_public_ubication_show_by_slug', ['ubicationSlug' => $ubication->slug()]));
} catch (\Exception $e) {
$errorMessage = '<br/>' . $this->translator()->trans($e->getMessage());
$this->setFlash('error', $this->translator()->trans('admin.town.show.exception') . $errorMessage);
return new RedirectResponse($this->router()->generate('web_init', array(), true));
}
}
/**
* @Route("/show/name/{ubicationSlug}", name="web_public_ubication_show_by_slug", defaults={"_format" = "html"})
*/
public function showLocationBySlugAction(Request $request, $ubicationSlug)
{
$ubication = null;
$registerOwnershipRequestForm = null;
$enableRequestOwnership = true;
try {
$ubication = $this->handle(new GetUbicationBySlugCommand($ubicationSlug));
if ($ubication == null) {
return new RedirectResponse($this->router()->generate('web_init', array(), true));
}
$registerOwnershipRequestForm = $this->getFormFactory()->create(
AppRegisterUbicationOwnershipRequestType::class,
AppRegisterUbicationOwnershipRequestCommand::convertToDTO(),
array(
'csrf_protection' => true
)
);
if ($request->isMethod('POST')) {
$registerOwnershipRequestForm->handleRequest($request);
if ($registerOwnershipRequestForm->isSubmitted() && $registerOwnershipRequestForm->isValid()) {
$this->handle(AppRegisterUbicationOwnershipRequestCommand::fromDTO($registerOwnershipRequestForm->getData()));
$this->setFlash('success', $this->translator()->trans('public.ownership_request.register.success'));
$enableRequestOwnership = false;
} else {
$this->setFlash('error', $this->translator()->trans('public.ownership_request.register.invalid_form'));
}
}
} catch (\Exception $e) {
$errorMessage = '<br/>' . $this->translator()->trans($e->getMessage());
$this->setFlash('error', $this->translator()->trans('public.ownership_request.show.exception') . $errorMessage);
}
return $this->render('Zones/public_ubication_show.html.twig', array(
'ubication' => $ubication,
'form' => $registerOwnershipRequestForm->createView(),
'enableRequestOwnership' => $enableRequestOwnership
));
}
/**
* @Route("/show/establishment/{establishmentId}", name="web_public_establishment_show", defaults={"_format" = "html"})
*/
public function showEstablishmentAction(Request $request, $establishmentId)
{
$establishment = null;
$distributionNetwork = null;
try {
$establishment = $this->handle(new GetEstablishmentByIdCommand($establishmentId));
if ($establishment == null) {
return new RedirectResponse($this->router()->generate('web_init', array(), true));
}
$distributionNetwork = $establishment->distributionNetwork();
} catch (\Exception $e) {
$errorMessage = '<br/>' . $this->translator()->trans($e->getMessage());
$this->setFlash('error', $this->translator()->trans('admin.establishment.show.exception') . $errorMessage);
}
return $this->render('Zones/establishment_show.html.twig', array(
'establishment' => $establishment,
'distributionNetwork' => $distributionNetwork
));
}
}