/** * Hydrate $object with the provided $data. * * @param array $data * @param Contact $object * * @return object */ public function hydrate(array $data, $object) { unset($data['contact_organisation']); $this->prepare($object); /** * Reformat the phone, address and community for the Contact object * */ if ($object instanceof Contact) { /* * Reset the data array and store the values locally */ $phoneData = $data['phone']; $data['phone'] = []; $addressInfo = $data['address']; $data['address'] = []; /** * @var $contact Contact */ $contact = $this->hydrateByValue($data, $object); /** * @var $currentPhoneNumbers Phone[] */ $currentPhoneNumbers = $contact->getPhone()->getSnapshot(); //Reset the array $contact->getPhone()->clear(); foreach ($phoneData as $phoneTypeId => $phoneElement) { if (!empty($phoneElement['phone'])) { $phone = new Phone(); $phone->setType($this->objectManager->getReference('Contact\\Entity\\PhoneType', $phoneTypeId)); $phone->setPhone($phoneElement['phone']); $phone->setContact($contact); $contact->getPhone()->add($phone); } } foreach ($currentPhoneNumbers as $phone) { if (!in_array($phone->getType()->getId(), [PhoneType::PHONE_TYPE_MOBILE, PhoneType::PHONE_TYPE_DIRECT])) { $contact->getPhone()->add($phone); } } $currentAddress = $contact->getAddress()->getSnapshot(); /* * Reformat the address */ $contact->getAddress()->clear(); if (array_key_exists('address', $addressInfo)) { if (!empty($addressInfo['address'])) { $address = new Address(); $address->setType($this->objectManager->getReference('Contact\\Entity\\AddressType', AddressType::ADDRESS_TYPE_MAIL)); $address->setAddress($addressInfo['address']); $address->setZipCode($addressInfo['zipCode']); $address->setCity($addressInfo['city']); $address->setContact($contact); $address->setCountry($this->objectManager->getReference('General\\Entity\\Country', $addressInfo['country'])); $contact->getAddress()->add($address); } } foreach ($currentAddress as $address) { if (!in_array($address->getType()->getId(), [AddressType::ADDRESS_TYPE_MAIL])) { $contact->getAddress()->add($address); } } $contact->getProfile()->setContact($contact); return $contact; } return $this->hydrateByValue($data, $object); }
/** * Body function, creating the contactObjects. */ private function prepareContent() { foreach ($this->content as $key => $content) { //See first if the contact can be found $contact = $this->getContactService()->findContactByEmail($content[$this->headerKeys['email']]); if (!is_null($contact)) { $contact->key = $key; $this->contacts[] = $contact; continue; } /** * 'organisation_id' => int 0 * 'organisation' => int 1 * 'firstname' => int 2 * 'middlename' => int 3 * 'lastname' => int 4 * 'position' => int 5 * 'email' => int 6 * 'phone' => int 7 * 'country' => int 8 * 'gender' => int 8 * 'title' => int 8 */ //Contact is not found $contact = new Contact(); $contact->key = $key; $contact->setFirstName($content[$this->headerKeys['firstname']]); $contact->setMiddleName($content[$this->headerKeys['middlename']]); $contact->setLastName($content[$this->headerKeys['lastname']]); $contact->setEmail($content[$this->headerKeys['email']]); $gender = null; $title = null; if (isset($this->headerKeys['gender']) && !empty($content[$this->headerKeys['gender']])) { $gender = $contact->setGender($this->getGeneralService()->findGenderByGender($content[$this->headerKeys['gender']])); } if (!is_null($gender)) { $contact->setGender($gender); } else { $contact->setGender($this->getGeneralService()->findEntityById('gender', Gender::GENDER_UNKNOWN)); } if (isset($this->headerKeys['title']) && !empty($content[$this->headerKeys['title']])) { $title = $contact->setTitle($this->getGeneralService()->findTitleByTitle($content[$this->headerKeys['title']])); } if (!is_null($title)) { $contact->setTitle($title); } else { $contact->setTitle($this->getGeneralService()->findEntityById('title', Title::TITLE_UNKNOWN)); } $contact->setPosition($content[$this->headerKeys['position']]); //If found, set the phone number if (isset($this->headerKeys['phone']) && !empty($content[$this->headerKeys['phone']])) { $phone = new Phone(); $phoneType = $this->getContactService()->findEntityById('phoneType', PhoneType::PHONE_TYPE_DIRECT); $phone->setType($phoneType); $phone->setPhone($content[$this->headerKeys['phone']]); $phone->setContact($contact); $phones = new ArrayCollection(); $phones->add($phone); $contact->setPhone($phones); } //Try to find the country $country = $this->getGeneralService()->findCountryByName($content[$this->headerKeys['country']]); //Try to find the organisation $organisationName = $content[$this->headerKeys['organisation']]; $organisation = null; if (isset($this->headerKeys['organisation_id']) && !empty($content[$this->headerKeys['organisation_id']])) { $organisation = $this->getOrganisationService()->setOrganisationId($content[$this->headerKeys['organisation_id']])->getOrganisation(); } if (is_null($organisation) && !is_null($country)) { $organisation = $this->getOrganisationService()->findOrganisationByNameCountry($organisationName, $country); } //If the organisation does not exist, create it if (is_null($organisation) && !is_null($country)) { $organisation = new Organisation(); $organisation->setOrganisation($organisationName); $organisation->setCountry($country); //Add the type $organisationType = $this->getOrganisationService()->findEntityById('type', Type::TYPE_UNKNOWN); $organisation->setType($organisationType); //Add the domain $validate = new EmailAddress(); $validate->isValid($content[$this->headerKeys['email']]); $organisationWebElements = new ArrayCollection(); $organisationWeb = new Web(); $organisationWeb->setWeb($validate->hostname); $organisationWeb->setMain(Web::MAIN); $organisationWeb->setOrganisation($organisation); $organisationWebElements->add($organisationWeb); if (isset($this->headerKeys['website']) && !is_null($content[$this->headerKeys['website']])) { //Strip the http:// and https:// $website = str_replace('http://', '', $content[$this->headerKeys['website']]); $website = str_replace('https://', '', $website); $organisationWebsite = new Web(); $organisationWebsite->setMain(Web::MAIN); $organisationWebsite->setWeb($website); $organisationWebsite->setOrganisation($organisation); $organisationWebElements->add($organisationWebsite); } $organisation->setWeb($organisationWebElements); } /** * If an organisation is found, add it to the contact */ if (!is_null($organisation)) { $contactOrganisation = new ContactOrganisation(); $contactOrganisation->setOrganisation($organisation); $contactOrganisation->setContact($contact); $contact->setContactOrganisation($contactOrganisation); } /** Add the contact to the contacts array */ $this->contacts[] = $contact; } }