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

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