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

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