src/Controller/DefaultController.php line 26

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 Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  15. use Symfony\Component\HttpFoundation\JsonResponse;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  19. use Symfony\Component\Process\Exception\ProcessFailedException;
  20. use Symfony\Component\Process\Process;
  21. use Symfony\Component\Routing\Annotation\Route;
  22. use Symfony\Contracts\Translation\TranslatorInterface;
  23. class DefaultController extends FrontendController
  24. {
  25.     private function getCategories(Factory $ecommerceFactory$parentId$level 1$values null): array
  26.     {
  27.         if ($values === null){
  28.             $indexService $ecommerceFactory->getIndexService();
  29.             $products $indexService->getProductListForCurrentTenant();
  30.             $products->setVariantMode(ProductListInterface::VARIANT_MODE_HIDE);
  31.             $products->addCondition("productstatus NOT IN ('new', 'decatelogized', 'for_delete') OR productstatus IS NULL"'productstatus');
  32.             $filterService $ecommerceFactory->getFilterService();
  33.             $filterDefinition Config::getWebsiteConfig()->get('fallbackFilterdefinition');
  34.             $listHelper = new ListHelper();
  35.             $listHelper->setupProductList($filterDefinition$products$params$filterServicetrue);
  36.             $rawValues $filterService->getFilterValues($filterDefinition->getFilters()->getItems()[1], $products, ['parentCategoryIds' => null])['values'];
  37.             $values = [];
  38.             foreach ($rawValues as $entry) {
  39.                 $values[$entry['value']] = $entry['count'];
  40.             }
  41.         }
  42.         $categoryList = new ProductCategory\Listing();
  43.         $categoryList->setUnpublished(false);
  44.         $categoryList->setOrderKey("order");
  45.         $categoryList->setOrder("ASC");
  46.         $categoryList->setCondition("o_parentId = ?", [$parentId]);
  47.         $loadedCategories $categoryList->load();
  48.         $result = [];
  49.         foreach ($loadedCategories as $category) {
  50.             if (!isset($values[$category->getId()])) {
  51.                 continue;
  52.             }
  53.             $subcategories = [];
  54.             if ($level 3) {
  55.                 $subcategories $this->getCategories($ecommerceFactory$category->getId(), $level 1$values);
  56.             }
  57.             $result[] = [
  58.                 'category' => $category,
  59.                 'children' => $subcategories,
  60.             ];
  61.         }
  62.         return $result;
  63.     }
  64.     public function navbarAction(Factory $ecommerceFactory): Response
  65.     {
  66.         $categories $this->getCategories($ecommerceFactory10);
  67.         return $this->render('layout/includes/navegation/navbar.html.twig', [
  68.             'categories' => $categories
  69.         ]);
  70.     }
  71.     #[Route('/featuredProducts/{categoryId}'name'featured_products'methods: ['GET'])]
  72.     public function featuredProductAction($categoryIdRequest $request, ):Response
  73.     {
  74.         $category ProductCategory::getById($categoryId);
  75.         $featuredProduct $category->getFeaturedproduct();
  76.         if ($request->getLocale() === 'en') {
  77.             $document Document::getById(8);// English Document
  78.         }else{
  79.             $document Document::getById(1);// Spanish Document
  80.         }
  81.         return $this->render('layout/includes/navegation/featuredProduct.html.twig',[
  82.             'featuredProduct' => $featuredProduct,
  83.             'title' => $category->getFeaturedProductTitle(),
  84.             'document' => $document,
  85.             'prefix' => $request->getLocale(),
  86.         ]);
  87.     }
  88.     #[Route('/load-brands/{categoryId}'name'load_brands'methods: ['GET'])]
  89.     public function loadBrandsAction(Request $request$categoryIdFactory $ecommerceFactory): JsonResponse
  90.     {
  91.         $category ShopCategory::getById($categoryId);
  92.         if ($request->getLocale() === 'en') {
  93.             $document Document::getById(8);// English Document
  94.         }else{
  95.             $document Document::getById(1);// Spanish Document
  96.         }
  97.         $params = [
  98.             'rootCategory' => null,
  99.             'document' => $document,
  100.             'prefix' => $request->getLocale(),
  101.         ];
  102.         $categoryUrl $category->getDetailUrl($params);
  103.         $indexService $ecommerceFactory->getIndexService();
  104.         $products $indexService->getProductListForCurrentTenant();
  105.         $products->setVariantMode(ProductListInterface::VARIANT_MODE_HIDE);
  106.         $products->addCondition("productstatus NOT IN ('new', 'decatelogized', 'for_delete') OR productstatus IS NULL"'productstatus');
  107.         $params['parentCategoryIds'] = $categoryId;
  108.         $filterService $ecommerceFactory->getFilterService();
  109.         $filterDefinition Config::getWebsiteConfig()->get('fallbackFilterdefinition');
  110.         $listHelper = new ListHelper();
  111.         $listHelper->setupProductList($filterDefinition$products$params$filterServicetrue);
  112.         $brandsData $filterService->getFilterValues($filterDefinition->getFilters()->getItems()[0], $products, ['marca' => null]);
  113.         $brands = [];
  114.         foreach ($brandsData['values'] as $brandData) {
  115.             $brand ProductMarca::getById($brandData['value']);
  116.             if ($brand) {
  117.                 $brands[] = [
  118.                     'id' => $brand->getId(),
  119.                     'name' => $brand->getName(),
  120.                     'url'   => $categoryUrl '?marca[]=' $brand->getId(),
  121.                     'image' => $brand->getLogo() ? $brand->getLogo()->getThumbnail('navbar-brands')->getPath() : null
  122.                 ];
  123.             }
  124.         }
  125.         usort($brands, function ($a$b) {
  126.             return strcmp($a['name'], $b['name']);
  127.         });
  128.         return $this->json(['brands' => $brands]);
  129.     }
  130.     public function usermanualAction(){
  131.         return $this->render('layout/includes/user-manual-links.html.twig');
  132.     }
  133.     public function addProductAction(Request $request){
  134.         if($request->get('type') == 'object')
  135.         {
  136.             if($object AbstractProduct::getById($request->get('id'))){
  137.                 return $this->render('Content/newsProduct.html.twig',['object' => $object]);
  138.             }
  139.         }
  140.         throw new NotFoundHttpException('Object not found.');
  141.     }
  142.     #[Route('/admin/send-registration-emails'name'send_emails'methods: ['POST'])]
  143.     #[IsGranted('ROLE_PIMCORE_USER')]
  144.     public function sendEmails(): JsonResponse
  145.     {
  146.         try {
  147.             $projectDir $this->getParameter('kernel.project_dir');
  148.             $process = new Process(['php'$projectDir '/bin/console''send-registration-mail']);
  149.             $process->setTimeout(300); // 5 minutes
  150.             $process->run();
  151.             if (!$process->isSuccessful()) {
  152.                 throw new ProcessFailedException($process);
  153.             }
  154.             $outputLines explode("\n"trim($process->getOutput()));
  155.             $lastLine end($outputLines);
  156.             $summary json_decode($lastLinetrue);
  157.             if (!is_array($summary) || !isset($summary['total'], $summary['successful'], $summary['failed'])) {
  158.                 throw new \Exception('Invalid output from command');
  159.             }
  160.             return new JsonResponse([
  161.                 'success' => true,
  162.                 'message' => 'Emails sent successfully',
  163.                 'summary' => $summary
  164.             ]);
  165.         } catch (\Exception $e) {
  166.             return new JsonResponse([
  167.                 'success' => false,
  168.                 'error' => $e->getMessage()
  169.             ], 500);
  170.         }
  171.     }
  172. }