<?php
namespace App\Controller;
use App\Model\DataObject\AbstractProduct;
use App\Model\ShopCategory;
use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
use Pimcore\Bundle\EcommerceFrameworkBundle\FilterService\ListHelper;
use Pimcore\Bundle\EcommerceFrameworkBundle\IndexService\ProductList\ProductListInterface;
use Pimcore\Config;
use Pimcore\Controller\FrontendController;
use Pimcore\Model\DataObject\Product;
use Pimcore\Model\DataObject\ProductCategory;
use Pimcore\Model\DataObject\ProductMarca;
use Pimcore\Model\Document;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class DefaultController extends FrontendController
{
private function getCategories(Factory $ecommerceFactory, $parentId, $level = 1, $values = null): array
{
if ($values === null){
$indexService = $ecommerceFactory->getIndexService();
$products = $indexService->getProductListForCurrentTenant();
$products->setVariantMode(ProductListInterface::VARIANT_MODE_HIDE);
$products->addCondition("productstatus NOT IN ('new', 'decatelogized', 'for_delete') OR productstatus IS NULL", 'productstatus');
$filterService = $ecommerceFactory->getFilterService();
$filterDefinition = Config::getWebsiteConfig()->get('fallbackFilterdefinition');
$listHelper = new ListHelper();
$listHelper->setupProductList($filterDefinition, $products, $params, $filterService, true);
$rawValues = $filterService->getFilterValues($filterDefinition->getFilters()->getItems()[1], $products, ['parentCategoryIds' => null])['values'];
$values = [];
foreach ($rawValues as $entry) {
$values[$entry['value']] = $entry['count'];
}
}
$categoryList = new ProductCategory\Listing();
$categoryList->setUnpublished(false);
$categoryList->setOrderKey("order");
$categoryList->setOrder("ASC");
$categoryList->setCondition("o_parentId = ?", [$parentId]);
$loadedCategories = $categoryList->load();
$result = [];
foreach ($loadedCategories as $category) {
if (!isset($values[$category->getId()])) {
continue;
}
$subcategories = [];
if ($level < 3) {
$subcategories = $this->getCategories($ecommerceFactory, $category->getId(), $level + 1, $values);
}
$result[] = [
'category' => $category,
'children' => $subcategories,
];
}
return $result;
}
public function navbarAction(Factory $ecommerceFactory): Response
{
$categories = $this->getCategories($ecommerceFactory, 10);
return $this->render('layout/includes/navegation/navbar.html.twig', [
'categories' => $categories
]);
}
#[Route('/featuredProducts/{categoryId}', name: 'featured_products', methods: ['GET'])]
public function featuredProductAction($categoryId, Request $request, ):Response
{
$category = ProductCategory::getById($categoryId);
$featuredProduct = $category->getFeaturedproduct();
if ($request->getLocale() === 'en') {
$document = Document::getById(8);// English Document
}else{
$document = Document::getById(1);// Spanish Document
}
return $this->render('layout/includes/navegation/featuredProduct.html.twig',[
'featuredProduct' => $featuredProduct,
'title' => $category->getFeaturedProductTitle(),
'document' => $document,
'prefix' => $request->getLocale(),
]);
}
#[Route('/load-brands/{categoryId}', name: 'load_brands', methods: ['GET'])]
public function loadBrandsAction(Request $request, $categoryId, Factory $ecommerceFactory): JsonResponse
{
$category = ShopCategory::getById($categoryId);
if ($request->getLocale() === 'en') {
$document = Document::getById(8);// English Document
}else{
$document = Document::getById(1);// Spanish Document
}
$params = [
'rootCategory' => null,
'document' => $document,
'prefix' => $request->getLocale(),
];
$categoryUrl = $category->getDetailUrl($params);
$indexService = $ecommerceFactory->getIndexService();
$products = $indexService->getProductListForCurrentTenant();
$products->setVariantMode(ProductListInterface::VARIANT_MODE_HIDE);
$products->addCondition("productstatus NOT IN ('new', 'decatelogized', 'for_delete') OR productstatus IS NULL", 'productstatus');
$params['parentCategoryIds'] = $categoryId;
$filterService = $ecommerceFactory->getFilterService();
$filterDefinition = Config::getWebsiteConfig()->get('fallbackFilterdefinition');
$listHelper = new ListHelper();
$listHelper->setupProductList($filterDefinition, $products, $params, $filterService, true);
$brandsData = $filterService->getFilterValues($filterDefinition->getFilters()->getItems()[0], $products, ['marca' => null]);
$brands = [];
foreach ($brandsData['values'] as $brandData) {
$brand = ProductMarca::getById($brandData['value']);
if ($brand) {
$brands[] = [
'id' => $brand->getId(),
'name' => $brand->getName(),
'url' => $categoryUrl . '?marca[]=' . $brand->getId(),
'image' => $brand->getLogo() ? $brand->getLogo()->getThumbnail('navbar-brands')->getPath() : null
];
}
}
usort($brands, function ($a, $b) {
return strcmp($a['name'], $b['name']);
});
return $this->json(['brands' => $brands]);
}
public function usermanualAction(){
return $this->render('layout/includes/user-manual-links.html.twig');
}
public function addProductAction(Request $request){
if($request->get('type') == 'object')
{
if($object = AbstractProduct::getById($request->get('id'))){
return $this->render('Content/newsProduct.html.twig',['object' => $object]);
}
}
throw new NotFoundHttpException('Object not found.');
}
}