src/FlexApp/Controller/VisitController.php line 147

Open in your IDE?
  1. <?php
  2. namespace FlexApp\Controller;
  3. use Doctrine\ORM\NonUniqueResultException;
  4. use Exception;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use WebBundle\Helper\App;
  8. use WebBundle\Helper\HumanDataHelper;
  9. use WebBundle\Helper\UserHelper;
  10. use WebBundle\Repository\VisitRepository;
  11. use OpenApi\Annotations as OA;
  12. use Nelmio\ApiDocBundle\Annotation\Model;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use WebBundle\Controller\ExtendedController;
  15. use FlexApp\OpenApi\Schemas\HistoryCollection;
  16. /**
  17.  * Visit controller.
  18.  */
  19. class VisitController extends ExtendedController
  20. {
  21.     /**
  22.      * Репозиторий посещений
  23.      *
  24.      * @var \WebBundle\Repository\VisitRepository $visitRepository
  25.      */
  26.     private $visitRepository;
  27.     public function __construct()
  28.     {
  29.         parent::__construct();
  30.         $this->visitRepository App::getRepository(
  31.             'WebBundle:Visit'
  32.         ); // TODO: переделать после переноса репозитория во Flex
  33.     }
  34.     /**
  35.      * @Route("/{_locale}/history/{page}",
  36.      *         methods={"GET"},
  37.      *         name="visit",
  38.      *         requirements={"_locale"="%lang_country%", "page"="\d+"}
  39.      * )
  40.      * @OA\Response(
  41.      *     response=200,
  42.      *     description="ОК. Список страниц, посещеных пользователем.",
  43.      *     @OA\JsonContent(
  44.      *          type="object",
  45.      *          @OA\Property(property="page", description="текущая страница", example="1"),
  46.      *          @OA\Property(property="list", type="array", description="Список коллекций", @OA\Items(ref=@Model(type=HistoryCollection::class)))
  47.      *     )
  48.      * )
  49.      * @OA\Parameter(
  50.      *      name="_locale",
  51.      *      in="path",
  52.      *      required=true,
  53.      *      description="локаль в которой надо отдать результат",
  54.      *      @OA\Schema(type="string")
  55.      * )
  56.      * @OA\Parameter(
  57.      *      name="page",
  58.      *      in="path",
  59.      *      description="Страница пагинации. Для page=1 вернется страница HTML",
  60.      *      @OA\Schema(type="integer")
  61.      * )
  62.      * @OA\Tag(name="История")
  63.      *
  64.      * @param int $page
  65.      * @return Response
  66.      * @throws NonUniqueResultException
  67.      * @throws Exception
  68.      */
  69.     public function indexAction($page 1)
  70.     {
  71.         $limit 30;
  72.         $offset $limit * ($page 1);
  73.         /** @var VisitRepository $repoVisit */
  74.         $userToken UserHelper::getInstance()->getToken();
  75.         $list $this->visitRepository->getVisited($userToken$limit$offset);
  76.         $list array_map(function ($item) {
  77.             $item['relativeVisitDate'] = HumanDataHelper::get($item['visitDate']->getTimestamp());
  78.             $item['visitDate'] = $item['visitDate']->format('c');
  79.             return $item;
  80.         }, $list);
  81.         if ($page == 1) {
  82.             $visitedCount $this->visitRepository->getVisitedAll($userToken);
  83.             $res $this->renderReact(
  84.                 '@Web/History/index.html.twig',
  85.                 [
  86.                     'initialState' => [
  87.                         'history' => [
  88.                             'visitedCount' => $visitedCount,
  89.                             'page' => $page,
  90.                             'list' => $list,
  91.                             'trans' => [
  92.                                 'titleHistory' => App::trans('header_history'),
  93.                                 'emptyHistory' => App::trans('your_history_empty'),
  94.                             ]
  95.                         ]
  96.                     ]
  97.                 ]
  98.             );
  99.         } else {
  100.             $data = [
  101.                 'page' => $page,
  102.                 'list' => $list,
  103.             ];
  104.             $res $this->json($data);
  105.         }
  106.         return $res;
  107.     }
  108.     /**
  109.      * @Route("/{_locale}/last-history",
  110.      *         methods={"GET"},
  111.      *         name="app_visit_last",
  112.      *         requirements={"_locale"="%lang_country%"}
  113.      * )
  114.      *
  115.      * @return Response
  116.      * @throws Exception
  117.      */
  118.     public function lastAction()
  119.     {
  120.         return $this->render(
  121.             '@Web/Visit/last.html.twig',
  122.             [
  123.                 'visited' => $this->visitRepository->getVisited(UserHelper::getInstance()->getToken(), 5000),
  124.             ]
  125.         );
  126.     }
  127.     /**
  128.      * @Route("/{_locale}/history/save",
  129.      *         methods={"POST", "GET"},
  130.      *         name="app_visit_save",
  131.      *         requirements={"_locale"="%lang_country%"}
  132.      * )
  133.      * @return Response
  134.      * @throws Exception
  135.      */
  136.     public function saveAction()
  137.     {
  138.         if (App::getRequest()->isMethod('GET')) {
  139.             return $this->redirectToRoute('visit' );
  140.         }
  141.         $data App::getRequest()->get('data');
  142.         if ($data && !empty($data['url']) && !empty($data['title'])) {
  143.             $icon = !empty($data['icon']) ? $data['icon'] : null;
  144.             try {
  145.                 $vType = !empty($data['vType']) ? $data['vType'] : 0;
  146.                 $vObjectId = !empty($data['vObjectId']) ? $data['vObjectId'] : 0;
  147.                 $this->visitRepository->setVisit(
  148.                     $data['title'],
  149.                     $icon,
  150.                     $data['url'],
  151.                     null,
  152.                     $vType,
  153.                     $vObjectId
  154.                 );
  155.                 return $this->render(
  156.                     '@Web/Visit/last.html.twig',
  157.                     [
  158.                         'visited' => $this->visitRepository->getVisited(UserHelper::getInstance()->getToken(), 100),
  159.                     ]
  160.                 );
  161.             } catch (Exception $e) {
  162.                 App::getLogger()->error(
  163.                     'saveAction: ' $e->getMessage()
  164.                 );
  165.                 return new JsonResponse(['err' => 'Error setVisit']);
  166.             }
  167.         } else {
  168.             return new JsonResponse(['err' => 'Data error']);
  169.         }
  170.     }
  171. }