/** * Load the Contact * * @param ObjectManager $manager */ public function load(ObjectManager $manager) { $contact = new \Contact\Entity\Contact(); $contact->setFirstName('Jan'); $contact->setMiddleName('van der'); $contact->setLastName('Dam'); $contact->setEmail('*****@*****.**'); $contact->setState(1); $contact->setPassword(md5('password')); $contact->setMessenger('messenger'); $contact->setDateOfBirth(new \DateTime()); $contact->setGender($manager->find('General\\Entity\\Gender', 1)); $contact->setTitle($manager->find('General\\Entity\\Title', 1)); $manager->persist($contact); $manager->flush(); $contactOrganisation = new ContactOrganisation(); $contactOrganisation->setContact($contact); $contactOrganisation->setOrganisation($manager->find("Organisation\\Entity\\Organisation", 1)); $manager->persist($contactOrganisation); $manager->flush(); }
/** * 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; } }
/** * @return ViewModel */ public function newAction() { $contact = new Contact(); $data = array_merge($this->getRequest()->getPost()->toArray()); $form = $this->getFormService()->prepare($contact, $contact, $data); //Disable the inarray validator for organisations $form->get('contact')->get('organisation')->setDisableInArrayValidator(true); /** Show or hide buttons based on the status of a contact */ $form->remove('reactivate'); $form->remove('deactivate'); if ($this->getRequest()->isPost()) { /** Cancel the form */ if (isset($data['cancel'])) { return $this->redirect()->toRoute('zfcadmin/contact-admin/list'); } /** Handle the form */ if ($form->isValid()) { /** * @var $contact Contact */ $contact = $form->getData(); $contact = $this->getContactService()->updateEntity($contact); $contactOrganisation = new ContactOrganisation(); $contactOrganisation->setContact($contact); $contactOrganisation->setBranch(strlen($data['contact']['branch']) === 0 ? null : $data['contact']['branch']); $contactOrganisation->setOrganisation($this->getOrganisationService()->setOrganisationId($data['contact']['organisation'])->getOrganisation()); $this->getContactService()->updateEntity($contactOrganisation); return $this->redirect()->toRoute('zfcadmin/contact-admin/view', ['id' => $contact->getId()]); } } return new ViewModel(['form' => $form]); }
/** * We use this function to update the contactOrganisation of a user. * As input we use the corresponding contact entity and the array containing the * contactOrganisation information. * * $contactOrganisation['organisation_id'] > id of the chosen organisation * $contactOrganisation['branch'] > value of the branch (if an organisation_id is chosen) * $contactOrganisation['organisation'] > Name of the organisation * $contactOrganisation['country'] > CountryId * * @param Contact $contact * @param array $contactOrganisation */ public function updateContactOrganisation(Contact $contact, array $contactOrganisation) { /** * Find the current contactOrganisation, or create a new one if this empty (in case of a new contact) */ $currentContactOrganisation = $contact->getContactOrganisation(); if (is_null($currentContactOrganisation)) { $currentContactOrganisation = new ContactOrganisation(); $currentContactOrganisation->setContact($contact); } /** * The trigger for this update is the presence of a $contactOrganisation['organisation_id']. * If this value != 0, a choice has been made from the dropdown and we will then take the branch as default */ if (isset($contactOrganisation['organisation_id']) && $contactOrganisation['organisation_id'] != '0') { $organisation = $this->getOrganisationService()->findEntityById('organisation', (int) $contactOrganisation['organisation_id']); $currentContactOrganisation->setOrganisation($organisation); //Take te branch form the form element ($contactOrganisation['branch']) if (!empty($contactOrganisation['branch'])) { $currentContactOrganisation->setBranch($contactOrganisation['branch']); } else { $currentContactOrganisation->setBranch(null); } } else { /** * No organisation is chosen (the option 'none of the above' was taken, so create the organisation */ /** * Don't do anything when the organisationName = empty */ if (empty($contactOrganisation['organisation'])) { return; } $country = $this->getGeneralService()->findEntityById('country', (int) $contactOrganisation['country']); /* * Look for the organisation based on the name (without branch) and country + email */ $organisation = $this->getOrganisationService()->findOrganisationByNameCountryAndEmailAddress($contactOrganisation['organisation'], $country, $contact->getEmail()); $organisationFound = false; /* * We did not find an organisation, so we need to create it */ if (sizeof($organisation) === 0) { $organisation = new Organisation(); $organisation->setOrganisation($contactOrganisation['organisation']); $organisation->setCountry($country); $organisation->setType($this->organisationService->findEntityById('Type', 0)); //Unknown /* * Add the domain in the saved domains for this new company * Use the ZF2 EmailAddress validator to strip the hostname out of the EmailAddress */ $validateEmail = new EmailAddress(); $validateEmail->isValid($contact->getEmail()); $organisationWeb = new Web(); $organisationWeb->setOrganisation($organisation); $organisationWeb->setWeb($validateEmail->hostname); $organisationWeb->setMain(Web::MAIN); //Skip hostnames like yahoo, gmail and hotmail, outlook if (!in_array($organisation->getWeb(), ['gmail.com', 'hotmail.com', 'outlook.com', 'yahoo.com'])) { $this->getOrganisationService()->newEntity($organisationWeb); } $currentContactOrganisation->setOrganisation($organisation); } else { $foundOrganisation = null; /* * Go over the found organisation to match the branching */ foreach ($organisation as $foundOrganisation) { /* * Stop when we have found an exact match and reset the branch if set */ if ($foundOrganisation->getOrganisation() === $contactOrganisation['organisation'] && $country->getId() === $foundOrganisation->getCountry()->getId()) { $currentContactOrganisation->setOrganisation($foundOrganisation); $currentContactOrganisation->setBranch(null); break; } if (!$organisationFound) { //Create only a branch when the name is found and the given names do not match in length if (strlen($foundOrganisation->getOrganisation()) < strlen($contactOrganisation['organisation']) - strlen($currentContactOrganisation->getBranch())) { $currentContactOrganisation->setBranch(str_replace($contactOrganisation['organisation'], '~', $foundOrganisation->getOrganisation())); } else { //Reset the branch otherwise $currentContactOrganisation->setBranch(null); } /* * We have found a match of the organisation in the string and */ $organisationFound = true; } } $currentContactOrganisation->setOrganisation($foundOrganisation); } } $this->updateEntity($currentContactOrganisation); }
/** * We use this function to update the contactOrganisation of a user. * As input we use the corresponding contact entity and the array containing the * contactOrganisation information * * $contactOrganisation['organisation'] > Name of the organisation * $contactOrganisation['country'] > CountryId * * @param Contact $contact * @param array $contactOrganisation * * @return void */ public function updateContactOrganisation(Contact $contact, array $contactOrganisation) { /** * Don't do anything when the organisationName = empty */ if (empty($contactOrganisation['organisation'])) { return; } $country = $this->getGeneralService()->findEntityById('country', (int) $contactOrganisation['country']); $currentContactOrganisation = $contact->getContactOrganisation(); if (is_null($currentContactOrganisation)) { $currentContactOrganisation = new ContactOrganisation(); $currentContactOrganisation->setContact($contact); } /** * Look for the organisation based on the name (without branch) and country + email */ $organisation = $this->getOrganisationService()->findOrganisationByNameCountryAndEmailAddress($contactOrganisation['organisation'], $country, $contact->getEmail()); $organisationFound = false; /** * We did not find an organisation, so we need to create it */ if (sizeof($organisation) === 0) { $organisation = new Organisation(); $organisation->setOrganisation($contactOrganisation['organisation']); $organisation->setCountry($country); $organisation->setType($this->organisationService->findEntityById('Type', 0)); //Unknown /** * Add the domain in the saved domains for this new company * Use the ZF2 EmailAddress validator to strip the hostname out of the EmailAddress */ $validateEmail = new EmailAddress(); $validateEmail->isValid($contact->getEmail()); $organisationWeb = new Web(); $organisationWeb->setOrganisation($organisation); $organisationWeb->setWeb($validateEmail->hostname); $organisationWeb->setMain(Web::MAIN); $this->getOrganisationService()->newEntity($organisationWeb); $currentContactOrganisation->setOrganisation($organisation); } else { $foundOrganisation = null; /** * Go over the found organisation to match the branching */ foreach ($organisation as $foundOrganisation) { /** * Stop when we have found an exact match and reset the branch if set */ if ($foundOrganisation->getOrganisation() === $contactOrganisation['organisation'] && $country->getId() === $foundOrganisation->getCountry()->getId()) { $currentContactOrganisation->setOrganisation($foundOrganisation); $currentContactOrganisation->setBranch(null); break; } if (!$organisationFound) { //Create only a branch when the name is found and the given names do not match in length if (strlen($foundOrganisation->getOrganisation()) < strlen($contactOrganisation['organisation']) - strlen($currentContactOrganisation->getBranch())) { $currentContactOrganisation->setBranch(str_replace($contactOrganisation['organisation'], '~', $foundOrganisation->getOrganisation())); } /** * We have found a match of the organisation in the string and */ $organisationFound = true; } } $currentContactOrganisation->setOrganisation($foundOrganisation); } $this->updateEntity($currentContactOrganisation); }
public function testCanAcceptInvite() { $invite = $this->inviteService->setInviteId(1)->getInvite(); $contact = $this->entityManager->find('Contact\\Entity\\Contact', 1); $organisation = $this->entityManager->find("Organisation\\Entity\\Organisation", 1); $contactOrganisation = new ContactOrganisation(); $contactOrganisation->setContact($contact); $contactOrganisation->setOrganisation($organisation); $this->assertTrue($this->inviteService->acceptInvitation($invite, $contact, InviteAccept::OPTION_NEW_PARTNER)); $this->assertTrue($this->inviteService->acceptInvitation($invite, $contact, InviteAccept::OPTION_FINANCIAL_CONTACT)); $this->assertTrue($this->inviteService->acceptInvitation($invite, $contact, InviteAccept::OPTION_ADDITIONAL_CONTACT)); $this->assertTrue($this->inviteService->acceptInvitation($invite, $contact, InviteAccept::OPTION_MAIN_TECHNICAL_CONTACT)); }