/** * There can be only one primary entity * * @param Contact $entity */ public function updatePrimaryEntities(Contact $entity) { // update addresses $addresses = $entity->getAddresses(); $primaryAddress = $this->getPrimaryEntity($addresses); if ($primaryAddress) { $entity->setPrimaryAddress($primaryAddress); } elseif ($addresses->count() > 0) { $entity->setPrimaryAddress($addresses->first()); } // update emails $emails = $entity->getEmails(); $primaryEmail = $this->getPrimaryEntity($emails); if ($primaryEmail) { $entity->setPrimaryEmail($primaryEmail); } elseif ($emails->count() > 0) { $entity->setPrimaryEmail($emails->first()); } // update phones $phones = $entity->getPhones(); $primaryPhone = $this->getPrimaryEntity($phones); if ($primaryPhone) { $entity->setPrimaryPhone($primaryPhone); } elseif ($phones->count() > 0) { $entity->setPrimaryPhone($phones->first()); } }
/** * Gets a list of all phone numbers available for the given Contact object * * @param Contact $object * * @return array of [phone number, phone owner] */ public function getPhoneNumbers($object) { $result = []; foreach ($object->getPhones() as $phone) { $result[] = [$phone->getPhone(), $object]; } return $result; }
/** * Get ContactPhone from contact by ContactPhone * * @param Contact $contact * @param ContactPhone $contactPhone * * @return mixed */ protected function getContactPhoneFromContact(Contact $contact, ContactPhone $contactPhone) { $filtered = $contact->getPhones()->filter(function (ContactPhone $phone) use($contactPhone) { return $phone && $phone->getId() === $contactPhone->getId(); }); return $filtered->first(); }
/** * @param Contact $entity * @param array $result * * @return array */ protected function prepareContactEntities(Contact $entity, array $result) { // use contact source name instead of label $source = $entity->getSource(); if ($source) { $result['source'] = $source->getName(); } else { $result['source'] = null; } // use contact method name instead of label $method = $entity->getMethod(); if ($method) { $result['method'] = $method->getName(); } else { $result['method'] = null; } $result['emails'] = array(); foreach ($entity->getEmails() as $email) { $result['emails'][] = array('email' => $email->getEmail(), 'primary' => $email->isPrimary()); } $result['phones'] = array(); foreach ($entity->getPhones() as $phone) { $result['phones'][] = array('phone' => $phone->getPhone(), 'primary' => $phone->isPrimary()); } // set contact group data $groupsData = array(); foreach ($entity->getGroups() as $group) { $groupsData[] = parent::getPreparedItem($group); } $result['groups'] = $groupsData; // convert addresses to plain array $addressData = array(); /** @var $address ContactAddress */ foreach ($entity->getAddresses() as $address) { $addressArray = parent::getPreparedItem($address); $addressArray['types'] = $address->getTypeNames(); $addressArray = $this->removeUnusedValues($addressArray, array('owner')); // @todo: just a temporary workaround until new API is implemented // the normal solution can be to use region_name virtual field and // exclusion rule declared in oro/entity.yml // - for 'region' field use a region text if filled; otherwise, use region name // - remove regionText field from a result if (!empty($addressArray['regionText'])) { $addressArray['region'] = $addressArray['regionText']; } unset($addressArray['regionText']); $addressData[] = $addressArray; } $result['addresses'] = $addressData; return $result; }
/** * Filtered phone by phone number from contact and return entity or null * * @param Contact $contact * @param ContactPhone $contactPhone * * @return ContactPhone|null */ protected function getContactPhoneFromContact(Contact $contact, ContactPhone $contactPhone) { foreach ($contact->getPhones() as $phone) { if ($phone->getPhone() === $contactPhone->getPhone()) { $hash = spl_object_hash($phone); if (array_key_exists($hash, $this->processedEntities)) { // skip if contact phone used for previously imported phone continue; } $this->processedEntities[$hash] = $phone; return $phone; } } return null; }
/** * @param Contact $entity * @return Contact */ protected function afterProcessEntity($entity) { // there can be only one primary entity $addresses = $entity->getAddresses(); $primaryAddress = $this->getPrimaryEntity($addresses); if ($primaryAddress) { /** @var ContactAddress $primaryAddress */ $entity->setPrimaryAddress($primaryAddress); } elseif ($addresses->count() > 0) { $entity->setPrimaryAddress($addresses->first()); } $emails = $entity->getEmails(); $primaryEmail = $this->getPrimaryEntity($emails); if ($primaryEmail) { /** @var ContactEmail $primaryEmail */ $entity->setPrimaryEmail($primaryEmail); } elseif ($emails->count() > 0) { $entity->setPrimaryEmail($emails->first()); } $phones = $entity->getPhones(); $primaryPhone = $this->getPrimaryEntity($phones); if ($primaryPhone) { /** @var ContactPhone $primaryPhone */ $entity->setPrimaryPhone($primaryPhone); } elseif ($phones->count() > 0) { $entity->setPrimaryPhone($phones->first()); } return $entity; }