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