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

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.         
  88.          // Pasar flag para listar solo subartículos si la ruta lo define
  89.         if ($request->attributes->get('isSubarticle')) {
  90.             $request->query->set('isSubarticle'1);
  91.         }
  92.         $json $repository->getAllArticles($request->query$this->container);
  93.         $response = new Response(json_encode($json));
  94.         $response->headers->set('Content-Type''application/json');
  95.         return $response;
  96.     }
  97.     public function articleHistoryXhr(Request $request)
  98.     {
  99.         $json = array();
  100.         $repository $this->getDoctrine()->getRepository('UVDeskSupportCenterBundle:Article');
  101.         $params = ['articleId' => $request->attributes->get('id')];
  102.         $json $repository->getAllHistoryByArticle($params);
  103.         if ($json) {
  104.             foreach($json as $key => $js) {
  105.                 $json[$key]['dateAdded'] = [
  106.                     'format' => $this->container->get('user.service')->convertToTimezone($js['dateAdded']),
  107.                     'timestamp' => $this->container->get('user.service')->convertToDatetimeTimezoneTimestamp($js['dateAdded']),
  108.                 ];
  109.             }
  110.         }
  111.         $response = new Response(json_encode($json));
  112.         $response->headers->set('Content-Type''application/json');
  113.         return $response;
  114.     }
  115.     public function articleRelatedXhr(Request $request)
  116.     {
  117.         $json = array();
  118.         $repository $this->getDoctrine()->getRepository('UVDeskSupportCenterBundle:Article');
  119.         $params = ['articleId' => $request->attributes->get('id')];
  120.         $json $repository->getAllRelatedyByArticle($params);
  121.         $response = new Response(json_encode($json));
  122.         $response->headers->set('Content-Type''application/json');
  123.         return $response;
  124.     }
  125.     protected function getArticle($filterArray = array())
  126.     {
  127.         if ($filterArray) {
  128.             return $this->getDoctrine()
  129.                 ->getRepository('UVDeskSupportCenterBundle:Article')
  130.                 ->findOneBy($filterArray);
  131.         }
  132.         return false;
  133.     }
  134.     public function article(Request $request)
  135.     {
  136.         if ($request->attributes->get('id')) {
  137.             $article $this->getArticle(['id' => $request->attributes->get('id')]);
  138.             if(!$article)
  139.                 $this->noResultFound();
  140.         } else {
  141.             $article = new ArticleEntity;
  142.         }
  143.         $articleCategory $articleTags = [];
  144.         if ($article->getId()) {
  145.             $articleCategory $this->getDoctrine()
  146.                 ->getRepository('UVDeskSupportCenterBundle:Article')
  147.                 ->getCategoryByArticle($article->getId());
  148.             $articleTags $this->getDoctrine()
  149.                 ->getRepository('UVDeskSupportCenterBundle:Article')
  150.                 ->getTagsByArticle($article->getId());
  151.         }
  152.         $categories $this->getDoctrine()
  153.             ->getRepository('UVDeskSupportCenterBundle:SolutionCategory')
  154.             ->getAllCategories(null$this->container'a.id, a.name');
  155.             
  156.         $conn $this->getDoctrine()->getConnection();
  157. $rows $conn->fetchAll('SELECT id FROM uv_article WHERE parent_article_id IS NULL ORDER BY name ASC');
  158. $ids array_column($rows'id');
  159. $parentArticles = [];
  160. if (!empty($ids)) {
  161.     $parentArticles $this->getDoctrine()
  162.         ->getRepository('UVDeskSupportCenterBundle:Article')
  163.         ->findBy(['id' => $ids], ['name' => 'ASC']);
  164. }
  165.         if ($request->attributes->get('id')) {
  166.             return  $this->render('@UVDeskSupportCenter/Staff/Articles/articleForm.html.twig', [
  167.                 'article' => $article,
  168.                 'articleCategory' => $articleCategory,
  169.                 'articleTags' => $articleTags,
  170.                 'categories' => $categories,
  171.                 'parentArticles' => $parentArticles,
  172.             ]);
  173.         }
  174.          return $this->render('@UVDeskSupportCenter/Staff/Articles/articleAddForm.html.twig', [
  175.             'article' => $article,
  176.             'parentArticles' => $parentArticles,
  177.         ]);
  178.     }
  179.     public function articleXhr(Request $request)
  180.     {
  181.         $json = array();
  182.         if ($request->getMethod() == "POST") {
  183.             $data $request->request->get("data");
  184.             $entityManager $this->getDoctrine()->getManager();
  185.             if (isset($data['actionType'])) {
  186.                 switch ($data['actionType']) {
  187.                     case 'articleUpdate':
  188.                     case 'articleSave':
  189.                         if ('articleSave' == $data['actionType']  && !empty($resources['articles']['showAlert']) ) {
  190.                             $json['alertClass'] = 'danger';
  191.                             return new JsonResponse($json);
  192.                         }
  193.                         if ($data['ids'][0]) {
  194.                             $article $this->getArticle(['id' => $data['ids'][0]]);
  195.                         } else {
  196.                             $article = new ArticleEntity;
  197.                         }
  198.                         $json['errors'] = [];
  199.                         if ($article) {
  200.                             if (strlen($data['name']) > 200) {
  201.                                 $json['errors']['name'] = 'Name length must not be greater than 200 !!';
  202.                             }
  203.                             if (!$json['errors']) {
  204.                                 unset($json['errors']);
  205.                                 $article->setName($data['name']);
  206.                                 $article->setSlug($data['slug']);
  207.                                 $article->setMetaTitle($data['metaTitle']);
  208.                                 $article->setKeywords($data['keywords']);
  209.                                 $article->setMetaDescription($data['metaDescription']);
  210.                                 // Parent article (subarticle support)
  211.                                 $parentId = isset($data['parentArticleId']) && $data['parentArticleId'] !== '' ? (int) $data['parentArticleId'] : null;
  212.                                 $article->setParentArticleId($parentId);
  213.                                 
  214.                                 $updateRevisionHistory false;
  215.                                 if ($article->getContent() == null || trim($article->getContent()) != trim($data['content'])) {
  216.                                     $updateRevisionHistory true;
  217.                                     $article->setContent($data['content']);
  218.                                 }
  219.                                 
  220.                                  // Persistir primero el artículo con Doctrine
  221.                                 $entityManager->persist($article);
  222.                                 $entityManager->flush();
  223.                                 // Actualizar parent_article_id mediante SQL nativo
  224.                                 $parentId = isset($data['parentArticleId']) && $data['parentArticleId'] !== '' ? (int) $data['parentArticleId'] : null;
  225.                                 $conn $entityManager->getConnection();
  226.                                 if ($parentId !== null) {
  227.                                     $conn->executeUpdate(
  228.                                         'UPDATE uv_article SET parent_article_id = :pid WHERE id = :id',
  229.                                         ['pid' => $parentId'id' => $article->getId()]
  230.                                     );
  231.                                 } else {
  232.                                     $conn->executeUpdate(
  233.                                         'UPDATE uv_article SET parent_article_id = NULL WHERE id = :id',
  234.                                         ['id' => $article->getId()]
  235.                                     );
  236.                                 }
  237.                                 $entityManager->persist($article);
  238.                                 $entityManager->flush();
  239.                                 $json['alertClass'] = 'success';
  240.                                 $json['alertMessage'] = $this->get('translator')->trans('Success! Article updated successfully');
  241.                                 if (!$data['ids'][0]) {
  242.                                     $json['redirect'] = $this->generateUrl('helpdesk_member_knowledgebase_update_article', array('id' => $article->getId()));
  243.                                 }
  244.                             } else {
  245.                                 $json['alertClass'] = 'danger';
  246.                                 $json['alertMessage'] ='Warning! Correct all field values first!';
  247.                             }
  248.                         } else {
  249.                             $json['alertClass'] = 'danger';
  250.                             $json['alertMessage'] = $this->get('translator')->trans('Warning ! This is not a valid request');
  251.                         }
  252.                         break;
  253.                     case 'status':
  254.                         $entityManager->getRepository('UVDeskSupportCenterBundle:Article')->bulkArticleStatusUpdate($data['ids'], $data['targetId']);
  255.                         $json['alertClass'] = 'success';
  256.                         $json['alertMessage'] = $this->get('translator')->trans('Success ! Article updated successfully.');
  257.                         break;
  258.                     case 'tagUpdate':
  259.                         if ($data['action'] == 'remove') {
  260.                             $entityManager->getRepository('UVDeskSupportCenterBundle:Article')->removeTagByArticle($data['ids'][0], [$data['entityId']]);
  261.                             $json['alertClass'] = 'success';
  262.                             $json['alertMessage'] = $this->get('translator')->trans('Success ! Tag removed successfully.');
  263.                             break;
  264.                         } elseif ($data['action'] == 'add') {
  265.                             $articleTagMapping = new ArticleTags();
  266.                             $articleTagMapping->setArticleId($data['ids'][0]);
  267.                             $articleTagMapping->setTagId($data['entityId']);
  268.                             $entityManager->persist($articleTagMapping);
  269.                             $entityManager->flush();
  270.                         } elseif ($data['action'] == 'create') {
  271.                             $tag $entityManager->getRepository('UVDeskCoreFrameworkBundle:Tag')->findOneBy(['name' => $data['name']]);
  272.                             if (!$tag) {
  273.                                 $tag = new Tag();
  274.                                 $tag->setName($data['name']);
  275.                                 $entityManager->persist($tag);
  276.                                 $entityManager->flush();
  277.                             }
  278.                             $articleTagMapping = new ArticleTags();
  279.                             $articleTagMapping->setArticleId($data['ids'][0]);
  280.                             $articleTagMapping->setTagId($tag->getId());
  281.                             $entityManager->persist($tag);
  282.                             $entityManager->persist($articleTagMapping);
  283.                             $entityManager->flush();
  284.                             $json['tagId'] = $tag->getId();
  285.                             $json['tagName'] = $tag->getName();
  286.                         }
  287.                         $json['alertClass'] = 'success';
  288.                         $json['alertMessage'] = $this->get('translator')->trans('Success ! Tags Saved successfully.');
  289.                         break;
  290.                     case 'contentUpdate':
  291.                         $article $this->getArticle(['id' => $data['ids'][0]]);
  292.                         if ($article) {
  293.                             if (trim($article->getContent()) != trim($data['content']))
  294.                                 $this->updateContent($article$data['content']);
  295.                             $json['alertClass'] = 'success';
  296.                             $json['alertMessage'] = $this->get('translator')->trans('Success ! Revision restored successfully.');
  297.                         } else {
  298.                             $json['alertClass'] = 'danger';
  299.                             $json['alertMessage'] = $this->get('translator')->trans('Warning ! This is not a valid request');
  300.                         }
  301.                         break;
  302.                     case 'categoryUpdate':
  303.                         if ($data['action'] == 'remove') {
  304.                             $this->getDoctrine()
  305.                                 ->getRepository('UVDeskSupportCenterBundle:Article')
  306.                                 ->removeCategoryByArticle($data['ids'][0], [$data['entityId']]);
  307.                         } else if ($data['action'] == 'add') {
  308.                             $articleCategoryMapping = new ArticleCategory();
  309.                             $articleCategoryMapping->setArticleId($data['ids'][0]);
  310.                             $articleCategoryMapping->setCategoryId($data['entityId']);
  311.                             $entityManager->persist($articleCategoryMapping);
  312.                             $entityManager->flush();
  313.                         }
  314.                         $json['alertClass'] = 'success';
  315.                         $json['alertMessage'] = $this->get('translator')->trans('Success ! Categories updated successfully.');
  316.                         break;
  317.                     case 'relatedUpdate':
  318.                         if ($data['action'] == 'remove') {
  319.                             $entityManager->getRepository('UVDeskSupportCenterBundle:Article')->removeRelatedByArticle($data['ids'][0], [$data['entityId']]);
  320.                             $json['alertClass'] = 'success';
  321.                             $json['alertMessage'] = $this->get('translator')->trans('Success ! Article Related removed successfully.');
  322.                         } else if($data['action'] == 'add') {
  323.                             $relatedArticles $entityManager->getRepository('UVDeskSupportCenterBundle:ArticleRelatedArticle')->findBy([
  324.                                 'articleId' => $data['ids'][0],
  325.                                 'relatedArticleId' => $data['entityId'],
  326.                             ]);
  327.                             if (count($relatedArticles)) {
  328.                                 $json['alertClass'] = 'success';
  329.                                 $json['alertMessage'] ='Success ! Article Related is already added.';
  330.                             } elseif ($data['ids'][0] == $data['entityId']) {
  331.                                 $json['alertClass'] = 'danger';
  332.                                 $json['alertMessage'] = $this->get('translator')->trans('Success ! Cannot add self as relative article.');
  333.                             } else {
  334.                                 $articleRelatedMapping = new ArticleRelatedArticle();
  335.                                 $articleRelatedMapping->setArticleId($data['ids'][0]);
  336.                                 $articleRelatedMapping->setRelatedArticleId($data['entityId']);
  337.                                 $entityManager->persist($articleRelatedMapping);
  338.                                 $entityManager->flush();
  339.                                 $json['alertClass'] = 'success';
  340.                                 $json['alertMessage'] = $this->get('translator')->trans('Success ! Article Related updated successfully.');
  341.                             }
  342.                         }
  343.                         break;
  344.                     case 'delete':
  345.                         if ($data['ids']) {
  346.                             foreach ($data['ids'] as $id) {
  347.                                 $article $entityManager->getRepository('UVDeskSupportCenterBundle:Article')->find($id);
  348.                                 if ($article) {
  349.                                     $entityManager->remove($article);
  350.                                     $entityManager->flush();
  351.                                 }
  352.                             }
  353.                             $this->removeArticle($article);
  354.                         }
  355.                         $json['alertClass'] = 'success';
  356.                         $json['alertMessage'] = $this->get('translator')->trans('Success ! Articles removed successfully.');
  357.                         break;
  358.                     default:
  359.                         $json['alertClass'] = 'danger';
  360.                         $json['alertMessage'] = $this->get('translator')->trans('Warning ! This is not a valid request');
  361.                 }
  362.             }
  363.         } elseif ($request->getMethod() == "PATCH") {
  364.             $entityManager $this->getDoctrine()->getManager();
  365.             $data json_decode($request->getContent(), true);
  366.             if (isset($data['editType']))
  367.                 switch($data['editType']) {
  368.                     case 'status':
  369.                         $entityManager->getRepository('UVDeskSupportCenterBundle:Article')->bulkArticleStatusUpdate([$data['id']], $data['value']);
  370.                         $json['alertClass'] = 'success';
  371.                         $json['alertMessage'] = $this->get('translator')->trans('Success ! Article status updated successfully.');
  372.                         break;
  373.                     case "stared":
  374.                         $article $entityManager->getRepository('UVDeskSupportCenterBundle:Article')->findOneBy(['id' => $data['id']]);
  375.                         if ($article) {
  376.                             $article->setStared( (isset($data['value']) && $data['value'] == 1) ? );
  377.                             $entityManager->persist($article);
  378.                             $entityManager->flush();
  379.                         }
  380.                         $json['alertClass'] = 'success';
  381.                         $json['alertMessage'] ='Success ! Article star updated successfully.';
  382.                         break;
  383.                     case "update":
  384.                         $articleBase $this->getDoctrine()
  385.                             ->getRepository('UVDeskSupportCenterBundle:SolutionCategory')
  386.                             ->find($data['id']);
  387.                         if ($articleBase) {
  388.                             if (isset($data['name']) && strlen($data['name']) > 200) {
  389.                                 $json['alertClass'] = 'danger';
  390.                                 $json['alertMessage'] = 'Name length must not be greater than 200 !!';
  391.                             } else {
  392.                                 $articleBase->setName($this->get('uvdesk.service')->htmlfilter($data['name']));
  393.                                 if(trim($articleBase->getContent()) != trim($data['content']))
  394.                                     $this->updateContent($request$articleBase$data['content']);
  395.                                 $json['alertClass'] = 'success';
  396.                                 $json['alertMessage'] = $this->get('translator')->trans('Success! Article updated successfully');
  397.                             }
  398.                         }
  399.                     case 'status':
  400.                         $entityManager->getRepository('WebkulSupportCenterBundle:Article')->bulkArticleStatusUpdate([$data['id']], $data['value']);
  401.                         $json['alertClass'] = 'success';
  402.                         $json['alertMessage'] =  $this->get('translator')->trans('Success ! Article status updated successfully.');
  403.                         break;
  404.                     default:
  405.                         $json['alertClass'] = 'danger';
  406.                         $json['alertMessage'] =  $this->get('translator')->trans('Warning ! This is not a valid request');
  407.                 }
  408.         }
  409.         $response = new Response(json_encode($json));
  410.         $response->headers->set('Content-Type''application/json');
  411.         return $response;
  412.     }
  413.     private function updateContent($articleBase$content$updateArticle true)
  414.     {
  415.         $entityManager $this->getDoctrine()->getManager();
  416.         $articleHistory = new ArticleHistory;
  417.         $articleHistory->setUserId($this->getUser()->getId());
  418.         $articleHistory->setArticleId($articleBase->getId());
  419.         $articleHistory->setContent($articleBase->getContent());
  420.         if ($updateArticle) {
  421.             $articleBase->setContent($content);
  422.             $entityManager->persist($articleBase);
  423.         }
  424.         $entityManager->persist($articleHistory);
  425.         $entityManager->flush();
  426.     }
  427.     private function removeArticle($article)
  428.     {
  429.         $this->getDoctrine()
  430.             ->getRepository('UVDeskSupportCenterBundle:Article')
  431.             ->removeEntryByArticle($article->getId());
  432.     }
  433. }