<?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\AppRegisterTownOwnershipRequestCommand;
use Whater\Application\UseCase\Zones\CommandRequest\GetTownByIdCommand;
use Whater\Application\UseCase\Zones\CommandRequest\GetTownBySlugCommand;
use Whater\Infrastructure\UserBundle\Form\Type\AppRegisterTownOwnershipRequestType;
/**
* @Route("public/town")
*/
class PublicTownController extends AbstractBusController
{
/**
* @Route("/show/id/{townId}", name="web_public_town_show", defaults={"_format" = "html"})
*/
public function showTownAction(Request $request, $townId)
{
$town = null;
try {
$town = $this->handle(new GetTownByIdCommand($townId));
if ($town == null) {
return new RedirectResponse($this->router()->generate('web_init', array(), true));
}
return new RedirectResponse($this->router()->generate('web_public_town_show_by_slug', ['townSlug' => $town->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/{townSlug}", name="web_public_town_show_by_slug", defaults={"_format" = "html"})
*/
public function showTownBySlugAction(Request $request, $townSlug)
{
$town = null;
$distributionNetworks = null;
$geometryPolygonArray = null;
$registerOwnershipRequestForm = null;
$enableRequestOwnership = true;
try {
$town = $this->handle(new GetTownBySlugCommand($townSlug));
if ($town == null) {
return new RedirectResponse($this->router()->generate('web_init', array(), true));
}
if ($town->geometryPolygon() != null) {
$geometryPolygonArray = json_encode($town->geometryPolygon()->toArray());
}
$distributionNetworks = $town->distributionNetworks();
$registerOwnershipRequestForm = $this->getFormFactory()->create(
AppRegisterTownOwnershipRequestType::class,
AppRegisterTownOwnershipRequestCommand::convertToDTO(),
array(
'csrf_protection' => true
)
);
if ($request->isMethod('POST')) {
$registerOwnershipRequestForm->handleRequest($request);
if ($registerOwnershipRequestForm->isSubmitted() && $registerOwnershipRequestForm->isValid()) {
$this->handle(AppRegisterTownOwnershipRequestCommand::fromDTO($registerOwnershipRequestForm->getData()));
$this->setFlash('success', $this->translator()->trans('admin.ownership_request.register.success'));
$enableRequestOwnership = false;
} else {
$this->setFlash('error', $this->translator()->trans('admin.country.edit.invalid_form'));
}
}
} catch (\Exception $e) {
$this->setFlash('error', $this->translator()->trans($e->getMessage()));
}
return $this->render('Zones/public_town_show.html.twig', array(
'town' => $town,
'distributionNetworks' => $distributionNetworks,
'form' => $registerOwnershipRequestForm->createView(),
'geometryPolygonArray' => $geometryPolygonArray,
'enableRequestOwnership' => $enableRequestOwnership
));
}
}