src/Front/Modules/OnlineService/Controller/CreatorController.php line 25

  1. <?php
  2. namespace App\Front\Modules\OnlineService\Controller;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\JsonResponse;
  5. use App\Admin\Modules\OnlineService\Service\CreatorService as AdminCreatorService;
  6. use App\UserPanel\Modules\OnlineService\Form\CreatorType as UserPanelCreatorType;
  7. use App\Front\Controller\BaseController;
  8. use App\Front\Modules\Cart\Service\CartService;
  9. class CreatorController extends BaseController
  10. {
  11.     private $_adminCreatorService;
  12.     private $_cartService;
  13.     
  14.     public function __construct(
  15.             AdminCreatorService $adminCreatorService,
  16.             CartService $cartService)
  17.     {
  18.         $this->_adminCreatorService $adminCreatorService;
  19.         $this->_cartService $cartService;
  20.     }
  21.     
  22.     public function index(Request $request$categoryId)
  23.     {
  24.         $creator $this->_adminCreatorService->getCreator();
  25.         if ($creator && count($creator->getCategories()))
  26.         {
  27.             $categories = array();
  28.             
  29.             foreach($creator->getCategories() as $category)
  30.             {
  31.                 if (count($category->getItems()))
  32.                     $categories[] = $category;
  33.             }
  34.             
  35.             if ($categories)
  36.             {
  37.                 if ($categoryId)
  38.                 {
  39.                     $category null;
  40.                     foreach($categories as $_category)
  41.                     {
  42.                         if ($_category->getId() == $categoryId)
  43.                         {
  44.                             $category $_category;
  45.                             break;
  46.                         }
  47.                     }
  48.                     if (!$category)
  49.                         throw $this->createNotFoundException();
  50.                 }
  51.                 else
  52.                     $category $categories[0];
  53.                 $form $this->createForm(UserPanelCreatorType::class, null, ['category' => $category]);
  54.                 $form->handleRequest($request);
  55.                 if ($form->isSubmitted() && $form->isValid())
  56.                 {
  57.                     if ($form['items']->getData())
  58.                     {
  59.                         foreach($form['items']->getData() as $item)
  60.                             $this->_cartService->addItem($item);
  61.                         $this->addFlash('success''Wybrane usługi zostały dodane do koszyka.');
  62.                         return $this->redirectToRoute('front_online_service_creator_index', ['categoryId' => $category->getId()]);
  63.                     }
  64.                 }
  65.             }
  66.         }
  67.         return $this->render('Front/Modules/OnlineService/Templates/Creator/index.html.twig', [
  68.             'formType' => !empty($form) ? $form->getConfig()->getType()->getInnerType() : null,
  69.             'form' => !empty($form) ? $form->createView() : null,
  70.             'categories' => $categories ?? []
  71.         ]);
  72.     }
  73.     
  74.     public function updateFormAjax(Request $request$categoryId)
  75.     {
  76.         $creator $this->_adminCreatorService->getCreator();
  77.         
  78.         if (!$creator)
  79.             $this->throwHttpError400();
  80.         
  81.         $category null;
  82.         
  83.         foreach($creator->getCategories() as $_category)
  84.         {
  85.             if ($_category->getId() == $categoryId)
  86.             {
  87.                 $category $_category;
  88.                 break;
  89.             }
  90.         }
  91.         if (!$category)
  92.             throw $this->throwHttpError400();
  93.         
  94.         $form $this->createForm(UserPanelCreatorType::class, null, ['category' => $category'validation_groups' => false]);
  95.         $form->handleRequest($request);
  96.         
  97.         return new JsonResponse(array(
  98.             'html' => $this->renderView('UserPanel/Modules/OnlineService/Templates/Creator/partials/form.html.twig', array(
  99.                 'formType' => !empty($form) ? $form->getConfig()->getType()->getInnerType() : null,
  100.                 'form' => $form->createView()
  101.             ))
  102.         ));
  103.     }
  104. }