/** * @param Community $community * * @return string * @throws \Exception */ public function __invoke(Community $community) { $uri = '<a href="%s" title="%s" class="%s">%s</a>'; $img = '<img src="%s">'; $classes = []; $link = preg_replace('/^([^\\~]+)(\\~(.*))?$/', '${1}' . $community->getCommunity() . '$3', $community->getType()->getLink()); return sprintf($uri, $link, sprintf($this->translate("txt-go-to-%s-profile"), $community->getType()->getType()), implode($classes), sprintf($img, $this->getUrl('assets/style-image', array('source' => $community->getType()->getImage())))); }
/** * New function needed to make the hydrator happy * * @param Collections\Collection $communityCollection */ public function removeCommunity(Collections\Collection $communityCollection) { foreach ($communityCollection as $single) { $this->community->removeElement($single); } }
/** * Hydrate $object with the provided $data. * * @param array $data * @param Contact $object * * @return object */ public function hydrate(array $data, $object) { $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'] = []; $communityData = $data['community']; $data['community'] = []; $contact = $this->hydrateByValue($data, $object); $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(), array(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(), array(AddressType::ADDRESS_TYPE_MAIL))) { $contact->getAddress()->add($address); } } /** * Reformat the community */ $contact->getCommunity()->clear(); foreach ($communityData as $communityTypeId => $communityInfo) { if (!empty($communityInfo['community'])) { $community = new Community(); $community->setType($this->objectManager->getReference('General\\Entity\\CommunityType', $communityTypeId)); $community->setCommunity($communityInfo['community']); $community->setContact($contact); $contact->getCommunity()->add($community); } } $contact->getProfile()->setContact($contact); return $contact; } return $this->hydrateByValue($data, $object); }