src/FlexApp/Controller/VisitController.php line 96

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