uvdesk/support-center-bundle/Controller/Website.php line 39

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