vendor/uvdesk/support-center-bundle/Repository/MarketingModuleRepository.php line 24

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