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

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