vendor/uvdesk/core-framework/Repository/SavedRepliesRepository.php line 15

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\CoreFrameworkBundle\Repository;
  3. use Doctrine\ORM\Query;
  4. use Doctrine\ORM\EntityRepository;
  5. use Doctrine\Common\Collections\Criteria;
  6. use Symfony\Component\HttpFoundation\ParameterBag;
  7. class SavedRepliesRepository extends EntityRepository
  8. {
  9. const LIMIT = 10;
  10. public $safeFields = array('page', 'limit', 'sort', 'order', 'direction');
  11. public function getSavedReplies(ParameterBag $obj = null, $container)
  12. {
  13. $json = array();
  14. $qb = $this->getEntityManager()->createQueryBuilder();
  15. $qb->select('DISTINCT sr.id, sr.name')->from($this->getEntityName(), 'sr');
  16. $currentUserInstance = $container->get('user.service')->getCurrentUser()?->getAgentInstance();
  17. $data = $obj->all();
  18. $data = array_reverse($data);
  19. foreach ($data as $key => $value) {
  20. if (! in_array($key, $this->safeFields)) {
  21. if ($key != 'dateUpdated' and $key != 'dateAdded' and $key != 'search') {
  22. $qb->andWhere('sr.' . $key . ' = :' . $key);
  23. $qb->setParameter($key, $value);
  24. } else {
  25. if ($key == 'search') {
  26. $qb->andWhere('sr.name' . ' LIKE :name');
  27. $qb->setParameter('name', '%' . urldecode(trim($value)) . '%');
  28. }
  29. }
  30. }
  31. }
  32. // filter saved replies based on groups and teams.
  33. if (in_array($currentUserInstance->getSupportRole()->getCode(), ['ROLE_ADMIN', 'ROLE_SUPER_ADMIN'])) {
  34. $qb->andWhere('sr.user = :userId OR sr.groups IS NOT EMPTY OR sr.teams IS NOT EMPTY');
  35. $qb->setParameter('userId', $currentUserInstance->getId());
  36. } else {
  37. $this->addGroupTeamFilter($qb, $container);
  38. }
  39. if (!isset($data['sort']))
  40. $qb->orderBy('sr.id', Criteria::DESC);
  41. $paginator = $container->get('knp_paginator');
  42. $newQb = clone $qb;
  43. $newQb->select('COUNT(DISTINCT sr.id)');
  44. $results = $paginator->paginate(
  45. $qb->getQuery()->setHydrationMode(Query::HYDRATE_ARRAY)->setHint('knp_paginator.count', $newQb->getQuery()->getSingleScalarResult()),
  46. isset($data['page']) ? $data['page'] : 1,
  47. self::LIMIT,
  48. array('distinct' => false)
  49. );
  50. $paginationData = $results->getPaginationData();
  51. $queryParameters = $results->getParams();
  52. if (isset($queryParameters['template']))
  53. unset($queryParameters['template']);
  54. $paginationData['url'] = '#' . $container->get('uvdesk.service')->buildPaginationQuery($queryParameters);
  55. $json['savedReplies'] = $results->getItems();
  56. $json['pagination_data'] = $paginationData;
  57. return $json;
  58. }
  59. public function addGroupTeamFilter($qb, $container, $entityAlias = 'sr')
  60. {
  61. $qb->leftJoin($entityAlias . '.groups', 'grps')
  62. ->leftJoin($entityAlias . '.teams', 'tms');
  63. $user = $container->get('user.service')->getCurrentUser();
  64. $userCondition = $qb->expr()->orX();
  65. $userCondition->add($qb->expr()->eq($entityAlias . '.user', ':userId'));
  66. $qb->setParameter('userId', $container->get('user.service')->getCurrentUser()->getAgentInstance()->getId());
  67. if ($user->getAgentInstance()->getSupportGroups()) {
  68. foreach ($user->getAgentInstance()->getSupportGroups() as $key => $grp) {
  69. $userCondition->add($qb->expr()->eq('grps.id', ':groupId' . $key));
  70. $qb->setParameter('groupId' . $key, $grp->getId());
  71. }
  72. }
  73. $subgroupIds = $user->getAgentInstance()->getSupportTeams();
  74. foreach ($subgroupIds as $key => $teamId) {
  75. $userCondition->add($qb->expr()->eq('tms.id', ':teamId' . $key));
  76. $qb->setParameter('teamId' . $key, $teamId);
  77. }
  78. $qb->andWhere($userCondition);
  79. return $qb;
  80. }
  81. public function getSavedReply($id, $container)
  82. {
  83. $qb = $this->getEntityManager()->createQueryBuilder();
  84. $qb->select('sr')->from($this->getEntityName(), 'sr')
  85. ->andWhere('sr.id = :id')
  86. ->setParameter('id', $id);
  87. return $qb->getQuery()->getOneOrNullResult();
  88. }
  89. }