/** * Do merge of non-scalar fields such as emails or addresses * * @param Customer $remoteData * @param Customer $localData * @param Contact $contact * * @SuppressWarnings(PHPMD) * TODO Should be fixed during CRM-1185 */ public function mergeObjects(Customer $remoteData, Customer $localData, Contact $contact) { // process emails $email = $contact->getPrimaryEmail(); if ($email) { // if primary email exists try to merge $this->mergeScalars(['email'], $remoteData, $localData, $email); } elseif ($this->isRemotePrioritized()) { // if contact hasn't email and remote data has greater priority, then create it $email = new ContactEmail(); $email->setPrimary(true); $email->setEmail($remoteData->getEmail()); $contact->addEmail($email); } // process addresses $addresses = $contact->getAddresses(); $isLocalTypesChanged = $this->isLocalAddressesTypesChanged($addresses, $localData); foreach ($addresses as $address) { // find in update local data if $localAddress = $this->getCustomerAddressByContactAddress($localData, $address); if (!$localAddress && $this->isRemotePrioritized()) { $contact->removeAddress($address); } elseif ($localAddress) { $remoteAddress = $this->getCorrespondentRemoteAddress($remoteData, $localAddress); if ($remoteAddress) { // do update $this->mergeScalars($this->addressScalarFields, $remoteAddress, $localAddress, $address); if ($localAddress->getCountry()->getIso2Code() === $address->getCountry()->getIso2Code() || $this->isRemotePrioritized()) { $address->setCountry($remoteAddress->getCountry()); } if ($this->isRegionChanged($remoteAddress, $address) || $this->isRemotePrioritized()) { $address->setRegion($remoteAddress->getRegion()); if ($address->getRegion()) { $address->setRegionText(null); } } if ($this->isRemotePrioritized() || !$isLocalTypesChanged) { $address->setTypes($remoteAddress->getTypes()); } $contactPhone = null; if ($localAddress->getContactPhone()) { $contactPhone = $this->getContactPhoneFromContact($contact, $localAddress->getContactPhone()); } if ($contactPhone) { $this->mergeScalars(['phone'], $remoteAddress, $localAddress, $contactPhone); } elseif ($this->isRemotePrioritized() && $remoteAddress->getPhone() !== 'no phone') { $contactPhone = new ContactPhone(); $contactPhone->setPhone($remoteAddress->getPhone()); $contactPhone->setPrimary(!$contact->getPrimaryPhone()); $contact->addPhone($contactPhone); $localAddress->setContactPhone($contactPhone); } $this->prepareAddress($address); if (!$address->getCountry()) { $contact->removeAddress($address); } } else { $contact->removeAddress($address); } } } /** @var ArrayCollection|Address[] $newAddresses */ $newAddresses = $this->getOrphanRemoteAddresses($remoteData, $localData); foreach ($newAddresses as $address) { /* * Will create new address if remote data has higher priority and means * that address removed from contact and remove should be cancelled. * Another case if it's newly created address, then process it anyway */ if ($this->isRemotePrioritized() || !$address->getId()) { $contactAddress = new ContactAddress(); $this->mergeScalars($this->addressScalarFields, $address, $contactAddress, $contactAddress); $contactAddress->setCountry($address->getCountry()); $contactAddress->setRegion($address->getRegion()); $contactAddress->setTypes($address->getTypes()); $this->prepareAddress($contactAddress); if ($contactAddress->getCountry()) { $contact->addAddress($contactAddress); $address->setContactAddress($contactAddress); } if ($address->getContactPhone()) { $address->getContactPhone()->setOwner($contact); } } } /** @var ContactAddress $toBePrimary */ $toBePrimary = $contact->getAddresses()->first(); if (!$contact->getPrimaryAddress() && $toBePrimary) { $toBePrimary->setPrimary(true); } // Set contact primary phone if none if (!$contact->getPrimaryPhone()) { if ($contact->getPhones()->count() > 0) { $contact->getPhones()->first()->setPrimary(true); } } }
/** * Update $entity with new contact data * * @param Customer $remoteData * @param Customer $localData * @param Contact $contact */ protected function updateContact(Customer $remoteData, Customer $localData, Contact $contact) { $helper = new ContactImportHelper($localData->getChannel(), $this->addressHelper); if ($localData->getContact() && $localData->getContact()->getId()) { $helper->merge($remoteData, $localData, $localData->getContact()); } else { $addresses = $localData->getAddresses(); // loop by imported addresses, add new only /** @var \OroCRM\Bundle\ContactBundle\Entity\ContactAddress $address */ foreach ($contact->getAddresses() as $key => $address) { $helper->prepareAddress($address); if (!$address->getCountry()) { $contact->removeAddress($address); continue; } // @TODO find possible solution // guess parent address by key if ($entity = $addresses->get($key)) { $entity->setContactAddress($address); } } // @TODO find possible solution // guess parent $phone by key foreach ($contact->getPhones() as $key => $phone) { $contactPhone = $this->getContactPhoneFromContact($contact, $phone); if ($entity = $addresses->get($key)) { $entity->setContactPhone($contactPhone ? $contactPhone : $phone); } } // populate default owner only for new contacts $this->defaultOwnerHelper->populateChannelOwner($contact, $localData->getChannel()); $localData->setContact($contact); } }