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

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