src/Controller/ShopController.php line 271

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Shop;
  4. use App\Entity\ShopSetting;
  5. use App\Entity\Stock;
  6. use App\Form\ShopType;
  7. use App\Repository\ManufacturerRepository;
  8. use App\Repository\OptionRepository;
  9. use App\Repository\OrderRepository;
  10. use App\Repository\ShopSettingRepository;
  11. use App\Repository\SettingRepository;
  12. use App\Repository\ShopRepository;
  13. use App\Repository\StockRepository;
  14. use App\Repository\UserRepository;
  15. use Knp\Component\Pager\PaginatorInterface;
  16. use Knp\Snappy\Pdf;
  17. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  18. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
  19. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Component\HttpFoundation\Response;
  22. use Symfony\Component\Routing\Annotation\Route;
  23. #[Route('/{_locale<%app.supported_locales%>}/shop')]
  24. class   ShopController extends AbstractController
  25. {
  26.     #[Route('/'name'app_shop_index'methods: ['GET'])]
  27.     public function index(ShopRepository $shopRepositorySettingRepository $settingRepository): Response
  28.     {
  29.         $marketplaces=array();
  30.         $mps=$settingRepository->getMarketplaces();
  31.         foreach($mps as $mp) {
  32.             $marketplaces[$mp->id]=$mp->name;
  33.         }
  34.         return $this->render('shop/index.html.twig', [
  35.             'shops' => $shopRepository->findAll(),
  36.             'marketplaces'=>$marketplaces
  37.         ]);
  38.     }
  39.     #[Route('/new'name'app_shop_new'methods: ['GET''POST'])]
  40.     public function new(Request $requestSettingRepository $settingRepositoryShopRepository $shopRepositoryShopSettingRepository $shopSettingRepositoryOptionRepository $optionRepository): Response
  41.     {
  42.         $marketplaces=$settingRepository->getMarketplaces();
  43.         $shop = new Shop();
  44.         /*$settings=$settingRepository->findBy(array('target'=>'Shop'));
  45.         foreach($settings as $setting) {
  46.             if(!$shopSettingRepository->findOneBy(array('shop'=>$shop,'setting'=>$setting))) {
  47.                 $shopSetting = new ShopSetting();
  48.                 $shopSetting->setShop($shop);
  49.                 $shopSetting->setSetting($setting);
  50.                 $shopSetting->setValue($setting->getDefaultValue());
  51.                 $shopSettingRepository->add($shopSetting);
  52.             }
  53.         }
  54.         */
  55.         
  56.         $form $this->createForm(ShopType::class, $shop, ['marketplaces'=>$marketplaces]);
  57.         $form->handleRequest($request);
  58.         if ($form->isSubmitted() && $form->isValid()) {
  59.             $mp=$settingRepository->getMarketplaceById($shop->getWttId());
  60.             $shop->setWttName($mp->name);
  61.             foreach ($form['options']->getData()->getValues() as $o) {
  62.                 $option $optionRepository->find($o->getId());
  63.                 if ($option) {
  64.                     $option->addShop($shop);
  65.                 }
  66.             }
  67.             $shopRepository->add($shop);
  68.             return $this->redirectToRoute('app_shop_index', [], Response::HTTP_SEE_OTHER);
  69.         }
  70.         return $this->renderForm('shop/new.html.twig', [
  71.             'shop' => $shop,
  72.             'form' => $form,
  73.         ]);
  74.     }
  75.     #[Route('/{id}/users'name'app_shop_users'methods: ['GET'])]
  76.     public function users(Shop $shopUserRepository $userRepositoryPaginatorInterface $paginatorRequest $request): Response
  77.     {
  78.         $query=$userRepository->findAllDql($shop);
  79.         $users $paginator->paginate(
  80.             $query/* query NOT result */
  81.             $request->query->getInt('page'1), /*page number*/
  82.             30 /*limit per page*/
  83.         );
  84.         return $this->render('shop/users.html.twig', [
  85.             'shop' => $shop,
  86.             'users'=>$users
  87.         ]);
  88.     }
  89.     #[Route('/{id}'name'app_shop_show'methods: ['GET'])]
  90.     public function show(Shop $shop): Response
  91.     {
  92.         return $this->render('shop/show.html.twig', [
  93.             'shop' => $shop,
  94.         ]);
  95.     }
  96.     #[Route('/{id}/edit'name'app_shop_edit'methods: ['GET''POST'])]
  97.     public function edit(Request $requestShop $shopShopRepository $shopRepositoryStockRepository $stockRepositorySettingRepository $settingRepositoryShopSettingRepository $shopSettingRepositoryManufacturerRepository $manufacturerRepositoryOptionRepository $optionRepository): Response
  98.     {
  99.         $settings=$settingRepository->findBy(array('target'=>'Shop'));
  100.         $marketplaces=$settingRepository->getMarketplaces();
  101.         foreach($settings as $setting) {
  102.             if(!$shopSettingRepository->findOneBy(array('shop'=>$shop,'setting'=>$setting))) {
  103.                 $shopSetting = new ShopSetting();
  104.                 $shopSetting->setShop($shop);
  105.                 $shopSetting->setSetting($setting);
  106.                 $shopSetting->setValue($setting->getDefaultValue());
  107.                 $shopSetting->setUrl($setting->getDefaultUrl());
  108.                 $shopSettingRepository->add($shopSetting);
  109.             }
  110.         }
  111.         $user $this->getUser();
  112.         $stocks $stockRepository->findByShop($shop$request);
  113.         $stockController=new StockController();
  114.         $list=$stockController->list($stocks$manufacturerRepository);
  115.         $form $this->createForm(ShopType::class, $shop, ['marketplaces'=>$marketplaces]);
  116.         $form->handleRequest($request);
  117.         if ($form->isSubmitted() && $form->isValid()) {
  118.             $mp=$settingRepository->getMarketplaceById($shop->getWttId());
  119.             $shop->setWttName($mp->name);
  120.             foreach ($form['options']->getData()->getValues() as $o) {
  121.                 $option $optionRepository->find($o->getId());
  122.                 if ($option) {
  123.                     $option->addShop($shop);
  124.                 }
  125.             }
  126.             $shopRepository->add($shop);
  127.             return $this->redirectToRoute('app_shop_index', [], Response::HTTP_SEE_OTHER);
  128.         }
  129.         return $this->renderForm('shop/edit.html.twig', [
  130.             'shop' => $shop,
  131.             'form' => $form,
  132.             /*'stocks' => $list['stocks'],
  133.             'manufacturers' => $list['manufacturers'],
  134.             'models' => $list['models'],
  135.             'colors' => $list['colors'],
  136.             'grades' => $list['grades'],
  137.             'categories' => $list['categories'],
  138.             'shops' => $list['shops']*/
  139.         ]);
  140.     }
  141.     #[Route('/{id}'name'app_shop_delete'methods: ['POST'])]
  142.     public function delete(Request $requestShop $shopShopRepository $shopRepository): Response
  143.     {
  144.         if ($this->isCsrfTokenValid('delete'.$shop->getId(), $request->request->get('_token'))) {
  145.             $shopRepository->remove($shop);
  146.         }
  147.         return $this->redirectToRoute('app_shop_index', [], Response::HTTP_SEE_OTHER);
  148.     }
  149.     #[Route('/{id}/stock'name'app_shop_stock'methods: ['GET'])]
  150.     public function stock(Request $requestShop $shopStockRepository $stockRepositoryManufacturerRepository $manufacturerRepository): Response
  151.     {
  152.         $stocks $stockRepository->findByShop($shop$request);
  153.         $stockController=new StockController();
  154.         $list=$stockController->list($stocks$manufacturerRepository);
  155.         return $this->render('shop/stock.html.twig', [
  156.             'shop' => $shop,
  157.             'stocks' => $list['stocks'],
  158.             'manufacturers' => $list['manufacturers'],
  159.             'models' => $list['models'],
  160.             'colors' => $list['colors'],
  161.             'grades' => $list['grades'],
  162.             'categories' => $list['categories'],
  163.             'shops' => $list['shops']
  164.         ]);
  165.     }
  166.     /**
  167.      * @Route("/{id}/pdf", name="app_shop_stock_pdf", methods={"GET"})
  168.      */
  169.     public function stockPdf(Shop $shopStockRepository $stockRepositoryParameterBagInterface $parameterBagRequest $request, ): Response
  170.     {
  171.         $filename='bak2-stock-'.date('Y-m-d').'-'.$shop->getId().".pdf";
  172.         $filePath=$parameterBag->get('kernel.project_dir').'/public/uploads/stocks/'.$filename;
  173.         if(file_exists($filePath)) {
  174.             unlink($filePath);
  175.         }
  176.         $stocks $stockRepository->findByShop($shop$request);
  177.         $template='stock/pdf.html.twig';
  178.         $knpSnappyPdf=new Pdf($parameterBag->get('app.wkhtmltopdf'));
  179.         $knpSnappyPdf->setTemporaryFolder($parameterBag->get('kernel.project_dir')."/tmp");
  180.         $knpSnappyPdf->setOptions([
  181.             'enable-local-file-access' => true,
  182.             'keep-relative-links' => true,
  183.             'disable-smart-shrinking' => true,
  184.             'margin-top'=>"20mm",
  185.             'margin-right'=>"20mm",
  186.             'margin-bottom'=>"20mm",
  187.             'margin-left'=>"20mm",
  188.         ]);
  189.         $html=$this->renderView(
  190.             $template,
  191.             array(
  192.                 'shop'=>$shop,
  193.                 'stocks'  => $stocks,
  194.             )
  195.         );
  196.         $knpSnappyPdf->generateFromHtml(
  197.             $html,
  198.             $filePath
  199.         );
  200.         return $this->redirect('/uploads/stocks/'.$filename);
  201.     }
  202.     #[Route('/{id}/orders'name'app_shop_orders'methods: ['GET'])]
  203.     public function orders(Request $requestShop $shopShopRepository $shopRepositoryOrderRepository $orderRepositoryPaginatorInterface $paginator): Response
  204.     {
  205.         /*$orders=array();
  206.         foreach($users as $user) {
  207.             $orders = array_merge($orders , $orderRepository->findByUser($user, $request));
  208.         }*/
  209.         //$orders=$orderRepository->findBy(['shop'=>$shop->getId()]);
  210.         $query=$orderRepository->findByDql(['shop'=>$shop->getId()]);
  211.         $orders $paginator->paginate(
  212.             $query/* query NOT result */
  213.             $request->query->getInt('page'1), /*page number*/
  214.             30 /*limit per page*/
  215.         );
  216.         return $this->render('shop/orders.html.twig', [
  217.             'shop'=>$shop,
  218.             'orders' => $orders,
  219.         ]);
  220.     }
  221.     #[Route('/{id}/switch'name'app_shop_switch'methods: ['GET'])]
  222.     public function changeShop(Request $requestShop $shopShopRepository $shopRepositoryUserRepository $userRepository): Response
  223.     {
  224.         $user=$this->getUser();
  225.         $user->setShop($shop);
  226.         $userRepository->add($user);
  227.         return $this->redirectToRoute('app_stock_index', [], Response::HTTP_SEE_OTHER);
  228.     }
  229.     #[Route('/{id}/{shop}'name'app_shop_public'methods: ['GET'])]
  230.     public function public(Request $requestShop $shopShopRepository $shopRepository): Response
  231.     {
  232.         return "ee";
  233.     }
  234. }