<?php
namespace EADPlataforma\Controller\Website;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use EADPlataforma\Entity\Forum;
use EADPlataforma\Entity\ForumCategory;
use EADPlataforma\Entity\LikeControl;
use EADPlataforma\Entity\User;
use EADPlataforma\Enum\UserEnum;
use EADPlataforma\Enum\ForumEnum;
use EADPlataforma\Enum\ErrorEnum;
use EADPlataforma\Enum\ForumCategoryEnum;
use EADPlataforma\Enum\LikeControlEnum;
use EADPlataforma\Util\StringUtil;
/**
* @Route(
* schemes = {"http|https"}
* )
* @Cache(
* maxage = "0",
* smaxage = "0",
* expires = "now",
* public = false
* )
*/
class ForumController extends AbstractWebsiteController {
/**
* @Route(
* path = "/forum/{categorySlug}/{categoryId}",
* name = "forum",
* methods = {"GET"},
* defaults = { "categorySlug": null, "categoryId": 0 },
* requirements = { "categoryId" = "\d+" }
* )
*/
public function forumPage(Request $request) {
if(!$this->configuration->showForum(( $this->user ? true : false ))){
return $this->redirectToRoute('notFound');
}
$forumRepository = $this->em->getRepository(Forum::class);
$forumCategoryRepository = $this->em->getRepository(ForumCategory::class);
$likeControlRepository = $this->em->getRepository(LikeControl::class);
$categoryId = $request->get('categoryId');
$categorySlug = $request->get('categorySlug');
$search = $request->get('search');
$forumCategoryName = null;
if(!empty($categoryId)){
$forumCategory = $forumCategoryRepository->findOneBy([
"id" => $categoryId,
"deleted" => ForumCategoryEnum::ITEM_NO_DELETED
]);
if(!$forumCategory){
return $this->redirectToRoute('notFound');
}
$forumCategoryName = $forumCategory->getCategory(true);
}
$forumCategories = $forumCategoryRepository->findBy([
"deleted" => ForumCategoryEnum::ITEM_NO_DELETED
]);
$total = $forumRepository->countForumTolist(null, $search);
$itemTotal = (object)[
"id" => null,
"orderList" => 0,
"slug" => null,
"active" => (empty($categoryId) ? true : false),
"category" => $this->configuration->getLanguage('all_categories', 'forum'),
"total" => $total,
];
$dataCategoies = [];
foreach ($forumCategories as $key => $forumCategoryItem) {
$forumCategoryItem->active = false;
$forumCategoryItem->orderList = $forumCategoryItem->getOrder();
$forumCategoryItem->slug = $this->stringUtil->slug($forumCategoryItem->getCategory());
$forumCategoryItem->total = $forumRepository->countForumTolist(
$forumCategoryItem->getId(),
$search
);
if($categoryId == $forumCategoryItem->getId()){
$forumCategoryItem->active = true;
}
if(!empty($forumCategoryItem->total)){
$dataCategoies[$key] = $forumCategoryItem;
}
}
array_unshift($dataCategoies, $itemTotal);
usort($dataCategoies, function($a, $b) {return $a->orderList > $b->orderList;});
$forumItems = $forumRepository->getForumToList($categoryId, $search, 10);
$totalNumber = $forumRepository->countForumTolist($categoryId, $search);
foreach ($forumItems as $key => $forum) {
$forum->slug = $this->stringUtil->slug($forum->getTitle());
$forum->answers = $forumRepository->count([
"forum" => $forum->getId(),
"deleted" => ForumEnum::ITEM_NO_DELETED
]);
$forumItems[$key] = $forum;
}
$topUsers = $likeControlRepository->getRank();
foreach ($topUsers as $key => $value) {
$value['name'] = StringUtil::encodeStringStatic($value['name']);
$topUsers[$key] = $value;
}
$this->data['formName'] = "forumFormNewTopic";
$this->data['forumCategoriesSave'] = $forumCategories;
$this->data['forumCategories'] = $dataCategoies;
$this->data['categoryId'] = (!empty($categoryId) ? $categoryId : null );
$this->data['categorySlug'] = (!empty($categorySlug) ? $categorySlug : null );
$this->data['search'] = $search;
$this->data['forumCategoryName'] = $forumCategoryName;
$this->data['forumItems'] = $forumItems;
$this->data['topUsers'] = $topUsers;
$this->data['totalItems'] = $totalNumber;
$this->data['offset'] = 0;
$this->data['itemsLoadNumber'] = count($forumItems);
$preKey = md5("captcha");
$key = $preKey."_".md5(
$this->client->getDomainPrimary().date('Y-m-d H:i:s').$request->getClientIp()
);
$data = $this->stringUtil->randomText(6);
$this->memcacheService->saveData($key, $data, 60*60*24);
$this->data['keyCaptcha'] = $key;
return $this->renderEAD('forum/forum-list.html.twig');
}
/**
* @Route(
* path = "/forum/list/item/{categorySlug}/{categoryId}",
* name = "forumListItem",
* methods = {"GET"},
* defaults = { "categorySlug": null, "categoryId": null },
* requirements = { "categoryId" = "\d+" }
* )
*/
public function forumListItemPage(Request $request) {
if(!$this->configuration->showForum(( $this->user ? true : false ))){
return $this->redirectToRoute('notFound');
}
$forumRepository = $this->em->getRepository(Forum::class);
$forumCategoryRepository = $this->em->getRepository(ForumCategory::class);
$categoryId = $request->get('categoryId');
$categorySlug = $request->get('categorySlug');
$search = $request->get('search');
$offset = (int)$request->get('offset');
$itemsLoadNumber = (int)$request->get('itemsLoadNumber');
$forumItems = $forumRepository->getForumToList($categoryId, $search, 5, $offset);
$totalNumber = $forumRepository->countForumTolist($categoryId, $search);
foreach ($forumItems as $key => $forum) {
$forum->slug = $this->stringUtil->slug($forum->getTitle());
$forum->answers = $forumRepository->count([
"forum" => $forum->getId(),
"deleted" => ForumEnum::ITEM_NO_DELETED
]);
$forumItems[$key] = $forum;
}
$this->data['categoryId'] = $categoryId;
$this->data['categorySlug'] = $categorySlug;
$this->data['search'] = $search;
$this->data['forumItems'] = $forumItems;
$this->data['totalItems'] = $totalNumber;
$this->data['offset'] = $offset;
$this->data['itemsLoadNumber'] = $itemsLoadNumber + count($forumItems);
return $this->renderEAD('forum/forum-list-item.html.twig');
}
/**
* @Route(
* path = "/forum/{id}/{slug}",
* name = "forumItem",
* methods = {"GET"},
* requirements = { "id" = "\d+" }
* )
*/
public function forumItemPage(Request $request) {
if(!$this->configuration->showForum(( $this->user ? true : false ))){
return $this->redirectToRoute('notFound');
}
$forumRepository = $this->em->getRepository(Forum::class);
$likeControlRepository = $this->em->getRepository(LikeControl::class);
$forumCategoryRepository = $this->em->getRepository(ForumCategory::class);
$id = $request->get('id');
$slug = $request->get('slug');
$search = $request->get('search');
$forum = $forumRepository->findOneBy([
"id" => $id,
"deleted" => ForumEnum::ITEM_NO_DELETED
]);
if(!$forum){
return $this->redirectToRoute('notFound');
}
$permission = $this->userPermissionUtil->getPermission("website", "forum", "edit");
if($this->userPermissionUtil->isLow($permission) && $forum->getStatus() == ForumEnum::FILED){
return $this->redirectToRoute('notFound');
}
$likes = $likeControlRepository->getLikeByType(
$forum->getId(),
LikeControlEnum::FORUM
);
$answers = $forumRepository->getForumAnswerToList($forum, $search);
foreach ($answers as $key => $answer) {
$answer->isForum = false;
$answer->allowLike = true;
if($this->user){
$likeControl = $likeControlRepository->findOneBy([
"userFrom" => $this->user->getId(),
"element" => $answer->getId(),
"type" => LikeControlEnum::FORUM_ANSWER,
]);
if($likeControl){
$answer->allowLike = false;
}
}
$answer->likes = $likeControlRepository->getLikeByType(
$answer->getId(),
LikeControlEnum::FORUM_ANSWER
);
$answer->likeTotal = count($answer->likes);
$answers[$key] = $answer;
}
$allowLike = true;
if($this->user){
$likeControl = $likeControlRepository->findOneBy([
"userFrom" => $this->user->getId(),
"element" => $forum->getId(),
"type" => LikeControlEnum::FORUM,
]);
if($likeControl){
$allowLike = false;
}
}
$forum->likes = $likes;
$forum->likeTotal = count($likes);
$forum->allowLike = $allowLike;
$forum->isForum = true;
$forumCategories = $forumCategoryRepository->findBy([
"deleted" => ForumCategoryEnum::ITEM_NO_DELETED
]);
$totalNumber = $forumRepository->countForumAnswerToList($forum, $search);
$this->data['formName'] = "forumFormEditTopic";
$this->data['forumCategories'] = $forumCategories;
$this->data['forum'] = $forum;
$this->data['answers'] = $answers;
$this->data['slug'] = $slug;
$this->data['totalItems'] = $totalNumber;
$this->data['offset'] = 0;
$this->data['itemsLoadNumber'] = count($answers);
$this->data['keyCaptcha'] = $this->createCaptchaKey($request);
return $this->renderEAD('forum/forum-detail.html.twig');
}
/**
* @Route(
* path = "/forum/item/answers/{id}",
* name = "forumItemAnswers",
* methods = {"GET"},
* requirements = { "id" = "\d+" }
* )
*/
public function forumItemAnswersPage(Request $request) {
if(!$this->configuration->showForum(( $this->user ? true : false ))){
return $this->redirectToRoute('notFound');
}
$forumRepository = $this->em->getRepository(Forum::class);
$likeControlRepository = $this->em->getRepository(LikeControl::class);
$id = $request->get('id');
$search = $request->get('search');
$offset = (int)$request->get('offset');
$itemsLoadNumber = (int)$request->get('itemsLoadNumber');
$forum = $forumRepository->findOneBy([
"id" => $id,
"deleted" => ForumEnum::ITEM_NO_DELETED
]);
if(!$forum){
return $this->redirectToRoute('notFound');
}
$answers = $forumRepository->getForumAnswerToList($forum, $search, 2, $offset);
foreach ($answers as $key => $answer) {
$answer->isForum = false;
$answer->allowLike = true;
if($this->user){
$likeControl = $likeControlRepository->findOneBy([
"userFrom" => $this->user->getId(),
"element" => $answer->getId(),
"type" => LikeControlEnum::FORUM_ANSWER,
]);
if($likeControl){
$answer->allowLike = false;
}
}
$answer->likes = $likeControlRepository->getLikeByType(
$answer->getId(),
LikeControlEnum::FORUM_ANSWER
);
$answer->likeTotal = count($answer->likes);
$answers[$key] = $answer;
}
$totalNumber = $forumRepository->countForumAnswerToList($forum, $search);
$this->data['totalItems'] = $totalNumber;
$this->data['offset'] = $offset;
$this->data['itemsLoadNumber'] = $itemsLoadNumber + count($answers);
$this->data['answers'] = $answers;
return $this->renderEAD('forum/forum-list-answers.html.twig');
}
}