<?php
use App\Application\Service\Taxonomy\TaxonomyBreadcrumbService;
use App\Application\Service\Taxonomy\TaxonomyService;
use App\Application\Service\Taxonomy\TaxonomyTreeService;
use App\Entity\System\Language;
use App\Entity\System\Tag;
use App\Entity\System\TaxonomyLanguage;
use App\Manager\System\LanguageManager;
use App\Manager\System\TaxonomyLanguageManager;
use App\Manager\System\TaxonomyManager;
use App\Manager\System\TaxonomyStatisticManager;
use App\ViewManager\Landing\ViewService;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
class taxonomyController extends Controller
{
const CATEGORY_COMPLETE_CATALOG_ID = 2202;
public const AJAX_SHOW_PRODUCTS_TEMPLATE_LIST = 1;
public const MAX_IMAGE_ITERATIONS = 5;
/** @var TaxonomyService */
private $taxonomyService;
/** @var LanguageManager */
private $languageManager;
/** @var TaxonomyLanguageManager */
private $taxonomyLanguageManager;
/** @var int */
private $idFromCoreUrl;
/** @var TaxonomyStatisticManager */
private $taxonomyStatisticManager;
/** @var TaxonomyBreadcrumbService */
private $taxonomyBreadcrumbService;
/** @var TaxonomyTreeService */
private $taxonomyTreeService;
/** @var ViewService */
private $viewService;
/** @var TaxonomyManager */
private $taxonomyManager;
/** @var Environment */
private $twig;
public function __construct($id = null)
{
parent::__construct('Taxonomy', $id);
$this->idFromCoreUrl = $id;
$this->model = 'Taxonomy';
$this->taxonomyService = Tools::getSfService(TaxonomyService::class);
$this->taxonomyLanguageManager = Tools::getSfService(TaxonomyLanguageManager::class);
$this->taxonomyStatisticManager = Tools::getSfService(TaxonomyStatisticManager::class);
$this->taxonomyBreadcrumbService = Tools::getSfService(TaxonomyBreadcrumbService::class);
$this->languageManager = Tools::getSfService(LanguageManager::class);
$this->taxonomyTreeService = Tools::getSfService(TaxonomyTreeService::class);
$this->viewService = Tools::getSfService(ViewService::class);
$this->taxonomyManager = Tools::getSfService(TaxonomyManager::class);
$this->twig = Tools::getSfService('twig');
}
public function index()
{
}
/**
* @param int $taxonomyId
* @return void
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
* @throws \Psr\Cache\InvalidArgumentException
*/
public function view(int $taxonomyId): void
{
if ($taxonomyId === (int)Configuration::get('TAXONOMY_ALL_CATALOG')) {
$this->showLandingSeeAll();
return;
}
$this->showTaxonomy($taxonomyId);
}
/**
* @param int $taxonomyId
* @return void
* @throws \Psr\Cache\InvalidArgumentException
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function showTaxonomy(int $taxonomyId): void
{
if (!Tools::getValue('filters') && Tools::getValue('product_type')) {
$_GET['filters'] = [];
$_GET['filters']['product_type'] = [Tools::getValue('product_type')];
} elseif (isset($_SESSION['filters']['product_type']) && !Tools::getValue('product_type')) {
unset($_SESSION['filters']['product_type']);
}
if (!$taxonomyId) {
return;
}
$languageId = (int)Session::get('id_lang');
$language = \Session::get('lang');
$isoCode = \Language::getIsoById($languageId);
$numReferences = 0;
$taxonomy = $this->taxonomyManager->findOneById($taxonomyId);
if (!$taxonomy) {
return;
}
$taxonomyLang = $this->taxonomyLanguageManager->getAvailableByTaxonomyAndLanguage($taxonomy->getId(), $languageId);
if (!$taxonomyLang) {
return;
}
$allTaxonomiesForLanguageId = $this->taxonomyService->getAllCachedTaxonomiesForLanguage($languageId);
$breadCrumb = $this->taxonomyBreadcrumbService->getLegacyBreadCrumb($taxonomy, $languageId);
$taxonomyTree = $this->taxonomyTreeService->getLegacyTree($allTaxonomiesForLanguageId, $language, $breadCrumb);
$sections = $breadCrumb;
$taxonomyChildren = $taxonomyTree['selcat_childs'];
$hasChildren = !empty($taxonomyChildren);
$parentTaxonomyName = null;
if (!$hasChildren) {
$parentTaxonomyInfo = $taxonomy->getParent() !== null
? $allTaxonomiesForLanguageId[$taxonomy->getParent()->getId()] : null;
if ($parentTaxonomyInfo) {
$taxonomyChildren = $this->taxonomyTreeService->getLegacyTree($allTaxonomiesForLanguageId, $language, [$parentTaxonomyInfo])['selcat_childs'];
$parentTaxonomyName = $parentTaxonomyInfo['name'];
}
}
$viewFilters = $this->viewService->getFiltersByElasticSearchResults($taxonomyId, $this->model, ViewService::VIEW_TYPE_TAXONOMY);
if (0 === $viewFilters->getNumProducts() && !$this->ajax) {
$parent = \array_slice($breadCrumb, -2, 1)[0];
Tools::redirect($parent['link']);
}
$hrefLink = BASE_URL . $taxonomyLang['linkRewrite'] . '.html';
$method = 'taxonomy/ajax_show_taxonomy_products';
$paginatorData = $this->viewService->getPaginatorDataEncoded($hrefLink, $method, $viewFilters, $this->idFromCoreUrl);
$jsParameters = $this->viewService->getParameters($taxonomyId, 0, $viewFilters);
$codeJs = Tools::createCategoryFilters($jsParameters);
$this->addJs($this->viewService->addJs($paginatorData, $codeJs, $taxonomyId, ViewService::VIEW_JS_TYPE_TAXONOMY));
$this->metaNoindex = $this->getMetaNoIndex();
if ($viewFilters->getAjax()) {
$this->setDisplayFooter(false);
$this->setDisplayHeadAndHeader(false);
$this->setJsonArgs($this->viewService->setJsonArguments($viewFilters));
$this->setJsonResponse(true);
if (empty($viewFilters->getCategoryProducts())) {
$viewVars['_partials0'] = 'not_display';
} else {
$viewVars['_partials0'] = $viewFilters->getCategoryProducts();
}
$this->setView('ajax-product-list', $viewVars);
} else {
$taxonomyImage = isset($taxonomyTree['selected_taxonomy_image']['name']) ?
$taxonomyId .'-home/' . $taxonomyTree['selected_taxonomy_image']['name'] : null;
$viewVars = $this->viewService->generateViewVars
(
$sections,
$taxonomyTree,
$taxonomyLang['name'],
$taxonomyLang['description'],
$taxonomyId,
$taxonomyChildren,
$hasChildren,
$numReferences,
false,
$viewFilters,
$taxonomyImage,
$parentTaxonomyName,
false,
true,
null,
true
);
$viewVars['redirect'] = BASE_URL . 'taxonomy/filterTaxonomyMobile/' . (string)$taxonomyId;
$viewVars['domain'] = 'messages';
$viewVars['languageIsoCode'] = $isoCode;
$viewVars['tag_data'] = [
'blackfriday_id' => Tag::BLACK_FRIDAY_TAG_ID,
'blackfriday_text' => Tag::TAG_BLACK_FRIDAY_TEXT,
'hotdeals_id' => Tag::HOTDEAL_TAG_ID,
'hotdeals_text' => Tag::TAG_HOTDEALS_TEXT
];
$viewVars['rendered_twig_template'] = $this->twig->render($this->viewService->getTwigName(), $viewVars);
$this->setView('taxonomyList', $viewVars);
}
}
/**
* CatƔlogo completo
*/
public function showLandingSeeAll()
{
$isoCode = Session::get('lang');
$langId = (int)Session::get('id_lang');
$category = $this->getCategoryCompleteCatalog();
$allTaxonomyByLanguage = $this->taxonomyService->getAllCachedTaxonomiesForLanguage($langId);
$breadcrumb = $this->taxonomyBreadcrumbService->getLegacyBreadCrumb(null, $langId);
$taxonomyTree = $this->taxonomyTreeService->getLegacyTree($allTaxonomyByLanguage, $isoCode, []);
$taxonomyChilds = $this->processChilds($taxonomyTree['_layout0_Array']);
$this->metaNoindex = $this->getMetaNoIndex();
$numProductsTotal = 0;
foreach ($taxonomyChilds as $taxonomyChild) {
$numProductsTotal += $taxonomyChild['numProducts'];
}
$imageTaxonomy = $this->getRandomImageTaxonomy($taxonomyChilds);
$vars = [
'_partials0' => [
[
'sections' => $breadcrumb,
]
],
'_layout1' => [
'desc_category_extra' => '',
'_layout0_Array' => $taxonomyTree['_layout0_Array']
],
'_layout2' => [
'cat' => $imageTaxonomy,
'childs' => $taxonomyChilds,
'name_category' => str_replace('|','',$category->name[$langId]),
'desc_category' => Tools::replaceAutomatedLinks($category->description[$langId]),
'num_products' => $numProductsTotal,
'excludedCategories' => [2662, 2672, 2678]
]
];
$this->setView('seeAllTaxonomy', $vars);
}
private function getCategoryCompleteCatalog(): Category
{
return new Category(self::CATEGORY_COMPLETE_CATALOG_ID);
}
protected function processChilds($taxonomies)
{
$taxonomyStatistics = $this->taxonomyStatisticManager->getAllParentTaxonomyStatistics($taxonomies);
foreach ($taxonomyStatistics as $taxonomyStatistic) {
$taxonomies[$taxonomyStatistic['taxonomyId']]['numProducts'] = (!empty($taxonomyStatistic['numProducts'])) ? $taxonomyStatistic['numProducts'] : 0;
}
return $taxonomies;
}
/**
* @param int $taxonomyId
* @throws \Psr\Cache\InvalidArgumentException
*/
public function landingpage(int $taxonomyId): void
{
$isoCode = Session::get('lang');
/** @var Language $language */
$language = $this->languageManager->findOneBy(['isoCode' => $isoCode]);
/** @var TaxonomyLanguage $taxonomyLanguage */
$taxonomyLanguage = $this->taxonomyLanguageManager->findOneBy([
'language' => $language,
'taxonomy' => $taxonomyId,
]);
$taxonomy = $taxonomyLanguage->getTaxonomy();
$sections = $this->taxonomyBreadcrumbService->getLegacyBreadCrumb($taxonomy, $language->getId());
$homeTaxonomy = App\Entity\System\Category::HOME_ID;
$taxonomyTree = $this->taxonomyTreeService->getLegacyTree
(
$this->taxonomyService->getAllCachedTaxonomiesForLanguage($language->getId()),
$isoCode,
$sections
);
$taxonomyChilds = $taxonomyTree['selcat_childs'];
$mainTaxonomy = ($taxonomy->getId() === $homeTaxonomy);
$taxonomyAux = [];
foreach ($taxonomyTree['_layout0_Array'] as $taxonomyValue) {
if ($taxonomyValue['id'] !== $taxonomy->getId()) {
$taxonomyAux[] = $taxonomyValue;
} else {
array_unshift($taxonomyAux, $taxonomyValue);
}
}
$taxonomyTree['_layout0_Array'] = $taxonomyAux;
$mainavLinks = Category_mainav_link::getIdLinks($taxonomy->getId());
if ($mainavLinks) {
$this->category_mainav = $taxonomy->getId();
$this->category_mainav_links = $mainavLinks;
} else if ($mainTaxonomy) {
$this->category_mainav = $taxonomy->getId();
$this->category_mainav_links = [];
}
$this->_object->canonical = Link::getTaxonomyLink($taxonomyId);
$this->addJs([
['value' => 'public/js/tools.js?t=201606071238'],
['value' => 'public/js/categorylandingpage.js?t=201606071238'],
['value' => 'var filterMobile=0;',
'code' => true]
]);
$vars = [];
$vars['categMenuList'] = [];
$vars['_partials0'] = [
['sections' => $sections]
];
$vars['_layout1'] = [];
$vars['_layout1']['desc_category_extra'] = null;
$vars['_layout1']['_layout0_Array'] = $taxonomyTree['_layout0_Array'];
$vars['_layout2'] = [];
$vars['_layout2']['name_category'] = str_replace('|','', $taxonomyLanguage->getName());
$vars['_layout2']['desc_category'] = Tools::replaceAutomatedLinks($taxonomyLanguage->getDescription());
$vars['_layout2']['cat'] = $taxonomy->getId();
$vars['_layout2']['link_rewrite'] = $taxonomyLanguage->getLinkRewrite();
$vars['_layout2']['is_exception'] = in_array($taxonomy->getId(), Tools::jsonDecode(Configuration::get('CATEGORY_TEXT_EXCEPTION')), true);
$vars['_layout2']['_partials0'] = $taxonomyChilds;
$vars['_layout2']['childs'] = $taxonomyChilds;
$vars['_layout2']['childs_num_row'] = 3;
$countCategChild = count($taxonomyChilds);
for ($nRow = 4; $nRow <= 5; $nRow++) {
if (($countCategChild % $nRow) === 0) {
$vars['_layout2']['childs_num_row'] = $nRow;
}
}
if(Tools::isMobile()){
$categoryBread = array_slice($sections, -1, 1);
$vars['_partials0'][0]['name'] = $categoryBread[0]['name'];
$vars['_partials0'][0]['arrow'] = false;
$vars['_partials0'][0]['linkCategory'] = false;
$vars['_layout1'] = 'not_display';
$this->addJs([
['value' => 'var filterMobile=0;',
'code' => true]
]);
}
$this->setView('taxonomyLandingPage', $vars);
}
private function getRandomImageTaxonomy($taxonomyChilds): ?string
{
$i = 0;
do {
$taxonomyId = array_rand($taxonomyChilds);
$taxonomy = $taxonomyChilds[$taxonomyId];
$i++;
} while (null === $taxonomy['image']['id'] && $i < self::MAX_IMAGE_ITERATIONS);
return $taxonomy['image']['id'] . '-home/' . $taxonomy['image']['name'] . '.jpg';
}
/**
* @return void
*/
public function ajax_show_taxonomy_products(): void
{
$init = Tools::getValue('init');
$limit = Tools::getValue('limit');
$filters = Tools::getValue('filters', array());
$type = Tools::getValue('type');
$isMobile = Tools::getValue('mobile');
//Guardamos los filtros en sesiĆ³n para poder
Session::set('filters', $filters);
if (isset($filters['category']) && count($filters['category']) > 0 ) {
$filters ['taxonomy'] = $filters['category'];
unset($filters['category']);
} else {
$filters ['taxonomy'] = [ Tools::getValue('args') ];
}
$results = Tools::getResultsElasticSearch(
Session::get('lang'),
$init,
$limit,
$this->ajax,
$filters,
Session::get('is_wholesaler')
);
$numberProducts = $results['total_results'];
$resultProds = $results['result'];
if ($numberProducts === false) {
$numberProducts = 0;
}
$nextInit = $init + $limit;
$nextCallNum = $limit;
if ($numberProducts - $nextInit < $limit) {
$nextCallNum = $numberProducts - $nextInit;
}
$show = true;
if ($numberProducts <= $nextInit) {
$show = false;
}
$this->setDisplayFooter(false);
$this->setDisplayHeadAndHeader(false);
$numProductsPaginator = ($numberProducts > tools::MAX_LIST_PRODUCTS) ? tools::MAX_LIST_PRODUCTS : $numberProducts;
if ($isMobile) {
$this->setJsonArgs(['num_products' => $numberProducts]);
$this->setJsonResponse(true);
} else {
$this->setJsonArgs([
'init' => $nextInit,
'show' => $show,
'next_call_num' => $nextCallNum,
'num_products' => $numberProducts,
'num_products_paginator' => $numProductsPaginator,
'ga_productlist' => []
]);
$this->setJsonResponse(true);
if (empty($resultProds)) {
$resultProds = 'not_display';
}
if ($type === self::AJAX_SHOW_PRODUCTS_TEMPLATE_LIST) {
$vars = [
'_partials0' => $resultProds
];
$this->setView('ajax-product-list', $vars);
} else {
$vars = [
'_layout0' => [
'_partials0' => $resultProds
]
];
$this->setView('ajax-product-list-sliders', $vars, 'ajax-product-list-full');
}
}
}
/**
* @param int $taxonomyId
* @return void
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
*/
public function filterTaxonomyMobile(int $taxonomyId): void
{
$languageId = (int)Session::get('id_lang');
$isoCode = \Language::getIsoById($languageId);
$viewFilters = $this->viewService->getFiltersByElasticSearchResults($taxonomyId, $this->model, ViewService::VIEW_TYPE_TAXONOMY);
$linkRewrite = Link::getTaxonomyLink($taxonomyId, Session::get('id_lang'));
$href = BASE_URL . 'taxonomy/ajax_show_taxonomy_products/'.$taxonomyId;
$jsParameters = $this->viewService->getParametersToFilterMobile($taxonomyId, $viewFilters, $linkRewrite, $href);
$codeJs = Tools::createCategoryFilters($jsParameters);
$this->addJs($this->viewService->addFilterMobileJs($codeJs));
$viewVars = $this->viewService->generateVarsMobileFilter($viewFilters);
$this->headerClass = 'fullContent';
$viewVars['redirect'] = $linkRewrite;
$viewVars['domain'] = 'messages';
$viewVars['languageIsoCode'] = $isoCode;
$viewVars['rendered_twig_template'] = $this->twig->render('front/shop_b2b/mobile/filter_mobile.html.twig', $viewVars);
$this->setView('filterMobile', $viewVars);
}
protected function getMetaNoIndex(): bool
{
if (
strpos(Session::get('url'), 'taxonomy') !== false ||
strpos(Session::get('url'), 'Taxonomy') !== false
) {
return true;
}
return false;
}
}