vendor/uvdesk/core-framework/Repository/ThreadRepository.php line 150

Open in your IDE?
  1. <?php
  2. namespace Webkul\UVDesk\CoreFrameworkBundle\Repository;
  3. use Doctrine\ORM\Query;
  4. use Doctrine\Common\Collections\Criteria;
  5. use Webkul\UVDesk\CoreFrameworkBundle\Entity\User;
  6. use Webkul\UVDesk\CoreFrameworkBundle\Entity\Ticket;
  7. use Webkul\UVDesk\CoreFrameworkBundle\Entity\Attachment;
  8. use Webkul\UVDesk\CoreFrameworkBundle\Entity\Thread;
  9. /**
  10.  * ThreadRepository
  11.  *
  12.  * This class was generated by the Doctrine ORM. Add your own custom
  13.  * repository methods below.
  14.  */
  15. class ThreadRepository extends \Doctrine\ORM\EntityRepository
  16. {
  17.     const DEFAULT_PAGINATION_LIMIT 15;
  18.     public function findTicketBySubject($email$subject)
  19.     {
  20.         if (stripos($subject,"RE: ") !== false) {
  21.             $subject str_ireplace("RE: """$subject);
  22.         }
  23.         if (stripos($subject,"FWD: ") !== false) {
  24.             $subject str_ireplace("FWD: ","",$subject);
  25.         }
  26.         $entityManager $this->getEntityManager();
  27.         $queryBuilder $entityManager->createQueryBuilder()
  28.             ->select('t')
  29.             ->from(Ticket::class, 't')
  30.             ->where('t.subject LIKE :referenceIds')->setParameter('referenceIds'"%$subject%")
  31.             ->setMaxResults(1)
  32.         ;
  33.         $ticket $queryBuilder->getQuery()->getOneOrNullResult();
  34.         return ($ticket && strtolower($ticket->getCustomer()->getEmail()) == strtolower($email)) ? $ticket null;
  35.     }
  36.     public function getTicketCurrentThread($ticket)
  37.     {
  38.         return $this->getEntityManager()->createQueryBuilder()
  39.             ->select("thread")
  40.             ->from(Thread::class, 'thread')
  41.             ->where('thread.ticket = :ticket')->setParameter('ticket'$ticket)
  42.             ->orderBy('thread.id'Criteria::DESC)
  43.             ->setMaxResults(1)
  44.             ->getQuery()
  45.             ->getOneOrNullResult()
  46.         ;
  47.     }
  48.     public function prepareBasePaginationRecentThreadsQuery($ticket, array $params$enabledLockedThreads true)
  49.     {
  50.         $queryBuilder $this->getEntityManager()->createQueryBuilder()
  51.             ->select("thread, attachments, user, userInstance")
  52.             ->from(Thread::class, 'thread')
  53.             ->leftJoin('thread.user''user')
  54.             ->leftJoin('user.userInstance''userInstance')
  55.             ->leftJoin('thread.attachments''attachments')
  56.             ->where('thread.ticket = :ticket')->setParameter('ticket'$ticket)
  57.             ->andWhere('thread.threadType != :disabledThreadType')->setParameter('disabledThreadType''create')
  58.             ->orderBy('thread.id'Criteria::DESC)
  59.         ;
  60.         // Filter locked threads
  61.         if (false === $enabledLockedThreads) {
  62.             $queryBuilder->andWhere('thread.isLocked = :isThreadLocked')->setParameter('isThreadLocked'false);
  63.         }
  64.         // Filter threads by their type
  65.         switch (!empty($params['threadType']) ? $params['threadType'] : 'reply') {
  66.             case 'reply':
  67.                 $queryBuilder->andWhere('thread.threadType = :threadType')->setParameter('threadType''reply');
  68.                 break;
  69.             case 'forward':
  70.                 $queryBuilder->andWhere('thread.threadType = :threadType')->setParameter('threadType''forward');
  71.                 break;
  72.             case 'note':
  73.                 $queryBuilder->andWhere('thread.threadType = :threadType')->setParameter('threadType''note');
  74.                 break;
  75.             case 'bookmark':
  76.             case 'pinned':
  77.                 $queryBuilder->andWhere('thread.isBookmarked = :isBookmarked')->setParameter('isBookmarked'true);
  78.                 break;
  79.             case 'task':
  80.                 // $queryBuilder->andWhere('thread.threadType = :threadType')->setParameter('threadType', 'forward');
  81.                 break;
  82.             default:
  83.                 break;
  84.         }
  85.         return $queryBuilder;
  86.     }
  87.     public function getAllCustomerThreads($ticketId,\Symfony\Component\HttpFoundation\ParameterBag $obj null$container)
  88.     {
  89.         $json = array();
  90.         $entityManager $this->getEntityManager();
  91.         $qb $entityManager->createQueryBuilder()
  92.             ->select("th, a, u.id as userId, CONCAT(u.firstName, ' ', u.lastName) as fullname, userInstance.profileImagePath as smallThumbnail")->from($this->getEntityName(), 'th')
  93.             ->leftJoin('th.user''u')
  94.             ->leftJoin('th.attachments''a')
  95.             ->leftJoin('u.userInstance''userInstance')
  96.             ->andwhere('th.threadType = :threadType')
  97.             ->setParameter('threadType''reply')
  98.             ->andwhere('th.ticket = :ticketId')
  99.             ->setParameter('ticketId'$ticketId)
  100.             ->orderBy('th.id''DESC');
  101.         $data $obj->all();
  102.         $newQb = clone $qb;
  103.         $newQb->select('COUNT(DISTINCT th.id)');
  104.         $paginator $container->get('knp_paginator');
  105.         $results $paginator->paginate(
  106.             $qb->getQuery()->setHydrationMode(Query::HYDRATE_ARRAY)->setHint('knp_paginator.count'$newQb->getQuery()->getSingleScalarResult()),
  107.             isset($data['page']) ? $data['page'] : 1,
  108.             self::DEFAULT_PAGINATION_LIMIT,
  109.             array('distinct' => true)
  110.         );
  111.         $paginationData $results->getPaginationData();
  112.         $queryParameters $results->getParams();
  113.         $queryParameters['page'] = "replacePage";
  114.         $paginationData['url'] = '#'.$container->get('uvdesk.service')->buildPaginationQuery($queryParameters);
  115.         $data = array();
  116.         $userService $container->get('user.service');
  117.         $uvdeskFileSystemService $container->get('uvdesk.core.file_system.service');
  118.         foreach ($results->getItems() as $key => $row) {
  119.             $thread $row[0];
  120.             $threadResponse = [
  121.                 'id' => $thread['id'],
  122.                 'user' => $row['userId'] ? ['id' => $row['userId']] : null,
  123.                 'fullname' => $row['fullname'],
  124.                 'smallThumbnail'=> $row['smallThumbnail'],
  125.                 'reply' => html_entity_decode($thread['message']),
  126.                 'source' => $thread['source'],
  127.                 'threadType' => $thread['threadType'],
  128.                 'userType' => $thread['createdBy'],
  129.                 'formatedCreatedAt' => $userService->getLocalizedFormattedTime($thread['createdAt'], $userService->getSessionUser()),
  130.                 'timestamp' => $userService->convertToDatetimeTimezoneTimestamp($thread['createdAt']),
  131.                 'cc' => $thread['cc'],
  132.                 'bcc' => $thread['bcc'],
  133.                 'attachments' => $thread['attachments'],
  134.             ];
  135.             if (!empty($threadResponse['attachments'])) {
  136.                 $threadResponse['attachments'] = array_map(function ($attachment) use ($entityManager$uvdeskFileSystemService) {
  137.                     $attachmentReferenceObject $entityManager->getReference(Attachment::class, $attachment['id']);
  138.                     return $uvdeskFileSystemService->getFileTypeAssociations($attachmentReferenceObject);
  139.                 }, $threadResponse['attachments']);
  140.             }
  141.             array_push($data$threadResponse);
  142.         }
  143.         
  144.         $json['threads'] = $data;
  145.         $json['pagination'] = $paginationData;
  146.         return $json;
  147.     }
  148.     public function findThreadByRefrenceId($referenceIds)
  149.     {
  150.         $queryBuilder $this->getEntityManager()->createQueryBuilder()
  151.             ->select('t')
  152.             ->from(Ticket::class, 't')
  153.             ->where('t.referenceIds LIKE :referenceIds')->setParameter('referenceIds'"%$referenceIds%")
  154.             ->orderBy('t.id''DESC')
  155.             ->setMaxResults(1)
  156.         ;
  157.         return $queryBuilder->getQuery()->getOneOrNullResult();
  158.     }
  159. }