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

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