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

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\SupportCenterBundle\Repository;
  3. use Webkul\UVDesk\SupportCenterBundle\Entity\Announcement;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. use Doctrine\Common\Collections\Criteria;
  7. use Doctrine\Common\Collections;
  8. use Doctrine\ORM\Tools\Pagination\Paginator;
  9. /**
  10. * @method Announcement|null find($id, $lockMode = null, $lockVersion = null)
  11. * @method Announcement|null findOneBy(array $criteria, array $orderBy = null)
  12. * @method Announcement[] findAll()
  13. * @method Announcement[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  14. */
  15. class AnnouncementRepository extends ServiceEntityRepository
  16. {
  17. public $safeFields = array('page','limit','sort','order','direction');
  18. const LIMIT = 10;
  19. public function __construct(ManagerRegistry $registry)
  20. {
  21. parent::__construct($registry, Announcement::class);
  22. }
  23. public function getAllAnnouncements(\Symfony\Component\HttpFoundation\ParameterBag $obj = null, $container)
  24. {
  25. $json = array();
  26. $qb = $this->getEntityManager()->createQueryBuilder();
  27. $qb->select('a')->from($this->getEntityName(), 'a');
  28. $data = $obj->all();
  29. $data = array_reverse($data);
  30. foreach ($data as $key => $value) {
  31. if (! in_array($key, $this->safeFields)) {
  32. if ($key!='dateUpdated' AND $key!='dateAdded' AND $key!='search') {
  33. $qb->andWhere('a.'.$key.' = :'.$key);
  34. $qb->setParameter($key, $value);
  35. } else {
  36. if ($key == 'search') {
  37. $qb->orWhere('a.title'.' LIKE :name');
  38. $qb->setParameter('name', '%'.urldecode($value).'%');
  39. $qb->orWhere('a.promoText'.' LIKE :promoText');
  40. $qb->setParameter('promoText', '%'.urldecode($value).'%');
  41. }
  42. }
  43. }
  44. }
  45. if (! isset($data['sort'])) {
  46. $qb->orderBy('a.id',Criteria::DESC);
  47. }
  48. $paginator = $container->get('knp_paginator');
  49. $results = $paginator->paginate(
  50. $qb,
  51. isset($data['page']) ? $data['page'] : 1,
  52. self::LIMIT,
  53. array('distinct' => false)
  54. );
  55. $newResult = [];
  56. foreach ($results as $key => $result) {
  57. $newResult[] = array(
  58. 'id' => $result->getId(),
  59. 'title' => $result->getTitle(),
  60. 'promoText' => $result->getPromoText(),
  61. 'promoTag' => $result->getPromoTag(),
  62. 'tagColor' => $result->getTagColor(),
  63. 'linkText' => $result->getLinkText(),
  64. 'linkUrl' => $result->getLinkUrl(),
  65. 'isActive' => $result->getIsActive(),
  66. 'createdAt' => $result->getCreatedAt(),
  67. 'group' => array(
  68. 'id' => $result->getGroup()->getId(),
  69. 'name' => $result->getGroup()->getName()
  70. )
  71. );
  72. }
  73. $paginationData = $results->getPaginationData();
  74. $queryParameters = $results->getParams();
  75. $paginationData['url'] = '#'.$container->get('uvdesk.service')->buildPaginationQuery($queryParameters);
  76. $json['groups'] = $newResult;
  77. $json['pagination_data'] = $paginationData;
  78. return $json;
  79. }
  80. }