src/Controller/CalculatorProductBusinessCardController.php line 223

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Calculator;
  4. use App\Entity\CalculatorProductBusinessCard;
  5. use App\Entity\Product;
  6. use App\Form\CalculatorProductBusinessCardType;
  7. use App\Form\CalculatorType;
  8. use App\Repository\CalculatorProductBusinessCardRepository;
  9. use App\Repository\CalculatorRepository;
  10. use App\Repository\PaperSizeRepository;
  11. use App\Repository\ProductRepository;
  12. use App\Service\BusinessCardCalculator;
  13. use Doctrine\ORM\EntityManagerInterface;
  14. use Knp\Component\Pager\PaginatorInterface;
  15. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\Routing\Annotation\Route;
  20. /**
  21.  * @Route("/admin/calculator_product_business_card")
  22.  */
  23. class CalculatorProductBusinessCardController extends AbstractController
  24. {
  25.     /**
  26.      * @Route("/index", name="calculator_product_business_card_index")
  27.      */
  28.     public function index(PaginatorInterface $paginatorRequest $request,
  29.                           CalculatorProductBusinessCardRepository $calculatorProductBCRepository): Response
  30.     {
  31.         $name trim($request->query->get('name'));
  32.         $query $calculatorProductBCRepository->findBySearchQuery($name);
  33.         //$product = $query->getResult();
  34.         //$products = $productRepository->findAll();
  35.         $pagination $paginator->paginate(
  36.             $query/* query NOT result */
  37.             $request->query->getInt('page'1), /*page number*/
  38.             20/*limit per page*/
  39.             [
  40.                 'defaultSortFieldName'      => 'c.createdAt',
  41.                 'defaultSortDirection' => 'desc'
  42.             ]
  43.         );
  44.         return $this->render('calculator_product_business_card/index.html.twig', [
  45.             'pagination' => $pagination,
  46.             'calculator' => $calculatorProductBCRepository->find(2),
  47.         ]);
  48.     }
  49.     /**
  50.      * @Route("/edit/{id}",
  51.      *     name="calculator_product_business_card_edit", methods={"GET", "POST"})
  52.      */
  53.     function editCalculatorProductBusinessCard(Request $request,
  54.                                                CalculatorProductBusinessCard $product,
  55.                                                CalculatorProductBusinessCardRepository $bcCalculatorRepository): Response
  56.     {
  57.         $form $this->createForm(CalculatorProductBusinessCardType::class, $product);
  58.         $form->handleRequest($request);
  59.         $calculator $product->getCalculator();
  60.         if ($form->isSubmitted() && $form->isValid()) {
  61.             //Create a new one
  62.             if ($form->getClickedButton() && 'saveAndAddBtn' === $form->getClickedButton()->getName()) {
  63.                 //Save as new cal
  64.                 $calculatorProductBusinessCardToCopy $form->getData();
  65.                 $newCalculatorProductBusinessCard = clone $calculatorProductBusinessCardToCopy;
  66.                 $code $calculatorProductBusinessCardToCopy->getCode().' '.time();
  67.                 $newCalculatorProductBusinessCard->setCode($code);
  68.                 $bcCalculatorRepository->add($newCalculatorProductBusinessCardtrue);
  69.                 /*try{
  70.                     $calculatorHelper->denormalizeBusinessCardProduct($calculatorProductBusinessCard, $formData);
  71.                 }catch (\Exception $exception){
  72.                     return $this->json(['result' => 'error']);
  73.                 }*/
  74.                 $this->addFlash('success''Calculator: '.$newCalculatorProductBusinessCard->getCode().' created.');
  75.                 return $this->redirectToRoute('calculator_product_business_card_edit',
  76.                     ['id' => $newCalculatorProductBusinessCard->getId()], Response::HTTP_SEE_OTHER);
  77.             }else{
  78.                 $bcCalculatorRepository->add($producttrue);
  79.                 $this->addFlash('success''Calculator: Saved.');
  80.                 return $this->redirectToRoute('calculator_product_business_card_edit',
  81.                     ['id' => $product->getId()], Response::HTTP_SEE_OTHER);
  82.             }
  83.         }
  84.         return $this->renderForm('calculator_product_business_card/calculator_product_business_card.html.twig', [
  85.             'calculator' => $calculator,
  86.             'product' => $product,
  87.             'form' => $form,
  88.         ]);
  89.     }
  90.     /**
  91.      * @Route("/new",name="calculator_product_business_card_new", methods={"GET", "POST"})
  92.      * @param Request $request
  93.      * @param CalculatorRepository $calculatorRepository
  94.      * @param CalculatorProductBusinessCardRepository $bcCalculatorRepository
  95.      * @param BusinessCardCalculator $calculatorHelper
  96.      * @return Response
  97.      */
  98.     function newCalculatorProductBusinessCard(Request $request,
  99.                                               CalculatorRepository $calculatorRepository,
  100.                                               CalculatorProductBusinessCardRepository $bcCalculatorRepository,
  101.                                               BusinessCardCalculator $calculatorHelper
  102.     ): Response
  103.     {
  104.         $calculator $calculatorRepository->find(2);
  105.         $calculatorProductBusinessCard = new CalculatorProductBusinessCard();
  106.         $calculatorProductBusinessCard->setCalculator($calculator);
  107.         $form $this->createForm(CalculatorProductBusinessCardType::class,
  108.             $calculatorProductBusinessCard,['testing' => true'new' => true]
  109.         );
  110.         $form->handleRequest($request);
  111.         $globalErrors $form->getErrors(true);
  112.         if ($form->isSubmitted() && $form->isValid()) {
  113.             $bcCalculatorRepository->add($calculatorProductBusinessCardtrue);
  114.             $this->addFlash('success''Calculator: '.$calculatorProductBusinessCard->getCode().' created.');
  115.             return $this->redirectToRoute('calculator_product_business_card_edit',
  116.                 ['id' => $calculatorProductBusinessCard->getId()], Response::HTTP_SEE_OTHER);
  117.         }
  118.         return $this->renderForm('calculator_product_business_card/calculator_product_business_card.html.twig', [
  119.             'calculator' => $calculator,
  120.             'form' => $form,
  121.         ]);
  122.     }
  123.     /**
  124.      * @Route("/calculate", name="calculator_product_business_card_calculate", methods={"POST"}, options={"expose"=true})
  125.      */
  126.     public function calculate(Request $request,
  127.                               CalculatorRepository $calculatorRepository,
  128.                               BusinessCardCalculator $calculatorHelper
  129.     ): Response
  130.     {
  131.         $calculator $calculatorRepository->find(2);
  132.         $calculatorProductBusinessCard = new CalculatorProductBusinessCard();
  133.         $form $this->createForm(CalculatorProductBusinessCardType::class, $calculatorProductBusinessCard,['testing' => false]);
  134.         $form->handleRequest($request);
  135.         $data $form->getData();
  136.         $result = [
  137.             'result' => 'Error',
  138.             'html' => '<p>Error</>'
  139.         ];
  140.         //Get all data
  141.         $data $request->request->all();
  142.         $result $calculatorHelper->getOptionPricing($form->getData(), $calculator);
  143.         return $this->json([
  144.             'result' => $result,
  145.             'html' => $this->renderView('calculator/business_card_calculator_result.html.twig', [
  146.                 'pricing' => $result
  147.             ])
  148.         ]);
  149.     }
  150.     /**
  151.      * @Route("/paper_search", name="calculator_product_business_card_paper_search", methods={"GET"}, options={"expose"=true})
  152.      * @param Request $request
  153.      * @param PaperSizeRepository $paperSizeRepository
  154.      * @param ProductRepository $productRepository
  155.      * @return Response
  156.      */
  157.     function paperSearch(Request $requestPaperSizeRepository $paperSizeRepositoryProductRepository $productRepository) : Response
  158.     {
  159.         $results = [
  160.         ];
  161.         $paperSizeId $request->query->get('paper_size_id');
  162.         $paperSize $paperSizeRepository->find($paperSizeId);
  163.         $query $request->query->get('q''');
  164.         $str implode("%"explode(" "$query));
  165.         $products $productRepository->findPapersBySizeAndQuery($paperSize->getWidth(), $paperSize->getHeight(), $str);
  166.         foreach ($products as $product){
  167.             /* @var Product $product */
  168.             $results['results'][] = [
  169.                 'id' => htmlspecialchars($product->getId(), \ENT_COMPAT | \ENT_HTML5),
  170.                 'text' => htmlspecialchars($product->getCalculatorPaperSearchDisplayName(),
  171.                     \ENT_COMPAT | \ENT_HTML5),
  172.                 'gsm' => $product->getGsm(),
  173.                 'name' => $product->getName(),
  174.                 'description' => $product->getDescription(),
  175.             ];
  176.         }
  177.         return $this->json($results);
  178.     }
  179.     /**
  180.      * @Route("/get_paper_type", name="calculator_product_business_card_get_paper_type", methods={"GET"}, options={"expose"=true})
  181.      * @param Request $request
  182.      * @param ProductRepository $productRepository
  183.      * @return Response
  184.      */
  185.     public function get_paper_type(Request $requestProductRepository $productRepository): Response
  186.     {
  187.         $results = [];
  188.         $paperId $request->query->get('paper_search_id');
  189.         if ($paperId){
  190.             $paper $productRepository->find($paperId);
  191.             $products $productRepository->findPaperSizesByGsmNameDescription($paper->getGsm(), $paper->getName(), $paper->getDescription());
  192.             foreach ($products as $product){
  193.                 /* @var Product $product */
  194.                 $results['results'][] = [
  195.                     'id' => htmlspecialchars($product->getId(), \ENT_COMPAT | \ENT_HTML5),
  196.                     'text' => htmlspecialchars($product->getCalculatorPaperDisplayName(),
  197.                         \ENT_COMPAT | \ENT_HTML5)
  198.                 ];
  199.             }
  200.         }
  201.         return $this->json($results);
  202.     }
  203.     /**
  204.      * @Route("/add_new_size", name="calculator_product_business_card_add_new_size", options={"expose"=true})
  205.      * @param Request $request
  206.      * @param BusinessCardCalculator $businessCardCalculator
  207.      * @return Response
  208.      */
  209.     public function addNewSize(Request $requestBusinessCardCalculator $businessCardCalculator,
  210.                                ProductRepository $productRepository): Response
  211.     {
  212.         $paperId $request->request->get('paper_id');
  213.         $dimension1 $request->request->get('dimension1');
  214.         $dimension2 $request->request->get('dimension2');
  215.         $paper $productRepository->find($paperId);
  216.         if ($paper){
  217.             $newPaper $businessCardCalculator->addNewSize($paper$dimension1$dimension2);
  218.             $result = [
  219.                 'success' => true,
  220.                 'text' => $newPaper->getCalculatorPaperDisplayName(),
  221.                 'id' => $newPaper->getId()
  222.             ];
  223.         }else{
  224.             $result = [
  225.                 'success' => false,
  226.                 'error' => 'No such product found.'
  227.             ];
  228.         }
  229.         return $this->json($result);
  230.     }
  231.     /**
  232.      * @Route("/save/{id}",
  233.      *      name="calculator_product_business_card_save", methods={"POST"})
  234.      * @param Request $request
  235.      * @param CalculatorProductBusinessCard $product
  236.      * @param BusinessCardCalculator $businessCardCalculator
  237.      * @param EntityManagerInterface $entityManager
  238.      * @return JsonResponse
  239.      */
  240.     function saveCalculatorBusinessCard(Request $requestCalculatorProductBusinessCard $product,
  241.                                         BusinessCardCalculator $businessCardCalculatorEntityManagerInterface $entityManager
  242.     ): JsonResponse
  243.     {
  244.         //Get all data
  245.         $data $request->request->all();
  246.         try{
  247.             $businessCardCalculator->denormalizeBusinessCardProduct($product$data);
  248.         }catch (\Exception $exception){
  249.             return $this->json(['result' => 'error']);
  250.         }
  251.         return $this->json([
  252.             'success' => true
  253.         ]);
  254.     }
  255.     /**
  256.      * @Route("/{id}", name="calculator_product_business_card_delete", methods={"POST"})
  257.      */
  258.     public function delete(Request $requestCalculatorProductBusinessCard $product,
  259.                            CalculatorProductBusinessCardRepository $calculatorProductBCRepository): Response
  260.     {
  261.         if ($this->isCsrfTokenValid('delete'.$product->getId(), $request->request->get('_token'))) {
  262.             $this->addFlash('success''ID:'.$product->getId().' deleted.');
  263.             $calculatorProductBCRepository->remove($producttrue);
  264.         }
  265.         return $this->redirectToRoute('calculator_product_business_card_index', [],
  266.             Response::HTTP_SEE_OTHER);
  267.     }
  268. }