src/WebBundle/Controller/CollectionController.php line 303

Open in your IDE?
  1. <?php
  2. namespace WebBundle\Controller;
  3. use Exception;
  4. use FlexApp\Constant\TimeConstant;
  5. use FlexApp\Helper\SecuriHelper;
  6. use Memcache;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use WebBundle\Constant\CookieKeysConstant;
  12. use WebBundle\Entity\Article;
  13. use WebBundle\Entity\Collection;
  14. use WebBundle\Helper\App;
  15. use WebBundle\Helper\ArticleSettingsHelper;
  16. use WebBundle\Helper\CookieHelper;
  17. use WebBundle\Helper\LocaleHelper;
  18. use WebBundle\Helper\StrHelper;
  19. use WebBundle\Helper\UserHelper;
  20. use WebBundle\Repository\CollectionAlsoViewedRepository;
  21. use WebBundle\Repository\CollectionRepository;
  22. use WebBundle\Repository\FilterRepository;
  23. use WebBundle\Repository\IdeaRepository;
  24. use WebBundle\Service\CollectionService;
  25. /**
  26.  * Class CollectionController
  27.  * @package WebBundle\Controller
  28.  */
  29. class CollectionController extends ExtendedController
  30. {
  31.     /** @required */
  32.     public CollectionService $collectionService;
  33.     private CollectionRepository $collectionRepository;
  34.     private FilterRepository $filterRepository;
  35.     private IdeaRepository $ideaRepository;
  36.     private CollectionAlsoViewedRepository $collectionAlsoViewedRepository;
  37.     public function __construct(
  38.         CollectionRepository $collectionRepository,
  39.         FilterRepository $filterRepository,
  40.         CollectionAlsoViewedRepository $collectionAlsoViewedRepository,
  41.         IdeaRepository $ideaRepository
  42.     ) {
  43.         parent::__construct();
  44.         $this->collectionRepository $collectionRepository;
  45.         $this->filterRepository $filterRepository;
  46.         $this->collectionAlsoViewedRepository $collectionAlsoViewedRepository;
  47.         $this->ideaRepository $ideaRepository;
  48.     }
  49.     /**
  50.      * Для редиректа с неполной ссылки на коллекцию
  51.      * @param string $factoryUrl
  52.      * @return Response
  53.      */
  54.     public function factoryRedirectAction(string $factoryUrl): Response
  55.     {
  56.         return $this->redirect($this->generateUrl('app_catalog', [
  57.             'key'     => StrHelper::toLower($factoryUrl),
  58.             '_locale' => App::getCurLocale(true),
  59.         ]), 301);
  60.     }
  61.     /**
  62.      * Для редиректа кривых ссылок пинтереса
  63.      * @param string $fUrl
  64.      * @param string $cUrl
  65.      * @param string|null $type
  66.      * @param string|null $elId
  67.      * @return RedirectResponse
  68.      * @throws Exception
  69.      */
  70.     public function noTileLinkAction(string $fUrlstring $cUrlstring $type nullstring $elId null): RedirectResponse
  71.     {
  72.         $oServiceColl App::getContainer()->get('app.service.collection');
  73.         $collection $oServiceColl->findCollectionInDb($fUrl$cUrl);
  74.         if ($collection) {
  75.             return $this->redirect($this->generateUrl('app_collection', [
  76.                 '_locale' => App::getCurLocale(true),
  77.                 'factoryUrl' => $fUrl,
  78.                 'collectionUrl' => $cUrl,
  79.                 'type' => $type,
  80.                 'elementId' => $elId,
  81.             ]), 301);
  82.         } else {
  83.             throw $this->createNotFoundException('Not found');
  84.         }
  85.     }
  86.     /**
  87.      * Генерирует список присвоенных коллекции свойств
  88.      * @param $coll
  89.      * @return Response
  90.      * @throws Exception
  91.      */
  92.     public function settingsListAction($coll): Response
  93.     {
  94.         $collection $this->collectionRepository->getCollectionForInformer((int) $coll);
  95.         $settings $this->collectionService->getSettings($collection);
  96.         return new Response($settings);
  97.     }
  98.     /**
  99.      * @param string $key
  100.      * @param array $param
  101.      * @return mixed
  102.      * @throws Exception
  103.      */
  104.     public function transS(string $key, array $param = [])
  105.     {
  106.         return str_replace(
  107.             ['('')''\'''"''`'',''?''!'';'':''\\''/'],
  108.             '',
  109.             App::getTranslator()->trans($key$param'messages''en')
  110.         );
  111.     }
  112.     /**
  113.      * @param int $collectionId
  114.      * @return Response
  115.      * @throws Exception
  116.      */
  117.     function getInformerAction(int $collectionId): Response
  118.     {
  119.         $typeDO true;
  120.         $minPrice 0;
  121.         $collection $this->collectionRepository->getCollectionForInformer($collectionId);
  122.         $settings $this->collectionService->getSettings($collection);
  123.         /** @var Article $article */
  124.         foreach ($collection['articles'] ?? [] as $article) {
  125.             if (!empty($article['measure']) && $article['measure']['id'] == 1) {
  126.                 $typeDO false;
  127.                 break;
  128.             } else {
  129.                 $newMinPrice LocaleHelper::getPrice($article);
  130.                 if ($minPrice $newMinPrice && $newMinPrice != || $minPrice == 0) {
  131.                     $minPrice $newMinPrice;
  132.                 }
  133.             }
  134.         }
  135.         $prMin LocaleHelper::getPrMin($collection);
  136.         if ($typeDO) {
  137.             $price LocaleHelper::floatSign($minPrice) . ' ' LocaleHelper::getCurrency() .
  138.                 '/' $this->get('translator')->trans('measure_unit');
  139.         } else {
  140.             $measure LocaleHelper::measureGb() ? 'measure_ft' 'measure_mq';
  141.             $price LocaleHelper::floatSign($prMin) . ' ' LocaleHelper::getCurrency() .
  142.                 '/' $this->get('translator')->trans($measure);
  143.         }
  144.         $reviews $this->get('app.service.reviews')->getReviewByCollection($collection['code']);
  145.         // проверяем наличие выставок и получаем по ним данные, если есть
  146.         $exhibitions = [];
  147.         if (!empty($collection['exhibition'])) {
  148.             $exhibitions $this->filterRepository->getExhibitions($collection['exhibition']);
  149.         }
  150.         $collection['exhibition'] = $exhibitions;
  151.         return $this->render(
  152.             '@Web/Collection/collection_info.html.twig',
  153.             [
  154.                 'price' => $price,
  155.                 'collection' => $collection,
  156.                 'setting' => $settings,
  157.                 'reviews' => $reviews,
  158.             ]
  159.         );
  160.     }
  161.     /**
  162.      * @param Request $request
  163.      * @return JsonResponse
  164.      * /json/collection/get?unid=BC51EE40C7F24DEFC3257800001E341D&hash=11246411fec5b14b69f109ad2a4b4dff (хеш на дату 12/07/2016)
  165.      * @throws Exception
  166.      */
  167.     public function getAction(Request $request): JsonResponse
  168.     {
  169.         $factoryUnid $request->get('unid');
  170.         if (!SecuriHelper::checkHashPortal($request->get('hash'))) {
  171.             return new JsonResponse(['success' => false]);
  172.         }
  173.         $collections $this->collectionRepository->getCollectionsByFactoryUnid($factoryUnid);
  174.         $res = [];
  175.         /** @var Collection $collection */
  176.         foreach ($collections as $collection) {
  177.             $col = [
  178.                 'id' => $collection->getId(),
  179.                 'name' => $collection->getName(),
  180.                 'unid' => $collection->getUnid(),
  181.                 'code' => $collection->getCode(),
  182.                 'articles' => [],
  183.             ];
  184.             /** @var Article $article */
  185.             foreach ($collection->getArticles() as $article) {
  186.                 $col['articles'][] = [
  187.                     'id' => $article->getId(),
  188.                     'name' => $article->getName(),
  189.                     'code' => $article->getCode(),
  190.                     'size' => $article->getSizeX() . '*' $article->getSizeY(),
  191.                 ];
  192.             }
  193.             $res[] = $col;
  194.         }
  195.         return new JsonResponse(['unid' => $factoryUnid'collections' => $res]);
  196.     }
  197.     /**
  198.      * @param $collectionId
  199.      * @return Response
  200.      * @throws Exception
  201.      */
  202.     public function alsoViewedAction($collectionId): Response
  203.     {
  204.         $session App::getRequest()->getSession();
  205.         if ($session->get('cid') && $session->get('cid') != $collectionId) {
  206.             $this->collectionAlsoViewedRepository->setCollectionAlsoViewed($collectionId$session->get('cid'));
  207.         }
  208.         $session->set('cid'$collectionId);
  209.         $minElem 5;
  210.         $maxElem 16;
  211.         #todo при переходе на sf4 проанализировать и переделать скорее всего с ссылками на коллекции через join
  212.         $ides $this->collectionAlsoViewedRepository->getCollectionAlsoViewed($collectionId);
  213.         $idesCount count($ides);
  214.         if ($idesCount 0) {
  215.             $viewedAlias $this->collectionRepository->getCollectionAlsoViewedNative($ides$maxElem);
  216.         } else {
  217.             $viewedAlias = [];
  218.         }
  219.         if ($idesCount $minElem) {
  220.             $collections $this->collectionRepository->getCollectionAlsoViewedNative(array_merge([$collectionId], $ides), ($minElem count($ides)), 'NOT IN');
  221.             foreach ($collections as $elem) {
  222.                 $ides[] = $elem['id'];
  223.             }
  224.         } else {
  225.             $collections = [];
  226.         }
  227.         $dependentCollections array_merge($viewedAlias$collections);
  228.         // получаем данные по наградам
  229.         foreach ($dependentCollections as $i => $dc) {
  230.             if (!empty($dc['awards'])) {
  231.                 $aAwards $this->filterRepository->getAwards($dc['awards']);
  232.                 if ($aAwards) {
  233.                     $dependentCollections[$i]['awards'] = $aAwards;
  234.                 }
  235.             }
  236.         }
  237.         $favorites $this->ideaRepository->getInteriorsIdeasByToken(UserHelper::getInstance()->getToken());
  238.         return $this->render(
  239.             '@Web/Collection/most_viewed.html.twig',
  240.             [
  241.                 'linkAlsoColl' => str_replace('%26''&'$this->generateUrl(
  242.                     'app_catalog',
  243.                     [
  244.                         '_locale' => App::getCurLocale(true),
  245.                         'key' => 'coll',
  246.                         'subkey' => $collectionId,
  247.                     ]
  248.                 )),
  249.                 'fav' => $favorites,
  250.                 'mostWatchedCollections' => $dependentCollections,
  251.             ]
  252.         );
  253.     }
  254.     /**
  255.      * @param int $id
  256.      * @param int|null $filter
  257.      * @return array|false|string|JsonResponse|Response
  258.      * @throws Exception
  259.      */
  260.     public function articleSettingsListAction(int $id, ?int $filter 0)
  261.     {
  262.         $memKey md5($id $filter '3' App::getCurLocale('true') . CookieHelper::get(CookieKeysConstant::CURRENCY) .
  263.             CookieHelper::get(CookieKeysConstant::MEASURE) . CookieHelper::get(CookieKeysConstant::MEASURE_FT));
  264.         $memcache = new Memcache;
  265.         $memcache->connect(App::getParameter('memcache_host'), App::getParameter('memcache_port'));
  266.         $response $memcache->get($memKey);
  267.         if ($response !== false) {
  268.             return $response;
  269.         }
  270.         $settings ArticleSettingsHelper::getArticleSettings($idnullnullnull, !empty($filter));
  271.         $desc $settings['description'];
  272.         unset($settings['description']);
  273.         if ($filter == 1) {
  274.             $response = new JsonResponse($settings);
  275.         } else {
  276.             unset($settings['filters']);
  277.             $response $this->render(
  278.                 '@Web/Collection/article-setting.html.twig',
  279.                 ['desc' => str_replace('¶'' '$desc), 'settings' => $settings]
  280.             );
  281.         }
  282.         $response->setCache(['no_cache' => true]);
  283.         $memcache->add($memKey$responsefalseTimeConstant::WEEK);
  284.         return $response;
  285.     }
  286. }