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

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