vendor/uvdesk/support-center-bundle/Controller/Article.php line 147

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\SupportCenterBundle\Controller;
  3. use Doctrine\Common\Collections\ArrayCollection;
  4. use Doctrine\Common\Collections\Criteria;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  10. use Webkul\UVDesk\SupportCenterBundle\Form;
  11. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  12. use Webkul\UVDesk\CoreFrameworkBundle\Services\UserService;
  13. use Webkul\UVDesk\CoreFrameworkBundle\Services\UVDeskService;
  14. use Symfony\Contracts\Translation\TranslatorInterface;
  15. use Symfony\Component\DependencyInjection\ContainerInterface;
  16. use Webkul\UVDesk\SupportCenterBundle\Entity as SupportEntites;
  17. use Webkul\UVDesk\CoreFrameworkBundle\Entity as CoreEntites
  18. class Article extends AbstractController
  19. {
  20.     private $userService;
  21.     private $eventDispatcher;
  22.     private $translator;
  23.     private $uvdeskService;
  24.     public function __construct(UserService $userServiceUVDeskService $uvdeskService,EventDispatcherInterface $eventDispatcherTranslatorInterface $translator)
  25.     {
  26.         $this->userService $userService;
  27.         $this->eventDispatcher $eventDispatcher;
  28.         $this->translator $translator;
  29.         $this->uvdeskService $uvdeskService;
  30.     }
  31.     public function ArticleList(Request $requestContainerInterface $container)
  32.     {
  33.         if (!$this->userService->isAccessAuthorized('ROLE_AGENT_MANAGE_KNOWLEDGEBASE')) {
  34.             return $this->redirect($this->generateUrl('helpdesk_member_dashboard'));
  35.         }
  36.         $solutions $this->getDoctrine()
  37.             ->getRepository(SupportEntites\Solutions::class)
  38.             ->getAllSolutions(null$container'a.id, a.name');
  39.         if ($solutions) {
  40.             foreach($solutions as $key => $solution) {
  41.                 $solutions[$key]['categories'] = $this->getDoctrine()
  42.                     ->getRepository(SupportEntites\Solutions::class)
  43.                     ->getCategoriesWithCountBySolution($solution['id']);
  44.             }
  45.         }
  46.         return $this->render('@UVDeskSupportCenter/Staff/Articles/articleList.html.twig', [
  47.             'solutions' => $solutions
  48.         ]);
  49.     }
  50.     public function ArticleListByCategory(Request $requestContainerInterface $container)
  51.     {
  52.         $category $this->getDoctrine()
  53.             ->getRepository(SupportEntites\SolutionCategory::class)
  54.             ->findCategoryById(['id' => $request->attributes->get('category')]);
  55.         if ($category) {
  56.             return $this->render('@UVDeskSupportCenter/Staff/Articles/articleListByCategory.html.twig',[
  57.                 'category' => $category,
  58.                 'articleCount'      => $this->getDoctrine()
  59.                     ->getRepository(SupportEntites\SolutionCategory::class)
  60.                     ->getArticlesCountByCategory($request->attributes->get('category')),
  61.                 'categorySolutions' => $this->getDoctrine()
  62.                     ->getRepository(SupportEntites\SolutionCategory::class)
  63.                     ->getSolutionsByCategory($request->attributes->get('category')),
  64.                 'solutions'         => $this->getDoctrine()
  65.                     ->getRepository(SupportEntites\Solutions::class)
  66.                     ->getAllSolutions(null$container'a.id, a.name')
  67.             ]);
  68.         } else {
  69.             $this->noResultFound();
  70.         }
  71.     }
  72.     public function ArticleListBySolution(Request $request)
  73.     {
  74.         $solution $this->getDoctrine()
  75.             ->getRepository(SupportEntites\Solutions::class)
  76.             ->findSolutionById(['id' => $request->attributes->get('solution')]);
  77.         if ($solution) {
  78.             return $this->render('@UVDeskSupportCenter/Staff/Articles/articleListBySolution.html.twig', [
  79.                 'solution' => $solution,
  80.                 'solutionArticleCount'  => $this->getDoctrine()
  81.                     ->getRepository(SupportEntites\Solutions::class)
  82.                     ->getArticlesCountBySolution($request->attributes->get('solution')),
  83.                 'solutionCategoryCount' => $this->getDoctrine()
  84.                     ->getRepository(SupportEntites\Solutions::class)
  85.                     ->getCategoriesCountBySolution($request->attributes->get('solution')),
  86.             ]);
  87.         } else {
  88.             $this->noResultFound();
  89.         }
  90.     }
  91.     public function ArticleListXhr(Request $requestContainerInterface $container)
  92.     {
  93.         $json = array();
  94.         $repository $this->getDoctrine()->getRepository(SupportEntites\Article::class);
  95.         if($request->attributes->get('category'))
  96.             $request->query->set('categoryId'$request->attributes->get('category'));
  97.         if($request->attributes->get('solution'))
  98.             $request->query->set('solutionId'$request->attributes->get('solution'));
  99.         $json $repository->getAllArticles($request->query$container);
  100.         $response = new Response(json_encode($json));
  101.         $response->headers->set('Content-Type''application/json');
  102.         return $response;
  103.     }
  104.     public function articleHistoryXhr(Request $request)
  105.     {
  106.         $json = array();
  107.         $repository $this->getDoctrine()->getRepository(SupportEntites\Article::class);
  108.         $params = ['articleId' => $request->attributes->get('id')];
  109.         $json $repository->getAllHistoryByArticle($params);
  110.         if ($json) {
  111.             foreach($json as $key => $js) {
  112.                 $json[$key]['dateAdded'] = [
  113.                     'format' => $this->userService->convertToTimezone($js['dateAdded']),
  114.                     'timestamp' => $this->userService->convertToDatetimeTimezoneTimestamp($js['dateAdded']),
  115.                 ];
  116.             }
  117.         }
  118.         $response = new Response(json_encode($json));
  119.         $response->headers->set('Content-Type''application/json');
  120.         return $response;
  121.     }
  122.     public function articleRelatedXhr(Request $request)
  123.     {
  124.         $json = array();
  125.         $repository $this->getDoctrine()->getRepository(SupportEntites\Article::class);
  126.         $params = ['articleId' => $request->attributes->get('id')];
  127.         $json $repository->getAllRelatedyByArticle($params);
  128.         $response = new Response(json_encode($json));
  129.         $response->headers->set('Content-Type''application/json');
  130.         return $response;
  131.     }
  132.     protected function getArticle($filterArray = array())
  133.     {
  134.         if ($filterArray) {
  135.             return $this->getDoctrine()
  136.                 ->getRepository(SupportEntites\Article::class)
  137.                 ->findOneBy($filterArray);
  138.         }
  139.         return false;
  140.     }
  141.     public function article(Request $requestContainerInterface $container)
  142.     {
  143.         if ($request->attributes->get('id')) {
  144.             $article $this->getArticle(['id' => $request->attributes->get('id')]);
  145.             if(!$article)
  146.                 $this->noResultFound();
  147.         } else {
  148.             $article = new SupportEntites\Article;
  149.         }
  150.         $articleCategory $articleTags = [];
  151.         if ($article->getId()) {
  152.             $articleCategory $this->getDoctrine()
  153.                 ->getRepository(SupportEntites\Article::class)
  154.                 ->getCategoryByArticle($article->getId());
  155.             $articleTags $this->getDoctrine()
  156.                 ->getRepository(SupportEntites\Article::class)
  157.                 ->getTagsByArticle($article->getId());
  158.         }
  159.         $categories $this->getDoctrine()
  160.             ->getRepository(SupportEntites\SolutionCategory::class)
  161.             ->getAllCategories(null$container'a.id, a.name');
  162.         if ($request->attributes->get('id')) {
  163.             return  $this->render('@UVDeskSupportCenter/Staff/Articles/articleForm.html.twig', [
  164.                 'article' => $article,
  165.                 'articleCategory' => $articleCategory,
  166.                 'articleTags' => $articleTags,
  167.                 'categories' => $categories
  168.             ]);
  169.         }
  170.         return $this->render'@UVDeskSupportCenter/Staff/Articles/articleAddForm.html.twig', [ 'article' => $article ] );
  171.     }
  172.     public function articleXhr(Request $request)
  173.     {
  174.         // Proceed only if user has access to the resource        
  175.         if( (!$this->userService->getSessionUser()) || (!$this->userService->isAccessAuthorized('ROLE_AGENT_MANAGE_KNOWLEDGEBASE')) )
  176.         {
  177.             throw new \Exception('Access Denied'403); 
  178.         }
  179.         $json = array();
  180.         if ($request->getMethod() == "POST") {
  181.             $data $request->request->get("data");
  182.             $entityManager $this->getDoctrine()->getManager();
  183.             if (isset($data['actionType'])) {
  184.                 switch ($data['actionType']) {
  185.                     case 'articleUpdate':
  186.                         if ('articleSave' == $data['actionType']  && !empty($resources['articles']['showAlert']) ) {
  187.                             $json['alertClass'] = 'danger';
  188.                             return new JsonResponse($json);
  189.                         }
  190.                         if ($data['ids'][0]) {
  191.                             $article $this->getArticle(['id' => $data['ids'][0]]);
  192.                         } else {
  193.                             $article = new SupportEntites\Article;
  194.                         }
  195.                         $json['errors'] = [];
  196.                         if ($article) {
  197.                             if (strlen($data['name']) > 200) {
  198.                                 $json['errors']['name'] = $this->translator->trans('Name length must not be greater than 200 !!');
  199.                             }
  200.                             if (!$json['errors']) {
  201.                                 unset($json['errors']);
  202.                                 $article->setName($data['name']);
  203.                                 $article->setSlug($data['slug']);
  204.                                 $article->setMetaTitle($data['metaTitle']);
  205.                                 $article->setKeywords($data['keywords']);
  206.                                 $article->setMetaDescription($data['metaDescription']);
  207.                                 $updateRevisionHistory false;
  208.                                 if ($article->getContent() == null || trim($article->getContent()) != trim($data['content'])) {
  209.                                     $updateRevisionHistory true;
  210.                                     $article->setContent($data['content']);
  211.                                 }
  212.                                 $entityManager->persist($article);
  213.                                 $entityManager->flush();
  214.                                 $json['alertClass'] = 'success';
  215.                                 $json['alertMessage'] = $this->translator->trans('Success! Article updated successfully');
  216.                                 if (!$data['ids'][0]) {
  217.                                     $json['redirect'] = $this->generateUrl('helpdesk_member_knowledgebase_update_article', array('id' => $article->getId()));
  218.                                 }
  219.                             } else {
  220.                                 $json['alertClass'] = 'danger';
  221.                                 $json['alertMessage'] = $this->translator->trans('Warning! Correct all field values first!');
  222.                             }
  223.                         } else {
  224.                             $json['alertClass'] = 'danger';
  225.                             $json['alertMessage'] = $this->translator->trans('Warning ! This is not a valid request');
  226.                         }
  227.                         break;
  228.                     case 'articleSave':
  229.                         if ('articleSave' == $data['actionType']  && !empty($resources['articles']['showAlert']) ) {
  230.                             $json['alertClass'] = 'danger';
  231.                             return new JsonResponse($json);
  232.                         }
  233.                         if ($data['ids'][0]) {
  234.                             $article $this->getArticle(['id' => $data['ids'][0]]);
  235.                         } else {
  236.                             $article = new SupportEntites\Article;
  237.                         }
  238.                         $json['errors'] = [];
  239.                         if ($article) {
  240.                             if (strlen($data['name']) > 200) {
  241.                                 $json['errors']['name'] = $this->translator->trans('Name length must not be greater than 200 !!');
  242.                             }
  243.                             $slug $entityManager->getRepository(SupportEntites\Article::class)->findOneBy(['slug' => $data['slug']]);
  244.                             
  245.                             if (!empty($slug)) {
  246.                                 $json['errors']['slug'] = $this->translator->trans('Warning! Article slug is not available.');
  247.                             }
  248.                             if (!$json['errors']) {
  249.                                 unset($json['errors']);
  250.                                 $article->setName($data['name']);
  251.                                 $article->setSlug($data['slug']);
  252.                                 $article->setMetaTitle($data['metaTitle']);
  253.                                 $article->setKeywords($data['keywords']);
  254.                                 $article->setMetaDescription($data['metaDescription']);
  255.                                 $updateRevisionHistory false;
  256.                                 if ($article->getContent() == null || trim($article->getContent()) != trim($data['content'])) {
  257.                                     $updateRevisionHistory true;
  258.                                     $article->setContent($data['content']);
  259.                                 }
  260.                                 $entityManager->persist($article);
  261.                                 $entityManager->flush();
  262.                                 $json['alertClass'] = 'success';
  263.                                 $json['alertMessage'] = $this->translator->trans('Success! Article updated successfully');
  264.                                 if (!$data['ids'][0]) {
  265.                                     $json['redirect'] = $this->generateUrl('helpdesk_member_knowledgebase_update_article', array('id' => $article->getId()));
  266.                                 }
  267.                             } else {
  268.                                 $json['alertClass'] = 'danger';
  269.                                 $json['alertMessage'] = $this->translator->trans('Warning! Correct all field values first!');
  270.                             }
  271.                         } else {
  272.                             $json['alertClass'] = 'danger';
  273.                             $json['alertMessage'] = $this->translator->trans('Warning ! This is not a valid request');
  274.                         }
  275.                         break;
  276.                     case 'status':
  277.                         $entityManager->getRepository(SupportEntites\Article::class)->bulkArticleStatusUpdate($data['ids'], $data['targetId']);
  278.                         $json['alertClass'] = 'success';
  279.                         $json['alertMessage'] = $this->translator->trans('Success ! Article updated successfully.');
  280.                         break;
  281.                     case 'tagUpdate':
  282.                         if ($data['action'] == 'remove') {
  283.                             $entityManager->getRepository(SupportEntites\Article::class)->removeTagByArticle($data['ids'][0], [$data['entityId']]);
  284.                             $json['alertClass'] = 'success';
  285.                             $json['alertMessage'] = $this->translator->trans('Success ! Tag removed successfully.');
  286.                             break;
  287.                         } elseif ($data['action'] == 'add') {
  288.                             $articleTagMapping = new SupportEntites\ArticleTags();
  289.                             $articleTagMapping->setArticleId($data['ids'][0]);
  290.                             $articleTagMapping->setTagId($data['entityId']);
  291.                             $entityManager->persist($articleTagMapping);
  292.                             $entityManager->flush();
  293.                         } elseif ($data['action'] == 'create') {
  294.                             $tag $entityManager->getRepository(CoreEntites\Tag::class)->findOneBy(['name' => $data['name']]);
  295.                             if (!$tag) {
  296.                                 $tag = new CoreEntites\Tag();
  297.                                 $tag->setName($data['name']);
  298.                                 $entityManager->persist($tag);
  299.                                 $entityManager->flush();
  300.                             }
  301.                             $articleTagMapping = new SupportEntites\ArticleTags();
  302.                             $articleTagMapping->setArticleId($data['ids'][0]);
  303.                             $articleTagMapping->setTagId($tag->getId());
  304.                             $entityManager->persist($tag);
  305.                             $entityManager->persist($articleTagMapping);
  306.                             $entityManager->flush();
  307.                             $json['tagId'] = $tag->getId();
  308.                             $json['tagName'] = $tag->getName();
  309.                         }
  310.                         $json['alertClass'] = 'success';
  311.                         $json['alertMessage'] = $this->translator->trans('Success ! Tags Saved successfully.');
  312.                         break;
  313.                     case 'contentUpdate':
  314.                         $article $this->getArticle(['id' => $data['ids'][0]]);
  315.                         if ($article) {
  316.                             if (trim($article->getContent()) != trim($data['content']))
  317.                                 $this->updateContent($article$data['content']);
  318.                             $json['alertClass'] = 'success';
  319.                             $json['alertMessage'] = $this->translator->trans('Success ! Revision restored successfully.');
  320.                         } else {
  321.                             $json['alertClass'] = 'danger';
  322.                             $json['alertMessage'] = $this->translator->trans('Warning ! This is not a valid request');
  323.                         }
  324.                         break;
  325.                     case 'categoryUpdate':
  326.                         if ($data['action'] == 'remove') {
  327.                             $this->getDoctrine()
  328.                                 ->getRepository(SupportEntites\Article::class)
  329.                                 ->removeCategoryByArticle($data['ids'][0], [$data['entityId']]);
  330.                         } else if ($data['action'] == 'add') {
  331.                             $articleCategoryMapping = new SupportEntites\ArticleCategory();
  332.                             $articleCategoryMapping->setArticleId($data['ids'][0]);
  333.                             $articleCategoryMapping->setCategoryId($data['entityId']);
  334.                             $entityManager->persist($articleCategoryMapping);
  335.                             $entityManager->flush();
  336.                         }
  337.                         $json['alertClass'] = 'success';
  338.                         $json['alertMessage'] = $this->translator->trans('Success ! Categories updated successfully.');
  339.                         break;
  340.                     case 'relatedUpdate':
  341.                         if ($data['action'] == 'remove') {
  342.                             $entityManager->getRepository(SupportEntites\Article::class)->removeRelatedByArticle($data['ids'][0], [$data['entityId']]);
  343.                             $json['alertClass'] = 'success';
  344.                             $json['alertMessage'] = $this->translator->trans('Success ! Article Related removed successfully.');
  345.                         } else if($data['action'] == 'add') {
  346.                             $relatedArticles $entityManager->getRepository(SupportEntites\ArticleRelatedArticle::class)->findBy([
  347.                                 'articleId' => $data['ids'][0],
  348.                                 'relatedArticleId' => $data['entityId'],
  349.                             ]);
  350.                             if (count($relatedArticles)) {
  351.                                 $json['alertClass'] = 'success';
  352.                                 $json['alertMessage'] = $this->translator->trans('Success ! Article Related is already added.');
  353.                             } elseif ($data['ids'][0] == $data['entityId']) {
  354.                                 $json['alertClass'] = 'danger';
  355.                                 $json['alertMessage'] = $this->translator->trans('Success ! Cannot add self as relative article.');
  356.                             } else {
  357.                                 $articleRelatedMapping = new SupportEntites\ArticleRelatedArticle();
  358.                                 $articleRelatedMapping->setArticleId($data['ids'][0]);
  359.                                 $articleRelatedMapping->setRelatedArticleId($data['entityId']);
  360.                                 $entityManager->persist($articleRelatedMapping);
  361.                                 $entityManager->flush();
  362.                                 $json['alertClass'] = 'success';
  363.                                 $json['alertMessage'] = $this->translator->trans('Success ! Article Related updated successfully.');
  364.                             }
  365.                         }
  366.                         break;
  367.                     case 'delete':
  368.                         if ($data['ids']) {
  369.                             foreach ($data['ids'] as $id) {
  370.                                 $article $entityManager->getRepository(SupportEntites\Article::class)->find($id);
  371.                                 if ($article) {
  372.                                     $entityManager->remove($article);
  373.                                     $entityManager->flush();
  374.                                 }
  375.                             }
  376.                             $this->removeArticle($article);
  377.                         }
  378.                         $json['alertClass'] = 'success';
  379.                         $json['alertMessage'] = $this->translator->trans('Success ! Articles removed successfully.');
  380.                         break;
  381.                     default:
  382.                         $json['alertClass'] = 'danger';
  383.                         $json['alertMessage'] = $this->translator->trans('Warning ! This is not a valid request');
  384.                 }
  385.             }
  386.         } elseif ($request->getMethod() == "PATCH") {
  387.             $entityManager $this->getDoctrine()->getManager();
  388.             $data json_decode($request->getContent(), true);
  389.             if (isset($data['editType']))
  390.                 switch($data['editType']) {
  391.                     case 'status':
  392.                         $entityManager->getRepository(SupportEntites\Article::class)->bulkArticleStatusUpdate([$data['id']], $data['value']);
  393.                         $json['alertClass'] = 'success';
  394.                         $json['alertMessage'] = $this->translator->trans('Success ! Article status updated successfully.');
  395.                         break;
  396.                     case "stared":
  397.                         $article $entityManager->getRepository(SupportEntites\Article::class)->findOneBy(['id' => $data['id']]);
  398.                         if ($article) {
  399.                             $article->setStared( (isset($data['value']) && $data['value'] == 1) ? );
  400.                             $entityManager->persist($article);
  401.                             $entityManager->flush();
  402.                         }
  403.                         $json['alertClass'] = 'success';
  404.                         $json['alertMessage'] = $this->translator->trans('Success ! Article star updated successfully.');
  405.                         break;
  406.                     case "update":
  407.                         $articleBase $this->getDoctrine()
  408.                             ->getRepository(SupportEntites\SolutionCategory::class)
  409.                             ->find($data['id']);
  410.                         if ($articleBase) {
  411.                             if (isset($data['name']) && strlen($data['name']) > 200) {
  412.                                 $json['alertClass'] = 'danger';
  413.                                 $json['alertMessage'] = $this->translator->trans('Name length must not be greater than 200 !!');
  414.                             } else {
  415.                                 $articleBase->setName($this->uvdeskService->htmlfilter($data['name']));
  416.                                 if(trim($articleBase->getContent()) != trim($data['content']))
  417.                                     $this->updateContent($request$articleBase$data['content']);
  418.                                 $json['alertClass'] = 'success';
  419.                                 $json['alertMessage'] = $this->translator->trans('Success! Article updated successfully');
  420.                             }
  421.                         }
  422.                     case 'status':
  423.                         $entityManager->getRepository(SupportEntites\Article::class)->bulkArticleStatusUpdate([$data['id']], $data['value']);
  424.                         $json['alertClass'] = 'success';
  425.                         $json['alertMessage'] =  $this->translator->trans('Success ! Article status updated successfully.');
  426.                         break;
  427.                     default:
  428.                         $json['alertClass'] = 'danger';
  429.                         $json['alertMessage'] =  $this->translator->trans('Warning ! This is not a valid request');
  430.                 }
  431.         }
  432.         $response = new Response(json_encode($json));
  433.         $response->headers->set('Content-Type''application/json');
  434.         return $response;
  435.     }
  436.     private function updateContent($articleBase$content$updateArticle true)
  437.     {
  438.         $entityManager $this->getDoctrine()->getManager();
  439.         $articleHistory = new SupportEntites\ArticleHistory;
  440.         $articleHistory->setUserId($this->getUser()->getId());
  441.         $articleHistory->setArticleId($articleBase->getId());
  442.         $articleHistory->setContent($articleBase->getContent());
  443.         if ($updateArticle) {
  444.             $articleBase->setContent($content);
  445.             $entityManager->persist($articleBase);
  446.         }
  447.         $entityManager->persist($articleHistory);
  448.         $entityManager->flush();
  449.     }
  450.     private function removeArticle($article)
  451.     {
  452.         $this->getDoctrine()
  453.             ->getRepository(SupportEntites\Article::class)
  454.             ->removeEntryByArticle($article->getId());
  455.     }
  456.     /**
  457.      * If customer is playing with url and no result is found then what will happen
  458.      * @return 
  459.      */
  460.     protected function noResultFound()
  461.     {
  462.         throw new NotFoundHttpException('Not Found!');
  463.     }
  464. }