src/Controller/DefaultController.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Model\DataObject\AbstractProduct;
  4. use App\Model\ShopCategory;
  5. use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
  6. use Pimcore\Bundle\EcommerceFrameworkBundle\FilterService\ListHelper;
  7. use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ProductListInterface;
  8. use Pimcore\Config;
  9. use Pimcore\Controller\FrontendController;
  10. use Pimcore\Model\DataObject\Product;
  11. use Pimcore\Model\DataObject\ProductCategory;
  12. use Pimcore\Model\DataObject\ProductMarca;
  13. use Pimcore\Model\Document;
  14. use Symfony\Component\HttpFoundation\JsonResponse;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpFoundation\Response;
  17. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  18. use Symfony\Component\Routing\Annotation\Route;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. class DefaultController extends FrontendController
  21. {
  22.     private function getCategories(Factory $ecommerceFactory$parentId$level 1$values null): array
  23.     {
  24.         if ($values === null){
  25.             $indexService $ecommerceFactory->getIndexService();
  26.             $products $indexService->getProductListForCurrentTenant();
  27.             $products->setVariantMode(ProductListInterface::VARIANT_MODE_HIDE);
  28.             $products->addCondition("productstatus NOT IN ('new', 'decatelogized', 'for_delete') OR productstatus IS NULL"'productstatus');
  29.             $filterService $ecommerceFactory->getFilterService();
  30.             $filterDefinition Config::getWebsiteConfig()->get('fallbackFilterdefinition');
  31.             $listHelper = new ListHelper();
  32.             $listHelper->setupProductList($filterDefinition$products$params$filterServicetrue);
  33.             $rawValues $filterService->getFilterValues($filterDefinition->getFilters()->getItems()[1], $products, ['parentCategoryIds' => null])['values'];
  34.             $values = [];
  35.             foreach ($rawValues as $entry) {
  36.                 $values[$entry['value']] = $entry['count'];
  37.             }
  38.         }
  39.         $categoryList = new ProductCategory\Listing();
  40.         $categoryList->setUnpublished(false);
  41.         $categoryList->setOrderKey("order");
  42.         $categoryList->setOrder("ASC");
  43.         $categoryList->setCondition("o_parentId = ?", [$parentId]);
  44.         $loadedCategories $categoryList->load();
  45.         $result = [];
  46.         foreach ($loadedCategories as $category) {
  47.             if (!isset($values[$category->getId()])) {
  48.                 continue;
  49.             }
  50.             $subcategories = [];
  51.             if ($level 3) {
  52.                 $subcategories $this->getCategories($ecommerceFactory$category->getId(), $level 1$values);
  53.             }
  54.             $result[] = [
  55.                 'category' => $category,
  56.                 'children' => $subcategories,
  57.             ];
  58.         }
  59.         return $result;
  60.     }
  61.     public function navbarAction(Factory $ecommerceFactory): Response
  62.     {
  63.         $categories $this->getCategories($ecommerceFactory10);
  64.         return $this->render('layout/includes/navegation/navbar.html.twig', [
  65.             'categories' => $categories
  66.         ]);
  67.     }
  68.     #[Route('/featuredProducts/{categoryId}'name'featured_products'methods: ['GET'])]
  69.     public function featuredProductAction($categoryIdRequest $request, ):Response
  70.     {
  71.         $category ProductCategory::getById($categoryId);
  72.         $featuredProduct $category->getFeaturedproduct();
  73.         if ($request->getLocale() === 'en') {
  74.             $document Document::getById(8);// English Document
  75.         }else{
  76.             $document Document::getById(1);// Spanish Document
  77.         }
  78.         return $this->render('layout/includes/navegation/featuredProduct.html.twig',[
  79.             'featuredProduct' => $featuredProduct,
  80.             'title' => $category->getFeaturedProductTitle(),
  81.             'document' => $document,
  82.             'prefix' => $request->getLocale(),
  83.         ]);
  84.     }
  85.     #[Route('/load-brands/{categoryId}'name'load_brands'methods: ['GET'])]
  86.     public function loadBrandsAction(Request $request$categoryIdFactory $ecommerceFactory): JsonResponse
  87.     {
  88.         $category ShopCategory::getById($categoryId);
  89.         if ($request->getLocale() === 'en') {
  90.             $document Document::getById(8);// English Document
  91.         }else{
  92.             $document Document::getById(1);// Spanish Document
  93.         }
  94.         $params = [
  95.             'rootCategory' => null,
  96.             'document' => $document,
  97.             'prefix' => $request->getLocale(),
  98.         ];
  99.         $categoryUrl $category->getDetailUrl($params);
  100.         $indexService $ecommerceFactory->getIndexService();
  101.         $products $indexService->getProductListForCurrentTenant();
  102.         $products->setVariantMode(ProductListInterface::VARIANT_MODE_HIDE);
  103.         $products->addCondition("productstatus NOT IN ('new', 'decatelogized', 'for_delete') OR productstatus IS NULL"'productstatus');
  104.         $params['parentCategoryIds'] = $categoryId;
  105.         $filterService $ecommerceFactory->getFilterService();
  106.         $filterDefinition Config::getWebsiteConfig()->get('fallbackFilterdefinition');
  107.         $listHelper = new ListHelper();
  108.         $listHelper->setupProductList($filterDefinition$products$params$filterServicetrue);
  109.         $brandsData $filterService->getFilterValues($filterDefinition->getFilters()->getItems()[0], $products, ['marca' => null]);
  110.         $brands = [];
  111.         foreach ($brandsData['values'] as $brandData) {
  112.             $brand ProductMarca::getById($brandData['value']);
  113.             if ($brand) {
  114.                 $brands[] = [
  115.                     'id' => $brand->getId(),
  116.                     'name' => $brand->getName(),
  117.                     'url'   => $categoryUrl '?marca[]=' $brand->getId(),
  118.                     'image' => $brand->getLogo() ? $brand->getLogo()->getThumbnail('navbar-brands')->getPath() : null
  119.                 ];
  120.             }
  121.         }
  122.         usort($brands, function ($a$b) {
  123.             return strcmp($a['name'], $b['name']);
  124.         });
  125.         return $this->json(['brands' => $brands]);
  126.     }
  127.     public function usermanualAction(){
  128.         return $this->render('layout/includes/user-manual-links.html.twig');
  129.     }
  130.     public function addProductAction(Request $request){
  131.         if($request->get('type') == 'object')
  132.         {
  133.             if($object AbstractProduct::getById($request->get('id'))){
  134.                 return $this->render('Content/newsProduct.html.twig',['object' => $object]);
  135.             }
  136.         }
  137.         throw new NotFoundHttpException('Object not found.');
  138.     }
  139. }