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

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($registryAnnouncement::class);
  22.     }
  23.     
  24.     public function getAllAnnouncements(\Symfony\Component\HttpFoundation\ParameterBag $obj null$container)
  25.     {
  26.         $json = array();
  27.         $qb $this->getEntityManager()->createQueryBuilder();
  28.         $qb->select('a')->from($this->getEntityName(), 'a');
  29.         $data $obj->all();
  30.         $data array_reverse($data);
  31.         foreach ($data as $key => $value) {
  32.             if(!in_array($key,$this->safeFields)) {
  33.                 if($key!='dateUpdated' AND $key!='dateAdded' AND $key!='search') {
  34.                     $qb->Andwhere('a.'.$key.' = :'.$key);
  35.                     $qb->setParameter($key$value);
  36.                 } else {
  37.                     if($key == 'search') {
  38.                         $qb->orwhere('a.title'.' LIKE :name');
  39.                         $qb->setParameter('name''%'.urldecode($value).'%');
  40.                         $qb->orwhere('a.promoText'.' LIKE :promoText');
  41.                         $qb->setParameter('promoText''%'.urldecode($value).'%');
  42.                     }
  43.                 }
  44.             }
  45.         }
  46.         if(!isset($data['sort'])){
  47.             $qb->orderBy('a.id',Criteria::DESC);
  48.         }
  49.         $paginator  $container->get('knp_paginator');
  50.         $results $paginator->paginate(
  51.             $qb,
  52.             isset($data['page']) ? $data['page'] : 1,
  53.             self::LIMIT,
  54.             array('distinct' => false)
  55.         );
  56.         $newResult = [];
  57.         foreach ($results as $key => $result) {
  58.             $newResult[] = array(
  59.                 'id'        => $result->getId(),
  60.                 'title'      => $result->getTitle(),
  61.                 'promoText' => $result->getPromoText(),
  62.                 'promoTag'  => $result->getPromoTag(),
  63.                 'tagColor'  => $result->getTagColor(),
  64.                 'linkText'  => $result->getLinkText(),
  65.                 'linkUrl'   => $result->getLinkUrl(),
  66.                 'isActive'  => $result->getIsActive(),
  67.                 'createdAt' => $result->getCreatedAt(),
  68.                 'group'     => array(
  69.                     'id'    => $result->getGroup()->getId(),
  70.                     'name'  => $result->getGroup()->getName()
  71.                 )
  72.             );
  73.         }
  74.         $paginationData $results->getPaginationData();
  75.         $queryParameters $results->getParams();
  76.         $paginationData['url'] = '#'.$container->get('uvdesk.service')->buildPaginationQuery($queryParameters);
  77.         $json['groups'] = $newResult;
  78.         $json['pagination_data'] = $paginationData;
  79.         return $json;
  80.     }
  81. }