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

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