src/Application/Controller/CalcController.php line 99

Open in your IDE?
  1. <?php
  2. namespace App\Application\Controller;
  3. use App\Admin\Document\Article;
  4. use App\Admin\Document\Category;
  5. use App\Admin\Document\Page;
  6. use App\Admin\Document\Product;
  7. use App\Admin\Document\Tag;
  8. use App\Application\Cart\Cart;
  9. use Symfony\Component\HttpFoundation\JsonResponse;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use \Symfony\Component\Routing\Annotation\Route;
  12. class CalcController extends AbstractPageController
  13. {
  14.     protected function getPageUrl() {
  15.         return 'calc';
  16.     }
  17.     protected function getIndexParams(Request $request) {
  18.         $product null;
  19.         $id $request->get('id');
  20.         if ($id) {
  21.             /**
  22.              * @var Product $product
  23.              */
  24.             $product $this->getDocumentRepository(Product::class)->find($id);
  25.             if (!$product || !$product->getActive()) {
  26.                 throw $this->createNotFoundException('product not found or not active: ' $id);
  27.             }
  28.             if ($product && $product->isGlueCalculator()) {
  29.                 if (!$this->cart->hasCalcItem($id)) {
  30.                     $this->cart->addToCalc($id);
  31.                 }
  32.             }
  33.             if ($product && $product->isDecorCalculator()) {
  34.                 if (!$this->cart->hasDecorCalcItem($id)) {
  35.                     $this->cart->addToDecorCalc($id);
  36.                 }
  37.             }
  38.         }
  39.         return [
  40.             'selectedProduct' => $product,
  41.             'decorCategories' => $this->findCategories(Category::DECOR_CALC_TYPES),
  42.             'glueCategories' => $this->findCategories([Category::CALC_TYPE_GLUE]),
  43.             'glueProduct' => $this->getGlueProduct()
  44.         ];
  45.     }
  46.     private function findCategories(array $calcTypes) {
  47.         return $this->getDocumentRepository(Category::class)->findBy(['active' => true'calcType' => ['$in' => $calcTypes]], ['orderNumber' => 'asc']);
  48.     }
  49.     /**
  50.      * @Route("/calc-get-products", name="calc-get-products")
  51.      */
  52.     public function getProductsAjax(Request $requestCart $cart)
  53.     {
  54.         $id $request->get('id');
  55.         $products $this->getDocumentRepository(Product::class)->findBy(['active' => true'categories' => $id], ['name' => 'asc']);
  56.         return new JsonResponse([
  57.             'productsByNameHtml' => $this->renderView('application/calc/products-by-name-select.html.twig', ['products' => $products]),
  58.         ]);
  59.     }
  60.     /**
  61.      * @Route("/calc", name="calc")
  62.      * @param Request $request
  63.      * @return \Symfony\Component\HttpFoundation\Response
  64.      */
  65.     public function index(Request $request)
  66.     {
  67.         return parent::index($request);
  68.     }
  69.     /**
  70.      * @Route("/calc-decor", name="calc-decor")
  71.      * @param Request $request
  72.      * @return \Symfony\Component\HttpFoundation\Response
  73.      */
  74.     public function decorCalc(Request $request)
  75.     {
  76.         $this->pageUrl 'calc-decor';
  77.         $params $this->getIndexParams($request);
  78.         $params['onlyDecor'] = true;
  79.         
  80.         $page $this->getDocumentRepository(Page::class)->findOneBy(['url' => $this->pageUrl]);
  81.         if (!$page) {
  82.             $page $this->getDocumentRepository(Page::class)->findOneBy(['url' => 'calc']);
  83.         }
  84.         $params['page'] = $page;
  85.         
  86.         return $this->render('application/calc/decor.html.twig'$params);
  87.     }
  88.     /**
  89.      * @Route("/calc-add-ajax", name="calc-add-ajax")
  90.      */
  91.     public function addAjax(Request $requestCart $cart)
  92.     {
  93.         $id $request->get('id');
  94.         $decor $request->get('decor');
  95.         /**
  96.          * @var Product $product
  97.          */
  98.         $product $this->getDocumentRepository(Product::class)->find($id);
  99.         if (!$product) {
  100.             throw $this->createNotFoundException('product not found: ' $id);
  101.         }
  102.         if ($decor) {
  103.             $cart->addToDecorCalc($id);
  104.         } else {
  105.             $cart->addToCalc($id);
  106.         }
  107.         return $this->getView($decor$cart);
  108.     }
  109.     /**
  110.      * @Route("/calc-put-ajax", name="calc-put-ajax")
  111.      */
  112.     public function putAjax(Request $requestCart $cart)
  113.     {
  114.         $id $request->get('id');
  115.         $decor $request->get('decor');
  116.         /**
  117.          * @var Product $product
  118.          */
  119.         $product $this->getDocumentRepository(Product::class)->find($id);
  120.         if (!$product) {
  121.             throw $this->createNotFoundException('product not found: ' $id);
  122.         }
  123.         if ($decor) {
  124.             throw $this->createNotFoundException('not implemented');
  125.         } else {
  126.             $cart->addToCalc($id$request->get('count'), true);
  127.         }
  128.         return $this->getView($decor$cart);
  129.     }
  130.     /**
  131.      * @Route("/calc-remove-ajax", name="calc-remove-ajax")
  132.      */
  133.     public function removeAjax(Request $requestCart $cart)
  134.     {
  135.         $id $request->get('id');
  136.         $decor $request->get('decor');
  137.         if ($decor) {
  138.             $cart->removeFromDecorCalc($id);
  139.         } else {
  140.             $cart->removeFromCalc($id);
  141.         }
  142.         return $this->getView($decor$cart);
  143.     }
  144.     /**
  145.      * @Route("/calc-set-ajax", name="calc-set-ajax")
  146.      */
  147.     public function setAjax(Request $requestCart $cart)
  148.     {
  149.         $setter 'setCalc' $request->get('type');
  150.         $cart->$setter($request->get('value'));
  151.         return $this->getView(true$cart);
  152.     }
  153.     private function getView($decorCart $cart) {
  154.         return new JsonResponse(
  155.             [
  156.                 'html' => $this->renderView($decor 'application/calc/decor-products.html.twig' 'application/calc/products.html.twig'),
  157.                 'glueHtml' => $this->renderView($decor 'application/calc/decor-glue-product.html.twig' 'application/calc/glue-product.html.twig',
  158.                     [
  159.                         'glueProduct' => $this->getGlueProduct()
  160.                     ]),
  161.             ]
  162.         );
  163.     }
  164.     private function getGlueProduct() {
  165.         return $this->getDocumentRepository(Product::class)->findOneBy(['id1C' => '307ef102-3c55-11ea-8db7-005056a56343']);
  166.     }
  167.     /**
  168.      * @Route("/calc-add-to-cart-ajax", name="calc-add-to-cart-ajax")
  169.      * @param Request $request
  170.      * @return \Symfony\Component\HttpFoundation\Response
  171.      */
  172.     public function addToCart(Request $requestCart $cart)
  173.     {
  174.         $id $request->get('id');
  175.         $cart->add($id$cart->getDecorGlueCount());
  176.         foreach ($cart->getDecorCalcItems() as $item) {
  177.             $cart->add($item->product->getId(), $item->quantity);
  178.         }
  179.         return new JsonResponse([
  180.             'cartHtml' => $this->renderView('application/partial/cart.html.twig'),
  181.         ]);
  182.     }
  183. }