<?php
namespace App\Controller;
use App\Entity\Calculator;
use App\Entity\CalculatorProductBusinessCard;
use App\Entity\Product;
use App\Form\CalculatorProductBusinessCardType;
use App\Form\CalculatorType;
use App\Repository\CalculatorProductBusinessCardRepository;
use App\Repository\CalculatorRepository;
use App\Repository\PaperSizeRepository;
use App\Repository\ProductRepository;
use App\Service\BusinessCardCalculator;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
/**
* @Route("/admin/calculator_product_business_card")
*/
class CalculatorProductBusinessCardController extends AbstractController
{
/**
* @Route("/index", name="calculator_product_business_card_index")
*/
public function index(PaginatorInterface $paginator, Request $request,
CalculatorProductBusinessCardRepository $calculatorProductBCRepository): Response
{
$name = trim($request->query->get('name'));
$query = $calculatorProductBCRepository->findBySearchQuery($name);
//$product = $query->getResult();
//$products = $productRepository->findAll();
$pagination = $paginator->paginate(
$query, /* query NOT result */
$request->query->getInt('page', 1), /*page number*/
20, /*limit per page*/
[
'defaultSortFieldName' => 'c.createdAt',
'defaultSortDirection' => 'desc'
]
);
return $this->render('calculator_product_business_card/index.html.twig', [
'pagination' => $pagination,
'calculator' => $calculatorProductBCRepository->find(2),
]);
}
/**
* @Route("/edit/{id}",
* name="calculator_product_business_card_edit", methods={"GET", "POST"})
*/
function editCalculatorProductBusinessCard(Request $request,
CalculatorProductBusinessCard $product,
CalculatorProductBusinessCardRepository $bcCalculatorRepository): Response
{
$form = $this->createForm(CalculatorProductBusinessCardType::class, $product);
$form->handleRequest($request);
$calculator = $product->getCalculator();
if ($form->isSubmitted() && $form->isValid()) {
//Create a new one
if ($form->getClickedButton() && 'saveAndAddBtn' === $form->getClickedButton()->getName()) {
//Save as new cal
$calculatorProductBusinessCardToCopy = $form->getData();
$newCalculatorProductBusinessCard = clone $calculatorProductBusinessCardToCopy;
$code = $calculatorProductBusinessCardToCopy->getCode().' '.time();
$newCalculatorProductBusinessCard->setCode($code);
$bcCalculatorRepository->add($newCalculatorProductBusinessCard, true);
/*try{
$calculatorHelper->denormalizeBusinessCardProduct($calculatorProductBusinessCard, $formData);
}catch (\Exception $exception){
return $this->json(['result' => 'error']);
}*/
$this->addFlash('success', 'Calculator: '.$newCalculatorProductBusinessCard->getCode().' created.');
return $this->redirectToRoute('calculator_product_business_card_edit',
['id' => $newCalculatorProductBusinessCard->getId()], Response::HTTP_SEE_OTHER);
}else{
$bcCalculatorRepository->add($product, true);
$this->addFlash('success', 'Calculator: Saved.');
return $this->redirectToRoute('calculator_product_business_card_edit',
['id' => $product->getId()], Response::HTTP_SEE_OTHER);
}
}
return $this->renderForm('calculator_product_business_card/calculator_product_business_card.html.twig', [
'calculator' => $calculator,
'product' => $product,
'form' => $form,
]);
}
/**
* @Route("/new",name="calculator_product_business_card_new", methods={"GET", "POST"})
* @param Request $request
* @param CalculatorRepository $calculatorRepository
* @param CalculatorProductBusinessCardRepository $bcCalculatorRepository
* @param BusinessCardCalculator $calculatorHelper
* @return Response
*/
function newCalculatorProductBusinessCard(Request $request,
CalculatorRepository $calculatorRepository,
CalculatorProductBusinessCardRepository $bcCalculatorRepository,
BusinessCardCalculator $calculatorHelper
): Response
{
$calculator = $calculatorRepository->find(2);
$calculatorProductBusinessCard = new CalculatorProductBusinessCard();
$calculatorProductBusinessCard->setCalculator($calculator);
$form = $this->createForm(CalculatorProductBusinessCardType::class,
$calculatorProductBusinessCard,['testing' => true, 'new' => true]
);
$form->handleRequest($request);
$globalErrors = $form->getErrors(true);
if ($form->isSubmitted() && $form->isValid()) {
$bcCalculatorRepository->add($calculatorProductBusinessCard, true);
$this->addFlash('success', 'Calculator: '.$calculatorProductBusinessCard->getCode().' created.');
return $this->redirectToRoute('calculator_product_business_card_edit',
['id' => $calculatorProductBusinessCard->getId()], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('calculator_product_business_card/calculator_product_business_card.html.twig', [
'calculator' => $calculator,
'form' => $form,
]);
}
/**
* @Route("/calculate", name="calculator_product_business_card_calculate", methods={"POST"}, options={"expose"=true})
*/
public function calculate(Request $request,
CalculatorRepository $calculatorRepository,
BusinessCardCalculator $calculatorHelper
): Response
{
$calculator = $calculatorRepository->find(2);
$calculatorProductBusinessCard = new CalculatorProductBusinessCard();
$form = $this->createForm(CalculatorProductBusinessCardType::class, $calculatorProductBusinessCard,['testing' => false]);
$form->handleRequest($request);
$data = $form->getData();
$result = [
'result' => 'Error',
'html' => '<p>Error</>'
];
//Get all data
$data = $request->request->all();
$result = $calculatorHelper->getOptionPricing($form->getData(), $calculator);
return $this->json([
'result' => $result,
'html' => $this->renderView('calculator/business_card_calculator_result.html.twig', [
'pricing' => $result
])
]);
}
/**
* @Route("/paper_search", name="calculator_product_business_card_paper_search", methods={"GET"}, options={"expose"=true})
* @param Request $request
* @param PaperSizeRepository $paperSizeRepository
* @param ProductRepository $productRepository
* @return Response
*/
function paperSearch(Request $request, PaperSizeRepository $paperSizeRepository, ProductRepository $productRepository) : Response
{
$results = [
];
$paperSizeId = $request->query->get('paper_size_id');
$paperSize = $paperSizeRepository->find($paperSizeId);
$query = $request->query->get('q', '');
$str = implode("%", explode(" ", $query));
$products = $productRepository->findPapersBySizeAndQuery($paperSize->getWidth(), $paperSize->getHeight(), $str);
foreach ($products as $product){
/* @var Product $product */
$results['results'][] = [
'id' => htmlspecialchars($product->getId(), \ENT_COMPAT | \ENT_HTML5),
'text' => htmlspecialchars($product->getCalculatorPaperSearchDisplayName(),
\ENT_COMPAT | \ENT_HTML5),
'gsm' => $product->getGsm(),
'name' => $product->getName(),
'description' => $product->getDescription(),
];
}
return $this->json($results);
}
/**
* @Route("/get_paper_type", name="calculator_product_business_card_get_paper_type", methods={"GET"}, options={"expose"=true})
* @param Request $request
* @param ProductRepository $productRepository
* @return Response
*/
public function get_paper_type(Request $request, ProductRepository $productRepository): Response
{
$results = [];
$paperId = $request->query->get('paper_search_id');
if ($paperId){
$paper = $productRepository->find($paperId);
$products = $productRepository->findPaperSizesByGsmNameDescription($paper->getGsm(), $paper->getName(), $paper->getDescription());
foreach ($products as $product){
/* @var Product $product */
$results['results'][] = [
'id' => htmlspecialchars($product->getId(), \ENT_COMPAT | \ENT_HTML5),
'text' => htmlspecialchars($product->getCalculatorPaperDisplayName(),
\ENT_COMPAT | \ENT_HTML5)
];
}
}
return $this->json($results);
}
/**
* @Route("/add_new_size", name="calculator_product_business_card_add_new_size", options={"expose"=true})
* @param Request $request
* @param BusinessCardCalculator $businessCardCalculator
* @return Response
*/
public function addNewSize(Request $request, BusinessCardCalculator $businessCardCalculator,
ProductRepository $productRepository): Response
{
$paperId = $request->request->get('paper_id');
$dimension1 = $request->request->get('dimension1');
$dimension2 = $request->request->get('dimension2');
$paper = $productRepository->find($paperId);
if ($paper){
$newPaper = $businessCardCalculator->addNewSize($paper, $dimension1, $dimension2);
$result = [
'success' => true,
'text' => $newPaper->getCalculatorPaperDisplayName(),
'id' => $newPaper->getId()
];
}else{
$result = [
'success' => false,
'error' => 'No such product found.'
];
}
return $this->json($result);
}
/**
* @Route("/save/{id}",
* name="calculator_product_business_card_save", methods={"POST"})
* @param Request $request
* @param CalculatorProductBusinessCard $product
* @param BusinessCardCalculator $businessCardCalculator
* @param EntityManagerInterface $entityManager
* @return JsonResponse
*/
function saveCalculatorBusinessCard(Request $request, CalculatorProductBusinessCard $product,
BusinessCardCalculator $businessCardCalculator, EntityManagerInterface $entityManager
): JsonResponse
{
//Get all data
$data = $request->request->all();
try{
$businessCardCalculator->denormalizeBusinessCardProduct($product, $data);
}catch (\Exception $exception){
return $this->json(['result' => 'error']);
}
return $this->json([
'success' => true
]);
}
/**
* @Route("/{id}", name="calculator_product_business_card_delete", methods={"POST"})
*/
public function delete(Request $request, CalculatorProductBusinessCard $product,
CalculatorProductBusinessCardRepository $calculatorProductBCRepository): Response
{
if ($this->isCsrfTokenValid('delete'.$product->getId(), $request->request->get('_token'))) {
$this->addFlash('success', 'ID:'.$product->getId().' deleted.');
$calculatorProductBCRepository->remove($product, true);
}
return $this->redirectToRoute('calculator_product_business_card_index', [],
Response::HTTP_SEE_OTHER);
}
}