vendor/uvdesk/support-center-bundle/Repository/AnnouncementRepository.php line 27

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\SupportCenterBundle\Repository;
  3. use Doctrine\Persistence\ManagerRegistry;
  4. use Doctrine\Common\Collections\Criteria;
  5. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  6. use Webkul\UVDesk\CoreFrameworkBundle\Entity\Ticket;
  7. use Webkul\UVDesk\SupportCenterBundle\Entity\Announcement;
  8. /**
  9. * @method Announcement|null find($id, $lockMode = null, $lockVersion = null)
  10. * @method Announcement|null findOneBy(array $criteria, array $orderBy = null)
  11. * @method Announcement[] findAll()
  12. * @method Announcement[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  13. */
  14. class AnnouncementRepository extends ServiceEntityRepository
  15. {
  16. public $safeFields = array('page', 'limit', 'sort', 'order', 'direction');
  17. const LIMIT = 10;
  18. public function __construct(ManagerRegistry $registry)
  19. {
  20. parent::__construct($registry, Announcement::class);
  21. }
  22. public function getAllAnnouncements(\Symfony\Component\HttpFoundation\ParameterBag $obj = null, $container)
  23. {
  24. $json = array();
  25. $qb = $this->getEntityManager()->createQueryBuilder();
  26. $qb->select('a')->from($this->getEntityName(), 'a');
  27. $data = $obj->all();
  28. $data = array_reverse($data);
  29. foreach ($data as $key => $value) {
  30. if (! in_array($key, $this->safeFields)) {
  31. if ($key != 'dateUpdated' and $key != 'dateAdded' and $key != 'search') {
  32. $qb->andWhere('a.' . $key . ' = :' . $key);
  33. $qb->setParameter($key, $value);
  34. } else {
  35. if ($key == 'search') {
  36. $qb->orWhere('a.title' . ' LIKE :name');
  37. $qb->setParameter('name', '%' . urldecode($value) . '%');
  38. $qb->orWhere('a.promoText' . ' LIKE :promoText');
  39. $qb->setParameter('promoText', '%' . urldecode($value) . '%');
  40. }
  41. }
  42. }
  43. }
  44. if (! isset($data['sort'])) {
  45. $qb->orderBy('a.id', Criteria::DESC);
  46. }
  47. $paginator = $container->get('knp_paginator');
  48. $results = $paginator->paginate(
  49. $qb,
  50. isset($data['page']) ? $data['page'] : 1,
  51. self::LIMIT,
  52. array('distinct' => false)
  53. );
  54. $newResult = [];
  55. foreach ($results as $key => $result) {
  56. $newResult[] = array(
  57. 'id' => $result->getId(),
  58. 'title' => $result->getTitle(),
  59. 'promoText' => $result->getPromoText(),
  60. 'promoTag' => $result->getPromoTag(),
  61. 'tagColor' => $result->getTagColor(),
  62. 'linkText' => $result->getLinkText(),
  63. 'linkUrl' => $result->getLinkUrl(),
  64. 'isActive' => $result->getIsActive(),
  65. 'createdAt' => $result->getCreatedAt(),
  66. 'group' => array(
  67. 'id' => $result->getGroup()->getId(),
  68. 'name' => $result->getGroup()->getName()
  69. )
  70. );
  71. }
  72. $paginationData = $results->getPaginationData();
  73. $queryParameters = $results->getParams();
  74. $paginationData['url'] = '#' . $container->get('uvdesk.service')->buildPaginationQuery($queryParameters);
  75. $json['groups'] = $newResult;
  76. $json['pagination_data'] = $paginationData;
  77. return $json;
  78. }
  79. public function getAllAnnouncementForCustomer($query, $container, $customer)
  80. {
  81. $order = array_rand(array(
  82. 'DESC' => 'DESC',
  83. 'ASC' => 'ASC'
  84. ));
  85. $column = array_rand(array(
  86. 'ma.id' => 'ma.id',
  87. 'ma.createdAt' => 'ma.createdAt'
  88. ));
  89. $qb = $this->getEntityManager()->createQueryBuilder();
  90. $entityClass = Announcement::class;
  91. $limit = 10;
  92. $qb->select('ma')
  93. ->from($entityClass, 'ma')
  94. ->join(Ticket::class, 't', 'WITH', 'ma.group = t.supportGroup')
  95. ->where('ma.isActive = :isActive')
  96. ->andWhere('t.customer = :userId')
  97. ->groupBy('ma.id')
  98. ->orderBy($column, $order)
  99. ->setParameter('isActive', 1)
  100. ->setParameter('userId', $customer)
  101. ->setMaxResults($limit);
  102. $paginator = $container->get('knp_paginator');
  103. $results = $paginator->paginate(
  104. $qb,
  105. $query->get('page') ?: 1,
  106. $limit,
  107. array('distinct' => false)
  108. );
  109. $newResult = [];
  110. foreach ($results as $key => $result) {
  111. $newResult[] = array(
  112. 'id' => $result->getId(),
  113. 'title' => $result->getTitle(),
  114. 'promoText' => $result->getPromoText(),
  115. 'promoTag' => $result->getPromoTag(),
  116. 'isActive' => $result->getIsActive(),
  117. 'linkURL' => $result->getLinkUrl(),
  118. 'linkText' => $result->getLinkText(),
  119. 'createdAt' => $result->getCreatedAt(),
  120. 'updatedAt' => $result->getUpdatedAt(),
  121. 'group' => $result->getGroup()->getId() == 1 ? $group = ['name' => 'Default Group'] : $group = ['name' => $result->getGroup()->getName()],
  122. );
  123. }
  124. $paginationData = $results->getPaginationData();
  125. $json['modules'] = ($newResult);
  126. $json['pagination_data'] = $paginationData;
  127. return $json;
  128. }
  129. }