/** * Get emails in thread of current one * * @param EntityManager $entityManager * @param Email $entity * * @return Email[] */ public function getThreadEmails(EntityManager $entityManager, Email $entity) { $thread = $entity->getThread(); if ($thread) { /** @var QueryBuilder $queryBuilder */ $queryBuilder = $entityManager->getRepository('OroEmailBundle:Email')->createQueryBuilder('e'); $criteria = new Criteria(); $criteria->where($criteria->expr()->eq('thread', $thread)); $criteria->orderBy(['sentAt' => Criteria::DESC]); $queryBuilder->addCriteria($criteria); $result = $queryBuilder->getQuery()->getResult(); } else { $result = [$entity]; } return $result; }
/** * Updates email references' threadId * * @param EntityManager $entityManager * @param Email $entity */ protected function updateRefs(EntityManager $entityManager, Email $entity) { if ($entity->getThread()) { /** @var Email $email */ foreach ($this->emailThreadProvider->getEmailReferences($this->em, $entity) as $email) { if (!$email->getThread()) { $email->setThread($entity->getThread()); $entityManager->persist($email); } } } }
/** * @param Email $email * @param $data * * @return mixed */ protected function setReplaedEmailId($email, $data) { if ($email->getThread()) { $emails = $email->getThread()->getEmails(); // if there are just two email - add replayedEmailId to use on client side if (count($emails) === 2) { $data['replayedEmailId'] = $emails[0]->getId(); } } return $data; }
/** * Prepare emails to set status. If need get all from thread * * @param Email $entity * @param bool $checkThread Get threaded emails * * @return Email[] */ protected function prepareFlaggedEmailEntities(Email $entity, $checkThread) { $thread = $entity->getThread(); $emails = [$entity]; if ($checkThread && $thread) { $emails = $thread->getEmails(); return $emails->toArray(); } return $emails; }
/** * Gets array of EmailUser's by Email or EmailThread of this email depends on $checkThread flag * for current user and organization or which have mailbox owner. * * @param Email $email * @param User $user * @param Organization $organization * @param bool|false $checkThread * * @return EmailUser[] */ public function getAllEmailUsersByEmail(Email $email, User $user, Organization $organization, $checkThread = false) { $parameters = []; $queryBuilder = $this->createQueryBuilder('eu')->join('eu.email', 'e'); if ($checkThread && ($thread = $email->getThread())) { $queryBuilder->andWhere('e.thread = :thread'); $parameters['thread'] = $thread; } else { $queryBuilder->andWhere('eu.email = :email'); $parameters['email'] = $email; } $orx = $queryBuilder->expr()->orX(); $orx->add('eu.mailboxOwner IS NOT NULL')->add('eu.owner = :owner AND eu.organization = :organization'); return $queryBuilder->andWhere($orx)->setParameters(array_merge($parameters, ['owner' => $user, 'organization' => $organization]))->getQuery()->getResult(); }
/** * @param EntityManager $em * @param Email $email * @param [] $contexts */ protected function addContextsToThread(EntityManager $em, Email $email, $contexts) { $thread = $email->getThread(); if ($thread) { $relatedEmails = $em->getRepository(Email::ENTITY_CLASS)->findByThread($thread); } else { $relatedEmails = [$email]; } if (count($contexts) > 0) { foreach ($relatedEmails as $relatedEmail) { foreach ($contexts as $context) { $this->addAssociation($relatedEmail, $context); } } } }
/** * @param Email $email * @param EmailThread $thread */ protected function processThread(Email $email, EmailThread $thread) { if ($email->getId()) { if (!$email->getThread() || $email->getThread()->getId() != $thread->getId()) { throw $this->createInvalidPropertyException('Thread', $email->getThread() ? $email->getThread()->getId() : null, $thread->getId()); } } else { $email->setThread($thread); } }