src/Modules/VoucherCart/Controller/VoucherController.php line 56

  1. <?php
  2. namespace App\Modules\VoucherCart\Controller;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use App\Modules\VoucherCart\Service\VoucherService;
  6. use App\Modules\VoucherCart\Service\PredefinedVoucherService;
  7. use App\Modules\VoucherCart\Service\CartService;
  8. use App\Modules\VoucherCart\Form\VoucherType;
  9. class VoucherController extends BaseController
  10. {
  11.     private $_voucherService;
  12.     private $_predefinedVoucherService;
  13.     private $_cartService;
  14.     
  15.     public function __construct(
  16.             VoucherService $voucherService,
  17.             PredefinedVoucherService $predefinedVoucherService,
  18.             CartService $cartService)
  19.     {
  20.         $this->_voucherService $voucherService;
  21.         $this->_predefinedVoucherService $predefinedVoucherService;
  22.         $this->_cartService $cartService;
  23.     }
  24.     
  25.     public function config(Request $request)
  26.     {
  27.         $voucherSettingsData $this->_voucherService->getVoucherSettingsData(false);
  28.         if ($voucherSettingsData)
  29.         {
  30.             $form $this->createForm(VoucherType::class);
  31.             $form->handleRequest($request);
  32.             if ($form->isSubmitted())
  33.             {
  34.                 if ($form->isValid())
  35.                 {
  36.                     if ($this->_cartService->addVoucher($form$errorMessage))
  37.                         return $this->redirectToRoute('voucher_cart_voucher_config');
  38.                     else
  39.                         $this->addFlash('error'$errorMessage);
  40.                 }
  41.             }
  42.         }
  43.         return $this->render('Modules/VoucherCart/Templates/Voucher/config.html.twig', [
  44.             'formType' => isset($form) ? $form->getConfig()->getType()->getInnerType() : null,
  45.             'form' => isset($form) ? $form->createView() : null,
  46.             'cartEmpty' => $this->_cartService->isEmpty()
  47.         ]);
  48.     }
  49.     
  50.     public function predefinedVouchersWidget()
  51.     {
  52.         $vouchers = array();
  53.         
  54.         foreach($this->_predefinedVoucherService->getVouchers() as $voucher)
  55.             $vouchers[] = $this->_predefinedVoucherService->getDetails($voucher['id']);
  56.         if (!$vouchers)
  57.             return new Response();
  58.         return $this->render('Modules/VoucherCart/Templates/Voucher/partials/predefinedVouchersWidget.html.twig', [
  59.             'vouchers' => $vouchers
  60.         ]);
  61.     }
  62. }