/** * Deactivate email origin if mailbox is deleted. * * @ORM\PreRemove */ public function preRemove() { if ($this->origin !== null) { $this->origin->setActive(false); $this->origin->setMailbox(null); } }
/** * @return EmailFolder[] */ public function getFolders() { // retrieve folders from imap $folders = $this->connector->findFolders(); // transform folders into tree of models $emailFolderModels = $this->processFolders($folders); if ($this->origin->getId()) { // renew existing folders if origin already exists $existingFolders = $this->getExistingFolders(); // merge synced and existing folders $emailFolderModels = $this->mergeFolders($emailFolderModels, $existingFolders); // mark old folders as outdated $this->removeOutdated($existingFolders); // flush changes $this->em->flush(); } return $this->extractEmailFolders($emailFolderModels); }
/** * Updates a state of the given email origin * * @param EmailOrigin $origin * @param int $syncCode Can be one of self::SYNC_CODE_* constants * @param \DateTime|null $synchronizedAt * @return bool true if the synchronization code was updated; false if no any changes are needed */ protected function changeOriginSyncState(EmailOrigin $origin, $syncCode, $synchronizedAt = null) { $repo = $this->getEntityManager()->getRepository($this->getEmailOriginClass()); $qb = $repo->createQueryBuilder('o')->update()->set('o.syncCode', ':code')->set('o.syncCodeUpdatedAt', ':updated')->where('o.id = :id')->setParameter('code', $syncCode)->setParameter('updated', $this->getCurrentUtcDateTime())->setParameter('id', $origin->getId()); if ($synchronizedAt !== null) { $qb->set('o.synchronizedAt', ':synchronized')->setParameter('synchronized', $synchronizedAt); } if ($syncCode === self::SYNC_CODE_IN_PROCESS) { $qb->andWhere('(o.syncCode IS NULL OR o.syncCode <> :code)'); } if ($syncCode === self::SYNC_CODE_SUCCESS) { $qb->set('o.syncCount', 'o.syncCount + 1'); } $affectedRows = $qb->getQuery()->execute(); return $affectedRows > 0; }
/** * @param $randomTemplate * @param User $owner * @param Contact $contact * @param EmailOrigin $origin * * @return EmailUser */ protected function addEmailUser($randomTemplate, $owner, $contact, $origin) { $emailUser = $this->emailEntityBuilder->emailUser($this->templates[$randomTemplate]['Subject'], $owner->getEmail(), $contact->getPrimaryEmail()->getEmail(), new \DateTime('now'), new \DateTime('now'), new \DateTime('now')); $this->setSecurityContext($owner); $emailUser->setFolder($origin->getFolder(FolderType::SENT)); $emailUser->setOwner($owner); $emailUser->setOrganization($owner->getOrganization()); $emailBody = $this->emailEntityBuilder->body("Hi,\n" . $this->templates[$randomTemplate]['Text'], false, true); $emailUser->getEmail()->setEmailBody($emailBody); $emailUser->getEmail()->setMessageId(sprintf('<id.%s@%s', uniqid(), '@bap.migration.generated>')); return $emailUser; }
/** * @param EmailOrigin $emailOrigin */ protected function initEnv(EmailOrigin $emailOrigin) { $this->currentUser = $this->em->getReference('Oro\\Bundle\\UserBundle\\Entity\\User', $emailOrigin->getOwner()->getId()); $this->currentOrganization = $this->em->getReference('Oro\\Bundle\\OrganizationBundle\\Entity\\Organization', $emailOrigin->getOrganization()->getId()); }
/** * Register EmailOrigin object * * @param EmailOrigin $origin * @throws \LogicException */ public function addOrigin(EmailOrigin $origin) { $key = $origin->getId(); if (isset($this->origins[$key])) { throw new \LogicException(sprintf('The origin %s already exists in the batch.', $origin)); } $this->origins[$key] = $origin; }
/** * @param EmailOrigin $origin * * @return mixed * @throws \Doctrine\ORM\NonUniqueResultException */ public function getOriginOwner(EmailOrigin $origin) { $qb = $this->createQueryBuilder('u')->select('u')->innerJoin('u.emailOrigins', 'o')->where('o.id = :originId')->setParameter('originId', $origin->getId())->setMaxResults(1); return $qb->getQuery()->getOneOrNullResult(); }
/** * Register EmailOrigin object * * @param EmailOrigin $origin * @throws \LogicException */ public function addOrigin(EmailOrigin $origin) { $key = $origin->getId(); $this->origins[$key] = $origin; }
/** * Return test for error message * * @param EmailOrigin $origin - entity EmailOrigin * * @return string */ protected function getErrorMessage(EmailOrigin $origin) { $message = 'Cannot find an email flag manager loader. Origin id: %d.'; return sprintf($message, $origin->getId()); }
/** * Get email ids and create iterator * * @param EmailOrigin $origin * @param ImapEmailFolder $imapFolder * @param EmailFolder $folder * * @return ImapEmailIterator */ protected function getEmailIterator(EmailOrigin $origin, ImapEmailFolder $imapFolder, EmailFolder $folder) { if ($origin->getMailbox()) { // build search query for emails sync $sqb = $this->manager->getSearchQueryBuilder(); if ($origin->getSynchronizedAt() && $folder->getSynchronizedAt()) { if ($folder->getType() === FolderType::SENT) { $sqb->sent($folder->getSynchronizedAt()); } else { $sqb->received($folder->getSynchronizedAt()); } } $searchQuery = $sqb->get(); $this->logger->info(sprintf('Loading emails from "%s" folder ...', $folder->getFullName())); $this->logger->info(sprintf('Query: "%s".', $searchQuery->convertToSearchString())); $emails = $this->manager->getEmails($searchQuery); } else { $lastUid = $this->em->getRepository('OroImapBundle:ImapEmail')->findLastUidByFolder($imapFolder); $this->logger->info(sprintf('Previous max email UID "%s"', $lastUid)); $emails = $this->manager->getEmailsUidBased(sprintf('%s:*', ++$lastUid)); } return $emails; }
/** * Get origin's folder * * @param string $email * @param EmailOrigin $origin * @return EmailFolder */ protected function getFolder($email, EmailOrigin $origin) { $folder = $origin->getFolder(FolderType::SENT); //In case when 'useremailorigin' origin doesn't have folder, get folder from internal origin if (!$folder && $origin instanceof UserEmailOrigin) { $originKey = InternalEmailOrigin::BAP . $email; if (array_key_exists($originKey, $this->origins)) { unset($this->origins[$originKey]); } $origin = $this->getEmailOrigin($email, null, InternalEmailOrigin::BAP, false); return $origin->getFolder(FolderType::SENT); } return $folder; }
/** * @param EmailOrigin $origin * * @return EmailBodyLoaderInterface */ protected function getBodyLoader(EmailOrigin $origin) { $originId = $origin->getId(); if (!isset($this->emailBodyLoaders[$originId])) { $this->emailBodyLoaders[$originId] = $this->selector->select($origin); } return $this->emailBodyLoaders[$originId]; }
/** * Check the given folders and if needed correct them * * @param EmailFolder[] $folders * @param EmailOrigin $origin */ protected function ensureFoldersInitialized(array &$folders, EmailOrigin $origin) { if (!empty($folders) && count($folders) >= 2) { return; } $this->log->notice('Retrieving folders from an email server ...'); $srcFolders = $this->manager->getFolders(null, true); $this->log->notice(sprintf('Retrieved %d folder(s).', count($srcFolders))); foreach ($srcFolders as $srcFolder) { $type = null; if ($srcFolder->hasFlag(Folder::FLAG_INBOX)) { $type = EmailFolder::INBOX; } elseif ($srcFolder->hasFlag(Folder::FLAG_SENT)) { $type = EmailFolder::SENT; } if ($type !== null) { $globalName = $srcFolder->getGlobalName(); if ($this->isFolderExist($folders, $type, $globalName)) { continue; } $this->log->notice(sprintf('Persisting "%s" folder ...', $globalName)); $folder = new EmailFolder(); $folder->setFullName($globalName)->setName($srcFolder->getLocalName())->setType($type); $origin->addFolder($folder); $this->em->persist($origin); $this->em->persist($folder); $folders[] = $folder; $this->log->notice(sprintf('The "%s" folder was persisted.', $globalName)); } } $this->em->flush(); }
public function __construct($id = null) { parent::__construct(); $this->id = $id; }