/** * Find or Create Person and attach it with incomming mail activity * @param unknown $id activity ID * @param string $userDocument userDocument name * @param string $language language name * @param MongoId $marketingSource marking source id * @return boolean */ private function _importSingleIncommingMailPerson($id, $userDocument, $language, $marketingSource = null) { $incommingEmailDocument = $this->_dm->find($this->_documentClassName, $id); //Import only if not already imported and email not deleted if (!$incommingEmailDocument->getFromPerson() && $incommingEmailDocument->getDeleted() == false) { $from = $incommingEmailDocument->getFromEmailString(); //Extract email $pattern = '/([\\w.+-]+@[\\w-]+\\.[\\w-.]+)/'; preg_match($pattern, $from, $matches, PREG_OFFSET_CAPTURE, 3); $email = $matches[1][0]; //Find person $this->_documentClassName = 'MoveIn4Person\\Document\\PersonDocument'; $personDoc = parent::findOne(["email.email" => $email, 'deleted' => false]); //Create person if not found create new one if (!$personDoc) { $namestring = preg_replace([$pattern, '/ \\<\\>/'], '', $from); $nameData = explode(" ", $namestring); $personDoc = new PersonDocument(); $personDoc->setFirstName($nameData[0]); if (isset($nameData[1]) && empty($nameData[1])) { $nameData[1] = $nameData[0]; } else { if (!isset($nameData[1])) { $nameData[1] = $nameData[0]; } } $personDoc->setSurname($nameData[1]); //Create email doc $emailEmbed = new EmailDocument(); $emailEmbed->setEmail($email); $emailEmbed->setPreferred(true); // add to make email preferred $personDoc->addEmail($emailEmbed); //Create lead doc for setting generating tool $leadEmbed = new LeadDocument(); $referenceId = $this->formatIdByStrategy('MoveIn4ReferenceList\\Document\\ReferenceListDocument', '897'); $genTool = $this->_dm->find('MoveIn4ReferenceList\\Document\\ReferenceListDocument', $referenceId); $leadEmbed->setGeneratingTool($genTool); if ($marketingSource) { $marketingSourceDoc = $this->_dm->find('MoveIn4MarketingSource\\Document\\MarketingSourceDocument', $marketingSource); $leadEmbed->setSourceDetail($marketingSourceDoc); } $leadEmbed->setCapturedOn(new \DateTime('now')); $personDoc->addLead($leadEmbed); $this->_dm->persist($personDoc); $ch = $this->serviceManager->get('Base\\Model\\ChangeHistory'); $ch->save($personDoc, $userDocument, $language); } else { $personDoc = $this->_dm->find($this->_documentClassName, (string) $personDoc['_id']->{'$id'}); } //Assign person to activity $incommingEmailDocument->setFromPerson($personDoc); //$incommingEmailDocument->setUnread(false); $incommingEmailDocument->setOwner($userDocument); $this->_dm->persist($incommingEmailDocument); $chActivity = $this->serviceManager->get('Base\\Model\\ChangeHistory'); $chActivity->save($incommingEmailDocument, $userDocument, $language); $this->_dm->flush(); } $this->_documentClassName = 'MoveIn4Activity\\Document\\ActivityIncomingEmailDocument'; }