src/Controller/Website/ForumController.php line 213

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Controller\Website;
  3. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\Routing\Annotation\Route;
  6. use EADPlataforma\Entity\Forum;
  7. use EADPlataforma\Entity\ForumCategory;
  8. use EADPlataforma\Entity\LikeControl;
  9. use EADPlataforma\Entity\User;
  10. use EADPlataforma\Enum\UserEnum;
  11. use EADPlataforma\Enum\ForumEnum;
  12. use EADPlataforma\Enum\ErrorEnum;
  13. use EADPlataforma\Enum\ForumCategoryEnum;
  14. use EADPlataforma\Enum\LikeControlEnum;
  15. use EADPlataforma\Util\StringUtil;
  16. /**
  17.  * @Route(
  18.  *      schemes         = {"http|https"}
  19.  * )
  20.  * @Cache(
  21.  *      maxage          = "0",
  22.  *      smaxage         = "0",
  23.  *      expires         = "now",
  24.  *      public          = false
  25.  * )
  26.  */
  27. class ForumController extends AbstractWebsiteController {
  28.     /**
  29.      * @Route(
  30.      *      path          = "/forum/{categorySlug}/{categoryId}",
  31.      *      name          = "forum",
  32.      *      methods       = {"GET"},
  33.      *      defaults      = { "categorySlug": null, "categoryId": 0 },
  34.      *      requirements  = { "categoryId" = "\d+" }
  35.      * )
  36.      */
  37.     public function forumPage(Request $request) {
  38.         if(!$this->configuration->showForum(( $this->user true false ))){
  39.             return $this->redirectToRoute('notFound');
  40.         }
  41.         $forumRepository $this->em->getRepository(Forum::class);
  42.         $forumCategoryRepository $this->em->getRepository(ForumCategory::class);
  43.         $likeControlRepository $this->em->getRepository(LikeControl::class);
  44.         $categoryId $request->get('categoryId');
  45.         $categorySlug $request->get('categorySlug');
  46.         $search $request->get('search');
  47.         $forumCategoryName null;
  48.         if(!empty($categoryId)){
  49.             $forumCategory $forumCategoryRepository->findOneBy([
  50.                 "id" => $categoryId,
  51.                 "deleted" => ForumCategoryEnum::ITEM_NO_DELETED
  52.             ]);
  53.             if(!$forumCategory){
  54.                 return $this->redirectToRoute('notFound');
  55.             }
  56.             $forumCategoryName $forumCategory->getCategory(true);
  57.         }
  58.         $forumCategories $forumCategoryRepository->findBy([ 
  59.             "deleted" => ForumCategoryEnum::ITEM_NO_DELETED
  60.         ]);
  61.         $total $forumRepository->countForumTolist(null$search);
  62.         $itemTotal = (object)[
  63.             "id" => null,
  64.             "orderList" => 0,
  65.             "slug" => null,
  66.             "active" => (empty($categoryId) ? true false),
  67.             "category" => $this->configuration->getLanguage('all_categories''forum'), 
  68.             "total" => $total,
  69.         ];
  70.         $dataCategoies = [];
  71.         foreach ($forumCategories as $key => $forumCategoryItem) {
  72.             $forumCategoryItem->active false;
  73.             $forumCategoryItem->orderList $forumCategoryItem->getOrder();
  74.             $forumCategoryItem->slug $this->stringUtil->slug($forumCategoryItem->getCategory());
  75.             $forumCategoryItem->total $forumRepository->countForumTolist(
  76.                 $forumCategoryItem->getId(), 
  77.                 $search
  78.             );
  79.             if($categoryId == $forumCategoryItem->getId()){
  80.                 $forumCategoryItem->active true;
  81.             }
  82.             if(!empty($forumCategoryItem->total)){
  83.                 $dataCategoies[$key] = $forumCategoryItem;
  84.             }
  85.         }
  86.         array_unshift($dataCategoies$itemTotal);
  87.         usort($dataCategoies, function($a$b) {return $a->orderList $b->orderList;});
  88.         $forumItems $forumRepository->getForumToList($categoryId$search10);
  89.         $totalNumber $forumRepository->countForumTolist($categoryId$search);
  90.         foreach ($forumItems as $key => $forum) {
  91.            
  92.             $forum->slug $this->stringUtil->slug($forum->getTitle());
  93.             $forum->answers $forumRepository->count([
  94.                 "forum" => $forum->getId(),
  95.                 "deleted" => ForumEnum::ITEM_NO_DELETED
  96.             ]);
  97.            $forumItems[$key] = $forum;
  98.         }
  99.         $topUsers $likeControlRepository->getRank();
  100.         foreach ($topUsers as $key => $value) {
  101.             $value['name'] = StringUtil::encodeStringStatic($value['name']);
  102.             $topUsers[$key] = $value;
  103.         }
  104.         $this->data['formName'] = "forumFormNewTopic";
  105.         $this->data['forumCategoriesSave'] = $forumCategories;
  106.         $this->data['forumCategories'] = $dataCategoies;
  107.         $this->data['categoryId'] = (!empty($categoryId) ? $categoryId null );
  108.         $this->data['categorySlug'] = (!empty($categorySlug) ? $categorySlug null );
  109.         $this->data['search'] = $search;
  110.         $this->data['forumCategoryName'] = $forumCategoryName;
  111.         $this->data['forumItems'] = $forumItems;
  112.         $this->data['topUsers'] = $topUsers;
  113.         $this->data['totalItems'] = $totalNumber;
  114.         $this->data['offset'] = 0;
  115.         $this->data['itemsLoadNumber'] = count($forumItems);
  116.         $preKey md5("captcha");
  117.         $key $preKey."_".md5(
  118.             $this->client->getDomainPrimary().date('Y-m-d H:i:s').$request->getClientIp()
  119.         );
  120.         $data $this->stringUtil->randomText(6);
  121.         $this->memcacheService->saveData($key$data60*60*24);
  122.         $this->data['keyCaptcha'] = $key;
  123.         
  124.         return $this->renderEAD('forum/forum-list.html.twig');
  125.     }
  126.     /**
  127.      * @Route(
  128.      *      path          = "/forum/list/item/{categorySlug}/{categoryId}",
  129.      *      name          = "forumListItem",
  130.      *      methods       = {"GET"},
  131.      *      defaults      = { "categorySlug": null, "categoryId": null },
  132.      *      requirements  = { "categoryId" = "\d+" }
  133.      * )
  134.      */
  135.     public function forumListItemPage(Request $request) {
  136.         if(!$this->configuration->showForum(( $this->user true false ))){
  137.             return $this->redirectToRoute('notFound');
  138.         }
  139.         $forumRepository $this->em->getRepository(Forum::class);
  140.         $forumCategoryRepository $this->em->getRepository(ForumCategory::class);
  141.         $categoryId $request->get('categoryId');
  142.         $categorySlug $request->get('categorySlug');
  143.         $search $request->get('search');
  144.         $offset = (int)$request->get('offset');
  145.         $itemsLoadNumber = (int)$request->get('itemsLoadNumber');
  146.         $forumItems $forumRepository->getForumToList($categoryId$search5$offset);
  147.         $totalNumber $forumRepository->countForumTolist($categoryId$search);
  148.         foreach ($forumItems as $key => $forum) {
  149.             $forum->slug $this->stringUtil->slug($forum->getTitle());
  150.             $forum->answers $forumRepository->count([
  151.                 "forum" => $forum->getId(),
  152.                 "deleted" => ForumEnum::ITEM_NO_DELETED
  153.             ]);
  154.            $forumItems[$key] = $forum;
  155.         }
  156.         $this->data['categoryId'] = $categoryId;
  157.         $this->data['categorySlug'] = $categorySlug;
  158.         $this->data['search'] = $search;
  159.         $this->data['forumItems'] = $forumItems;
  160.         $this->data['totalItems'] = $totalNumber;
  161.         $this->data['offset'] = $offset;
  162.         $this->data['itemsLoadNumber'] = $itemsLoadNumber count($forumItems);
  163.         return $this->renderEAD('forum/forum-list-item.html.twig');
  164.     }
  165.     /**
  166.      * @Route(
  167.      *      path          = "/forum/{id}/{slug}",
  168.      *      name          = "forumItem",
  169.      *      methods       = {"GET"},
  170.      *      requirements  = { "id" = "\d+" }
  171.      * )
  172.      */
  173.     public function forumItemPage(Request $request) {
  174.         if(!$this->configuration->showForum(( $this->user true false ))){
  175.             return $this->redirectToRoute('notFound');
  176.         }
  177.         $forumRepository $this->em->getRepository(Forum::class);
  178.         $likeControlRepository $this->em->getRepository(LikeControl::class);
  179.         $forumCategoryRepository $this->em->getRepository(ForumCategory::class);
  180.         
  181.         $id $request->get('id');
  182.         $slug $request->get('slug');
  183.         $search $request->get('search');
  184.         $forum $forumRepository->findOneBy([
  185.             "id" => $id,
  186.             "deleted" => ForumEnum::ITEM_NO_DELETED
  187.         ]);
  188.         if(!$forum){
  189.             return $this->redirectToRoute('notFound');
  190.         }
  191.         $permission $this->userPermissionUtil->getPermission("website""forum""edit");
  192.         if($this->userPermissionUtil->isLow($permission) && $forum->getStatus() == ForumEnum::FILED){
  193.             return $this->redirectToRoute('notFound');
  194.         }
  195.         $likes $likeControlRepository->getLikeByType(
  196.             $forum->getId(), 
  197.             LikeControlEnum::FORUM
  198.         );
  199.         $answers $forumRepository->getForumAnswerToList($forum$search);
  200.         foreach ($answers as $key => $answer) {
  201.             $answer->isForum false;
  202.             $answer->allowLike true;
  203.             if($this->user){
  204.                 $likeControl $likeControlRepository->findOneBy([
  205.                     "userFrom" => $this->user->getId(),
  206.                     "element" => $answer->getId(),
  207.                     "type" => LikeControlEnum::FORUM_ANSWER,
  208.                 ]);
  209.                 if($likeControl){
  210.                     $answer->allowLike false;
  211.                 }
  212.             }
  213.             $answer->likes $likeControlRepository->getLikeByType(
  214.                 $answer->getId(), 
  215.                 LikeControlEnum::FORUM_ANSWER
  216.             );
  217.             
  218.             $answer->likeTotal count($answer->likes);
  219.             $answers[$key] = $answer;
  220.         }
  221.         $allowLike true;
  222.         if($this->user){
  223.             $likeControl $likeControlRepository->findOneBy([
  224.                 "userFrom" => $this->user->getId(),
  225.                 "element" => $forum->getId(),
  226.                 "type" => LikeControlEnum::FORUM,
  227.             ]);
  228.             if($likeControl){
  229.                 $allowLike false;
  230.             }
  231.         }
  232.         $forum->likes $likes;
  233.         $forum->likeTotal count($likes);
  234.         $forum->allowLike $allowLike;
  235.         $forum->isForum true;
  236.         $forumCategories $forumCategoryRepository->findBy([ 
  237.             "deleted" => ForumCategoryEnum::ITEM_NO_DELETED
  238.         ]);
  239.         $totalNumber $forumRepository->countForumAnswerToList($forum$search);
  240.         $this->data['formName'] = "forumFormEditTopic";
  241.         $this->data['forumCategories'] = $forumCategories;
  242.         $this->data['forum'] = $forum;
  243.         $this->data['answers'] = $answers;
  244.         $this->data['slug'] = $slug;
  245.         $this->data['totalItems'] = $totalNumber;
  246.         $this->data['offset'] = 0;
  247.         $this->data['itemsLoadNumber'] = count($answers);
  248.         $this->data['keyCaptcha'] = $this->createCaptchaKey($request);
  249.         return $this->renderEAD('forum/forum-detail.html.twig');
  250.     }
  251.     /**
  252.      * @Route(
  253.      *      path          = "/forum/item/answers/{id}",
  254.      *      name          = "forumItemAnswers",
  255.      *      methods       = {"GET"},
  256.      *      requirements  = { "id" = "\d+" }
  257.      * )
  258.      */
  259.     public function forumItemAnswersPage(Request $request) {
  260.         if(!$this->configuration->showForum(( $this->user true false ))){
  261.             return $this->redirectToRoute('notFound');
  262.         }
  263.         $forumRepository $this->em->getRepository(Forum::class);
  264.         $likeControlRepository $this->em->getRepository(LikeControl::class);
  265.         $id $request->get('id');
  266.         $search $request->get('search');
  267.         $offset = (int)$request->get('offset');
  268.         $itemsLoadNumber = (int)$request->get('itemsLoadNumber');
  269.         $forum $forumRepository->findOneBy([
  270.             "id" => $id,
  271.             "deleted" => ForumEnum::ITEM_NO_DELETED
  272.         ]);
  273.         if(!$forum){
  274.             return $this->redirectToRoute('notFound');
  275.         }
  276.         $answers $forumRepository->getForumAnswerToList($forum$search2$offset);
  277.         foreach ($answers as $key => $answer) {
  278.             $answer->isForum false;
  279.             $answer->allowLike true;
  280.             if($this->user){
  281.                 $likeControl $likeControlRepository->findOneBy([
  282.                     "userFrom" => $this->user->getId(),
  283.                     "element" => $answer->getId(),
  284.                     "type" => LikeControlEnum::FORUM_ANSWER,
  285.                 ]);
  286.                 if($likeControl){
  287.                     $answer->allowLike false;
  288.                 }
  289.             }
  290.             $answer->likes $likeControlRepository->getLikeByType(
  291.                 $answer->getId(), 
  292.                 LikeControlEnum::FORUM_ANSWER
  293.             );
  294.             
  295.             $answer->likeTotal count($answer->likes);
  296.             $answers[$key] = $answer;
  297.         }
  298.         $totalNumber $forumRepository->countForumAnswerToList($forum$search);
  299.         $this->data['totalItems'] = $totalNumber;
  300.         $this->data['offset'] = $offset;
  301.         $this->data['itemsLoadNumber'] = $itemsLoadNumber count($answers);
  302.         $this->data['answers'] = $answers;
  303.         return $this->renderEAD('forum/forum-list-answers.html.twig');
  304.     }
  305. }