Turba_Driver::toASContact PHP Method

toASContact() public method

Convert the contact to an ActiveSync contact message
public toASContact ( Turba_Object $object, array $options = [] ) : Horde_ActiveSync_Message_Contact
$object Turba_Object The turba object to convert
$options array Options: - protocolversion: (float) The EAS version to support DEFAULT: 2.5 - bodyprefs: (array) A BODYPREFERENCE array. DEFAULT: none (No body prefs enforced). - truncation: (integer) Truncate event body to this length DEFAULT: none (No truncation). - device: (Horde_ActiveSync_Device) The device object.
return Horde_ActiveSync_Message_Contact
    public function toASContact(Turba_Object $object, array $options = array())
    {
        global $injector;
        $message = new Horde_ActiveSync_Message_Contact(array('logger' => $injector->getInstance('Horde_Log_Logger'), 'protocolversion' => $options['protocolversion'], 'device' => !empty($options['device']) ? $options['device'] : null));
        $hash = $object->getAttributes();
        if (!isset($hash['lastname']) && isset($hash['name'])) {
            $this->_guessName($hash);
        }
        // Ensure we have at least a good guess as to separate address fields.
        // Not ideal, but EAS does not have a single "address" field so we must
        // map "common" to either home or work. I choose home since
        // work/non-personal installs will be more likely to have separated
        // address fields.
        if (!empty($hash['commonAddress'])) {
            if (!isset($hash['commonStreet'])) {
                $hash['commonStreet'] = $hash['commonHome'];
            }
            foreach (array('Address', 'Street', 'POBox', 'Extended', 'City', 'Province', 'PostalCode', 'Country') as $field) {
                $hash['home' . $field] = $hash['common' . $field];
            }
        } else {
            if (isset($hash['homeAddress']) && !isset($hash['homeStreet'])) {
                $hash['homeStreet'] = $hash['homeAddress'];
            }
            if (isset($hash['workAddress']) && !isset($hash['workStreet'])) {
                $hash['workStreet'] = $hash['workAddress'];
            }
        }
        $hooks = $injector->getInstance('Horde_Core_Hooks');
        $decode_hook = $hooks->hookExists('decode_attribute', 'turba');
        foreach ($hash as $field => $value) {
            if ($decode_hook) {
                try {
                    $value = $hooks->callHook('decode_attribute', 'turba', array($field, $value, $object));
                } catch (Turba_Exception $e) {
                    Horde::log($e);
                }
            }
            if (isset(self::$_asMap[$field])) {
                try {
                    $message->{self::$_asMap[$field]} = $value;
                } catch (InvalidArgumentException $e) {
                }
                continue;
            }
            switch ($field) {
                case 'photo':
                    $message->picture = base64_encode($value);
                    break;
                case 'homeCountry':
                    $message->homecountry = !empty($hash['homeCountryFree']) ? $hash['homeCountryFree'] : !empty($hash['homeCountry']) ? Horde_Nls::getCountryISO($hash['homeCountry']) : null;
                    break;
                case 'otherCountry':
                    $message->othercountry = !empty($hash['otherCountryFree']) ? $hash['otherCountryFree'] : !empty($hash['otherCountry']) ? Horde_Nls::getCountryISO($hash['otherCountry']) : null;
                    break;
                case 'workCountry':
                    $message->businesscountry = !empty($hash['workCountryFree']) ? $hash['workCountryFree'] : !empty($hash['workCountry']) ? Horde_Nls::getCountryISO($hash['workCountry']) : null;
                    break;
                case 'email':
                    $message->email1address = $value;
                    break;
                case 'homeEmail':
                    $message->email2address = $value;
                    break;
                case 'workEmail':
                    $message->email3address = $value;
                    break;
                case 'emails':
                    $address = 1;
                    foreach (explode(',', $value) as $email) {
                        while ($address <= 3 && $message->{'email' . $address . 'address'}) {
                            $address++;
                        }
                        if ($address > 3) {
                            break;
                        }
                        $message->{'email' . $address . 'address'} = $email;
                        $address++;
                    }
                    break;
                case 'children':
                    // Children FROM horde are a simple string value. Even though EAS
                    // uses an array stucture to pass them, we pass as a single
                    // string since we can't assure what delimter the user will
                    // use and (at least in some languages) a comma can be used
                    // within a full name.
                    $message->children = array($value);
                    break;
                case 'notes':
                    if (strlen($value) && $options['protocolversion'] > Horde_ActiveSync::VERSION_TWOFIVE) {
                        $bp = $options['bodyprefs'];
                        $note = new Horde_ActiveSync_Message_AirSyncBaseBody();
                        // No HTML supported in Turba's notes. Always use plaintext.
                        $note->type = Horde_ActiveSync::BODYPREF_TYPE_PLAIN;
                        if (isset($bp[Horde_ActiveSync::BODYPREF_TYPE_PLAIN]['truncationsize']) && Horde_String::length($value) > $bp[Horde_ActiveSync::BODYPREF_TYPE_PLAIN]['truncationsize']) {
                            $note->data = Horde_String::substr($value, 0, $bp[Horde_ActiveSync::BODYPREF_TYPE_PLAIN]['truncationsize']);
                            $note->truncated = 1;
                        } else {
                            $note->data = $value;
                        }
                        $note->estimateddatasize = Horde_String::length($value);
                        $message->airsyncbasebody = $note;
                    } elseif (strlen($value)) {
                        // EAS 2.5
                        $message->body = $value;
                        $message->bodysize = strlen($message->body);
                        $message->bodytruncated = 0;
                    }
                    break;
                case 'birthday':
                case 'anniversary':
                    if (!empty($value) && $value != '0000-00-00') {
                        try {
                            $date = new Horde_Date($value);
                        } catch (Horde_Date_Exception $e) {
                            $message->{$field} = null;
                        }
                        // Some sanity checking to make sure the date was
                        // successfully parsed.
                        if ($date->month != 0) {
                            $message->{$field} = $date;
                        } else {
                            $message->{$field} = null;
                        }
                    } else {
                        $message->{$field} = null;
                    }
                    break;
            }
        }
        /* Get tags. */
        $message->categories = $injector->getInstance('Turba_Tagger')->split($object->getValue('__tags'));
        if (empty($this->fileas)) {
            $message->fileas = Turba::formatName($object);
        }
        return $message;
    }