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

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