<?php
namespace Whater\UI\WebBundle\Controller\Product;
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\Product\CommandRequest\GetProductForShowCommand;
use Whater\Domain\Product\Model\Product;
use Whater\Domain\Whater\Model\WhaterOrganization;
use JMS\Serializer\SerializationContext;
use Whater\Application\UseCase\Product\CommandRequest\ListPublicProductAttributeCommand;
use Whater\Application\UseCase\Product\CommandRequest\ListPublicProductCategoryCommand;
/**
* @Route("public/product")
*/
class PublicProductController extends AbstractBusController
{
/**
* @Route("/list", name="web_public_product_list", defaults={"_format" = "html"})
*/
public function productListAction(Request $request)
{
$productCategories = [];
try {
$grantedUser = $this->getGrantedUser();
if ($grantedUser != null) {
return new RedirectResponse($this->router()->generate('web_app_product_list'));
}
$listPublicProductCategoryCommand = new ListPublicProductCategoryCommand(1, 120, "product_category_name", "asc", "isMainLevel", "T");
$productCategoriesPage = $this->handle($listPublicProductCategoryCommand);
$productCategories = $productCategoriesPage->getCurrentPageResults();
} catch (\Exception $e) {
$errorMessage = '<br/>' . $this->translator()->trans($e->getMessage());
$this->setFlash('error', $this->translator()->trans('product.public.list.exception') . $errorMessage);
}
return $this->render(
'Product/public_product_list.html.twig',
[
'productCategories' => $productCategories
]
);
}
/**
* @Route("/product/show/{productSlug}", name="web_public_show_product", defaults={"_format" = "html"})
*/
public function postPublicProductBySlugAction(Request $request, string $productSlug)
{
$product = null;
$attributes = null;
try {
$product = $this->handle(new GetProductForShowCommand($productSlug));
if ($product == null) {
$this->setFlash('warning', 'Producto no encontrado!');
return new RedirectResponse($this->router()->generate('web_public_product_list'));
}
$attributes = $this->handle(new ListPublicProductAttributeCommand());
if ($product->productType() == Product::PRODUCT_TYPE_WATER_SUPPLY) {
$whaterpoint = $product->whaterPoint();
return new RedirectResponse($this->router()->generate('web_public_whater_point_show_by_slug', ['whaterPointSlug' => $whaterpoint->slug()]));
} else if ($product->productType() == Product::PRODUCT_TYPE_WATERCOINS) {
return new RedirectResponse($this->router()->generate('web_app_cart_order_buy_whatercoins'));
} else if ($product->productType() == Product::PRODUCT_TYPE_LICENSE) {
throw new \Exception('TODO: Redirect to license product page!');
}
if ($product->whaterOrganization()->stripeExpressAccountStatus() != WhaterOrganization::STRIPE_CONNECT_STATUS_CONNECTED) {
return new RedirectResponse($this->router()->generate('web_public_product_list'));
}
$productVariationsSerialized = $this->jmsSerializer()->serialize(
$product,
'json',
SerializationContext::create()->setGroups(['PublicProductVariationsShow'])
);
} catch (\Exception $e) {
$errorMessage = '<br/>' . $this->translator()->trans($e->getMessage());
$this->setFlash('error', $this->translator()->trans('product.public.show.exception') . $errorMessage);
return new RedirectResponse($this->router()->generate('web_public_product_list'));
}
return $this->render('Product/public_product_show.html.twig', array(
'product' => $product,
'productVariationsSerialized' => $productVariationsSerialized,
'attributes' => $attributes
));
}
}