vendor/uvdesk/support-center-bundle/Controller/Website.php line 126

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\SupportCenterBundle\Controller;
  3. use Doctrine\Common\Collections\Criteria;
  4. use Webkul\UVDesk\SupportCenterBundle\Form;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\ParameterBag;
  8. use Webkul\UVDesk\SupportCenterBundle\Entity\Article;
  9. use Webkul\UVDesk\SupportCenterBundle\Entity\Solutions;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Webkul\UVDesk\SupportCenterBundle\Entity\ArticleViewLog;
  12. use Webkul\UVDesk\SupportCenterBundle\Entity\SolutionCategory;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use Webkul\UVDesk\SupportCenterBundle\Entity\KnowledgebaseWebsite;
  15. use Webkul\UVDesk\CoreFrameworkBundle\Entity\Website as CoreWebsite;
  16. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  17. use Webkul\UVDesk\CoreFrameworkBundle\Services\UserService;
  18. use Symfony\Contracts\Translation\TranslatorInterface;
  19. use Symfony\Component\DependencyInjection\ContainerInterface;
  20. class Website extends AbstractController
  21. {
  22.     private $visibility = ['public'];
  23.     private $limit 5;
  24.     private $company;
  25.     private $userService;
  26.     private $translator;
  27.     private $constructContainer;
  28.     public function __construct(UserService $userServiceTranslatorInterface $translatorContainerInterface $constructContainer)
  29.     {
  30.         $this->userService $userService;
  31.         $this->translator $translator;
  32.         $this->constructContainer $constructContainer;
  33.     }
  34.     private function isKnowledgebaseActive()
  35.     {
  36.         $entityManager $this->getDoctrine()->getManager();
  37.         $website $entityManager->getRepository(CoreWebsite::class)->findOneByCode('knowledgebase');
  38.         if (!empty($website)) {
  39.             $knowledgebaseWebsite $entityManager->getRepository(KnowledgebaseWebsite::class)->findOneBy(['website' => $website->getId(), 'status' => true]);
  40.             if (!empty($knowledgebaseWebsite) && true == $knowledgebaseWebsite->getIsActive()) {
  41.                 return true;
  42.             }
  43.         }
  44.         throw new NotFoundHttpException('Page Not Found');
  45.     }
  46.     public function home(Request $request)
  47.     {
  48.         $this->isKnowledgebaseActive();
  49.         $parameterBag = [
  50.             'visibility' => 'public',
  51.             'sort' => 'id',
  52.             'direction' => 'desc'
  53.         ];
  54.         $articleRepository $this->getDoctrine()->getRepository('UVDeskSupportCenterBundle:Article');
  55.         $solutionRepository $this->getDoctrine()->getRepository('UVDeskSupportCenterBundle:Solutions');
  56.         $twigResponse = [
  57.             'searchDisable' => false,
  58.             'popArticles' => $articleRepository->getPopularTranslatedArticles($request->getLocale()),
  59.             'solutions' => $solutionRepository->getAllSolutions(new ParameterBag($parameterBag), $this->constructContainer'a', [1]),
  60.         ];
  61.         $newResult = [];
  62.        
  63.         foreach ($twigResponse['solutions'] as $key => $result) {
  64.             $newResult[] = [
  65.                 'id' => $result->getId(),
  66.                 'name' => $result->getName(),
  67.                 'description' => $result->getDescription(),
  68.                 'visibility' => $result->getVisibility(),
  69.                 'solutionImage' => ($result->getSolutionImage() == null) ? '' $result->getSolutionImage(),
  70.                 'categoriesCount' => $solutionRepository->getCategoriesCountBySolution($result->getId()),
  71.                 'categories' => $solutionRepository->getCategoriesWithCountBySolution($result->getId()),
  72.                 'articleCount' => $solutionRepository->getArticlesCountBySolution($result->getId()),
  73.             ];
  74.         }
  75.         $twigResponse['solutions']['results'] = $newResult;
  76.         $twigResponse['solutions']['categories'] = array_map(function($category) use ($articleRepository) {
  77.             $parameterBag = [
  78.                 'categoryId' => $category['id'],
  79.                 'status' => 1,
  80.                 'sort' => 'id',
  81.                 'limit'=>10,
  82.                 'direction' => 'desc'
  83.             ];
  84.             $article =  $articleRepository->getAllArticles(new ParameterBag($parameterBag), $this->constructContainer'a.id, a.name, a.slug, a.stared');
  85.              
  86.             return [
  87.                 'id' => $category['id'],
  88.                 'name' => $category['name'],
  89.                 'description' => $category['description'],
  90.                 'articles' => $article
  91.             ];
  92.         }, $solutionRepository->getAllCategories(102));
  93.         return $this->render('@UVDeskSupportCenter//Knowledgebase//index.html.twig'$twigResponse);
  94.     }
  95.     public function listCategories(Request $request)
  96.     {
  97.         $this->isKnowledgebaseActive();
  98.         $solutionRepository $this->getDoctrine()->getRepository('UVDeskSupportCenterBundle:Solutions');
  99.         $categoryCollection $solutionRepository->getAllCategories(104);
  100.         
  101.         return $this->render('@UVDeskSupportCenter/Knowledgebase/categoryListing.html.twig', [
  102.             'categories' => $categoryCollection,
  103.             'categoryCount' => count($categoryCollection),
  104.         ]);
  105.     }
  106.     public function viewFolder(Request $request)
  107.     {
  108.         $this->isKnowledgebaseActive();
  109.         
  110.         if(!$request->attributes->get('solution'))
  111.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  112.         $filterArray = ['id' => $request->attributes->get('solution')];
  113.         $solution $this->getDoctrine()
  114.                     ->getRepository('UVDeskSupportCenterBundle:Solutions')
  115.                     ->findOneBy($filterArray);
  116.         if(!$solution)
  117.             $this->noResultFound();
  118.         $breadcrumbs = [
  119.             [
  120.                 'label' => $this->translator->trans('Support Center'),
  121.                 'url' => $this->generateUrl('helpdesk_knowledgebase')
  122.             ],
  123.             [
  124.                 'label' => $solution->getName(),
  125.                 'url' => '#'
  126.             ],
  127.         ];
  128.         $testArray = [1234];
  129.         foreach ($testArray as $test) {
  130.             $categories[] = [
  131.                 'id' => $test,
  132.                 'name' => $test " name",
  133.                 'articleCount' => $test " articleCount",
  134.             ];
  135.         }
  136.         return $this->render('@UVDeskSupportCenter//Knowledgebase//folder.html.twig', [
  137.             'folder' => $solution,
  138.             'categoryCount' => $this->getDoctrine()
  139.                 ->getRepository('UVDeskSupportCenterBundle:Solutions')
  140.                 ->getCategoriesCountBySolution($solution->getId()),
  141.             'categories' => $this->getDoctrine()
  142.                 ->getRepository('UVDeskSupportCenterBundle:Solutions')
  143.                 ->getCategoriesWithCountBySolution($solution->getId()),
  144.             'breadcrumbs' => $breadcrumbs
  145.         ]);
  146.     }
  147.     public function viewFolderArticle(Request $request)
  148.     {
  149.         $this->isKnowledgebaseActive();
  150.         if(!$request->attributes->get('solution'))
  151.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  152.         $filterArray = ['id' => $request->attributes->get('solution')];
  153.         $solution $this->getDoctrine()
  154.                     ->getRepository('UVDeskSupportCenterBundle:Solutions')
  155.                     ->findOneBy($filterArray);
  156.         if(!$solution)
  157.             $this->noResultFound();
  158.         $breadcrumbs = [
  159.             [
  160.                 'label' => $this->translator->trans('Support Center'),
  161.                 'url' => $this->generateUrl('helpdesk_knowledgebase')
  162.             ],
  163.             [
  164.                 'label' => $solution->getName(),
  165.                 'url' => '#'
  166.             ],
  167.         ];
  168.         $parameterBag = [
  169.             'solutionId' => $solution->getId(),
  170.             'status' => 1,
  171.             'sort' => 'id',
  172.             'direction' => 'desc'
  173.         ];
  174.         $article_data = [
  175.             'folder' => $solution,
  176.             'articlesCount' => $this->getDoctrine()
  177.                 ->getRepository('UVDeskSupportCenterBundle:Solutions')
  178.                 ->getArticlesCountBySolution($solution->getId(), [1]),
  179.             'articles' => $this->getDoctrine()
  180.                 ->getRepository('UVDeskSupportCenterBundle:Article')
  181.                 ->getAllArticles(new ParameterBag($parameterBag), $this->constructContainer'a.id, a.name, a.slug, a.stared'),
  182.             'breadcrumbs' => $breadcrumbs,
  183.         ];
  184.         return $this->render('@UVDeskSupportCenter/Knowledgebase/folderArticle.html.twig'$article_data);
  185.     }
  186.     public function viewCategory(Request $request)
  187.     {
  188.         $this->isKnowledgebaseActive();
  189.         if(!$request->attributes->get('category'))
  190.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  191.         $filterArray = array(
  192.                             'id' => $request->attributes->get('category'),
  193.                             'status' => 1,
  194.                         );
  195.        
  196.         $category $this->getDoctrine()
  197.                     ->getRepository('UVDeskSupportCenterBundle:SolutionCategory')
  198.                     ->findOneBy($filterArray);
  199.     
  200.         if(!$category)
  201.             $this->noResultFound();
  202.         $breadcrumbs = [
  203.             [ 'label' => $this->translator->trans('Support Center'),'url' => $this->generateUrl('helpdesk_knowledgebase') ],
  204.             [ 'label' => $category->getName(),'url' => '#' ],
  205.         ];
  206.         
  207.         $parameterBag = [
  208.             'categoryId' => $category->getId(),
  209.             'status' => 1,
  210.             'sort' => 'id',
  211.             'direction' => 'desc'
  212.         ];
  213.         $category_data=  array(
  214.             'category' => $category,
  215.             'articlesCount' => $this->getDoctrine()
  216.                             ->getRepository('UVDeskSupportCenterBundle:SolutionCategory')
  217.                             ->getArticlesCountByCategory($category->getId(), [1]),
  218.             'articles' => $this->getDoctrine()
  219.                         ->getRepository('UVDeskSupportCenterBundle:Article')
  220.                         ->getAllArticles(new ParameterBag($parameterBag), $this->constructContainer'a.id, a.name, a.slug, a.stared'),
  221.             'breadcrumbs' => $breadcrumbs
  222.         );
  223.         return $this->render('@UVDeskSupportCenter/Knowledgebase/category.html.twig',$category_data);
  224.     }
  225.    
  226.     public function viewArticle(Request $request)
  227.     {
  228.         $this->isKnowledgebaseActive();
  229.        
  230.         if (!$request->attributes->get('article') && !$request->attributes->get('slug')) {
  231.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  232.         }
  233.         $entityManager $this->getDoctrine()->getManager();
  234.         $user $this->userService->getCurrentUser();
  235.         $articleRepository $entityManager->getRepository('UVDeskSupportCenterBundle:Article');
  236.         if ($request->attributes->get('article')) {
  237.             $article $articleRepository->findOneBy(['status' => 1'id' => $request->attributes->get('article')]);
  238.         } else {
  239.             $article $articleRepository->findOneBy(['status' => 1,'slug' => $request->attributes->get('slug')]);
  240.         }
  241.        
  242.         if (empty($article)) {
  243.             $this->noResultFound();
  244.         }
  245.         $stringReplace str_replace("<ol>","<ul>",$article->getContent());
  246.         $stringReplace str_replace("</ol>","</ul>",$stringReplace);
  247.         $article->setContent($stringReplace);
  248.         $article->setViewed((int) $article->getViewed() + 1);
  249.         
  250.         // Log article view
  251.         $articleViewLog = new ArticleViewLog();
  252.         $articleViewLog->setUser(($user != null && $user != 'anon.') ? $user null);
  253.         
  254.         $articleViewLog->setArticle($article);
  255.         $articleViewLog->setViewedAt(new \DateTime('now'));
  256.         $entityManager->persist($article);
  257.         $entityManager->persist($articleViewLog);
  258.         $entityManager->flush();
  259.         
  260.         // Get article feedbacks
  261.         $feedbacks = ['enabled' => false'submitted' => false'article' => $articleRepository->getArticleFeedbacks($article)];
  262.         if (!empty($user) && $user != 'anon.') {
  263.             $feedbacks['enabled'] = true;
  264.             if (!empty($feedbacks['article']['collection']) && in_array($user->getId(), array_column($feedbacks['article']['collection'], 'user'))) {
  265.                 $feedbacks['submitted'] = true;
  266.             }
  267.         }
  268.         // @TODO: App popular articles
  269.         $article_details = [
  270.             'article' => $article,
  271.             'breadcrumbs' => [
  272.                 ['label' => $this->translator->trans('Support Center'), 'url' => $this->generateUrl('helpdesk_knowledgebase')],
  273.                 ['label' => $article->getName(), 'url' => '#']
  274.             ],
  275.             'dateAdded' => $this->userService->convertToTimezone($article->getDateAdded()),
  276.             'articleTags' => $articleRepository->getTagsByArticle($article->getId()),
  277.             'articleAuthor' => $articleRepository->getArticleAuthorDetails($article->getId()),
  278.             'relatedArticles' => $articleRepository->getAllRelatedyByArticle(['locale' => $request->getLocale(), 'articleId' => $article->getId()], [1]),
  279.             'popArticles'  => $articleRepository->getPopularTranslatedArticles($request->getLocale())
  280.         ];
  281.         return $this->render('@UVDeskSupportCenter/Knowledgebase/article.html.twig',$article_details);
  282.     }
  283.     public function searchKnowledgebase(Request $request)
  284.     {
  285.         $this->isKnowledgebaseActive();
  286.         $searchQuery $request->query->get('s');
  287.         if (empty($searchQuery)) {
  288.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  289.         }
  290.         $articleCollection $this->getDoctrine()->getRepository('UVDeskSupportCenterBundle:Article')->getArticleBySearch($request);
  291.         return $this->render('@UVDeskSupportCenter/Knowledgebase/search.html.twig', [
  292.             'search' => $searchQuery,
  293.             'articles' => $articleCollection,
  294.         ]);
  295.     }
  296.     public function viewTaggedResources(Request $request)
  297.     {
  298.         $this->isKnowledgebaseActive();
  299.         $tagQuery $request->attributes->get('tag');
  300.         if (empty($tagQuery)) {
  301.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  302.         }
  303.         $tagLabel $request->attributes->get('name');
  304.         $articleCollection $this->getDoctrine()->getRepository('UVDeskSupportCenterBundle:Article')->getArticleByTags([$tagLabel]);
  305.         return $this->render('@UVDeskSupportCenter/Knowledgebase/search.html.twig', [
  306.             'articles' => $articleCollection,
  307.             'search' => $tagLabel,
  308.             'breadcrumbs' => [
  309.                 ['label' => $this->translator->trans('Support Center'), 'url' => $this->generateUrl('helpdesk_knowledgebase')],
  310.                 ['label' => $tagLabel'url' => '#'],
  311.             ],
  312.         ]);
  313.     }
  314.     public function rateArticle($articleIdRequest $request)
  315.     {
  316.         $this->isKnowledgebaseActive();
  317.         // @TODO: Refactor
  318.             
  319.         // if ($request->getMethod() != 'POST') {
  320.         //     return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  321.         // }
  322.         // $company = $this->getCompany();
  323.         // $user = $this->userService->getCurrentUser();
  324.         $response = ['code' => 404'content' => ['alertClass' => 'danger''alertMessage' => 'An unexpected error occurred. Please try again later.']];
  325.         // if (!empty($user) && $user != 'anon.') {
  326.         //     $entityManager = $this->getDoctrine()->getEntityManager();
  327.         //     $article = $entityManager->getRepository('WebkulSupportCenterBundle:Article')->findOneBy(['id' => $articleId, 'companyId' => $company->getId()]);
  328.         //     if (!empty($article)) {
  329.         //         $providedFeedback = $request->request->get('feedback');
  330.         //         if (!empty($providedFeedback) && in_array(strtolower($providedFeedback), ['positive', 'neagtive'])) {
  331.         //             $isArticleHelpful = ('positive' == strtolower($providedFeedback)) ? true : false;
  332.         //             $articleFeedback = $entityManager->getRepository('WebkulSupportCenterBundle:ArticleFeedback')->findOneBy(['article' => $article, 'ratedCustomer' => $user]);
  333.         //             $response = ['code' => 200, 'content' => ['alertClass' => 'success', 'alertMessage' => 'Feedback saved successfully.']];
  334.         //             if (empty($articleFeedback)) {
  335.         //                 $articleFeedback = new \Webkul\SupportCenterBundle\Entity\ArticleFeedback();
  336.         //                 // $articleBadge->setDescription('');
  337.         //                 $articleFeedback->setIsHelpful($isArticleHelpful);
  338.         //                 $articleFeedback->setArticle($article);
  339.         //                 $articleFeedback->setRatedCustomer($user);
  340.         //                 $articleFeedback->setCreatedAt(new \DateTime('now'));
  341.         //             } else {
  342.         //                 $articleFeedback->setIsHelpful($isArticleHelpful);
  343.         //                 $response['content']['alertMessage'] = 'Feedback updated successfully.';
  344.         //             }
  345.         //             $entityManager->persist($articleFeedback);
  346.         //             $entityManager->flush();
  347.         //         } else {
  348.         //             $response['content']['alertMessage'] = 'Invalid feedback provided.';
  349.         //         }
  350.         //     } else {
  351.         //         $response['content']['alertMessage'] = 'Article not found.';
  352.         //     }
  353.         // } else {
  354.         //     $response['content']['alertMessage'] = 'You need to login to your account before can perform this action.';
  355.         // }
  356.         return new Response(json_encode($response['content']), $response['code'], ['Content-Type: application/json']);
  357.     }
  358.     /**
  359.      * If customer is playing with url and no result is found then what will happen
  360.      * @return 
  361.      */
  362.     protected function noResultFound()
  363.     {
  364.         throw new NotFoundHttpException('Not Found!');
  365.     }
  366. }