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

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\SupportCenterBundle\Controller;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpFoundation\ParameterBag;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  8. use Webkul\UVDesk\CoreFrameworkBundle\Services\UserService;
  9. use Symfony\Contracts\Translation\TranslatorInterface;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  11. use Webkul\UVDesk\CoreFrameworkBundle\Entity as CoreEntities
  12. use Webkul\UVDesk\SupportCenterBundle\Entity as SupportEntities;
  13. class Website extends AbstractController
  14. {
  15.     private $visibility = ['public'];
  16.     private $limit 5;
  17.     private $company;
  18.     private $userService;
  19.     private $translator;
  20.     private $constructContainer;
  21.     public function __construct(UserService $userServiceTranslatorInterface $translatorContainerInterface $constructContainer)
  22.     {
  23.         $this->userService $userService;
  24.         $this->translator $translator;
  25.         $this->constructContainer $constructContainer;
  26.     }
  27.     private function isKnowledgebaseActive()
  28.     {
  29.         $entityManager $this->getDoctrine()->getManager();
  30.         $website $entityManager->getRepository(CoreEntities\Website::class)->findOneByCode('knowledgebase');
  31.         if (! empty($website)) {
  32.             $knowledgebaseWebsite $entityManager->getRepository(SupportEntities\KnowledgebaseWebsite::class)->findOneBy(['website' => $website->getId(), 'status' => true]);
  33.             if (! empty($knowledgebaseWebsite) && true == $knowledgebaseWebsite->getIsActive()) {
  34.                 return true;
  35.             }
  36.         }
  37.         throw new NotFoundHttpException('Page Not Found');
  38.     }
  39.     public function home(Request $request)
  40.     {
  41.         $this->isKnowledgebaseActive();
  42.         $parameterBag = [
  43.             'visibility' => 'public',
  44.             'sort'       => 'id',
  45.             'direction'  => 'desc'
  46.         ];
  47.         $articleRepository $this->getDoctrine()->getRepository(SupportEntities\Article::class);
  48.         $solutionRepository $this->getDoctrine()->getRepository(SupportEntities\Solutions::class);
  49.         $twigResponse = [
  50.             'searchDisable' => false,
  51.             'popArticles'   => $articleRepository->getPopularTranslatedArticles($request->getLocale()),
  52.             'solutions'     => $solutionRepository->getAllSolutions(new ParameterBag($parameterBag), $this->constructContainer'a', [1]),
  53.         ];
  54.         $newResult = [];
  55.        
  56.         foreach ($twigResponse['solutions'] as $key => $result) {
  57.             $newResult[] = [
  58.                 'id'              => $result->getId(),
  59.                 'name'            => $result->getName(),
  60.                 'description'     => $result->getDescription(),
  61.                 'visibility'      => $result->getVisibility(),
  62.                 'solutionImage'   => ($result->getSolutionImage() == null) ? '' $result->getSolutionImage(),
  63.                 'categoriesCount' => $solutionRepository->getCategoriesCountBySolution($result->getId()),
  64.                 'categories'      => $solutionRepository->getCategoriesWithCountBySolution($result->getId()),
  65.                 'articleCount'    => $solutionRepository->getArticlesCountBySolution($result->getId()),
  66.             ];
  67.         }
  68.         $twigResponse['solutions']['results'] = $newResult;
  69.         $twigResponse['solutions']['categories'] = array_map(function($category) use ($articleRepository) {
  70.             $parameterBag = [
  71.                 'categoryId' => $category['id'],
  72.                 'status'     => 1,
  73.                 'sort'       => 'id',
  74.                 'limit'      => 10,
  75.                 'direction'  => 'desc'
  76.             ];
  77.             $article =  $articleRepository->getAllArticles(new ParameterBag($parameterBag), $this->constructContainer'a.id, a.name, a.slug, a.stared');
  78.              
  79.             return [
  80.                 'id'          => $category['id'],
  81.                 'name'        => $category['name'],
  82.                 'description' => $category['description'],
  83.                 'articles'    => $article
  84.             ];
  85.         }, $solutionRepository->getAllCategories(102));
  86.         return $this->render('@UVDeskSupportCenter//Knowledgebase//index.html.twig'$twigResponse);
  87.     }
  88.     public function listCategories(Request $request)
  89.     {
  90.         $this->isKnowledgebaseActive();
  91.         $solutionRepository $this->getDoctrine()->getRepository(SupportEntities\Solutions::class);
  92.         $categoryCollection $solutionRepository->getAllCategories(104);
  93.         
  94.         return $this->render('@UVDeskSupportCenter/Knowledgebase/categoryListing.html.twig', [
  95.             'categories'    => $categoryCollection,
  96.             'categoryCount' => count($categoryCollection),
  97.         ]);
  98.     }
  99.     public function viewFolder(Request $request)
  100.     {
  101.         $this->isKnowledgebaseActive();
  102.         
  103.         if (!$request->attributes->get('solution'))
  104.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  105.         $filterArray = ['id' => $request->attributes->get('solution')];
  106.         $solution $this->getDoctrine()
  107.                     ->getRepository(SupportEntities\Solutions::class)
  108.                     ->findOneBy($filterArray);
  109.         if (! $solution)
  110.             $this->noResultFound();
  111.         if ($solution->getVisibility() == 'private'
  112.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  113.         $breadcrumbs = [
  114.             [
  115.                 'label' => $this->translator->trans('Support Center'),
  116.                 'url'   => $this->generateUrl('helpdesk_knowledgebase')
  117.             ],
  118.             [
  119.                 'label' => $solution->getName(),
  120.                 'url'   => '#'
  121.             ],
  122.         ];
  123.         $testArray = [1234];
  124.         foreach ($testArray as $test) {
  125.             $categories[] = [
  126.                 'id'           => $test,
  127.                 'name'         => $test " name",
  128.                 'articleCount' => $test " articleCount",
  129.             ];
  130.         }
  131.         return $this->render('@UVDeskSupportCenter//Knowledgebase//folder.html.twig', [
  132.             'folder'        => $solution,
  133.             'categoryCount' => $this->getDoctrine()
  134.                 ->getRepository(SupportEntities\Solutions::class)
  135.                 ->getCategoriesCountBySolution($solution->getId()),
  136.             'categories'    => $this->getDoctrine()
  137.                 ->getRepository(SupportEntities\Solutions::class)
  138.                 ->getCategoriesWithCountBySolution($solution->getId()),
  139.             'breadcrumbs'   => $breadcrumbs
  140.         ]);
  141.     }
  142.     public function viewFolderArticle(Request $request)
  143.     {
  144.         $this->isKnowledgebaseActive();
  145.         if (! $request->attributes->get('solution'))
  146.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  147.         $filterArray = ['id' => $request->attributes->get('solution')];
  148.         $solution $this->getDoctrine()
  149.                     ->getRepository(SupportEntities\Solutions::class)
  150.                     ->findOneBy($filterArray);
  151.         if (! $solution)
  152.             $this->noResultFound();
  153.             
  154.         if ($solution->getVisibility() == 'private')
  155.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  156.         $breadcrumbs = [
  157.             [
  158.                 'label' => $this->translator->trans('Support Center'),
  159.                 'url'   => $this->generateUrl('helpdesk_knowledgebase')
  160.             ],
  161.             [
  162.                 'label' => $solution->getName(),
  163.                 'url'   => '#'
  164.             ],
  165.         ];
  166.         $parameterBag = [
  167.             'solutionId'  => $solution->getId(),
  168.             'status'      => 1,
  169.             'sort'        => 'id',
  170.             'direction'   => 'desc'
  171.         ];
  172.         $article_data = [
  173.             'folder'        => $solution,
  174.             'articlesCount' => $this->getDoctrine()
  175.                 ->getRepository(SupportEntities\Solutions::class)
  176.                 ->getArticlesCountBySolution($solution->getId(), [1]),
  177.             'articles'      => $this->getDoctrine()
  178.                 ->getRepository(SupportEntities\Article::class)
  179.                 ->getAllArticles(new ParameterBag($parameterBag), $this->constructContainer'a.id, a.name, a.slug, a.stared'),
  180.             'breadcrumbs'   => $breadcrumbs,
  181.         ];
  182.         return $this->render('@UVDeskSupportCenter/Knowledgebase/folderArticle.html.twig'$article_data);
  183.     }
  184.     public function viewCategory(Request $request)
  185.     {
  186.         $this->isKnowledgebaseActive();
  187.         if (!$request->attributes->get('category'))
  188.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  189.         $filterArray = array(
  190.                             'id'     => $request->attributes->get('category'),
  191.                             'status' => 1,
  192.                         );
  193.        
  194.         $category $this->getDoctrine()
  195.                     ->getRepository(SupportEntities\SolutionCategory::class)
  196.                     ->findOneBy($filterArray);
  197.     
  198.         if (! $category)
  199.             $this->noResultFound();
  200.         $breadcrumbs = [
  201.             [ 'label' => $this->translator->trans('Support Center'),'url' => $this->generateUrl('helpdesk_knowledgebase') ],
  202.             [ 'label' => $category->getName(),'url' => '#' ],
  203.         ];
  204.         
  205.         $parameterBag = [
  206.             'categoryId' => $category->getId(),
  207.             'status'     => 1,
  208.             'sort'       => 'id',
  209.             'direction'  => 'desc'
  210.         ];
  211.         $category_data=  array(
  212.             'category'      => $category,
  213.             'articlesCount' => $this->getDoctrine()
  214.                                 ->getRepository(SupportEntities\SolutionCategory::class)
  215.                                 ->getArticlesCountByCategory($category->getId(), [1]),
  216.             'articles'      => $this->getDoctrine()
  217.                                 ->getRepository(SupportEntities\Article::class)
  218.                                 ->getAllArticles(new ParameterBag($parameterBag), $this->constructContainer'a.id, a.name, a.slug, a.stared'),
  219.             'breadcrumbs'   => $breadcrumbs
  220.         );
  221.         return $this->render('@UVDeskSupportCenter/Knowledgebase/category.html.twig',$category_data);
  222.     }
  223.    
  224.     public function viewArticle(Request $request)
  225.     {
  226.         $this->isKnowledgebaseActive();
  227.        
  228.         if (!$request->attributes->get('article') && !$request->attributes->get('slug')) {
  229.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  230.         }
  231.         $entityManager $this->getDoctrine()->getManager();
  232.         $user $this->userService->getCurrentUser();
  233.         $articleRepository $entityManager->getRepository(SupportEntities\Article::class);
  234.         if ($request->attributes->get('article')) {
  235.             $article $articleRepository->findOneBy(['status' => 1'id' => $request->attributes->get('article')]);
  236.         } else {
  237.             $article $articleRepository->findOneBy(['status' => 1,'slug' => $request->attributes->get('slug')]);
  238.         }
  239.        
  240.         if (empty($article)) {
  241.             $this->noResultFound();
  242.         }
  243.         $article->setContent($article->getContent());
  244.         $article->setViewed((int) $article->getViewed() + 1);
  245.         
  246.         // Log article view
  247.         $articleViewLog = new SupportEntities\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->getAllRelatedByArticle(['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(SupportEntities\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(SupportEntities\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. }