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

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. public function login(Request $request)
  31. {
  32. if ($request->getSession()->get('is_authenticated')) {
  33.         return $this->redirectToRoute('helpdesk_knowledgebase');
  34.     }
  35.     // check if user has submitted the login form
  36.     if ($request->isMethod('POST')) {
  37.         // retrieve the username and password from POST data
  38.         $username $request->request->get('username');
  39.         $password $request->request->get('password');
  40.     $em $this->getDoctrine()->getManager();
  41.         $conn $em->getConnection();
  42.         $stmt $conn->prepare('SELECT initial_check(:param1, :param2)');
  43.         $stmt->bindParam(':param1'$username);
  44.         $stmt->bindParam(':param2'$password);
  45.         $stmt->execute();
  46.         $result $stmt->fetchAll();
  47. $jsonString json_encode($result[0]);
  48. $data json_decode($jsonStringtrue);
  49. $value reset($data);
  50.         // check if the username and password are correct
  51.     if ($value === 1) {
  52.         $request->getSession()->set('is_authenticated'true);
  53.         $request->getSession()->set('user'$username);
  54.         return $this->redirectToRoute('helpdesk_knowledgebase');        
  55.     } else {
  56.         $request->getSession()->set('is_authenticated'false);
  57.         $this->addFlash('error''Invalid username or password');
  58.         return $this->redirectToRoute('login_route');
  59.         }
  60.     }
  61.     // if the user has not submitted the login form, display the login page
  62.     return $this->render('login.html.twig');
  63. }
  64.     private function isKnowledgebaseActive()
  65.     {
  66.         $entityManager $this->getDoctrine()->getManager();
  67.         $website $entityManager->getRepository(CoreEntites\Website::class)->findOneByCode('knowledgebase');
  68.         if (!empty($website)) {
  69.             $knowledgebaseWebsite $entityManager->getRepository(SupportEntites\KnowledgebaseWebsite::class)->findOneBy(['website' => $website->getId(), 'status' => true]);
  70.             if (!empty($knowledgebaseWebsite) && true == $knowledgebaseWebsite->getIsActive()) {
  71.                 return true;
  72.             }
  73.         }
  74.         throw new NotFoundHttpException('Page Not Found');
  75.     }
  76.     public function home(Request $request)
  77.     {
  78.         $this->isKnowledgebaseActive();
  79.     $is_authenticated $request->getSession()->get('is_authenticated'false);
  80.     if (!$is_authenticated) {
  81.             // redirect to login page
  82.             return $this->redirectToRoute('login_route');
  83.         }
  84.         $parameterBag = [
  85.             'visibility' => 'public',
  86.             'sort' => 'id',
  87.             'direction' => 'desc'
  88.         ];
  89.         $articleRepository $this->getDoctrine()->getRepository(SupportEntites\Article::class);
  90.         $solutionRepository $this->getDoctrine()->getRepository(SupportEntites\Solutions::class);
  91.         $twigResponse = [
  92.         'searchDisable' => false,
  93.             'popArticles' => $articleRepository->getPopularTranslatedArticles($request->getLocale()),
  94.             'solutions' => $solutionRepository->getAllSolutions(new ParameterBag($parameterBag), $this->constructContainer'a', [1]),
  95.         ];
  96.         $newResult = [];
  97.        
  98.         foreach ($twigResponse['solutions'] as $key => $result) {
  99.             $newResult[] = [
  100.                 'id' => $result->getId(),
  101.                 'name' => $result->getName(),
  102.                 'description' => $result->getDescription(),
  103.                 'visibility' => $result->getVisibility(),
  104.                 'solutionImage' => ($result->getSolutionImage() == null) ? '' $result->getSolutionImage(),
  105.                 'categoriesCount' => $solutionRepository->getCategoriesCountBySolution($result->getId()),
  106.                 'categories' => $solutionRepository->getCategoriesWithCountBySolution($result->getId()),
  107.                 'articleCount' => $solutionRepository->getArticlesCountBySolution($result->getId()),
  108.             ];
  109.         }
  110.         $twigResponse['solutions']['results'] = $newResult;
  111.         $twigResponse['solutions']['categories'] = array_map(function($category) use ($articleRepository) {
  112.             $parameterBag = [
  113.                 'categoryId' => $category['id'],
  114.                 'status' => 1,
  115.                 'sort' => 'id',
  116.                 'limit'=>10,
  117.                 'direction' => 'desc'
  118.             ];
  119.             $article =  $articleRepository->getAllArticles(new ParameterBag($parameterBag), $this->constructContainer'a.id, a.name, a.slug, a.stared');
  120.              
  121.             return [
  122.                 'id' => $category['id'],
  123.                 'name' => $category['name'],
  124.                 'description' => $category['description'],
  125.                 'articles' => $article
  126.             ];
  127.         }, $solutionRepository->getAllCategories(102));
  128.             return $this->render('@UVDeskSupportCenter//Knowledgebase//index.html.twig'$twigResponse);
  129.     }
  130.     public function listCategories(Request $request)
  131.     {
  132.         $this->isKnowledgebaseActive();
  133.     $is_authenticated $request->getSession()->get('is_authenticated'false);
  134.     if (!$is_authenticated) {
  135.             // redirect to login page
  136.             return $this->redirectToRoute('login_route');
  137.         }
  138.         $solutionRepository $this->getDoctrine()->getRepository(SupportEntites\Solutions::class);
  139.         $categoryCollection $solutionRepository->getAllCategories(104);
  140.         
  141.         return $this->render('@UVDeskSupportCenter/Knowledgebase/categoryListing.html.twig', [
  142.             'categories' => $categoryCollection,
  143.             'categoryCount' => count($categoryCollection),
  144.         ]);
  145.     }
  146.     public function viewFolder(Request $request)
  147.     {
  148.         $this->isKnowledgebaseActive();
  149.     $is_authenticated $request->getSession()->get('is_authenticated'false);
  150.     if (!$is_authenticated) {
  151.             // redirect to login page
  152.             return $this->redirectToRoute('login_route');
  153.         }
  154.         
  155.         if(!$request->attributes->get('solution'))
  156.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  157.         $filterArray = ['id' => $request->attributes->get('solution')];
  158.         $solution $this->getDoctrine()
  159.                     ->getRepository(SupportEntites\Solutions::class)
  160.                     ->findOneBy($filterArray);
  161.         if(!$solution)
  162.             $this->noResultFound();
  163.         $breadcrumbs = [
  164.             [
  165.                 'label' => $this->translator->trans('Support Center'),
  166.                 'url' => $this->generateUrl('helpdesk_knowledgebase')
  167.             ],
  168.             [
  169.                 'label' => $solution->getName(),
  170.                 'url' => '#'
  171.             ],
  172.         ];
  173.         $testArray = [1234];
  174.         foreach ($testArray as $test) {
  175.             $categories[] = [
  176.                 'id' => $test,
  177.                 'name' => $test " name",
  178.                 'articleCount' => $test " articleCount",
  179.             ];
  180.         }
  181.         return $this->render('@UVDeskSupportCenter//Knowledgebase//folder.html.twig', [
  182.             'folder' => $solution,
  183.             'categoryCount' => $this->getDoctrine()
  184.                 ->getRepository(SupportEntites\Solutions::class)
  185.                 ->getCategoriesCountBySolution($solution->getId()),
  186.             'categories' => $this->getDoctrine()
  187.                 ->getRepository(SupportEntites\Solutions::class)
  188.                 ->getCategoriesWithCountBySolution($solution->getId()),
  189.             'breadcrumbs' => $breadcrumbs
  190.         ]);
  191.     }
  192.     public function viewFolderArticle(Request $request)
  193.     {
  194.         $this->isKnowledgebaseActive();
  195.     $is_authenticated $request->getSession()->get('is_authenticated'false);
  196.     if (!$is_authenticated) {
  197.             // redirect to login page
  198.             return $this->redirectToRoute('login_route');
  199.         }
  200.         if(!$request->attributes->get('solution'))
  201.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  202.         $filterArray = ['id' => $request->attributes->get('solution')];
  203.         $solution $this->getDoctrine()
  204.                     ->getRepository(SupportEntites\Solutions::class)
  205.                     ->findOneBy($filterArray);
  206.         if(!$solution)
  207.             $this->noResultFound();
  208.         $breadcrumbs = [
  209.             [
  210.                 'label' => $this->translator->trans('Support Center'),
  211.                 'url' => $this->generateUrl('helpdesk_knowledgebase')
  212.             ],
  213.             [
  214.                 'label' => $solution->getName(),
  215.                 'url' => '#'
  216.             ],
  217.         ];
  218.         $parameterBag = [
  219.             'solutionId' => $solution->getId(),
  220.             'status' => 1,
  221.             'sort' => 'id',
  222.             'direction' => 'desc'
  223.         ];
  224.         $article_data = [
  225.             'folder' => $solution,
  226.             'articlesCount' => $this->getDoctrine()
  227.                 ->getRepository(SupportEntites\Solutions::class)
  228.                 ->getArticlesCountBySolution($solution->getId(), [1]),
  229.             'articles' => $this->getDoctrine()
  230.                 ->getRepository(SupportEntites\Article::class)
  231.                 ->getAllArticles(new ParameterBag($parameterBag), $this->constructContainer'a.id, a.name, a.slug, a.stared'),
  232.             'breadcrumbs' => $breadcrumbs,
  233.         ];
  234.         return $this->render('@UVDeskSupportCenter/Knowledgebase/folderArticle.html.twig'$article_data);
  235.     }
  236.     public function viewCategory(Request $request)
  237.     {
  238.         $this->isKnowledgebaseActive();
  239.     $is_authenticated $request->getSession()->get('is_authenticated'false);
  240.     if (!$is_authenticated) {
  241.             // redirect to login page
  242.             return $this->redirectToRoute('login_route');
  243.         }
  244.         if(!$request->attributes->get('category'))
  245.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  246.         $filterArray = array(
  247.                             'id' => $request->attributes->get('category'),
  248.                             'status' => 1,
  249.                         );
  250.        
  251.         $category $this->getDoctrine()
  252.                     ->getRepository(SupportEntites\SolutionCategory::class)
  253.                     ->findOneBy($filterArray);
  254.     
  255.         if(!$category)
  256.             $this->noResultFound();
  257.         $breadcrumbs = [
  258.             [ 'label' => $this->translator->trans('Support Center'),'url' => $this->generateUrl('helpdesk_knowledgebase') ],
  259.             [ 'label' => $category->getName(),'url' => '#' ],
  260.         ];
  261.         
  262.         $parameterBag = [
  263.             'categoryId' => $category->getId(),
  264.             'status' => 1,
  265.             'sort' => 'id',
  266.             'direction' => 'desc'
  267.         ];
  268.         $category_data=  array(
  269.             'category' => $category,
  270.             'articlesCount' => $this->getDoctrine()
  271.                             ->getRepository(SupportEntites\SolutionCategory::class)
  272.                             ->getArticlesCountByCategory($category->getId(), [1]),
  273.             'articles' => $this->getDoctrine()
  274.                         ->getRepository(SupportEntites\Article::class)
  275.                         ->getAllArticles(new ParameterBag($parameterBag), $this->constructContainer'a.id, a.name, a.slug, a.stared'),
  276.             'breadcrumbs' => $breadcrumbs
  277.         );
  278.         return $this->render('@UVDeskSupportCenter/Knowledgebase/category.html.twig',$category_data);
  279.     }
  280.    
  281.     public function viewArticle(Request $request)
  282.     {
  283.         $this->isKnowledgebaseActive();
  284.     $is_authenticated $request->getSession()->get('is_authenticated'false);
  285.     if (!$is_authenticated) {
  286.             // redirect to login page
  287.             return $this->redirectToRoute('login_route');
  288.         }
  289.        
  290.         if (!$request->attributes->get('article') && !$request->attributes->get('slug')) {
  291.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  292.         }
  293.         $entityManager $this->getDoctrine()->getManager();
  294.         $user $this->userService->getCurrentUser();
  295.         $articleRepository $entityManager->getRepository(SupportEntites\Article::class);
  296.         if ($request->attributes->get('article')) {
  297.             $article $articleRepository->findOneBy(['status' => 1'id' => $request->attributes->get('article')]);
  298.         } else {
  299.             $article $articleRepository->findOneBy(['status' => 1,'slug' => $request->attributes->get('slug')]);
  300.         }
  301.        
  302.         if (empty($article)) {
  303.             $this->noResultFound();
  304.         }
  305.         $stringReplace str_replace("<ol>","<ul>",$article->getContent());
  306.         $stringReplace str_replace("</ol>","</ul>",$stringReplace);
  307.         $article->setContent($stringReplace);
  308.         $article->setViewed((int) $article->getViewed() + 1);
  309.         
  310.         // Log article view
  311.         $articleViewLog = new SupportEntites\ArticleViewLog();
  312.         $articleViewLog->setUser(($user != null && $user != 'anon.') ? $user null);
  313.         
  314.         $articleViewLog->setArticle($article);
  315.         $articleViewLog->setViewedAt(new \DateTime('now'));
  316.         $entityManager->persist($article);
  317.         $entityManager->persist($articleViewLog);
  318.         $entityManager->flush();
  319.         
  320.         // Get article feedbacks
  321.         $feedbacks = ['enabled' => false'submitted' => false'article' => $articleRepository->getArticleFeedbacks($article)];
  322.         if (!empty($user) && $user != 'anon.') {
  323.             $feedbacks['enabled'] = true;
  324.             if (!empty($feedbacks['article']['collection']) && in_array($user->getId(), array_column($feedbacks['article']['collection'], 'user'))) {
  325.                 $feedbacks['submitted'] = true;
  326.             }
  327.         }
  328.         // @TODO: App popular articles
  329.         $article_details = [
  330.             'article' => $article,
  331.             'breadcrumbs' => [
  332.                 ['label' => $this->translator->trans('Support Center'), 'url' => $this->generateUrl('helpdesk_knowledgebase')],
  333.                 ['label' => $article->getName(), 'url' => '#']
  334.             ],
  335.             'dateAdded' => $this->userService->convertToTimezone($article->getDateAdded()),
  336.             'articleTags' => $articleRepository->getTagsByArticle($article->getId()),
  337.             'articleAuthor' => $articleRepository->getArticleAuthorDetails($article->getId()),
  338.             'relatedArticles' => $articleRepository->getAllRelatedyByArticle(['locale' => $request->getLocale(), 'articleId' => $article->getId()], [1]),
  339.             'popArticles'  => $articleRepository->getPopularTranslatedArticles($request->getLocale())
  340.         ];
  341.         return $this->render('@UVDeskSupportCenter/Knowledgebase/article.html.twig',$article_details);
  342.     }
  343.     public function searchKnowledgebase(Request $request)
  344.     {
  345.         $this->isKnowledgebaseActive();
  346.     $is_authenticated $request->getSession()->get('is_authenticated'false);
  347.     if (!$is_authenticated) {
  348.             // redirect to login page
  349.             return $this->redirectToRoute('login_route');
  350.         }
  351.         $searchQuery $request->query->get('s');
  352.         if (empty($searchQuery)) {
  353.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  354.         }
  355.     $user $request->getSession()->get('user');
  356.     $em $this->getDoctrine()->getManager();
  357.         $conn $em->getConnection();
  358.         $stmt $conn->prepare('CALL sp_custom_searchKeywords(:param1, :param2)');
  359.         $stmt->bindParam(':param1'$searchQuery);
  360.         $stmt->bindParam(':param2'$user);
  361.         $stmt->execute();
  362.         $articleCollection $this->getDoctrine()->getRepository(SupportEntites\Article::class)->getArticleBySearch($request);
  363.         return $this->render('@UVDeskSupportCenter/Knowledgebase/search.html.twig', [
  364.             'search' => $searchQuery,
  365.             'articles' => $articleCollection,
  366.         ]);
  367.     }
  368.     public function viewTaggedResources(Request $request)
  369.     {
  370.         $this->isKnowledgebaseActive();
  371.     $is_authenticated $request->getSession()->get('is_authenticated'false);
  372.     if (!$is_authenticated) {
  373.             // redirect to login page
  374.             return $this->redirectToRoute('login_route');
  375.         }
  376.         $tagQuery $request->attributes->get('tag');
  377.         if (empty($tagQuery)) {
  378.             return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  379.         }
  380.         $tagLabel $request->attributes->get('name');
  381.         $articleCollection $this->getDoctrine()->getRepository(SupportEntites\Article::class)->getArticleByTags([$tagLabel]);
  382.         return $this->render('@UVDeskSupportCenter/Knowledgebase/search.html.twig', [
  383.             'articles' => $articleCollection,
  384.             'search' => $tagLabel,
  385.             'breadcrumbs' => [
  386.                 ['label' => $this->translator->trans('Support Center'), 'url' => $this->generateUrl('helpdesk_knowledgebase')],
  387.                 ['label' => $tagLabel'url' => '#'],
  388.             ],
  389.         ]);
  390.     }
  391.     public function rateArticle($articleIdRequest $request)
  392.     {
  393.         $this->isKnowledgebaseActive();
  394.     $is_authenticated $request->getSession()->get('is_authenticated'false);
  395.     if (!$is_authenticated) {
  396.             // redirect to login page
  397.             return $this->redirectToRoute('login_route');
  398.         }
  399.         // @TODO: Refactor
  400.             
  401.         // if ($request->getMethod() != 'POST') {
  402.         //     return $this->redirect($this->generateUrl('helpdesk_knowledgebase'));
  403.         // }
  404.         // $company = $this->getCompany();
  405.         // $user = $this->userService->getCurrentUser();
  406.         $response = ['code' => 404'content' => ['alertClass' => 'danger''alertMessage' => 'An unexpected error occurred. Please try again later.']];
  407.         // if (!empty($user) && $user != 'anon.') {
  408.         //     $entityManager = $this->getDoctrine()->getEntityManager();
  409.         //     $article = $entityManager->getRepository('WebkulSupportCenterBundle:Article')->findOneBy(['id' => $articleId, 'companyId' => $company->getId()]);
  410.         //     if (!empty($article)) {
  411.         //         $providedFeedback = $request->request->get('feedback');
  412.         //         if (!empty($providedFeedback) && in_array(strtolower($providedFeedback), ['positive', 'neagtive'])) {
  413.         //             $isArticleHelpful = ('positive' == strtolower($providedFeedback)) ? true : false;
  414.         //             $articleFeedback = $entityManager->getRepository('WebkulSupportCenterBundle:ArticleFeedback')->findOneBy(['article' => $article, 'ratedCustomer' => $user]);
  415.         //             $response = ['code' => 200, 'content' => ['alertClass' => 'success', 'alertMessage' => 'Feedback saved successfully.']];
  416.         //             if (empty($articleFeedback)) {
  417.         //                 $articleFeedback = new \Webkul\SupportCenterBundle\Entity\ArticleFeedback();
  418.         //                 // $articleBadge->setDescription('');
  419.         //                 $articleFeedback->setIsHelpful($isArticleHelpful);
  420.         //                 $articleFeedback->setArticle($article);
  421.         //                 $articleFeedback->setRatedCustomer($user);
  422.         //                 $articleFeedback->setCreatedAt(new \DateTime('now'));
  423.         //             } else {
  424.         //                 $articleFeedback->setIsHelpful($isArticleHelpful);
  425.         //                 $response['content']['alertMessage'] = 'Feedback updated successfully.';
  426.         //             }
  427.         //             $entityManager->persist($articleFeedback);
  428.         //             $entityManager->flush();
  429.         //         } else {
  430.         //             $response['content']['alertMessage'] = 'Invalid feedback provided.';
  431.         //         }
  432.         //     } else {
  433.         //         $response['content']['alertMessage'] = 'Article not found.';
  434.         //     }
  435.         // } else {
  436.         //     $response['content']['alertMessage'] = 'You need to login to your account before can perform this action.';
  437.         // }
  438.         return new Response(json_encode($response['content']), $response['code'], ['Content-Type: application/json']);
  439.     }
  440.     /**
  441.      * If customer is playing with url and no result is found then what will happen
  442.      * @return 
  443.      */
  444.     protected function noResultFound()
  445.     {
  446.         throw new NotFoundHttpException('Not Found!');
  447.     }
  448. }