vendor/uvdesk/support-center-bundle/Repository/Solutions.php line 58

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\SupportCenterBundle\Repository;
  3. use Doctrine\Common\Collections\Criteria;
  4. use Doctrine\ORM\EntityRepository;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Webkul\UVDesk\SupportCenterBundle\Entity as SupportEntities;
  7. class Solutions extends \Doctrine\ORM\EntityRepository
  8. {
  9. const LIMIT = 10;
  10. private $defaultImage = '';
  11. private $defaultSort = 'a.id';
  12. private $direction = ['asc', 'desc'];
  13. private $sorting = ['a.name', 'a.dateAdded'];
  14. private $safeFields = ['page', 'limit', 'sort', 'order', 'direction'];
  15. private $allowedFormFields = ['search', 'name', 'description', 'visibility'];
  16. private function validateSorting($sorting)
  17. {
  18. return in_array($sorting, $this->sorting) ? $sorting : $this->defaultSort;
  19. }
  20. private function validateDirection($direction)
  21. {
  22. return in_array($direction, $this->direction) ? $direction : Criteria::DESC;
  23. }
  24. private function presetting(&$data)
  25. {
  26. $data['sort'] = $_GET['sort'] = $this->validateSorting(isset($data['sort']) ? $data['sort'] : false);
  27. $data['direction'] = $_GET['direction'] = $this->validateDirection(isset($data['direction']) ? $data['direction'] : false);
  28. $this->cleanAllData($data);
  29. }
  30. private function cleanAllData(&$data)
  31. {
  32. if (isset($data['isActive'])) {
  33. $data['visibility'] = ($data['isActive'] ? 'public' : 'private');
  34. unset($data['isActive']);
  35. }
  36. }
  37. public function getAllCategories($categoryLimit = null, $articleLimit = null)
  38. {
  39. $categoryResponse = [];
  40. $categoryQB = $this->getEntityManager()->createQueryBuilder()->select('sc.id, sc.name, sc.description')
  41. ->from(SupportEntities\SolutionCategory::class, 'sc')
  42. ->andWhere('sc.status = :status')->setParameter('status', true)
  43. ->orderBy('sc.dateAdded', 'DESC');
  44. return $categoryQB->getQuery()->getResult();
  45. }
  46. public function getAllSolutions(\Symfony\Component\HttpFoundation\ParameterBag $obj = null, $container, $allResult = false, $status = [0, 1])
  47. {
  48. $json = array();
  49. $qb = $this->getEntityManager()->createQueryBuilder();
  50. $qb->select('a')->from($this->getEntityName(), 'a');
  51. $data = $obj ? $obj->all() : [];
  52. $data = array_reverse($data);
  53. $this->presetting($data);
  54. foreach ($data as $key => $value) {
  55. if (
  56. ! in_array($key,$this->safeFields)
  57. && in_array($key, $this->allowedFormFields)
  58. ) {
  59. if ($key !='dateUpdated' AND $key != 'dateAdded' AND $key != 'search') {
  60. $qb->andWhere('a.'.$key.' = :'.$key);
  61. $qb->setParameter($key, $value);
  62. } else {
  63. if ($key == 'search') {
  64. $qb->orWhere('a.name'.' LIKE :name');
  65. $qb->setParameter('name', '%'.urldecode(trim($value)).'%');
  66. $qb->orWhere('a.description'.' LIKE :description');
  67. $qb->setParameter('description', '%'.urldecode(trim($value)).'%');
  68. }
  69. }
  70. }
  71. }
  72. if (! $allResult) {
  73. $paginator = $container->get('knp_paginator');
  74. $results = $paginator->paginate(
  75. $qb,
  76. isset($data['page']) ? $data['page'] : 1,
  77. self::LIMIT,
  78. array('distinct' => false)
  79. );
  80. } else {
  81. $qb->select($allResult);
  82. $results = $qb->getQuery()->getResult();
  83. return $results;
  84. }
  85. $newResult = [];
  86. foreach ($results as $key => $result) {
  87. $newResult[] = array(
  88. 'id' => $result->getId(),
  89. 'name' => $result->getName(),
  90. 'description' => $result->getDescription(),
  91. 'visibility' => $result->getVisibility(),
  92. 'solutionImage' => ($result->getSolutionImage() == null) ? $this->defaultImage : $result->getSolutionImage(),
  93. 'categoriesCount' => $this->getCategoriesCountBySolution($result->getId(), $status),
  94. 'categories' => $this->getCategoriesWithCountBySolution($result->getId(), $status),
  95. 'articleCount' => $this->getArticlesCountBySolution($result->getId(), $status)
  96. );
  97. }
  98. $paginationData = $results->getPaginationData();
  99. $queryParameters = $results->getParams();
  100. $paginationData['url'] = '#'.$container->get('uvdesk.service')->buildPaginationQuery($queryParameters);
  101. $json['results'] = $newResult;
  102. $json['pagination_data'] = $paginationData;
  103. return $json;
  104. }
  105. public function findSolutionById($filterArray = [])
  106. {
  107. $json = array();
  108. $qb = $this->getEntityManager()->createQueryBuilder();
  109. $qb->select('a')->from($this->getEntityName(), 'a');
  110. foreach ($filterArray as $key => $value) {
  111. $qb->andWhere('a.'.$key.' = :'.$key);
  112. $qb->setParameter($key, $value);
  113. }
  114. $result = $qb->getQuery()->getOneOrNullResult();
  115. return($result);
  116. }
  117. public function getCategoriesWithCountBySolution($id, $status = [1])
  118. {
  119. $queryBuilder = $this->createQueryBuilder('a');
  120. $categories = $queryBuilder
  121. ->select('sc.id, sc.name')
  122. ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\SolutionCategoryMapping','ac','WITH', 'ac.solutionId = a.id')
  123. ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\SolutionCategory','sc','WITH', 'ac.categoryId = sc.id')
  124. ->andWhere('ac.solutionId = :solutionId')
  125. ->andWhere('sc.status IN (:status)')
  126. ->setParameters([
  127. 'solutionId' => $id,
  128. 'status' => $status,
  129. ])
  130. ->orderBy('sc.sortOrder', Criteria::ASC)
  131. ->getQuery()
  132. ->getResult()
  133. ;
  134. if ($categories) {
  135. $solutionCategoryRepository = $this->getEntityManager()->getRepository(SupportEntities\SolutionCategory::class);
  136. foreach ($categories as $key => $category) {
  137. $categories[$key]['articleCount'] = $solutionCategoryRepository->getArticlesCountByCategory($category['id']);
  138. }
  139. }
  140. return $categories;
  141. }
  142. public function getCategoriesCountBySolution($id, $status = [1])
  143. {
  144. $queryBuilder = $this->createQueryBuilder('a');
  145. $result = $queryBuilder
  146. ->select('COUNT(a.id)')
  147. ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\SolutionCategoryMapping','ac','WITH', 'ac.solutionId = a.id')
  148. ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\SolutionCategory','sc','WITH', 'ac.categoryId = sc.id')
  149. ->andWhere('ac.solutionId = :solutionId')
  150. ->andWhere('sc.status IN (:status)')
  151. ->setParameters([
  152. 'solutionId' => $id ,
  153. 'status' => $status,
  154. ])
  155. ->getQuery()
  156. ->getSingleScalarResult()
  157. ;
  158. return $result;
  159. }
  160. public function getArticlesCountBySolution($id, $status = [1])
  161. {
  162. $queryBuilder = $this->createQueryBuilder('a');
  163. $result = $queryBuilder
  164. ->select('COUNT(DISTINCT aa.id)')
  165. ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\SolutionCategoryMapping','sac','WITH', 'sac.solutionId = a.id')
  166. ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\ArticleCategory','ac','WITH', 'sac.categoryId = ac.categoryId')
  167. ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\Article','aa','WITH', 'ac.articleId = aa.id')
  168. ->where('sac.solutionId = :solutionId')
  169. ->andWhere('ac.id IS NOT NULL')
  170. ->andWhere('aa.status != :status')
  171. ->setParameters([
  172. 'solutionId' => $id,
  173. 'status' => 0
  174. ])
  175. ->getQuery()
  176. ->getSingleScalarResult()
  177. ;
  178. return $result;
  179. }
  180. public function removeEntryBySolution($id)
  181. {
  182. $where = is_array($id) ? 'ac.solutionId IN (:id)' : 'ac.solutionId = :id';
  183. $queryBuilder = $this->createQueryBuilder('ac');
  184. $queryBuilder
  185. ->delete(SupportEntities\SolutionCategoryMapping::class, 'ac')
  186. ->andWhere($where)
  187. ->setParameters([
  188. 'id' => $id ,
  189. ])
  190. ->getQuery()
  191. ->execute()
  192. ;
  193. }
  194. }