<?php
namespace WebBundle\Controller;
use Exception;
use Import1CBundle\Helper\v3\ArticleHelper;
use Symfony\Component\HttpFoundation\Response;
use WebBundle\Entity\Factory;
use WebBundle\Helper\App;
use WebBundle\Helper\UserHelper;
use WebBundle\Repository\BuyOrderRepository;
use WebBundle\Repository\PublicationRepository;
/**
* Search controller.
*/
class SearchController extends ExtendedController
{
private const LIMIT = 30;
/**
* @return Response
* @throws Exception
*/
public function indexAction()
{
ini_set('memory_limit', '3072M');
$query = trim(urldecode(App::getRequest()->get('q')));
$fullTextSearcherService = $this->get('full_text_searcher');
$result = $fullTextSearcherService->search($query);
$result['articles'] = ArticleHelper::getArticlesForSearch($result);
$res = [
'filters' => [
'name' => array_slice($result['filters']['name'], 0, self::LIMIT),
'desc' => array_slice(
$result['filters']['desc'],
0,
self::LIMIT - count(array_slice($result['filters']['name'], 0, self::LIMIT))
),
'nCount' => count($result['filters']['name']),
'dCount' => count($result['filters']['desc']),
],
'publications' => [
'name' => array_slice($result['publications']['name'], 0, self::LIMIT),
'desc' => array_slice(
$result['publications']['desc'],
0,
self::LIMIT - count(array_slice($result['publications']['name'], 0, self::LIMIT))
),
'nCount' => count($result['publications']['name']),
'dCount' => count($result['publications']['desc']),
],
'factory' => [
'name' => array_slice($result['factory']['name'], 0, self::LIMIT),
'desc' => array_slice(
$result['factory']['desc'],
0,
self::LIMIT - count(array_slice($result['factory']['name'], 0, self::LIMIT))
),
'nCount' => count($result['factory']['name']),
'dCount' => count($result['factory']['desc']),
],
'collections' => [
'name' => array_slice($result['collections']['name'], 0, self::LIMIT),
'desc' => array_slice(
$result['collections']['desc'],
0,
self::LIMIT - count(array_slice($result['collections']['name'], 0, self::LIMIT))
),
'nCount' => count($result['collections']['name']),
'dCount' => count($result['collections']['desc']),
],
'articles' => [
'res' => array_slice($result['articles'], 0, self::LIMIT),
'count' => count($result['articles'])
]
];
$nameCount = $res['filters']['nCount'] + $res['factory']['nCount'] + $res['collections']['nCount'] +
$res['articles']['count'] + $res['publications']['nCount'];
$docsCount = $res['filters']['nCount'] + $res['filters']['dCount'] + $res['factory']['nCount'] +
$res['factory']['dCount'] + $res['collections']['nCount'] + $res['collections']['dCount'] +
$res['articles']['count'] + $res['publications']['nCount'] + $res['publications']['dCount'];
$trans = App::getTranslator();
// формируем имя фабрики с префиксом
if ($res['factory']['name']) {
foreach ($res['factory']['name'] as $i => $r) {
$key = Factory::TYPES_KEY[$r['f_type']];
$res['factory']['name'][$i]['f_title'] = $trans->trans($key, ['%factory%' => $r['f_name']]);
}
}
$token = UserHelper::getInstance()->getToken();
/** @var BuyOrderRepository $buyOrderRepo */
$buyOrderRepo = App::getContainer()->get('app.repo.order');
$buyOrderArticle = $buyOrderRepo->getInfoBuyOrderArticle($token);
/** @var PublicationRepository $repoPublication */
$repoPublication = App::getRepository('WebBundle:Publication');
$expressSampleTooltip = null;
if ($estLink = $repoPublication->getUrlBlogExpressSamples()) {
$estTxt = $trans->trans('read_more');
$expressSampleTooltip = [
'url' => $estLink,
'text' => $estTxt,
];
}
$output = [
'nameCount' => $nameCount,
'docsCount' => $docsCount,
'result' => $res,
'query' => $query,
'noSEOLink' => true,
'buyOrderArticle' => $buyOrderArticle,
'expressSampleTooltip' => $expressSampleTooltip,
//'noFloor' => true
];
return $this->render('@Web/Search/show.html.twig', $output);
}
/**
* @return Response
* @throws Exception
*/
public function reactAction()
{
ini_set('memory_limit', '3072M');
$query = trim(urldecode(App::getRequest()->get('q')));
$fullTextSearcherService = $this->get('full_text_searcher');
$result = $fullTextSearcherService->search($query);
$result['articles'] = ArticleHelper::getArticlesForSearch($result);
$res = [
'filters' => [
'name' => array_slice($result['filters']['name'], 0, self::LIMIT),
'desc' => array_slice(
$result['filters']['desc'],
0,
self::LIMIT - count(array_slice($result['filters']['name'], 0, self::LIMIT))
),
'nCount' => count($result['filters']['name']),
'dCount' => count($result['filters']['desc']),
],
'publications' => [
'name' => array_slice($result['publications']['name'], 0, self::LIMIT),
'desc' => array_slice(
$result['publications']['desc'],
0,
self::LIMIT - count(array_slice($result['publications']['name'], 0, self::LIMIT))
),
'nCount' => count($result['publications']['name']),
'dCount' => count($result['publications']['desc']),
],
'factory' => [
'name' => array_slice($result['factory']['name'], 0, self::LIMIT),
'desc' => array_slice(
$result['factory']['desc'],
0,
self::LIMIT - count(array_slice($result['factory']['name'], 0, self::LIMIT))
),
'nCount' => count($result['factory']['name']),
'dCount' => count($result['factory']['desc']),
],
'collections' => [
'name' => array_slice($result['collections']['name'], 0, self::LIMIT),
'desc' => array_slice(
$result['collections']['desc'],
0,
self::LIMIT - count(array_slice($result['collections']['name'], 0, self::LIMIT))
),
'nCount' => count($result['collections']['name']),
'dCount' => count($result['collections']['desc']),
],
'articles' => [
'res' => array_slice($result['articles'], 0, self::LIMIT),
'count' => count($result['articles'])
]
];
$nameCount = $res['filters']['nCount'] + $res['factory']['nCount'] + $res['collections']['nCount'] +
$res['articles']['count'] + $res['publications']['nCount'];
$docsCount = $res['filters']['nCount'] + $res['filters']['dCount'] + $res['factory']['nCount'] +
$res['factory']['dCount'] + $res['collections']['nCount'] + $res['collections']['dCount'] +
$res['articles']['count'] + $res['publications']['nCount'] + $res['publications']['dCount'];
$trans = App::getTranslator();
// формируем имя фабрики с префиксом
if ($res['factory']['name']) {
foreach ($res['factory']['name'] as $i => $r) {
$key = Factory::TYPES_KEY[$r['f_type']];
$res['factory']['name'][$i]['f_title'] = $trans->trans($key, ['%factory%' => $r['f_name']]);
}
}
$token = UserHelper::getInstance()->getToken();
/** @var BuyOrderRepository $buyOrderRepo */
$buyOrderRepo = App::getContainer()->get('app.repo.order');
$buyOrderArticle = $buyOrderRepo->getInfoBuyOrderArticle($token);
/** @var PublicationRepository $repoPublication */
$repoPublication = App::getRepository('WebBundle:Publication');
$expressSampleTooltip = null;
if ($estLink = $repoPublication->getUrlBlogExpressSamples()) {
$estTxt = $trans->trans('read_more');
$expressSampleTooltip = [
'url' => $estLink,
'text' => $estTxt,
];
}
$output = [
'nameCount' => $nameCount,
'docsCount' => $docsCount,
'result' => $res,
'query' => $query,
// 'noSEOLink' => true,
'buyOrderArticle' => $buyOrderArticle,
'expressSampleTooltip' => $expressSampleTooltip,
'trans' => self::trans()
//'noFloor' => true
];
return $this->renderReact('@Web/Search/index.html.twig', [
'title' => App::trans('search_result'),
'initialState' => [
'textSearch' => $output
]
]);
}
/**
* @param string $type [filters|factory|collections|articles|publications]
* @param int $p номерпорции
* @param int $limit
* @return Response
* @throws Exception
*/
public function portionAction($type = 'filter', $p = 1, $limit = self::LIMIT)
{
if (!in_array($type, ['filters', 'factory', 'collections', 'articles', 'publications'])) {
throw new Exception('Not valid type');
}
$trans = App::getTranslator();
$query = trim(urldecode(App::getRequest()->get('q')));
$fullTextSearcherService = $this->get('full_text_searcher');
$result = $fullTextSearcherService->search($query);
if ($type == 'articles') {
$result['articles'] = ArticleHelper::getArticlesForSearch($result);
$res = $result['articles'];
} else {
$res = array_merge($result[$type]['name'], $result[$type]['desc']);
}
// формируем имя фабрики с префиксом
if ($type == 'factory') {
foreach ($res as $i => $r) {
$key = Factory::TYPES_KEY[$r['f_type']];
$res[$i]['f_title'] = $trans->trans($key, ['%factory%' => $r['f_name']]);
}
}
return $this->render('@Web/Search/portion.html.twig', [
'res' => array_slice($res, ($p * $limit - $limit), $limit),
'type' => $type
]);
}
/**
* @return Response
*/
public function suggesterAction()
{
$query = trim(urldecode(App::getRequest()->get('q')));
$fullTextSearcherService = $this->get('full_text_searcher');
$result = $fullTextSearcherService->suggester($query);
return $this->render('@Web/Search/suggester.html.twig', $result);
}
/**
* Переводы для страницы коллекции
* @return array
* @throws Exception
*/
private function trans()
{
/** @var PublicationRepository $repoBlog */
$repoBlog = App::getRepository('WebBundle:Publication');
$translator = App::getTranslator();
return [
'loc' => App::getCurLocale(true),
'lang' => App::getCurLocale(),
'settings' => $translator->trans('collection_marks'),
'added' => $translator->trans('collection_added'),
'star' => $translator->trans('reviews.stars.one'),
'stars' => $translator->trans('reviews.stars.many'),
'reviewShowAll' => $translator->trans('product_review_show_all'),
'catalogMoreDown' => $translator->trans('catalog_more_down'),
'catalogMoreUp' => $translator->trans('catalog_more_up'),
'descCollectionHeader' => $translator->trans('descCollectionHeader'),
'collectionVideos' => $translator->trans('collection.videos'),
'collectionBack' => $translator->trans('collection_back'),
'productReview' => [
$translator->trans('product_review'),
$translator->trans('product_review_'),
$translator->trans('product_reviews')
],
'of' => $translator->trans('of'),
'more' => $translator->trans('show_more'),
'effect' => $translator->trans('left_menu_effect'),
'style' => $translator->trans('left_menu_style'),
'item' => [
'collection' => $translator->trans('collection_name'),
'factory' => $translator->trans('catalog_factory'),
'characteristics' => $translator->trans('article_characteristics'),
'tile' => $translator->trans('footer_tile'),
'item' => $translator->trans('collection_article'),
'header' => $translator->trans('collection_articles_d'),
'formats' => $translator->trans('article_item_formats'),
'cm' => $translator->trans('left_menu_cm'),
'inch' => '″',
'diameter' => $translator->trans('article_item_diameter'),
'pcs' => $translator->trans('buyOrder.th.pcs'),
'sets' => $translator->trans('measure_sets'),
'price' => $translator->trans('catalog_price'),
'vat' => [
'label' => $translator->trans('vat.label'),
'included' => $translator->trans('vat.included'),
'excluded' => $translator->trans('vat.excluded')
],
'info' => $translator->trans('article_item_more_info'),
'infoLess' => $translator->trans('article_item_less_info'),
'order' => [
'help' => $translator->trans('buyOrder.help'),
'ceilToPallet' => $translator->trans('collection_ceil_to_pallet'),
'atentionPallet' => $translator->trans('collection_atention_pallet'),
'popupAutoChangeCountArts' => $translator->trans('popup_auto_change_count_arts'),
],
'up' => $translator->trans('article.price.up'),
'down' => $translator->trans('article.price.down'),
'readMore' => $translator->trans('read_more'),
'titleAddedArticle' => $translator->trans('buyOrder.titleAddedArticle'),
'addIntoOrder' => $translator->trans('buy_order.add_into_order'),
'headerOrderAdd' => $translator->trans('header_order_add'),
'draft' => $translator->trans('order.status.draft'),
'typeMain' => $translator->trans('article.type.main'),
'typeSample' => $translator->trans('article.type.sample'),
'sampleText' => $translator->trans('article.type.sampleText'),
'typeCompare' => $translator->trans('article.type.compare'),
'compareText' => $translator->trans('article.type.compareText'),
'type3d' => $translator->trans('article.type.3d'),
'text3d' => $translator->trans('article.type.3dText'),
'articleOrderOver' => $translator->trans('article.order.over'),
'articleOrderMultiplies' => $translator->trans('article.order.multiplies'),
'articlePricePerPallet' => $translator->trans('article.price.perPallet'),
'articlePriceNoLessPallet' => $translator->trans('article.price.noLessPallet'),
'articlePricePalleteFrom' => $translator->trans('article.pricePallete.from'),
'articlePriceFrom' => $translator->trans('article.price.from'),
'articlePricePalleteUnder' => $translator->trans('article.pricePallete.under'),
'articlePriceFromTo' => $translator->trans('article.price.from_to'),
'articlePriceFromLine' => $translator->trans('catalog_price_from'),
'articlePriceToLine' => $translator->trans('dimensions_to'),
'boxArticleSingle' => $translator->trans('box_article_single'),
'boxArticlePlural' => $translator->trans('box_article_plural'),
'articleNoImgTitle' => $translator->trans('article.no_img'),
'collectionAddOrder' => $translator->trans('collection_add_order'),
'discountinued' => $translator->trans('article.discountinued'),
'tempUnavailable' => $translator->trans('article.temp_unavailable')
],
'sample' => [
'folder' => $translator->trans('sample.folder.text'),
'original' => $translator->trans('sample.original.text'),
'piece' => $translator->trans('sample.piece.text'),
'title' => $translator->trans('samplePopup.title'),
'noMoreShow' => $translator->trans('samplePopup.noMoreShow'),
'withDecl' => $translator->trans('article.type.sampleWithDecl')
],
'searchResultQuery' => $translator->trans('search_result_query'),
'headers' => [
'all' => $translator->trans('all'),
'catalog' => $translator->trans('catalog_catalog'),
'factories' => $translator->trans('left_menu_factories'),
'collections' => $translator->trans('left_menu_collections'),
'items' => $translator->trans('collection_articles_d'),
'blogs' => $translator->trans('blog_blogs')
],
'searchNoResult' => $translator->trans('search_no_result')
];
}
public function googleSearch(): Response
{
return $this->render('@Web/Search/google_search.html.twig');
}
}