<?php
namespace App\Application\Controller;
use App\Admin\Document\Article;
use App\Admin\Document\Category;
use App\Admin\Document\Page;
use App\Admin\Document\Product;
use App\Admin\Document\Tag;
use App\Application\Cart\Cart;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use \Symfony\Component\Routing\Annotation\Route;
class CalcController extends AbstractPageController
{
protected function getPageUrl() {
return 'calc';
}
protected function getIndexParams(Request $request) {
$product = null;
$id = $request->get('id');
if ($id) {
/**
* @var Product $product
*/
$product = $this->getDocumentRepository(Product::class)->find($id);
if (!$product || !$product->getActive()) {
throw $this->createNotFoundException('product not found or not active: ' . $id);
}
if ($product && $product->isGlueCalculator()) {
if (!$this->cart->hasCalcItem($id)) {
$this->cart->addToCalc($id);
}
}
if ($product && $product->isDecorCalculator()) {
if (!$this->cart->hasDecorCalcItem($id)) {
$this->cart->addToDecorCalc($id);
}
}
}
return [
'selectedProduct' => $product,
'decorCategories' => $this->findCategories(Category::DECOR_CALC_TYPES),
'glueCategories' => $this->findCategories([Category::CALC_TYPE_GLUE]),
'glueProduct' => $this->getGlueProduct()
];
}
private function findCategories(array $calcTypes) {
return $this->getDocumentRepository(Category::class)->findBy(['active' => true, 'calcType' => ['$in' => $calcTypes]], ['orderNumber' => 'asc']);
}
/**
* @Route("/calc-get-products", name="calc-get-products")
*/
public function getProductsAjax(Request $request, Cart $cart)
{
$id = $request->get('id');
$products = $this->getDocumentRepository(Product::class)->findBy(['active' => true, 'categories' => $id], ['name' => 'asc']);
return new JsonResponse([
'productsByNameHtml' => $this->renderView('application/calc/products-by-name-select.html.twig', ['products' => $products]),
]);
}
/**
* @Route("/calc", name="calc")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function index(Request $request)
{
return parent::index($request);
}
/**
* @Route("/calc-decor", name="calc-decor")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function decorCalc(Request $request)
{
$this->pageUrl = 'calc-decor';
$params = $this->getIndexParams($request);
$params['onlyDecor'] = true;
$page = $this->getDocumentRepository(Page::class)->findOneBy(['url' => $this->pageUrl]);
if (!$page) {
$page = $this->getDocumentRepository(Page::class)->findOneBy(['url' => 'calc']);
}
$params['page'] = $page;
return $this->render('application/calc/decor.html.twig', $params);
}
/**
* @Route("/calc-add-ajax", name="calc-add-ajax")
*/
public function addAjax(Request $request, Cart $cart)
{
$id = $request->get('id');
$decor = $request->get('decor');
/**
* @var Product $product
*/
$product = $this->getDocumentRepository(Product::class)->find($id);
if (!$product) {
throw $this->createNotFoundException('product not found: ' . $id);
}
if ($decor) {
$cart->addToDecorCalc($id);
} else {
$cart->addToCalc($id);
}
return $this->getView($decor, $cart);
}
/**
* @Route("/calc-put-ajax", name="calc-put-ajax")
*/
public function putAjax(Request $request, Cart $cart)
{
$id = $request->get('id');
$decor = $request->get('decor');
/**
* @var Product $product
*/
$product = $this->getDocumentRepository(Product::class)->find($id);
if (!$product) {
throw $this->createNotFoundException('product not found: ' . $id);
}
if ($decor) {
throw $this->createNotFoundException('not implemented');
} else {
$cart->addToCalc($id, $request->get('count'), true);
}
return $this->getView($decor, $cart);
}
/**
* @Route("/calc-remove-ajax", name="calc-remove-ajax")
*/
public function removeAjax(Request $request, Cart $cart)
{
$id = $request->get('id');
$decor = $request->get('decor');
if ($decor) {
$cart->removeFromDecorCalc($id);
} else {
$cart->removeFromCalc($id);
}
return $this->getView($decor, $cart);
}
/**
* @Route("/calc-set-ajax", name="calc-set-ajax")
*/
public function setAjax(Request $request, Cart $cart)
{
$setter = 'setCalc' . $request->get('type');
$cart->$setter($request->get('value'));
return $this->getView(true, $cart);
}
private function getView($decor, Cart $cart) {
return new JsonResponse(
[
'html' => $this->renderView($decor ? 'application/calc/decor-products.html.twig' : 'application/calc/products.html.twig'),
'glueHtml' => $this->renderView($decor ? 'application/calc/decor-glue-product.html.twig' : 'application/calc/glue-product.html.twig',
[
'glueProduct' => $this->getGlueProduct()
]),
]
);
}
private function getGlueProduct() {
return $this->getDocumentRepository(Product::class)->findOneBy(['id1C' => '307ef102-3c55-11ea-8db7-005056a56343']);
}
/**
* @Route("/calc-add-to-cart-ajax", name="calc-add-to-cart-ajax")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function addToCart(Request $request, Cart $cart)
{
$id = $request->get('id');
$cart->add($id, $cart->getDecorGlueCount());
foreach ($cart->getDecorCalcItems() as $item) {
$cart->add($item->product->getId(), $item->quantity);
}
return new JsonResponse([
'cartHtml' => $this->renderView('application/partial/cart.html.twig'),
]);
}
}