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

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