bb-legacy/controllers/front/taxonomyController.php line 31

Open in your IDE?
  1. <?php
  2. use App\Application\Service\Taxonomy\TaxonomyBreadcrumbService;
  3. use App\Application\Service\Taxonomy\TaxonomyService;
  4. use App\Application\Service\Taxonomy\TaxonomyTreeService;
  5. use App\Entity\System\Tag;
  6. use App\Manager\System\TaxonomyLanguageManager;
  7. use App\Manager\System\TaxonomyManager;
  8. use App\Manager\System\TaxonomyStatisticManager;
  9. use App\ViewManager\Landing\ViewService;
  10. use Twig\Environment;
  11. use Twig\Error\LoaderError;
  12. use Twig\Error\RuntimeError;
  13. use Twig\Error\SyntaxError;
  14. use function Sentry\captureException;
  15. class taxonomyController extends Controller
  16. {
  17.     public const AJAX_SHOW_PRODUCTS_TEMPLATE_LIST 1;
  18.     public const MAX_IMAGE_ITERATIONS 5;
  19.     private TaxonomyService $taxonomyService;
  20.     private TaxonomyLanguageManager $taxonomyLanguageManager;
  21.     private int $idFromCoreUrl;
  22.     private TaxonomyStatisticManager $taxonomyStatisticManager;
  23.     private TaxonomyBreadcrumbService $taxonomyBreadcrumbService;
  24.     private TaxonomyTreeService $taxonomyTreeService;
  25.     private ViewService $viewService;
  26.     private TaxonomyManager $taxonomyManager;
  27.     private Environment $twig;
  28.     public function __construct($id null)
  29.     {
  30.         parent::__construct('Taxonomy'$id);
  31.         $this->idFromCoreUrl $id;
  32.         $this->model 'Taxonomy';
  33.         $this->taxonomyService Tools::getSfService(TaxonomyService::class);
  34.         $this->taxonomyLanguageManager Tools::getSfService(TaxonomyLanguageManager::class);
  35.         $this->taxonomyStatisticManager Tools::getSfService(TaxonomyStatisticManager::class);
  36.         $this->taxonomyBreadcrumbService Tools::getSfService(TaxonomyBreadcrumbService::class);
  37.         $this->taxonomyTreeService Tools::getSfService(TaxonomyTreeService::class);
  38.         $this->viewService Tools::getSfService(ViewService::class);
  39.         $this->taxonomyManager Tools::getSfService(TaxonomyManager::class);
  40.         $this->twig Tools::getSfService('twig');
  41.     }
  42.     /**
  43.      * @deprecated moved to new architecture
  44.      *
  45.      * @param int $taxonomyId
  46.      * @return void
  47.      * @throws LoaderError
  48.      * @throws RuntimeError
  49.      * @throws SyntaxError
  50.      * @throws \Psr\Cache\InvalidArgumentException
  51.      */
  52.     public function view(int $taxonomyId): void
  53.     {
  54.         if (!Tools::getSfParameter('is_staging')) {
  55.             captureException(new \Exception('Using legacy taxonomy controller'));
  56.         }
  57.         if (!Tools::getValue('filters') && Tools::getValue('product_type')) {
  58.             $_GET['filters'] = [];
  59.             $_GET['filters']['product_type'] = [Tools::getValue('product_type')];
  60.         } elseif (isset($_SESSION['filters']['product_type']) && !Tools::getValue('product_type')) {
  61.             unset($_SESSION['filters']['product_type']);
  62.         }
  63.         if (!$taxonomyId) {
  64.             return;
  65.         }
  66.         $languageId = (int)Session::get('id_lang');
  67.         $language = \Session::get('lang');
  68.         $isoCode = \Language::getIsoById($languageId);
  69.         $numReferences 0;
  70.         $taxonomy $this->taxonomyManager->findOneById($taxonomyId);
  71.         if (!$taxonomy) {
  72.             return;
  73.         }
  74.         $taxonomyLang $this->taxonomyLanguageManager->getAvailableByTaxonomyAndLanguage($taxonomy->getId(), $languageId);
  75.         if (!$taxonomyLang) {
  76.             return;
  77.         }
  78.         $allTaxonomiesForLanguageId $this->taxonomyService->getAllCachedTaxonomiesForLanguage($languageId);
  79.         $breadCrumb $this->taxonomyBreadcrumbService->getLegacyBreadCrumb($taxonomy$languageId);
  80.         $taxonomyTree $this->taxonomyTreeService->getLegacyTree($allTaxonomiesForLanguageId$language$breadCrumb);
  81.         $sections $breadCrumb;
  82.         $taxonomyChildren $taxonomyTree['selcat_childs'];
  83.         $hasChildren = !empty($taxonomyChildren);
  84.         $parentTaxonomyName null;
  85.         if (!$hasChildren) {
  86.             $parentTaxonomyInfo $taxonomy->getParent() !== null
  87.                 $allTaxonomiesForLanguageId[$taxonomy->getParent()->getId()] : null;
  88.             if ($parentTaxonomyInfo) {
  89.                 $taxonomyChildren $this->taxonomyTreeService->getLegacyTree($allTaxonomiesForLanguageId$language,  [$parentTaxonomyInfo])['selcat_childs'];
  90.                 $parentTaxonomyName $parentTaxonomyInfo['name'];
  91.             }
  92.         }
  93.         $viewFilters $this->viewService->getFiltersByElasticSearchResults($taxonomyId$this->modelViewService::VIEW_TYPE_TAXONOMY);
  94.         if (=== $viewFilters->getNumProducts() && !$this->ajax) {
  95.             $parent = \array_slice($breadCrumb, -21)[0];
  96.             Tools::redirect($parent['link']);
  97.         }
  98.         $hrefLink BASE_URL $taxonomyLang['linkRewrite'] . '.html';
  99.         $method 'taxonomy/ajax_show_taxonomy_products';
  100.         $paginatorData $this->viewService->getPaginatorDataEncoded($hrefLink$method$viewFilters$this->idFromCoreUrl);
  101.         $jsParameters $this->viewService->getParameters($taxonomyId0$viewFilters);
  102.         $codeJs Tools::createCategoryFilters($jsParameters);
  103.         $this->addJs($this->viewService->addJs($paginatorData$codeJs$taxonomyIdViewService::VIEW_JS_TYPE_TAXONOMY));
  104.         $this->metaNoindex $this->getMetaNoIndex();
  105.         if ($viewFilters->getAjax()) {
  106.             $this->setDisplayFooter(false);
  107.             $this->setDisplayHeadAndHeader(false);
  108.             $this->setJsonArgs($this->viewService->setJsonArguments($viewFilters));
  109.             $this->setJsonResponse(true);
  110.             if (empty($viewFilters->getCategoryProducts())) {
  111.                 $viewVars['_partials0'] = 'not_display';
  112.             } else {
  113.                 $viewVars['_partials0'] = $viewFilters->getCategoryProducts();
  114.             }
  115.             $this->setView('ajax-product-list'$viewVars);
  116.         } else {
  117.             $taxonomyImage = isset($taxonomyTree['selected_taxonomy_image']['name']) ?
  118.                 $taxonomyId .'-home/' $taxonomyTree['selected_taxonomy_image']['name'] : null;
  119.             $viewVars $this->viewService->generateViewVars
  120.             (
  121.                 $sections,
  122.                 $taxonomyTree,
  123.                 $taxonomyLang['name'],
  124.                 $taxonomyLang['description'],
  125.                 $taxonomyId,
  126.                 $taxonomyChildren,
  127.                 $hasChildren,
  128.                 $numReferences,
  129.                 false,
  130.                 $viewFilters,
  131.                 $taxonomyImage,
  132.                 $parentTaxonomyName,
  133.                 false,
  134.                 true,
  135.                 null,
  136.                 true
  137.             );
  138.             $viewVars['redirect'] = BASE_URL 'taxonomy/filterTaxonomyMobile/' . (string)$taxonomyId;
  139.             $viewVars['domain'] = 'messages';
  140.             $viewVars['languageIsoCode'] = $isoCode;
  141.             $viewVars['tag_data'] = [
  142.                 'blackfriday_id' => Tag::BLACK_FRIDAY_TAG_ID,
  143.                 'blackfriday_text' => Tag::TAG_BLACK_FRIDAY_TEXT,
  144.                 'hotdeals_id' => Tag::HOTDEAL_TAG_ID,
  145.                 'hotdeals_text' => Tag::TAG_HOTDEALS_TEXT
  146.             ];
  147.             $viewVars['rendered_twig_template'] = $this->twig->render($this->viewService->getTwigName(), $viewVars);
  148.             $this->setView('taxonomyList'$viewVars);
  149.         }
  150.     }
  151.     /**
  152.      * Catálogo completo
  153.      */
  154.     public function showLandingSeeAll()
  155.     {
  156.         $isoCode Session::get('lang');
  157.         $langId = (int)Session::get('id_lang');
  158.         $category $this->getCategoryCompleteCatalog();
  159.         $allTaxonomyByLanguage $this->taxonomyService->getAllCachedTaxonomiesForLanguage($langId);
  160.         $breadcrumb $this->taxonomyBreadcrumbService->getLegacyBreadCrumb(null$langId);
  161.         $taxonomyTree $this->taxonomyTreeService->getLegacyTree($allTaxonomyByLanguage$isoCode, []);
  162.         $taxonomyChilds $this->processChilds($taxonomyTree['_layout0_Array']);
  163.         $this->metaNoindex $this->getMetaNoIndex();
  164.         $numProductsTotal 0;
  165.         foreach ($taxonomyChilds as $taxonomyChild) {
  166.             $numProductsTotal += $taxonomyChild['numProducts'];
  167.         }
  168.         $imageTaxonomy $this->getRandomImageTaxonomy($taxonomyChilds);
  169.         $vars = [
  170.             '_partials0' => [
  171.                 [
  172.                     'sections' => $breadcrumb,
  173.                 ]
  174.             ],
  175.             '_layout1' => [
  176.                 'desc_category_extra' => '',
  177.                 '_layout0_Array' => $taxonomyTree['_layout0_Array']
  178.             ],
  179.             '_layout2' => [
  180.                 'cat' => $imageTaxonomy,
  181.                 'childs' => $taxonomyChilds,
  182.                 'name_category' =>  str_replace('|','',$category->name[$langId]),
  183.                 'desc_category' => Tools::replaceAutomatedLinks($category->description[$langId]),
  184.                 'num_products' => $numProductsTotal,
  185.                 'excludedCategories' => [266226722678]
  186.             ]
  187.         ];
  188.         $this->setView('seeAllTaxonomy'$vars);
  189.     }
  190.     private function getCategoryCompleteCatalog(): Category
  191.     {
  192.         return new Category(\App\Entity\System\Category::FULL_CATALOGUE_CATEGORY_ID);
  193.     }
  194.     protected function processChilds($taxonomies)
  195.     {
  196.         $taxonomyStatistics $this->taxonomyStatisticManager->getAllParentTaxonomyStatistics($taxonomies);
  197.         foreach ($taxonomyStatistics as $taxonomyStatistic) {
  198.             $taxonomies[$taxonomyStatistic['taxonomyId']]['numProducts'] = (!empty($taxonomyStatistic['numProducts'])) ? $taxonomyStatistic['numProducts'] : 0;
  199.         }
  200.         
  201.        return $taxonomies;
  202.     }
  203.     private function getRandomImageTaxonomy($taxonomyChilds): ?string
  204.     {
  205.         $i 0;
  206.         do {
  207.             $taxonomyId array_rand($taxonomyChilds);
  208.             $taxonomy $taxonomyChilds[$taxonomyId];
  209.             $i++;
  210.         } while (null === $taxonomy['image']['id'] && $i self::MAX_IMAGE_ITERATIONS);
  211.         return $taxonomy['image']['id'] . '-home/' $taxonomy['image']['name'] . '.jpg';
  212.     }
  213.     /**
  214.      * @return void
  215.      */
  216.     public function ajax_show_taxonomy_products(): void
  217.     {
  218.         $init Tools::getValue('init');
  219.         $limit Tools::getValue('limit');
  220.         $filters Tools::getValue('filters', array());
  221.         $type Tools::getValue('type');
  222.         $isMobile Tools::getValue('mobile');
  223.         //Guardamos los filtros en sesión para poder
  224.         Session::set('filters'$filters);
  225.         
  226.         if (isset($filters['category']) && count($filters['category']) > ) {
  227.             $filters ['taxonomy'] = $filters['category'];
  228.             unset($filters['category']);
  229.         } else {
  230.             $filters ['taxonomy'] = [ Tools::getValue('args') ];
  231.         }
  232.         $results Tools::getResultsElasticSearch(
  233.             Session::get('lang'),
  234.             $init,
  235.             $limit,
  236.             $this->ajax,
  237.             $filters,
  238.             Session::get('is_wholesaler')
  239.         );
  240.         $numberProducts $results['total_results'];
  241.         $resultProds $results['result'];
  242.         if ($numberProducts === false) {
  243.             $numberProducts 0;
  244.         }
  245.         $nextInit $init $limit;
  246.         $nextCallNum $limit;
  247.         if ($numberProducts $nextInit $limit) {
  248.             $nextCallNum $numberProducts $nextInit;
  249.         }
  250.         $show true;
  251.         if ($numberProducts <= $nextInit) {
  252.             $show false;
  253.         }
  254.         $this->setDisplayFooter(false);
  255.         $this->setDisplayHeadAndHeader(false);
  256.         $numProductsPaginator = ($numberProducts tools::MAX_LIST_PRODUCTS) ? tools::MAX_LIST_PRODUCTS $numberProducts;
  257.         if ($isMobile) {
  258.             $this->setJsonArgs(['num_products' => $numberProducts]);
  259.             $this->setJsonResponse(true);
  260.         } else {
  261.             $this->setJsonArgs([
  262.                 'init' => $nextInit,
  263.                 'show' => $show,
  264.                 'next_call_num' => $nextCallNum,
  265.                 'num_products' => $numberProducts,
  266.                 'num_products_paginator' => $numProductsPaginator,
  267.                 'ga_productlist' => []
  268.             ]);
  269.             $this->setJsonResponse(true);
  270.             if (empty($resultProds)) {
  271.                 $resultProds 'not_display';
  272.             }
  273.             
  274.             if ($type === self::AJAX_SHOW_PRODUCTS_TEMPLATE_LIST) {
  275.                 $vars = [
  276.                     '_partials0' => $resultProds
  277.                 ];
  278.                 $this->setView('ajax-product-list'$vars);
  279.             } else {
  280.                 $vars = [
  281.                     '_layout0' => [
  282.                         '_partials0' => $resultProds
  283.                     ]
  284.                 ];
  285.                 $this->setView('ajax-product-list-sliders'$vars'ajax-product-list-full');
  286.             }
  287.         }
  288.     }
  289.     /**
  290.      * @param int $taxonomyId
  291.      * @return void
  292.      * @throws LoaderError
  293.      * @throws RuntimeError
  294.      * @throws SyntaxError
  295.      */
  296.     public function filterTaxonomyMobile(int $taxonomyId): void
  297.     {
  298.         $languageId = (int)Session::get('id_lang');
  299.         $isoCode = \Language::getIsoById($languageId);
  300.         $viewFilters $this->viewService->getFiltersByElasticSearchResults($taxonomyId$this->modelViewService::VIEW_TYPE_TAXONOMY);
  301.         $linkRewrite Link::getTaxonomyLink($taxonomyIdSession::get('id_lang'));
  302.         $href BASE_URL 'taxonomy/ajax_show_taxonomy_products/'.$taxonomyId;
  303.         $jsParameters $this->viewService->getParametersToFilterMobile($taxonomyId$viewFilters$linkRewrite$href);
  304.         $codeJs Tools::createCategoryFilters($jsParameters);
  305.         $this->addJs($this->viewService->addFilterMobileJs($codeJs));
  306.         $viewVars $this->viewService->generateVarsMobileFilter($viewFilters);
  307.         $this->headerClass 'fullContent';
  308.         $viewVars['redirect'] = $linkRewrite;
  309.         $viewVars['domain'] = 'messages';
  310.         $viewVars['languageIsoCode'] = $isoCode;
  311.         $viewVars['rendered_twig_template'] = $this->twig->render('front/shop_b2b/mobile/filter_mobile.html.twig'$viewVars);
  312.         $this->setView('filterMobile'$viewVars);
  313.     }
  314.     protected function getMetaNoIndex(): bool
  315.     {
  316.         if (
  317.             strpos(Session::get('url'), 'taxonomy') !== false  ||
  318.             strpos(Session::get('url'), 'Taxonomy') !== false
  319.         ) {
  320.             return true;
  321.         }
  322.         return false;
  323.     }
  324. }