<?php
/**
* Created by PhpStorm.
* User: ThanhDT
* Date: 12/4/2018
* Time: 9:18 AM
*/
namespace App\Controller\Tag;
use App\Controller\BaseController;
use App\Entity\PostPublishes;
use App\Entity\GroupBoxItems;
use App\Entity\Tags;
use App\Service\DataExchange;
use App\Service\Category;
use App\Service\CryptUtils;
use App\Service\ElasticSearch;
use App\Utils\Constants;
use App\Utils\Lib;
use Symfony\Component\HttpFoundation\Request;
class IndexController extends BaseController
{
/**
* tag detail action
*
* @param string $slug The slug
* @param DataExchange $exchangeService The exchange service
* @param Category $category The category
* @param CryptUtils $cryptUtils The crypt utilities
* @param Elasticsearch $elasticSearch The elastic search
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function detailAction(
$slug,
Request $request,
DataExchange $exchangeService,
CryptUtils $cryptUtils,
Elasticsearch $elasticSearch
)
{
$cacheService = $this->getCacheProvider(Constants::SERVER_CACHE_ARTICLE);
$tagData = $this->getTagDataBySlug($cacheService, $exchangeService, $slug);
if (!$tagData) {
throw $this->createNotFoundException('The requested tag could not be found');
// return $this->createNotFoundWithContent($slug, $elasticSearch, $exchangeService, $cryptUtils);
}
$ignoreIds = [];
$limit = Constants::SEARCH_POST_LIMIT;
list($posts, $loadMoreParams) = $this->getTagPosts(
$cacheService,
$exchangeService,
$cryptUtils,
$tagData['tagId'],
0,
($limit + 1),
0,
0,
$ignoreIds
);
$mostViewPosts = $this->getMostViewPosts($cacheService, $exchangeService);
$seo = $tagData['seo'];
$seo['title'] = "Tag - " . $slug;
$viewParams = [
'tag' => $tagData,
'seo' => $seo,
'posts' => $posts,
'loadMoreParams' => $loadMoreParams,
'cateId' => 0,
'cateSlug' => 'blog',
'mostViewPosts' => $mostViewPosts,
'totalPosts' => $tagData['postCount'],
'keyword' => $slug,
'searchType' => 'Tag',
];
return $this->renderHtml('search/tag.html.twig', $viewParams);
}//end detailAction()
/**
* Gets the tag posts.
*
* @param mixed $cacheService The cache service
* @param DataExchange $exchangeService The exchange service
* @param int $tagId The tag identifier
* @param int $limit The limit
* @param int $timeStamp The time stamp
* @param int $lastId The last identifier
* @param array $ignoreIds The ignore identifiers
*
* @return array The tag posts.
*/
protected function getTagPosts(
$cacheService,
$exchangeService,
CryptUtils $cryptUtils,
$tagId,
$page,
$limit,
$timeStamp,
$lastId,
$ignoreIds
)
{
$cacheKey = sprintf(Constants::CACHE_TAG_POST, $tagId, $timeStamp, $lastId, $limit);
if (($cacheData = $cacheService->get($cacheKey)) === false) {
$em = $this->getDoctrine()->getManager();
$posts = $em->getRepository(PostPublishes::class)->getPostsByTagId($tagId, $limit, $timeStamp, $lastId, $ignoreIds);
$postsData = [];
if ($posts) {
foreach ($posts as $post) {
$postsData[] = $exchangeService->exchangeRowPost(
$post,
Constants::LIST_RESULT_POSTS_IMAGE_SIZE,
null,
null,
Constants::LIST_RESULT_POSTS_IMAGE_MOBILE_SIZE
);
array_push($ignoreIds, $post['postId']);
}
$loadMoreParams = $this->getLoadMoreParams($cryptUtils, $tagId, $postsData, $page, ($limit - 1), $ignoreIds);
$postsData = Lib::groupItemByLength($postsData, Constants::FIRST_LIMIT_POST_LIST);
$cacheData = [$postsData, $loadMoreParams];
} else {
$cacheData = [false, false];
}//end if
$cacheService->set($cacheKey, $cacheData, $this->getParameter('cache_time')['hour']);
}//end if
return $cacheData;
}//end getTagPosts()
/**
* Gets the load more parameters.
*
* @param CryptUtils $cryptUtils The crypt utilities
* @param int $tagId The tag identifier
* @param array $posts The posts
* @param int $page The page offset
* @param int $limit The limit
* @param array $ignoreIds The ignore identifiers
*
* @return array|bool The load more parameters.
*/
protected function getLoadMoreParams(CryptUtils $cryptUtils, $tagId, &$posts, $page, $limit, $ignoreIds)
{
$loadMoreParams = false;
if ($posts && count($posts) > $limit) {
array_pop($posts);
array_pop($ignoreIds);
$lastPost = end($posts);
$loadMoreToken = [
'tagId' => $tagId,
'lastId' => $lastPost['id'],
'lastTimestamp' => $lastPost['publishedTimestamp'],
'page' => $page + 1,
'ignoreIds' => $ignoreIds
];
$loadMoreToken = $cryptUtils->encrypt(json_encode($loadMoreToken));
$loadMoreParams = [
'url' => $this->generateUrl('ajax_get_tag_posts'),
'token' => $loadMoreToken,
'fullUrl' => $this->getParameter('domain') . $this->generateUrl(
"ajax_get_tag_posts",
[
'token' => $loadMoreToken,
'is_amp' => 1
]
),
];
reset($posts);
}//end if
return $loadMoreParams;
}//end getLoadMoreParams()
/**
* Gets the tag data by slug.
*
* @param mixed $cacheService The cache service
* @param DataExchange $exchangeService The exchange service
* @param string $slug The tag slug
*
* @return array|bool The tag data by slug.
*/
public function getTagDataBySlug($cacheService, $exchangeService, $slug)
{
$cacheKey = sprintf(Constants::CACHE_TAG_DETAIL, $slug);
if (($tagData = $cacheService->get($cacheKey)) === false) {
$em = $this->getDoctrine()->getManager();
$tag = $em->getRepository(Tags::class)->getTagBySlug($slug);
if ($tag) {
$tagData = $exchangeService->exchangeTagDetail($tag);
$cacheService->set($cacheKey, $tagData, $this->getParameter('cache_time')['hour']);
}
}
return $tagData;
}//end getTagDataBySlug()
}//end class