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. $data = $obj->all();
  17. $data = array_reverse($data);
  18. foreach ($data as $key => $value) {
  19. if (! in_array($key,$this->safeFields)) {
  20. if ($key!='dateUpdated' AND $key!='dateAdded' AND $key!='search') {
  21. $qb->andWhere('sr.'.$key.' = :'.$key);
  22. $qb->setParameter($key, $value);
  23. } else {
  24. if ($key == 'search') {
  25. $qb->andWhere('sr.name'.' LIKE :name');
  26. $qb->setParameter('name', '%'.urldecode(trim($value)).'%');
  27. }
  28. }
  29. }
  30. }
  31. // filter saved replies based on groups and teams.
  32. $this->addGroupTeamFilter($qb, $container);
  33. if(!isset($data['sort']))
  34. $qb->orderBy('sr.id', Criteria::DESC);
  35. $paginator = $container->get('knp_paginator');
  36. $newQb = clone $qb;
  37. $newQb->select('COUNT(DISTINCT sr.id)');
  38. $results = $paginator->paginate(
  39. $qb->getQuery()->setHydrationMode(Query::HYDRATE_ARRAY)->setHint('knp_paginator.count', $newQb->getQuery()->getSingleScalarResult()),
  40. isset($data['page']) ? $data['page'] : 1,
  41. self::LIMIT,
  42. array('distinct' => false)
  43. );
  44. $paginationData = $results->getPaginationData();
  45. $queryParameters = $results->getParams();
  46. if (isset($queryParameters['template']))
  47. unset($queryParameters['template']);
  48. $paginationData['url'] = '#'.$container->get('uvdesk.service')->buildPaginationQuery($queryParameters);
  49. $json['savedReplies'] = $results->getItems();
  50. $json['pagination_data'] = $paginationData;
  51. return $json;
  52. }
  53. public function addGroupTeamFilter($qb, $container, $entityAlias = 'sr')
  54. {
  55. $qb->leftJoin($entityAlias.'.groups', 'grps')
  56. ->leftJoin($entityAlias.'.teams', 'tms');
  57. $user = $container->get('user.service')->getCurrentUser();
  58. $userCondition = $qb->expr()->orX();
  59. $userCondition->add($qb->expr()->eq($entityAlias.'.user', ':userId'));
  60. $qb->setParameter('userId', $container->get('user.service')->getCurrentUser()->getAgentInstance()->getId());
  61. if ($user->getAgentInstance()->getSupportGroups()) {
  62. foreach ($user->getAgentInstance()->getSupportGroups() as $key => $grp) {
  63. $userCondition->add($qb->expr()->eq('grps.id', ':groupId'.$key));
  64. $qb->setParameter('groupId'.$key, $grp->getId());
  65. }
  66. }
  67. $subgroupIds = $user->getAgentInstance()->getSupportTeams();
  68. foreach ($subgroupIds as $key => $teamId) {
  69. $userCondition->add($qb->expr()->eq('tms.id', ':teamId'.$key ));
  70. $qb->setParameter('teamId'.$key, $teamId);
  71. }
  72. $qb->andWhere($userCondition);
  73. return $qb;
  74. }
  75. public function getSavedReply($id, $container)
  76. {
  77. $qb = $this->getEntityManager()->createQueryBuilder();
  78. $qb->select('sr')->from($this->getEntityName(), 'sr')
  79. ->andWhere('sr.id = :id')
  80. ->setParameter('id', $id );
  81. return $qb->getQuery()->getOneOrNullResult();
  82. }
  83. }