src/Controller/ProductController.php line 81

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Product;
  4. use App\Entity\ProductBrand;
  5. use App\Entity\ProductCategory;
  6. use App\Entity\ProductCompany;
  7. use App\Entity\ProductSell;
  8. use App\Entity\ProductShop;
  9. use App\Entity\ProductShopBrand;
  10. use App\Entity\ProductShopVote;
  11. use App\Entity\ProductVote;
  12. use App\Entity\User;
  13. use App\EntityManager\BrandManager;
  14. use App\EntityManager\ProductManager;
  15. use App\Form\Product\AddEvaluationType;
  16. use App\Form\Product\AddShopBrandType;
  17. use App\Form\Product\AddShopEvaluationType;
  18. use App\Form\Product\BrandSearchType;
  19. use App\Form\Product\CreateBrandType;
  20. use App\Form\Product\CreateCompanyType;
  21. use App\Form\Product\CreateProductType;
  22. use App\Form\Product\CreateSellType;
  23. use App\Form\Product\CreateShopType;
  24. use App\Form\Product\ProductSearchType;
  25. use App\Form\Product\ShopListSearchType;
  26. use App\Model\SearchBrand;
  27. use App\Model\SearchProduct;
  28. use App\Model\SearchShopList;
  29. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  30. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  31. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  32. use Symfony\Component\HttpFoundation\Request;
  33. use Symfony\Component\Routing\Annotation\Route;
  34. /**
  35.  * @Route("/product")
  36.  */
  37. class ProductController extends AbstractController
  38. {
  39.     protected $productManager;
  40.     protected $brandManager;
  41.     public function __construct(
  42.         ProductManager $productManager,
  43.         BrandManager $brandManager
  44.     ) {
  45.         $this->productManager $productManager;
  46.         $this->brandManager $brandManager;
  47.     }
  48.     /**
  49.      * @Route("/", name="product_index", options={"expose": true})
  50.      * @Template()
  51.      */
  52.     public function productList(Request $request)
  53.     {
  54.         $searchObject = new SearchProduct();
  55.         $searchForm   $this->createForm(
  56.             ProductSearchType::class,
  57.             $searchObject,
  58.             ['method' => Request::METHOD_GET]
  59.         );
  60.         $searchForm->handleRequest($request);
  61.         $listCategories = ($searchObject->getCategid() != null) ? array_merge($this->productManager->getListCategories($searchObject), array($searchObject->getCategid())) : null;
  62.         $categsTree $attributes = array();
  63.         if ($searchObject->getCategid() != null) {
  64.             $categsTree array_reverse($this->productManager->getAllParentCategories($searchObject));
  65.             $attributes $this->productManager->getAttributes($searchObject);
  66.         }
  67.         return [
  68.             'categsTree' => $categsTree,
  69.             'categories' => $this->productManager->getCategories($searchObject),
  70.             'brands' => ($listCategories != null) ? $this->brandManager->getBrands($searchObject$listCategories) : null,
  71.             'attributes' => $attributes,
  72.             'sort' => $request->query->get('sort'),
  73.             'products' => $this->productManager->getProductList($searchObject$listCategories),
  74.             'comments' => $this->productManager->getLastComment($searchObject),
  75.             'moderators' => $this->productManager->getModerators(),
  76.             'searchObject' => $searchObject,
  77.             'searchForm' => $searchForm->createView()
  78.         ];
  79.     }
  80.     /**
  81.      * @Route("/item/{id}", name="product_view")
  82.      * @IsGranted("view", subject="product")
  83.      * @Template()
  84.      */
  85.     public function productView(Request $requestProduct $product)
  86.     {
  87.         return [
  88.             'product' => $product ?? new Product(),
  89.             'productVote' => $this->productManager->getProductVoteDetails($product),
  90.             'brandresellers' => $this->productManager->getBrandResellers($product),
  91.             'totalresellers' => $this->productManager->getTotalResellers($product),
  92.             'resellers' => $this->productManager->getResellers($producttrue5),
  93.             'votes' => $this->productManager->getProductVotes($product),
  94.             'favorites' => $this->productManager->getProductFavorite($product),
  95.             'hasFavorite' => $this->productManager->hasFavorite($product),
  96.             'hasEvaluation' => $this->productManager->hasEvaluation($product)
  97.         ];
  98.     }
  99.     /**
  100.      * @Route("/itemcreate/", name="product_create")
  101.      * @IsGranted(User::ROLE_PRODUCT_ADMIN)
  102.      * @Template("product/product_edit.html.twig")
  103.      */
  104.     public function productCreate(Request $request)
  105.     {
  106.         $product = new Product();
  107.         $form $this->createForm(CreateProductType::class, $product);
  108.         $form->handleRequest($request);
  109.         if ($form->isSubmitted() && $form->isValid()) {
  110.             $em $this->getDoctrine()->getManager();
  111.             $em->persist($product);
  112.             $em->flush();
  113.             $this->addFlash('success''product.create');
  114.             return $this->redirectToRoute('product_index');
  115.         }
  116.         return [
  117.             'form' => $form->createView()
  118.         ];
  119.     }
  120.     /**
  121.      * @Route("/itemedit/{id}", name="product_edit")
  122.      * @IsGranted("edit", subject="product")
  123.      * @Template()
  124.      */
  125.     public function productEdit(Request $requestProduct $product)
  126.     {
  127.         $form $this->createForm(CreateProductType::class, $product);
  128.         $form->handleRequest($request);
  129.         if ($form->isSubmitted() && $form->isValid()) {
  130.             $em $this->getDoctrine()->getManager();
  131.             $em->persist($product);
  132.             $em->flush();
  133.             $this->addFlash('success''product.update');
  134.             return $this->redirectToRoute('product_view', ['id' => $product->getId()]);
  135.         }
  136.         return [
  137.             'form' => $form->createView()
  138.         ];
  139.     }
  140.     /**
  141.      * @Route("/delete/{id}", name="product_delete")
  142.      * @IsGranted("delete", subject="product")
  143.      * @Template()
  144.      */
  145.     public function productDelete(Request $requestProduct $product)
  146.     {
  147.         $em $this->getDoctrine()->getManager();
  148.         $categid null;
  149.         if ($product->getCategory() instanceof ProductCategory) {
  150.             $categid $product->getCategory()->getId();
  151.         }
  152.         $em->remove($product);
  153.         $em->flush();
  154.         $this->addFlash('success''product.delete');
  155.         if ( $categid == null ) {
  156.             return $this->redirectToRoute('product_index');
  157.         }
  158.         return $this->redirectToRoute('product_index', ['catedid' => $categid]);
  159.     }
  160.     /**
  161.      * @Route("/evaluate/{id}", name="product_evaluate")
  162.      * @IsGranted("evaluate", subject="product")
  163.      * @Template()
  164.      */
  165.     public function evaluate(Request $requestProduct $product)
  166.     {
  167.         $evaluation $this->productManager->getEvaluation($product);
  168.         if (!$evaluation instanceof ProductVote) {
  169.             $evaluation = new ProductVote();
  170.         }
  171.         $isNew $evaluation->getId() == null;
  172.         $form $this->createForm(AddEvaluationType::class, $evaluation);
  173.         $form->handleRequest($request);
  174.         if ($form->isSubmitted() && $form->isValid()) {
  175.             if ($isNew) {
  176.                 $this->productManager->addEvaluation($product$evaluation);
  177.                 $this->addFlash('success''product.evaluation_add');
  178.             } else {
  179.                 $this->productManager->editEvaluation($evaluation);
  180.                 $this->addFlash('success''product.evaluation_edit');
  181.             }
  182.             return $this->redirectToRoute('product_view', ['id' => $product->getId()]);
  183.         }
  184.         return [
  185.             'product'   => $product,
  186.             'evaluation' => $evaluation,
  187.             'isNew'      => $isNew,
  188.             'form'       => $form->createView()
  189.         ];
  190.     }
  191.     /**
  192.      * @Route("/evaluation/{id}/delete", name="product_evaluate_delete")
  193.      * @IsGranted("delete", subject="evaluation")
  194.      * @Template()
  195.      */
  196.     public function deleteEvaluation(ProductVote $evaluation)
  197.     {
  198.         $productId $evaluation->getProduct()->getId();
  199.         $this->productManager->deleteEvaluation($evaluation);
  200.         $this->addFlash('success''product.evaluation_delete');
  201.         return $this->redirectToRoute('product_view', ['id' => $productId]);
  202.     }
  203.     /**
  204.      * @Route("/favorite/{id}", name="product_favorite")
  205.      * @Template()
  206.      */
  207.     public function favorite(Product $product)
  208.     {
  209.         $added $this->productManager->toggleFavorite($product);
  210.         $this->addFlash('success''product.favorite.'.($added 'added' 'removed'));
  211.         return $this->redirectToRoute('product_view', ['id' => $product->getId()]);
  212.     }
  213.     /**
  214.      * @Route("/shoplist/", name="shop_list")
  215.      * @Template()
  216.      */
  217.     public function shopList(Request $request)
  218.     {
  219.         $searchObject = new SearchShopList();
  220.         $searchForm   $this->createForm(
  221.             ShopListSearchType::class,
  222.             $searchObject,
  223.             ['method' => Request::METHOD_GET]
  224.         );
  225.         $searchForm->handleRequest($request);
  226.         return [
  227.             'shops' => $this->productManager->getShopList($searchObject),
  228.             'sort' => $request->query->get('sort'),
  229.             'form' => $searchForm->createView()
  230.         ];
  231.     }
  232.     /**
  233.      * @Route("/itemlistshop/{id}", name="item_list_shop")
  234.      * @Template()
  235.      */
  236.     public function itemListShop(Request $requestProduct $product)
  237.     {
  238.         return [
  239.             'product' => $product ?? new Product(),
  240.             'totalresellers' => $this->productManager->getTotalResellers($product),
  241.             'resellers' => $this->productManager->getResellers($product)
  242.         ];
  243.     }
  244.     /**
  245.      * @Route("/shop/{id}", name="shop_view")
  246.      * @Template()
  247.      */
  248.     public function shop(Request $requestProductShop $shop)
  249.     {
  250.         return [
  251.             'shop' => $shop,
  252.             'shopVotes' => $this->productManager->getShopVotes($shop),
  253.             'countShopVotes' =>  $this->productManager->getCountShopVotes($shop),
  254.             'shopBrands' => $this->productManager->getShopBrands($shop),
  255.             'hasEvaluation' => $this->productManager->hasShopEvaluation($shop)
  256.         ];
  257.     }
  258.     /**
  259.      * @Route("/shop_create/", name="shop_create")
  260.      * @IsGranted(User::ROLE_PRODUCT_ADMIN)
  261.      * @Template("product/shop_edit.html.twig")
  262.      */
  263.     public function shopCreate(Request $request)
  264.     {
  265.         $shop = new ProductShop();
  266.         $form $this->createForm(CreateShopType::class, $shop);
  267.         $form->handleRequest($request);
  268.         if ($form->isSubmitted() && $form->isValid()) {
  269.             $em $this->getDoctrine()->getManager();
  270.             $em->persist($shop);
  271.             $em->flush();
  272.             $this->addFlash('success''shop.create');
  273.             return $this->redirectToRoute('shop_view', ['id' => $shop->getId()]);
  274.         }
  275.         return [
  276.             'shop' => $shop,
  277.             'form' => $form->createView()
  278.         ];
  279.     }
  280.     /**
  281.      * @Route("/shop_edit/{id}", name="shop_edit")
  282.      * @IsGranted("edit", subject="shop")
  283.      * @Template()
  284.      */
  285.     public function shopEdit(Request $requestProductShop $shop)
  286.     {
  287.         $form $this->createForm(CreateShopType::class, $shop);
  288.         $form->handleRequest($request);
  289.         if ($form->isSubmitted() && $form->isValid()) {
  290.             $em $this->getDoctrine()->getManager();
  291.             $em->persist($shop);
  292.             $em->flush();
  293.             $this->addFlash('success''shop.update');
  294.             return $this->redirectToRoute('shop_view', ['id' => $shop->getId()]);
  295.         }
  296.         return [
  297.             'shop' => $shop,
  298.             'form' => $form->createView()
  299.         ];
  300.     }
  301.     /**
  302.      * @Route("/shop_delete/{id}", name="shop_delete")
  303.      * @IsGranted("delete", subject="shop")
  304.      * @Template()
  305.      */
  306.     public function shopDelete(Request $requestProductShop $shop)
  307.     {
  308.         $em $this->getDoctrine()->getManager();
  309.         $em->remove($shop);
  310.         $em->flush();
  311.         $this->addFlash('success''shop.delete');
  312.         return $this->redirectToRoute('shop_list');
  313.     }
  314.     /**
  315.      * @Route("/shopevaluate/{id}", name="shop_evaluate")
  316.      * @IsGranted("evaluate", subject="shop")
  317.      * @Template()
  318.      */
  319.     public function shopEvaluate(Request $requestProductShop $shop)
  320.     {
  321.         $evaluation $this->productManager->getShopEvaluation($shop);
  322.         if (!$evaluation instanceof ProductShopVote) {
  323.             $evaluation = new ProductShopVote();
  324.         }
  325.         $isNew $evaluation->getId() == null;
  326.         $form $this->createForm(AddShopEvaluationType::class, $evaluation);
  327.         $form->handleRequest($request);
  328.         if ($form->isSubmitted() && $form->isValid()) {
  329.             if ($isNew) {
  330.                 $this->productManager->addShopEvaluation($shop$evaluation);
  331.                 $this->addFlash('success''shop.evaluation_add');
  332.             } else {
  333.                 $this->productManager->editShopEvaluation($evaluation);
  334.                 $this->addFlash('success''shop.evaluation_edit');
  335.             }
  336.             return $this->redirectToRoute('shop_view', ['id' => $shop->getId()]);
  337.         }
  338.         return [
  339.             'shop' => $shop,
  340.             'evaluation' => $evaluation,
  341.             'isNew' => $isNew,
  342.             'form' => $form->createView()
  343.         ];
  344.     }
  345.     /**
  346.      * @Route("/shopevaluate/{id}/delete", name="shop_evaluate_delete")
  347.      * @IsGranted("delete", subject="evaluation")
  348.      * @Template()
  349.      */
  350.     public function shopEvaluateDelete(ProductShopVote $evaluation)
  351.     {
  352.         $shopid $evaluation->getShop()->getId();
  353.         $this->productManager->deleteShopEvaluation($evaluation);
  354.         $this->addFlash('success''shop.evaluation_delete');
  355.         return $this->redirectToRoute('shop_view', ['id' => $shopid]);
  356.     }
  357.     /**
  358.      * @Route("/shop_brand_edit/{id}", name="shop_brand_edit")
  359.      * @IsGranted(User::ROLE_PRODUCT_ADMIN)
  360.      * @Template()
  361.      */
  362.     public function shopBrandEdit(Request $requestProductShop $shop)
  363.     {
  364.         $shopbrand = new ProductShopBrand();
  365.         $form $this->createForm(AddShopBrandType::class, $shopbrand);
  366.         $form->handleRequest($request);
  367.         if ($form->isSubmitted() && $form->isValid()) {
  368.             $shopbrand->setShop($shop);
  369.             $em $this->getDoctrine()->getManager();
  370.             $em->persist($shopbrand);
  371.             $em->flush();
  372.             $this->addFlash('success''shopbrand.create');
  373.             return $this->redirectToRoute('shop_brand_edit', ['id' => $shop->getId()]);
  374.         }
  375.         return [
  376.             'shop' => $shop,
  377.             'shopbrands' => (!empty($shop)) ? $this->productManager->getShopBrands($shop) : null,
  378.             'form' => $form->createView()
  379.         ];
  380.     }
  381.     /**
  382.      * @Route("/shop_brand_delete/{id}", name="shop_brand_delete")
  383.      * @IsGranted(User::ROLE_PRODUCT_ADMIN)
  384.      * @Template()
  385.      */
  386.     public function shopBrandDelete(Request $requestProductShopBrand $shopbrand)
  387.     {
  388.         $em $this->getDoctrine()->getManager();
  389.         $shopid $shopbrand->getShop()->getId();
  390.         $em->remove($shopbrand);
  391.         $em->flush();
  392.         $this->addFlash('success''shopbrand.delete');
  393.         return $this->redirectToRoute('shop_brand_edit', ['id' => $shopid]);
  394.     }
  395.     /**
  396.      * @Route("/brandlist/", name="brand_list")
  397.      * @Template()
  398.      */
  399.     public function brandList(Request $request)
  400.     {
  401.         $searchObject = new SearchBrand();
  402.         $searchForm   $this->createForm(
  403.             BrandSearchType::class,
  404.             $searchObject,
  405.             ['method' => Request::METHOD_GET]
  406.         );
  407.         $searchForm->handleRequest($request);
  408.         $searchProductObject = new SearchProduct();
  409.         $searchProductObject->setCategid($searchObject->getCategid());
  410.         $listCategories = ($searchObject->getCategid() != null) ? array_merge($this->productManager->getListCategories($searchProductObject), array($searchObject->getCategid())) : null;
  411.         return [
  412.             'brands' => $this->brandManager->getAllBrands($searchObject$listCategories),
  413.             'searchObject' => $searchObject
  414.         ];
  415.     }
  416.     /**
  417.      * @Route("/brand_create/", name="brand_create")
  418.      * @IsGranted(User::ROLE_PRODUCT_ADMIN)
  419.      * @Template("product/brand_edit.html.twig")
  420.      */
  421.     public function brandCreate(Request $request)
  422.     {
  423.         $brand = new ProductBrand();
  424.         $form $this->createForm(CreateBrandType::class, $brand);
  425.         $form->handleRequest($request);
  426.         if ($form->isSubmitted() && $form->isValid()) {
  427.             $this->brandManager->createBrand($brand);
  428.             $this->addFlash('success''brand.create_brand');
  429.             return $this->redirectToRoute('brand_list');
  430.         }
  431.         return [
  432.             'form' => $form->createView()
  433.         ];
  434.     }
  435.     /**
  436.      * @Route("/brand_edit/{id}", name="brand_edit")
  437.      * @IsGranted(User::ROLE_PRODUCT_ADMIN)
  438.      * @Template()
  439.      */
  440.     public function brandEdit(Request $requestProductBrand $brand)
  441.     {
  442.         $form $this->createForm(CreateBrandType::class, $brand);
  443.         $form->handleRequest($request);
  444.         if ($form->isSubmitted() && $form->isValid()) {
  445.             $this->brandManager->createBrand($brand);
  446.             $this->addFlash('success''brand.edit_brand');
  447.             if ( empty($request->query->get('companyid')) ) {
  448.                 return $this->redirectToRoute('brand_list');
  449.             } else {
  450.                 return $this->redirectToRoute('brand_list', ['companyid' => $request->query->get('companyid')]);
  451.             }
  452.         }
  453.         return [
  454.             'form' => $form->createView()
  455.         ];
  456.     }
  457.     /**
  458.      * @Route("/companylist/", name="company_list")
  459.      * @Template()
  460.      */
  461.     public function companyList(Request $request)
  462.     {
  463.         return ['companies' => $this->productManager->getAllCompanies()];
  464.     }
  465.     /**
  466.      * @Route("/company/{id}", name="company_view")
  467.      * @Template()
  468.      */
  469.     public function companyView(Request $requestProductCompany $company)
  470.     {
  471.         return ['company' => $company];
  472.     }
  473.     /**
  474.      * @Route("/company_create/", name="company_create")
  475.      * @IsGranted(User::ROLE_PRODUCT_ADMIN)
  476.      * @Template("product/company_edit.html.twig")
  477.      */
  478.     public function companyCreate(Request $request)
  479.     {
  480.         $company = new ProductCompany();
  481.         $form $this->createForm(CreateCompanyType::class, $company);
  482.         $form->handleRequest($request);
  483.         if ($form->isSubmitted() && $form->isValid()) {
  484.             $this->productManager->createCompany($company);
  485.             $this->addFlash('success''company.create_company');
  486.             return $this->redirectToRoute('company_list');
  487.         }
  488.         return [
  489.             'form' => $form->createView()
  490.         ];
  491.     }
  492.     /**
  493.      * @Route("/company_edit/{id}", name="company_edit")
  494.      * @IsGranted(User::ROLE_PRODUCT_ADMIN)
  495.      * @Template()
  496.      */
  497.     public function companyEdit(Request $requestProductCompany $company)
  498.     {
  499.         $form $this->createForm(CreateCompanyType::class, $company);
  500.         $form->handleRequest($request);
  501.         if ($form->isSubmitted() && $form->isValid()) {
  502.             $this->productManager->createCompany($company);
  503.             $this->addFlash('success''company.edit_company');
  504.             return $this->redirectToRoute('company_view', ['id' => $company->getId()]);
  505.         }
  506.         return [
  507.             'form' => $form->createView()
  508.         ];
  509.     }
  510.     /**
  511.      * @Route("/item_sell_edit/{id}", name="item_sell_edit")
  512.      * @IsGranted(User::ROLE_PRODUCT_ADMIN)
  513.      * @Template()
  514.      */
  515.     public function itemSellEdit(Request $requestProduct $product)
  516.     {
  517.         return [
  518.             'product' => $product,
  519.             'sells' => $this->productManager->getSells($product)
  520.         ];
  521.     }
  522.     /**
  523.      * @Route("/sell_create/{id}", name="sell_create")
  524.      * @IsGranted(User::ROLE_PRODUCT_ADMIN)
  525.      * @Template("product/sell_edit.html.twig")
  526.      */
  527.     public function sellCreate(Request $requestProduct $product)
  528.     {
  529.         $em $this->getDoctrine()->getManager();
  530.         $sell = new ProductSell();
  531.         $sell->setProduct($product);
  532.         $sell->setStatus(true);
  533.         $form $this->createForm(CreateSellType::class, $sell);
  534.         $form->handleRequest($request);
  535.         if ($form->isSubmitted() && $form->isValid()) {
  536.             $em->persist($sell);
  537.             $em->flush();
  538.             $this->addFlash('success''sell.create');
  539.             return $this->redirectToRoute('item_sell_edit', ['id' => $product->getId()]);
  540.         }
  541.         return [
  542.             'form' => $form->createView(),
  543.             'product' => $product
  544.         ];
  545.     }
  546.     /**
  547.      * @Route("/sell_edit/{id}", name="sell_edit")
  548.      * @IsGranted(User::ROLE_PRODUCT_ADMIN)
  549.      * @Template()
  550.      */
  551.     public function sellEdit(Request $requestProductSell $sell)
  552.     {
  553.         $em $this->getDoctrine()->getManager();
  554.         $form $this->createForm(CreateSellType::class, $sell);
  555.         $form->handleRequest($request);
  556.         if ($form->isSubmitted() && $form->isValid()) {
  557.             $em->persist($sell);
  558.             $em->flush();
  559.             $this->addFlash('success''sell.update');
  560.             return $this->redirectToRoute('item_sell_edit', ['id' => $sell->getProduct()->getId()]);
  561.         }
  562.         return [
  563.             'form' => $form->createView(),
  564.             'sell' => $sell
  565.         ];
  566.     }
  567.     /**
  568.      * @Route("/sell_delete/{id}", name="sell_delete")
  569.      * @IsGranted(User::ROLE_PRODUCT_ADMIN)
  570.      * @Template()
  571.      */
  572.     public function sellDelete(Request $requestProductSell $sell)
  573.     {
  574.         $em $this->getDoctrine()->getManager();
  575.         $productId $sell->getProduct()->getId();
  576.         $em->remove($sell);
  577.         $em->flush();
  578.         $this->addFlash('success''sell.delete');
  579.         return $this->redirectToRoute('item_sell_edit', ['id' => $productId]);
  580.     }
  581. }