vendor/uvdesk/support-center-bundle/Repository/SolutionCategory.php line 52

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\SupportCenterBundle\Repository;
  3. use Doctrine\ORM\EntityRepository;
  4. use Doctrine\Common\Collections\Criteria;
  5. use Webkul\UVDesk\SupportCenterBundle\Entity as SupportEntities;
  6. /**
  7. * Website
  8. *
  9. * This class was generated by the Doctrine ORM. Add your own custom
  10. * repository methods below.
  11. */
  12. class SolutionCategory extends EntityRepository
  13. {
  14. const LIMIT = 1000;
  15. private $defaultSort = 'a.id';
  16. private $direction = ['asc', 'desc'];
  17. private $sorting = ['a.name', 'a.dateAdded', 'a.sortOrder'];
  18. private $safeFields = ['page', 'limit', 'sort', 'order', 'direction'];
  19. private $allowedFormFields = ['search', 'name', 'description', 'sorting', 'sortOrder', 'status'];
  20. private function validateSorting($sorting)
  21. {
  22. return in_array($sorting, $this->sorting) ? $sorting : $this->defaultSort;
  23. }
  24. private function validateDirection($direction)
  25. {
  26. return in_array($direction, $this->direction) ? $direction : Criteria::DESC;
  27. }
  28. private function presetting(&$data)
  29. {
  30. $data['sort'] = $_GET['sort'] = $this->validateSorting(isset($data['sort']) ? $data['sort'] : false);
  31. $data['direction'] = $_GET['direction'] = $this->validateDirection(isset($data['direction']) ? $data['direction'] : false);
  32. $this->cleanAllData($data);
  33. }
  34. private function cleanAllData(&$data)
  35. {
  36. if (isset($data['isActive'])) {
  37. $data['status'] = $data['isActive'];
  38. unset($data['isActive']);
  39. unset($data['solutionId']);
  40. }
  41. }
  42. public function getAllCategories(\Symfony\Component\HttpFoundation\ParameterBag $obj = null, $container, $allResult = false)
  43. {
  44. $json = array();
  45. $qb = $this->getEntityManager()->createQueryBuilder();
  46. $qb->select('a')->from($this->getEntityName(), 'a');
  47. $data = $obj ? $obj->all() : [];
  48. $data = array_reverse($data);
  49. $categories = [];
  50. if (isset($data['solutionId'])) {
  51. $qbS = $this->getEntityManager()->createQueryBuilder();
  52. $qbS->select('a.categoryId')->from('Webkul\UVDesk\SupportCenterBundle\Entity\SolutionCategoryMapping', 'a');
  53. $qbS->where('a.solutionId = :solutionId');
  54. $qbS->setParameter('solutionId', $data['solutionId']);
  55. $categories = $qbS->getQuery()->getResult();
  56. $categories = $categories ? $categories : [0];
  57. }
  58. $this->presetting($data);
  59. foreach ($data as $key => $value) {
  60. if (!in_array($key, $this->safeFields) && in_array($key, $this->allowedFormFields)) {
  61. if ($key != 'dateUpdated' and $key != 'dateAdded' and $key != 'search') {
  62. $qb->andWhere('a.' . $key . ' = :' . $key);
  63. $qb->setParameter($key, $value);
  64. } else {
  65. if ($key == 'search') {
  66. $qb->orWhere('a.name' . ' LIKE :name');
  67. $qb->setParameter('name', '%' . urldecode(trim($value)) . '%');
  68. $qb->orWhere('a.description' . ' LIKE :description');
  69. $qb->setParameter('description', '%' . urldecode(trim($value)) . '%');
  70. }
  71. }
  72. }
  73. }
  74. if ($categories) {
  75. $qb->andWhere('a.id IN (:categories)');
  76. $qb->setParameter('categories', $categories);
  77. }
  78. if (!$allResult) {
  79. $paginator = $container->get('knp_paginator');
  80. $results = $paginator->paginate(
  81. $qb,
  82. isset($data['page']) ? $data['page'] : 1,
  83. isset($data['limit']) ? $data['limit'] : self::LIMIT,
  84. array('distinct' => false)
  85. );
  86. } else {
  87. $qb->select($allResult);
  88. $results = $qb->getQuery()->getResult();
  89. return $results;
  90. }
  91. $newResult = [];
  92. foreach ($results as $key => $result) {
  93. $newResult[] = array(
  94. 'id' => $result->getId(),
  95. 'name' => $result->getName(),
  96. 'description' => $result->getDescription(),
  97. 'status' => $result->getStatus(),
  98. 'sorting' => $result->getSorting(),
  99. 'sortOrder' => $result->getSortOrder(),
  100. 'dateAdded' => date_format($result->getDateAdded(), "d-M h:i A"),
  101. 'articleCount' => $this->getArticlesCountByCategory($result->getId()),
  102. 'solutions' => ($categories ? [] : $this->getSolutionsByCategory($result->getId())),
  103. );
  104. }
  105. $paginationData = $results->getPaginationData();
  106. $queryParameters = $results->getParams();
  107. unset($queryParameters['solution']);
  108. $paginationData['url'] = '#' . $container->get('uvdesk.service')->buildPaginationQuery($queryParameters);
  109. $json['results'] = $newResult;
  110. $json['pagination_data'] = $paginationData;
  111. return $json;
  112. }
  113. public function findCategoryById($filterArray = [])
  114. {
  115. $qb = $this->getEntityManager()->createQueryBuilder();
  116. $qb->select('a')->from($this->getEntityName(), 'a');
  117. foreach ($filterArray as $key => $value) {
  118. $qb->andWhere('a.' . $key . ' = :' . $key);
  119. $qb->setParameter($key, $value);
  120. }
  121. return $qb->getQuery()->getOneOrNullResult();
  122. }
  123. public function getArticlesCountByCategory($categoryId, $status = 1)
  124. {
  125. $qbS = $this->createQueryBuilder('a');
  126. $result = $qbS->select('COUNT(DISTINCT ac.id)')
  127. ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\ArticleCategory', 'ac', 'WITH', 'ac.categoryId = a.id')
  128. ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\Article', 'aA', 'WITH', 'ac.articleId = aA.id')
  129. ->andWhere('ac.categoryId = :categoryId')
  130. ->andWhere('aA.status IN (:status)')
  131. ->setParameters([
  132. 'categoryId' => $categoryId,
  133. 'status' => $status,
  134. ])
  135. ->getQuery()
  136. ->getSingleScalarResult();
  137. return $result;
  138. }
  139. public function getSolutionsByCategory($categoryId)
  140. {
  141. $queryBuilder = $this->createQueryBuilder('a');
  142. $results = $queryBuilder->select('s.id, s.name')
  143. ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\SolutionCategoryMapping', 'ac', 'WITH', 'ac.categoryId = a.id')
  144. ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\Solutions', 's', 'WITH', 'ac.solutionId = s.id')
  145. ->andWhere('ac.categoryId = :categoryId')
  146. ->setParameters([
  147. 'categoryId' => $categoryId
  148. ])
  149. ->getQuery()
  150. ->getResult();
  151. return $results;
  152. }
  153. public function getArticlesByCategory($categoryId)
  154. {
  155. $queryBuilder = $this->createQueryBuilder('sc');
  156. $results = $queryBuilder->select('a.id, a.name, a.slug')
  157. ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\ArticleCategery', 'ac', 'WITH', 'ac.categoryId = sc.id')
  158. ->leftJoin('Webkul\UVDesk\SupportCenterBundle\Entity\Article', 'a', 'WITH', 'ac.id = a.id')
  159. ->andWhere('ac.categoryId = :categoryId')
  160. ->setParameters([
  161. 'categoryId' => $categoryId
  162. ])
  163. ->getQuery()
  164. ->getResult();
  165. return $results;
  166. }
  167. public function removeSolutionsByCategory($categoryId, $solutionId)
  168. {
  169. $queryBuilder = $this->createQueryBuilder('ac');
  170. $queryBuilder->delete(SupportEntities\SolutionCategoryMapping::class, 'ac')
  171. ->andWhere('ac.categoryId = :categoryId')
  172. ->andWhere('ac.solutionId IN (:solutionId)')
  173. ->setParameters([
  174. 'categoryId' => $categoryId,
  175. 'solutionId' => $solutionId,
  176. ])
  177. ->getQuery()
  178. ->execute()
  179. ;
  180. }
  181. public function removeEntryByCategory($categoryId)
  182. {
  183. $where = is_array($categoryId) ? 'ac.categoryId IN (:categoryId)' : 'ac.categoryId = :categoryId';
  184. $queryBuilder = $this->createQueryBuilder('ac');
  185. $queryBuilder->delete(SupportEntities\SolutionCategoryMapping::class, 'ac')
  186. ->andWhere($where)
  187. ->setParameters([
  188. 'categoryId' => $categoryId,
  189. ])
  190. ->getQuery()
  191. ->execute()
  192. ;
  193. $queryBuilder->delete(SupportEntities\ArticleCategory::class, 'ac')
  194. ->andWhere($where)
  195. ->setParameters([
  196. 'categoryId' => $categoryId,
  197. ])
  198. ->getQuery()
  199. ->execute()
  200. ;
  201. }
  202. public function bulkCategoryStatusUpdate($categoryIds, $status)
  203. {
  204. $query = 'UPDATE Webkul\UVDesk\SupportCenterBundle\Entity\SolutionCategory sc SET sc.status = ' . (int)$status . ' WHERE sc.id IN (' . implode(',', $categoryIds) . ')';
  205. $this->getEntityManager()->createQuery($query)->execute();
  206. }
  207. public function categorySortingUpdate($id, $sort)
  208. {
  209. $query = "UPDATE Webkul\UVDesk\SupportCenterBundle\Entity\SolutionCategory sc SET sc.sortOrder = '" . (int)$sort . "' WHERE sc.id = '" . (int)$id . "'";
  210. $this->getEntityManager()->createQuery($query)->execute();
  211. }
  212. }