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

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\Language;
  6. use App\Entity\System\Tag;
  7. use App\Entity\System\TaxonomyLanguage;
  8. use App\Manager\System\LanguageManager;
  9. use App\Manager\System\TaxonomyLanguageManager;
  10. use App\Manager\System\TaxonomyManager;
  11. use App\Manager\System\TaxonomyStatisticManager;
  12. use App\ViewManager\Landing\ViewService;
  13. use Twig\Environment;
  14. use Twig\Error\LoaderError;
  15. use Twig\Error\RuntimeError;
  16. use Twig\Error\SyntaxError;
  17. class taxonomyController extends Controller
  18. {
  19.     const CATEGORY_COMPLETE_CATALOG_ID 2202;
  20.     public const AJAX_SHOW_PRODUCTS_TEMPLATE_LIST 1;
  21.     public const MAX_IMAGE_ITERATIONS 5;
  22.     /** @var TaxonomyService */
  23.     private $taxonomyService;
  24.     /** @var LanguageManager */
  25.     private $languageManager;
  26.     /** @var TaxonomyLanguageManager */
  27.     private $taxonomyLanguageManager;
  28.     /** @var int */
  29.     private $idFromCoreUrl;
  30.     /** @var TaxonomyStatisticManager */
  31.     private $taxonomyStatisticManager;
  32.     /** @var TaxonomyBreadcrumbService */
  33.     private $taxonomyBreadcrumbService;
  34.     /** @var TaxonomyTreeService */
  35.     private $taxonomyTreeService;
  36.     /** @var ViewService */
  37.     private $viewService;
  38.     /** @var TaxonomyManager */
  39.     private $taxonomyManager;
  40.     /** @var Environment */
  41.     private $twig;
  42.     public function __construct($id null)
  43.     {
  44.         parent::__construct('Taxonomy'$id);
  45.         $this->idFromCoreUrl $id;
  46.         $this->model 'Taxonomy';
  47.         $this->taxonomyService Tools::getSfService(TaxonomyService::class);
  48.         $this->taxonomyLanguageManager Tools::getSfService(TaxonomyLanguageManager::class);
  49.         $this->taxonomyStatisticManager Tools::getSfService(TaxonomyStatisticManager::class);
  50.         $this->taxonomyBreadcrumbService Tools::getSfService(TaxonomyBreadcrumbService::class);
  51.         $this->languageManager Tools::getSfService(LanguageManager::class);
  52.         $this->taxonomyTreeService Tools::getSfService(TaxonomyTreeService::class);
  53.         $this->viewService Tools::getSfService(ViewService::class);
  54.         $this->taxonomyManager Tools::getSfService(TaxonomyManager::class);
  55.         $this->twig Tools::getSfService('twig');
  56.     }
  57.     public function index()
  58.     {
  59.     }
  60.     /**
  61.      * @param int $taxonomyId
  62.      * @return void
  63.      * @throws LoaderError
  64.      * @throws RuntimeError
  65.      * @throws SyntaxError
  66.      * @throws \Psr\Cache\InvalidArgumentException
  67.      */
  68.     public function view(int $taxonomyId): void
  69.     {
  70.         if ($taxonomyId === (int)Configuration::get('TAXONOMY_ALL_CATALOG')) {
  71.             $this->showLandingSeeAll();
  72.             return;
  73.         }
  74.         $this->showTaxonomy($taxonomyId);
  75.     }
  76.     /**
  77.      * @param int $taxonomyId
  78.      * @return void
  79.      * @throws \Psr\Cache\InvalidArgumentException
  80.      * @throws LoaderError
  81.      * @throws RuntimeError
  82.      * @throws SyntaxError
  83.      */
  84.     public function showTaxonomy(int $taxonomyId): void
  85.     {
  86.         if (!Tools::getValue('filters') && Tools::getValue('product_type')) {
  87.             $_GET['filters'] = [];
  88.             $_GET['filters']['product_type'] = [Tools::getValue('product_type')];
  89.         } elseif (isset($_SESSION['filters']['product_type']) && !Tools::getValue('product_type')) {
  90.             unset($_SESSION['filters']['product_type']);
  91.         }
  92.         if (!$taxonomyId) {
  93.             return;
  94.         }
  95.         $languageId = (int)Session::get('id_lang');
  96.         $language = \Session::get('lang');
  97.         $isoCode = \Language::getIsoById($languageId);
  98.         $numReferences 0;
  99.         $taxonomy $this->taxonomyManager->findOneById($taxonomyId);
  100.         if (!$taxonomy) {
  101.             return;
  102.         }
  103.         $taxonomyLang $this->taxonomyLanguageManager->getAvailableByTaxonomyAndLanguage($taxonomy->getId(), $languageId);
  104.         if (!$taxonomyLang) {
  105.             return;
  106.         }
  107.         $allTaxonomiesForLanguageId $this->taxonomyService->getAllCachedTaxonomiesForLanguage($languageId);
  108.         $breadCrumb $this->taxonomyBreadcrumbService->getLegacyBreadCrumb($taxonomy$languageId);
  109.         $taxonomyTree $this->taxonomyTreeService->getLegacyTree($allTaxonomiesForLanguageId$language$breadCrumb);
  110.         $sections $breadCrumb;
  111.         $taxonomyChildren $taxonomyTree['selcat_childs'];
  112.         $hasChildren = !empty($taxonomyChildren);
  113.         $parentTaxonomyName null;
  114.         if (!$hasChildren) {
  115.             $parentTaxonomyInfo $taxonomy->getParent() !== null
  116.                 $allTaxonomiesForLanguageId[$taxonomy->getParent()->getId()] : null;
  117.             if ($parentTaxonomyInfo) {
  118.                 $taxonomyChildren $this->taxonomyTreeService->getLegacyTree($allTaxonomiesForLanguageId$language,  [$parentTaxonomyInfo])['selcat_childs'];
  119.                 $parentTaxonomyName $parentTaxonomyInfo['name'];
  120.             }
  121.         }
  122.         $viewFilters $this->viewService->getFiltersByElasticSearchResults($taxonomyId$this->modelViewService::VIEW_TYPE_TAXONOMY);
  123.         if (=== $viewFilters->getNumProducts() && !$this->ajax) {
  124.             $parent = \array_slice($breadCrumb, -21)[0];
  125.             Tools::redirect($parent['link']);
  126.         }
  127.         $hrefLink BASE_URL $taxonomyLang['linkRewrite'] . '.html';
  128.         $method 'taxonomy/ajax_show_taxonomy_products';
  129.         $paginatorData $this->viewService->getPaginatorDataEncoded($hrefLink$method$viewFilters$this->idFromCoreUrl);
  130.         $jsParameters $this->viewService->getParameters($taxonomyId0$viewFilters);
  131.         $codeJs Tools::createCategoryFilters($jsParameters);
  132.         $this->addJs($this->viewService->addJs($paginatorData$codeJs$taxonomyIdViewService::VIEW_JS_TYPE_TAXONOMY));
  133.         $this->metaNoindex $this->getMetaNoIndex();
  134.         if ($viewFilters->getAjax()) {
  135.             $this->setDisplayFooter(false);
  136.             $this->setDisplayHeadAndHeader(false);
  137.             $this->setJsonArgs($this->viewService->setJsonArguments($viewFilters));
  138.             $this->setJsonResponse(true);
  139.             if (empty($viewFilters->getCategoryProducts())) {
  140.                 $viewVars['_partials0'] = 'not_display';
  141.             } else {
  142.                 $viewVars['_partials0'] = $viewFilters->getCategoryProducts();
  143.             }
  144.             $this->setView('ajax-product-list'$viewVars);
  145.         } else {
  146.             $taxonomyImage = isset($taxonomyTree['selected_taxonomy_image']['name']) ?
  147.                 $taxonomyId .'-home/' $taxonomyTree['selected_taxonomy_image']['name'] : null;
  148.             $viewVars $this->viewService->generateViewVars
  149.             (
  150.                 $sections,
  151.                 $taxonomyTree,
  152.                 $taxonomyLang['name'],
  153.                 $taxonomyLang['description'],
  154.                 $taxonomyId,
  155.                 $taxonomyChildren,
  156.                 $hasChildren,
  157.                 $numReferences,
  158.                 false,
  159.                 $viewFilters,
  160.                 $taxonomyImage,
  161.                 $parentTaxonomyName,
  162.                 false,
  163.                 true,
  164.                 null,
  165.                 true
  166.             );
  167.             $viewVars['redirect'] = BASE_URL 'taxonomy/filterTaxonomyMobile/' . (string)$taxonomyId;
  168.             $viewVars['domain'] = 'messages';
  169.             $viewVars['languageIsoCode'] = $isoCode;
  170.             $viewVars['tag_data'] = [
  171.                 'blackfriday_id' => Tag::BLACK_FRIDAY_TAG_ID,
  172.                 'blackfriday_text' => Tag::TAG_BLACK_FRIDAY_TEXT,
  173.                 'hotdeals_id' => Tag::HOTDEAL_TAG_ID,
  174.                 'hotdeals_text' => Tag::TAG_HOTDEALS_TEXT
  175.             ];
  176.             $viewVars['rendered_twig_template'] = $this->twig->render($this->viewService->getTwigName(), $viewVars);
  177.             $this->setView('taxonomyList'$viewVars);
  178.         }
  179.     }
  180.     /**
  181.      * CatĆ”logo completo
  182.      */
  183.     public function showLandingSeeAll()
  184.     {
  185.         $isoCode Session::get('lang');
  186.         $langId = (int)Session::get('id_lang');
  187.         $category $this->getCategoryCompleteCatalog();
  188.         $allTaxonomyByLanguage $this->taxonomyService->getAllCachedTaxonomiesForLanguage($langId);
  189.         $breadcrumb $this->taxonomyBreadcrumbService->getLegacyBreadCrumb(null$langId);
  190.         $taxonomyTree $this->taxonomyTreeService->getLegacyTree($allTaxonomyByLanguage$isoCode, []);
  191.         $taxonomyChilds $this->processChilds($taxonomyTree['_layout0_Array']);
  192.         $this->metaNoindex $this->getMetaNoIndex();
  193.         $numProductsTotal 0;
  194.         foreach ($taxonomyChilds as $taxonomyChild) {
  195.             $numProductsTotal += $taxonomyChild['numProducts'];
  196.         }
  197.         $imageTaxonomy $this->getRandomImageTaxonomy($taxonomyChilds);
  198.         $vars = [
  199.             '_partials0' => [
  200.                 [
  201.                     'sections' => $breadcrumb,
  202.                 ]
  203.             ],
  204.             '_layout1' => [
  205.                 'desc_category_extra' => '',
  206.                 '_layout0_Array' => $taxonomyTree['_layout0_Array']
  207.             ],
  208.             '_layout2' => [
  209.                 'cat' => $imageTaxonomy,
  210.                 'childs' => $taxonomyChilds,
  211.                 'name_category' =>  str_replace('|','',$category->name[$langId]),
  212.                 'desc_category' => Tools::replaceAutomatedLinks($category->description[$langId]),
  213.                 'num_products' => $numProductsTotal,
  214.                 'excludedCategories' => [266226722678]
  215.             ]
  216.         ];
  217.         $this->setView('seeAllTaxonomy'$vars);
  218.     }
  219.     private function getCategoryCompleteCatalog(): Category
  220.     {
  221.         return new Category(self::CATEGORY_COMPLETE_CATALOG_ID);
  222.     }
  223.     protected function processChilds($taxonomies)
  224.     {
  225.         $taxonomyStatistics $this->taxonomyStatisticManager->getAllParentTaxonomyStatistics($taxonomies);
  226.         foreach ($taxonomyStatistics as $taxonomyStatistic) {
  227.             $taxonomies[$taxonomyStatistic['taxonomyId']]['numProducts'] = (!empty($taxonomyStatistic['numProducts'])) ? $taxonomyStatistic['numProducts'] : 0;
  228.         }
  229.         
  230.        return $taxonomies;
  231.     }
  232.     /**
  233.      * @param int $taxonomyId
  234.      * @throws \Psr\Cache\InvalidArgumentException
  235.      */
  236.     public function landingpage(int $taxonomyId): void
  237.     {
  238.         $isoCode Session::get('lang');
  239.         /** @var Language $language */
  240.         $language $this->languageManager->findOneBy(['isoCode' => $isoCode]);
  241.         /** @var TaxonomyLanguage $taxonomyLanguage */
  242.         $taxonomyLanguage $this->taxonomyLanguageManager->findOneBy([
  243.             'language' => $language,
  244.             'taxonomy' => $taxonomyId,
  245.         ]);
  246.         $taxonomy $taxonomyLanguage->getTaxonomy();
  247.         $sections $this->taxonomyBreadcrumbService->getLegacyBreadCrumb($taxonomy$language->getId());
  248.         $homeTaxonomy App\Entity\System\Category::HOME_ID;
  249.         $taxonomyTree $this->taxonomyTreeService->getLegacyTree
  250.         (
  251.             $this->taxonomyService->getAllCachedTaxonomiesForLanguage($language->getId()),
  252.             $isoCode,
  253.             $sections
  254.         );
  255.         $taxonomyChilds $taxonomyTree['selcat_childs'];
  256.         $mainTaxonomy = ($taxonomy->getId() === $homeTaxonomy);
  257.         $taxonomyAux = [];
  258.         foreach ($taxonomyTree['_layout0_Array'] as $taxonomyValue) {
  259.             if ($taxonomyValue['id'] !== $taxonomy->getId()) {
  260.                 $taxonomyAux[] = $taxonomyValue;
  261.             } else {
  262.                 array_unshift($taxonomyAux$taxonomyValue);
  263.             }
  264.         }
  265.         $taxonomyTree['_layout0_Array'] = $taxonomyAux;
  266.         $mainavLinks Category_mainav_link::getIdLinks($taxonomy->getId());
  267.         if ($mainavLinks) {
  268.             $this->category_mainav $taxonomy->getId();
  269.             $this->category_mainav_links $mainavLinks;
  270.         } else if ($mainTaxonomy) {
  271.             $this->category_mainav $taxonomy->getId();
  272.             $this->category_mainav_links = [];
  273.         }
  274.         $this->_object->canonical Link::getTaxonomyLink($taxonomyId);
  275.         $this->addJs([
  276.             ['value' => 'public/js/tools.js?t=201606071238'],
  277.             ['value' => 'public/js/categorylandingpage.js?t=201606071238'],
  278.             ['value' => 'var filterMobile=0;',
  279.                 'code'  => true]
  280.         ]);
  281.         $vars = [];
  282.         $vars['categMenuList'] = [];
  283.         $vars['_partials0'] = [
  284.             ['sections' => $sections]
  285.         ];
  286.         $vars['_layout1'] = [];
  287.         $vars['_layout1']['desc_category_extra'] = null;
  288.         $vars['_layout1']['_layout0_Array'] = $taxonomyTree['_layout0_Array'];
  289.         $vars['_layout2'] = [];
  290.         $vars['_layout2']['name_category'] = str_replace('|',''$taxonomyLanguage->getName());
  291.         $vars['_layout2']['desc_category'] = Tools::replaceAutomatedLinks($taxonomyLanguage->getDescription());
  292.         $vars['_layout2']['cat'] = $taxonomy->getId();
  293.         $vars['_layout2']['link_rewrite'] = $taxonomyLanguage->getLinkRewrite();
  294.         $vars['_layout2']['is_exception'] = in_array($taxonomy->getId(), Tools::jsonDecode(Configuration::get('CATEGORY_TEXT_EXCEPTION')), true);
  295.         $vars['_layout2']['_partials0'] = $taxonomyChilds;
  296.         $vars['_layout2']['childs'] = $taxonomyChilds;
  297.         $vars['_layout2']['childs_num_row'] = 3;
  298.         $countCategChild count($taxonomyChilds);
  299.         for ($nRow 4$nRow <= 5$nRow++) {
  300.             if (($countCategChild $nRow) === 0) {
  301.                 $vars['_layout2']['childs_num_row'] = $nRow;
  302.             }
  303.         }
  304.         if(Tools::isMobile()){
  305.             $categoryBread array_slice($sections, -11);
  306.             $vars['_partials0'][0]['name'] = $categoryBread[0]['name'];
  307.             $vars['_partials0'][0]['arrow'] = false;
  308.             $vars['_partials0'][0]['linkCategory'] = false;
  309.             $vars['_layout1'] = 'not_display';
  310.             $this->addJs([
  311.                 ['value' => 'var filterMobile=0;',
  312.                     'code'  => true]
  313.             ]);
  314.         }
  315.         $this->setView('taxonomyLandingPage'$vars);
  316.     }
  317.     private function getRandomImageTaxonomy($taxonomyChilds): ?string
  318.     {
  319.         $i 0;
  320.         do {
  321.             $taxonomyId array_rand($taxonomyChilds);
  322.             $taxonomy $taxonomyChilds[$taxonomyId];
  323.             $i++;
  324.         } while (null === $taxonomy['image']['id'] && $i self::MAX_IMAGE_ITERATIONS);
  325.         return $taxonomy['image']['id'] . '-home/' $taxonomy['image']['name'] . '.jpg';
  326.     }
  327.     /**
  328.      * @return void
  329.      */
  330.     public function ajax_show_taxonomy_products(): void
  331.     {
  332.         $init Tools::getValue('init');
  333.         $limit Tools::getValue('limit');
  334.         $filters Tools::getValue('filters', array());
  335.         $type Tools::getValue('type');
  336.         $isMobile Tools::getValue('mobile');
  337.         //Guardamos los filtros en sesiĆ³n para poder
  338.         Session::set('filters'$filters);
  339.         
  340.         if (isset($filters['category']) && count($filters['category']) > ) {
  341.             $filters ['taxonomy'] = $filters['category'];
  342.             unset($filters['category']);
  343.         } else {
  344.             $filters ['taxonomy'] = [ Tools::getValue('args') ];
  345.         }
  346.         $results Tools::getResultsElasticSearch(
  347.             Session::get('lang'),
  348.             $init,
  349.             $limit,
  350.             $this->ajax,
  351.             $filters,
  352.             Session::get('is_wholesaler')
  353.         );
  354.         $numberProducts $results['total_results'];
  355.         $resultProds $results['result'];
  356.         if ($numberProducts === false) {
  357.             $numberProducts 0;
  358.         }
  359.         $nextInit $init $limit;
  360.         $nextCallNum $limit;
  361.         if ($numberProducts $nextInit $limit) {
  362.             $nextCallNum $numberProducts $nextInit;
  363.         }
  364.         $show true;
  365.         if ($numberProducts <= $nextInit) {
  366.             $show false;
  367.         }
  368.         $this->setDisplayFooter(false);
  369.         $this->setDisplayHeadAndHeader(false);
  370.         $numProductsPaginator = ($numberProducts tools::MAX_LIST_PRODUCTS) ? tools::MAX_LIST_PRODUCTS $numberProducts;
  371.         if ($isMobile) {
  372.             $this->setJsonArgs(['num_products' => $numberProducts]);
  373.             $this->setJsonResponse(true);
  374.         } else {
  375.             $this->setJsonArgs([
  376.                 'init' => $nextInit,
  377.                 'show' => $show,
  378.                 'next_call_num' => $nextCallNum,
  379.                 'num_products' => $numberProducts,
  380.                 'num_products_paginator' => $numProductsPaginator,
  381.                 'ga_productlist' => []
  382.             ]);
  383.             $this->setJsonResponse(true);
  384.             if (empty($resultProds)) {
  385.                 $resultProds 'not_display';
  386.             }
  387.             
  388.             if ($type === self::AJAX_SHOW_PRODUCTS_TEMPLATE_LIST) {
  389.                 $vars = [
  390.                     '_partials0' => $resultProds
  391.                 ];
  392.                 $this->setView('ajax-product-list'$vars);
  393.             } else {
  394.                 $vars = [
  395.                     '_layout0' => [
  396.                         '_partials0' => $resultProds
  397.                     ]
  398.                 ];
  399.                 $this->setView('ajax-product-list-sliders'$vars'ajax-product-list-full');
  400.             }
  401.         }
  402.     }
  403.     /**
  404.      * @param int $taxonomyId
  405.      * @return void
  406.      * @throws LoaderError
  407.      * @throws RuntimeError
  408.      * @throws SyntaxError
  409.      */
  410.     public function filterTaxonomyMobile(int $taxonomyId): void
  411.     {
  412.         $languageId = (int)Session::get('id_lang');
  413.         $isoCode = \Language::getIsoById($languageId);
  414.         $viewFilters $this->viewService->getFiltersByElasticSearchResults($taxonomyId$this->modelViewService::VIEW_TYPE_TAXONOMY);
  415.         $linkRewrite Link::getTaxonomyLink($taxonomyIdSession::get('id_lang'));
  416.         $href BASE_URL 'taxonomy/ajax_show_taxonomy_products/'.$taxonomyId;
  417.         $jsParameters $this->viewService->getParametersToFilterMobile($taxonomyId$viewFilters$linkRewrite$href);
  418.         $codeJs Tools::createCategoryFilters($jsParameters);
  419.         $this->addJs($this->viewService->addFilterMobileJs($codeJs));
  420.         $viewVars $this->viewService->generateVarsMobileFilter($viewFilters);
  421.         $this->headerClass 'fullContent';
  422.         $viewVars['redirect'] = $linkRewrite;
  423.         $viewVars['domain'] = 'messages';
  424.         $viewVars['languageIsoCode'] = $isoCode;
  425.         $viewVars['rendered_twig_template'] = $this->twig->render('front/shop_b2b/mobile/filter_mobile.html.twig'$viewVars);
  426.         $this->setView('filterMobile'$viewVars);
  427.     }
  428.     protected function getMetaNoIndex(): bool
  429.     {
  430.         if (
  431.             strpos(Session::get('url'), 'taxonomy') !== false  ||
  432.             strpos(Session::get('url'), 'Taxonomy') !== false
  433.         ) {
  434.             return true;
  435.         }
  436.         return false;
  437.     }
  438. }