src/WebBundle/Controller/CollectionController.php line 240

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\StrHelper;
  18. use WebBundle\Helper\UserHelper;
  19. use WebBundle\Repository\CollectionAlsoViewedRepository;
  20. use WebBundle\Repository\CollectionRepository;
  21. use WebBundle\Repository\FilterRepository;
  22. use WebBundle\Repository\IdeaRepository;
  23. use WebBundle\Service\CollectionService;
  24. use WebBundle\Service\ReviewsService;
  25. class CollectionController extends ExtendedController
  26. {
  27.     /** @required */
  28.     public CollectionService $collectionService;
  29.     /** @required */
  30.     public ReviewsService $reviewsService;
  31.     private CollectionRepository $collectionRepository;
  32.     private FilterRepository $filterRepository;
  33.     private IdeaRepository $ideaRepository;
  34.     private CollectionAlsoViewedRepository $collectionAlsoViewedRepository;
  35.     public function __construct(
  36.         CollectionRepository $collectionRepository,
  37.         FilterRepository $filterRepository,
  38.         CollectionAlsoViewedRepository $collectionAlsoViewedRepository,
  39.         IdeaRepository $ideaRepository
  40.     ) {
  41.         parent::__construct();
  42.         $this->collectionRepository $collectionRepository;
  43.         $this->filterRepository $filterRepository;
  44.         $this->collectionAlsoViewedRepository $collectionAlsoViewedRepository;
  45.         $this->ideaRepository $ideaRepository;
  46.     }
  47.     /**
  48.      * Для редиректа с неполной ссылки на коллекцию
  49.      * @param string $factoryUrl
  50.      * @return Response
  51.      */
  52.     public function factoryRedirectAction(string $factoryUrl): Response
  53.     {
  54.         return $this->redirect($this->generateUrl('app_catalog', [
  55.             'key' => StrHelper::toLower($factoryUrl),
  56.             '_locale' => App::getCurLocale(true),
  57.         ]), 301);
  58.     }
  59.     /**
  60.      * Для редиректа кривых ссылок пинтереса
  61.      * @param string $fUrl
  62.      * @param string $cUrl
  63.      * @param string|null $type
  64.      * @param string|null $elId
  65.      * @return RedirectResponse
  66.      * @throws Exception
  67.      */
  68.     public function noTileLinkAction(string $fUrlstring $cUrlstring $type nullstring $elId null): RedirectResponse
  69.     {
  70.         $oServiceColl App::getContainer()->get('app.service.collection');
  71.         $collection $oServiceColl->findCollectionInDb($fUrl$cUrl);
  72.         if (!$collection) {
  73.             throw $this->createNotFoundException('Not found');
  74.         }
  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.         ]), Response::HTTP_FOUND);
  82.     }
  83.     /**
  84.      * Генерирует список присвоенных коллекции свойств
  85.      * @param $coll
  86.      * @return Response
  87.      * @throws Exception
  88.      */
  89.     public function settingsListAction($coll): Response
  90.     {
  91.         $collection $this->collectionRepository->getCollectionForInformer((int) $coll);
  92.         $settings $this->collectionService->getSettings($collection);
  93.         return new Response($settings);
  94.     }
  95.     /**
  96.      * @param int $collectionId
  97.      * @return Response
  98.      * @throws Exception
  99.      */
  100.     public function getInformerAction(int $collectionId): Response
  101.     {
  102.         $collection $this->collectionService->getCollectionForInformer($collectionId);
  103.         $reviews $this->reviewsService->getReviewByCollection($collection['code']);
  104.         $settings $this->collectionService->getSettings($collection);
  105.         $price $this->collectionService->getPriceByCollection($collection);
  106.         return $this->render(
  107.             '@Web/Collection/collection_info.html.twig',
  108.             [
  109.                 'price' => $price,
  110.                 'collection' => $collection,
  111.                 'setting' => $settings,
  112.                 'reviews' => $reviews,
  113.             ]
  114.         );
  115.     }
  116.     /**
  117.      * @param Request $request
  118.      * @return JsonResponse
  119.      * /json/collection/get?unid=BC51EE40C7F24DEFC3257800001E341D&hash=11246411fec5b14b69f109ad2a4b4dff (хеш на дату 12/07/2016)
  120.      * @throws Exception
  121.      */
  122.     public function getAction(Request $request): JsonResponse
  123.     {
  124.         $factoryUnid $request->get('unid');
  125.         if (!SecuriHelper::checkHashPortal($request->get('hash'))) {
  126.             return new JsonResponse(['success' => false]);
  127.         }
  128.         $collections $this->collectionRepository->getCollectionsByFactoryUnid($factoryUnid);
  129.         $res = [];
  130.         /** @var Collection $collection */
  131.         foreach ($collections as $collection) {
  132.             $col = [
  133.                 'id' => $collection->getId(),
  134.                 'name' => $collection->getName(),
  135.                 'unid' => $collection->getUnid(),
  136.                 'code' => $collection->getCode(),
  137.                 'articles' => [],
  138.             ];
  139.             /** @var Article $article */
  140.             foreach ($collection->getArticles() as $article) {
  141.                 $col['articles'][] = [
  142.                     'id' => $article->getId(),
  143.                     'name' => $article->getName(),
  144.                     'code' => $article->getCode(),
  145.                     'size' => $article->getSizeX() . '*' $article->getSizeY(),
  146.                 ];
  147.             }
  148.             $res[] = $col;
  149.         }
  150.         return new JsonResponse(['unid' => $factoryUnid'collections' => $res]);
  151.     }
  152.     /**
  153.      * @param $collectionId
  154.      * @return Response
  155.      * @throws Exception
  156.      */
  157.     public function alsoViewedAction($collectionId): Response
  158.     {
  159.         $session App::getRequest()->getSession();
  160.         if ($session->get('cid') && $session->get('cid') != $collectionId) {
  161.             $this->collectionAlsoViewedRepository->setCollectionAlsoViewed($collectionId$session->get('cid'));
  162.         }
  163.         $session->set('cid'$collectionId);
  164.         $minElem 5;
  165.         $maxElem 16;
  166.         #todo при переходе на sf4 проанализировать и переделать скорее всего с ссылками на коллекции через join
  167.         $ides $this->collectionAlsoViewedRepository->getCollectionAlsoViewed($collectionId);
  168.         $idesCount count($ides);
  169.         $viewedAlias = [];
  170.         if ($idesCount 0) {
  171.             $viewedAlias $this->collectionRepository->getCollectionAlsoViewedNative($ides$maxElem);
  172.         }
  173.         $collections = [];
  174.         if ($idesCount $minElem) {
  175.             $collections $this->collectionRepository->getCollectionAlsoViewedNative(array_merge([$collectionId], $ides), ($minElem $idesCount), 'NOT IN');
  176.         }
  177.         $dependentCollections array_merge($viewedAlias$collections);
  178.         // получаем данные по наградам
  179.         foreach ($dependentCollections as $i => $dc) {
  180.             if (!empty($dc['awards'])) {
  181.                 $aAwards $this->filterRepository->getAwards($dc['awards']);
  182.                 if ($aAwards) {
  183.                     $dependentCollections[$i]['awards'] = $aAwards;
  184.                 }
  185.             }
  186.         }
  187.         $favorites $this->ideaRepository->getInteriorsIdeasByToken(UserHelper::getInstance()->getToken());
  188.         return $this->render(
  189.             '@Web/Collection/most_viewed.html.twig',
  190.             [
  191.                 'linkAlsoColl' => str_replace('%26''&'$this->generateUrl(
  192.                     'app_catalog',
  193.                     [
  194.                         '_locale' => App::getCurLocale(true),
  195.                         'key' => 'coll',
  196.                         'subkey' => $collectionId,
  197.                     ]
  198.                 )),
  199.                 'fav' => $favorites,
  200.                 'mostWatchedCollections' => $dependentCollections,
  201.             ]
  202.         );
  203.     }
  204.     /**
  205.      * @param int $id
  206.      * @param int|null $filter
  207.      * @return array|false|string|JsonResponse|Response
  208.      * @throws Exception
  209.      */
  210.     public function articleSettingsListAction(int $id, ?int $filter 0)
  211.     {
  212.         $memKey md5($id $filter '3' App::getCurLocale('true') . CookieHelper::get(CookieKeysConstant::CURRENCY) .
  213.             CookieHelper::get(CookieKeysConstant::MEASURE) . CookieHelper::get(CookieKeysConstant::MEASURE_FT));
  214.         $memcache = new Memcache();
  215.         $memcache->connect(App::getParameter('memcache_host'), App::getParameter('memcache_port'));
  216.         $response $memcache->get($memKey);
  217.         if ($response !== false) {
  218.             return $response;
  219.         }
  220.         $settings ArticleSettingsHelper::getArticleSettings($idnullnullnull, !empty($filter));
  221.         unset($settings['description']);
  222.         if ($filter == 1) {
  223.             $response = new JsonResponse($settings);
  224.         } else {
  225.             unset($settings['filters']);
  226.             $response $this->render(
  227.                 '@Web/Collection/article-setting.html.twig',
  228.                 ['settings' => $settings]
  229.             );
  230.         }
  231.         $response->setCache(['no_cache' => true]);
  232.         $memcache->add($memKey$responsefalseTimeConstant::WEEK);
  233.         return $response;
  234.     }
  235. }