<?php
namespace FlexApp\Controller;
use Doctrine\ORM\NonUniqueResultException;
use Exception;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use WebBundle\Helper\App;
use WebBundle\Helper\HumanDataHelper;
use WebBundle\Helper\UserHelper;
use WebBundle\Repository\VisitRepository;
use OpenApi\Annotations as OA;
use Nelmio\ApiDocBundle\Annotation\Model;
use Symfony\Component\Routing\Annotation\Route;
use WebBundle\Controller\ExtendedController;
use FlexApp\OpenApi\Schemas\HistoryCollection;
/**
* Visit controller.
*/
class VisitController extends ExtendedController
{
/**
* Репозиторий посещений
*
* @var \WebBundle\Repository\VisitRepository $visitRepository
*/
private $visitRepository;
public function __construct()
{
$this->visitRepository = App::getRepository('WebBundle:Visit'); // TODO: переделать после переноса репозитория во Flex
}
/**
* @Route("/{_locale}/history/{page}",
* methods={"GET"},
* name="visit",
* requirements={"_locale"="%lang_country%", "page"="\d+"}
* )
* @OA\Response(
* response=200,
* description="ОК. Список страниц, посещеных пользователем.",
* @OA\JsonContent(
* type="object",
* @OA\Property(property="page", description="текущая страница", example="1"),
* @OA\Property(property="list", type="array", description="Список коллекций", @OA\Items(ref=@Model(type=HistoryCollection::class)))
* )
* )
* @OA\Parameter(
* name="_locale",
* in="path",
* required=true,
* description="локаль в которой надо отдать результат",
* @OA\Schema(type="string")
* )
* @OA\Parameter(
* name="page",
* in="path",
* description="Страница пагинации. Для page=1 вернется страница HTML",
* @OA\Schema(type="integer")
* )
* @OA\Tag(name="История")
*
* @param int $page
* @return Response
* @throws NonUniqueResultException
* @throws Exception
*/
public function indexAction($page = 1)
{
$limit = 30;
$offset = $limit * ($page - 1);
/** @var VisitRepository $repoVisit */
$userToken = UserHelper::getInstance()->getToken();
$list = $this->visitRepository->getVisited($userToken, $limit, $offset);
$list = array_map(function($item) {
$item['relativeVisitDate'] = HumanDataHelper::get($item['visitDate']->getTimestamp());
$item['visitDate'] = $item['visitDate']->format('c');
return $item;
}, $list);
if ($page == 1) {
$visitedCount = $this->visitRepository->getVisitedAll($userToken);
$res = $this->renderReact('@Web/History/index.html.twig',
[
'initialState' => [
'history' => [
'visitedCount' => $visitedCount,
'page' => $page,
'list' => $list,
'trans' => [
'titleHistory' => App::trans('header_history'),
'emptyHistory' => App::trans('your_history_empty'),
]
]
]
]
);
} else {
$data = [
'page' => $page,
'list' => $list,
];
$res = $this->json($data);
}
return $res;
}
/**
* @Route("/{_locale}/last-history",
* methods={"GET"},
* name="app_visit_last",
* requirements={"_locale"="%lang_country%"}
* )
*
* @return Response
* @throws Exception
*/
public function lastAction()
{
return $this->render(
'@Web/Visit/last.html.twig',
[
'visited' => $this->visitRepository->getVisited(UserHelper::getInstance()->getToken(), 500, 0),
]
);
}
/**
* @Route("/{_locale}/history/save",
* methods={"POST"},
* name="app_visit_save",
* requirements={"_locale"="%lang_country%"}
* )
* @return Response
* @throws Exception
*/
public function saveAction()
{
$data = App::getRequest()->get('data');
if ($data && !empty($data['url']) && !empty($data['title'])) {
$icon = !empty($data['icon']) ? $data['icon'] : null;
try {
$vType = !empty($data['vType'])?$data['vType']:0;
$vObjectId = !empty($data['vObjectId'])?$data['vObjectId']:0;
$this->visitRepository->setVisit(
$data['title'],
$icon,
$data['url'],
null,
$vType,
$vObjectId
);
return $this->render(
'@Web/Visit/last.html.twig',
[
'visited' => $this->visitRepository->getVisited(UserHelper::getInstance()->getToken(), 100),
]
);
} catch (Exception $e) {
App::getLogger()->error(
'saveAction: ' . $e->getMessage()
);
return new JsonResponse(['err' => 'Error setVisit']);
}
} else {
return new JsonResponse(['err' => 'Data error']);
}
}
}