Turba_Driver::toHash PHP Method

toHash() public method

Function to convert a Horde_Icalendar_Vcard object into a Turba Object Hash with Turba attributes suitable as a parameter for add().
See also: add()
public toHash ( Horde_Icalendar_Vcard $vcard ) : array
$vcard Horde_Icalendar_Vcard The Horde_Icalendar_Vcard object to parse.
return array A Turba attribute hash.
    public function toHash(Horde_Icalendar_Vcard $vcard)
    {
        global $attributes;
        $hash = array();
        $attr = $vcard->getAllAttributes();
        foreach ($attr as $item) {
            switch ($item['name']) {
                case 'UID':
                    $hash['__uid'] = $item['value'];
                    break;
                case 'FN':
                    $hash['name'] = $item['value'];
                    break;
                case 'N':
                    $name = $item['values'];
                    if (!empty($name[Horde_Icalendar_Vcard::N_FAMILY])) {
                        $hash['lastname'] = $name[Horde_Icalendar_Vcard::N_FAMILY];
                    }
                    if (!empty($name[Horde_Icalendar_Vcard::N_GIVEN])) {
                        $hash['firstname'] = $name[Horde_Icalendar_Vcard::N_GIVEN];
                    }
                    if (!empty($name[Horde_Icalendar_Vcard::N_ADDL])) {
                        $hash['middlenames'] = $name[Horde_Icalendar_Vcard::N_ADDL];
                    }
                    if (!empty($name[Horde_Icalendar_Vcard::N_PREFIX])) {
                        $hash['namePrefix'] = $name[Horde_Icalendar_Vcard::N_PREFIX];
                    }
                    if (!empty($name[Horde_Icalendar_Vcard::N_SUFFIX])) {
                        $hash['nameSuffix'] = $name[Horde_Icalendar_Vcard::N_SUFFIX];
                    }
                    break;
                case 'NICKNAME':
                case 'X-EPOCSECONDNAME':
                    $hash['nickname'] = $item['value'];
                    $hash['alias'] = $item['value'];
                    break;
                    // We use LABEL but also support ADR.
                // We use LABEL but also support ADR.
                case 'LABEL':
                    if (isset($item['params']['HOME']) && !isset($hash['homeAddress'])) {
                        $hash['homeAddress'] = $item['value'];
                    } elseif (isset($item['params']['WORK']) && !isset($hash['workAddress'])) {
                        $hash['workAddress'] = $item['value'];
                    } elseif (!isset($hash['commonAddress'])) {
                        $hash['commonAddress'] = $item['value'];
                    }
                    break;
                case 'ADR':
                    if (isset($item['params']['TYPE'])) {
                        if (!is_array($item['params']['TYPE'])) {
                            $item['params']['TYPE'] = array($item['params']['TYPE']);
                        }
                    } else {
                        $item['params']['TYPE'] = array();
                        if (isset($item['params']['WORK'])) {
                            $item['params']['TYPE'][] = 'WORK';
                        }
                        if (isset($item['params']['HOME'])) {
                            $item['params']['TYPE'][] = 'HOME';
                        }
                        if (count($item['params']['TYPE']) == 0) {
                            $item['params']['TYPE'][] = 'COMMON';
                        }
                    }
                    $address = $item['values'];
                    foreach ($item['params']['TYPE'] as $adr) {
                        switch (Horde_String::upper($adr)) {
                            case 'HOME':
                                $prefix = 'home';
                                break;
                            case 'WORK':
                                $prefix = 'work';
                                break;
                            default:
                                $prefix = 'common';
                        }
                        if (isset($hash[$prefix . 'Address'])) {
                            continue;
                        }
                        $hash[$prefix . 'Address'] = '';
                        if (!empty($address[Horde_Icalendar_Vcard::ADR_STREET])) {
                            $hash[$prefix . 'Street'] = $address[Horde_Icalendar_Vcard::ADR_STREET];
                            $hash[$prefix . 'Address'] .= $hash[$prefix . 'Street'] . "\n";
                        }
                        if (!empty($address[Horde_Icalendar_Vcard::ADR_EXTEND])) {
                            $hash[$prefix . 'Extended'] = $address[Horde_Icalendar_Vcard::ADR_EXTEND];
                            $hash[$prefix . 'Address'] .= $hash[$prefix . 'Extended'] . "\n";
                        }
                        if (!empty($address[Horde_Icalendar_Vcard::ADR_POB])) {
                            $hash[$prefix . 'POBox'] = $address[Horde_Icalendar_Vcard::ADR_POB];
                            $hash[$prefix . 'Address'] .= $hash[$prefix . 'POBox'] . "\n";
                        }
                        if (!empty($address[Horde_Icalendar_Vcard::ADR_LOCALITY])) {
                            $hash[$prefix . 'City'] = $address[Horde_Icalendar_Vcard::ADR_LOCALITY];
                            $hash[$prefix . 'Address'] .= $hash[$prefix . 'City'];
                        }
                        if (!empty($address[Horde_Icalendar_Vcard::ADR_REGION])) {
                            $hash[$prefix . 'Province'] = $address[Horde_Icalendar_Vcard::ADR_REGION];
                            $hash[$prefix . 'Address'] .= ', ' . $hash[$prefix . 'Province'];
                        }
                        if (!empty($address[Horde_Icalendar_Vcard::ADR_POSTCODE])) {
                            $hash[$prefix . 'PostalCode'] = $address[Horde_Icalendar_Vcard::ADR_POSTCODE];
                            $hash[$prefix . 'Address'] .= ' ' . $hash[$prefix . 'PostalCode'];
                        }
                        if (!empty($address[Horde_Icalendar_Vcard::ADR_COUNTRY])) {
                            include 'Horde/Nls/Countries.php';
                            $country = array_search($address[Horde_Icalendar_Vcard::ADR_COUNTRY], $countries);
                            if ($country === false) {
                                $country = $address[Horde_Icalendar_Vcard::ADR_COUNTRY];
                            }
                            $hash[$prefix . 'Country'] = $country;
                            $hash[$prefix . 'Address'] .= "\n" . $address[Horde_Icalendar_Vcard::ADR_COUNTRY];
                        }
                        $hash[$prefix . 'Address'] = trim($hash[$prefix . 'Address']);
                    }
                    break;
                case 'TZ':
                    // We only support textual timezones.
                    if (!isset($item['params']['VALUE']) || Horde_String::lower($item['params']['VALUE']) != 'text') {
                        break;
                    }
                    $timezones = explode(';', $item['value']);
                    $available_timezones = Horde_Nls::getTimezones();
                    foreach ($timezones as $timezone) {
                        $timezone = trim($timezone);
                        if (isset($available_timezones[$timezone])) {
                            $hash['timezone'] = $timezone;
                            break 2;
                        }
                    }
                    break;
                case 'GEO':
                    if (isset($item['params']['HOME'])) {
                        $hash['homeLatitude'] = $item['value']['latitude'];
                        $hash['homeLongitude'] = $item['value']['longitude'];
                    } elseif (isset($item['params']['WORK'])) {
                        $hash['workLatitude'] = $item['value']['latitude'];
                        $hash['workLongitude'] = $item['value']['longitude'];
                    } else {
                        $hash['latitude'] = $item['value']['latitude'];
                        $hash['longitude'] = $item['value']['longitude'];
                    }
                    break;
                case 'TEL':
                    if (isset($item['params']['FAX'])) {
                        if (isset($item['params']['WORK']) && !isset($hash['workFax'])) {
                            $hash['workFax'] = $item['value'];
                        } elseif (isset($item['params']['HOME']) && !isset($hash['homeFax'])) {
                            $hash['homeFax'] = $item['value'];
                        } elseif (!isset($hash['fax'])) {
                            $hash['fax'] = $item['value'];
                        }
                    } elseif (isset($item['params']['PAGER']) && !isset($hash['pager'])) {
                        $hash['pager'] = $item['value'];
                    } elseif (isset($item['params']['TYPE'])) {
                        if (!is_array($item['params']['TYPE'])) {
                            $item['params']['TYPE'] = array($item['params']['TYPE']);
                        }
                        foreach ($item['params']['TYPE'] as &$type) {
                            $type = Horde_String::upper($type);
                        }
                        // For vCard 3.0.
                        if (in_array('CELL', $item['params']['TYPE'])) {
                            if (in_array('HOME', $item['params']['TYPE']) && !isset($hash['homeCellPhone'])) {
                                $hash['homeCellPhone'] = $item['value'];
                            } elseif (in_array('WORK', $item['params']['TYPE']) && !isset($hash['workCellPhone'])) {
                                $hash['workCellPhone'] = $item['value'];
                            } elseif (!isset($hash['cellPhone'])) {
                                $hash['cellPhone'] = $item['value'];
                            }
                        } elseif (in_array('FAX', $item['params']['TYPE'])) {
                            if (in_array('HOME', $item['params']['TYPE']) && !isset($hash['homeFax'])) {
                                $hash['homeFax'] = $item['value'];
                            } elseif (in_array('WORK', $item['params']['TYPE']) && !isset($hash['workFax'])) {
                                $hash['workFax'] = $item['value'];
                            } elseif (!isset($hash['fax'])) {
                                $hash['fax'] = $item['value'];
                            }
                        } elseif (in_array('VIDEO', $item['params']['TYPE'])) {
                            if (in_array('HOME', $item['params']['TYPE']) && !isset($hash['homeVideoCall'])) {
                                $hash['homeVideoCall'] = $item['value'];
                            } elseif (in_array('WORK', $item['params']['TYPE']) && !isset($hash['workVideoCall'])) {
                                $hash['workVideoCall'] = $item['value'];
                            } elseif (!isset($hash['videoCall'])) {
                                $hash['videoCall'] = $item['value'];
                            }
                        } elseif (in_array('PAGER', $item['params']['TYPE']) && !isset($hash['pager'])) {
                            $hash['pager'] = $item['value'];
                        } elseif (in_array('WORK', $item['params']['TYPE']) && !isset($hash['workPhone'])) {
                            $hash['workPhone'] = $item['value'];
                        } elseif (in_array('HOME', $item['params']['TYPE']) && !isset($hash['homePhone'])) {
                            $hash['homePhone'] = $item['value'];
                        } elseif (!isset($hash['phone'])) {
                            $hash['phone'] = $item['value'];
                        }
                    } elseif (isset($item['params']['CELL'])) {
                        if (isset($item['params']['WORK']) && !isset($hash['workCellPhone'])) {
                            $hash['workCellPhone'] = $item['value'];
                        } elseif (isset($item['params']['HOME']) && !isset($hash['homeCellPhone'])) {
                            $hash['homeCellPhone'] = $item['value'];
                        } elseif (!isset($hash['cellPhone'])) {
                            $hash['cellPhone'] = $item['value'];
                        }
                    } elseif (isset($item['params']['VIDEO'])) {
                        if (isset($item['params']['WORK']) && !isset($hash['workVideoCall'])) {
                            $hash['workVideoCall'] = $item['value'];
                        } elseif (isset($item['params']['HOME']) && !isset($hash['homeVideoCall'])) {
                            $hash['homeVideoCall'] = $item['value'];
                        } elseif (!isset($hash['videoCall'])) {
                            $hash['videoCall'] = $item['value'];
                        }
                    } else {
                        if (isset($item['params']['WORK']) && !isset($hash['workPhone'])) {
                            $hash['workPhone'] = $item['value'];
                        } elseif (isset($item['params']['HOME']) && !isset($hash['homePhone'])) {
                            $hash['homePhone'] = $item['value'];
                        } else {
                            $hash['phone'] = $item['value'];
                        }
                    }
                    break;
                case 'EMAIL':
                    $email_set = false;
                    if (isset($item['params']['HOME']) && !empty($this->map['homeEmail']) && (!isset($hash['homeEmail']) || isset($item['params']['PREF']))) {
                        $e = Horde_Icalendar_Vcard::getBareEmail($item['value']);
                        $hash['homeEmail'] = $e ? $e : '';
                        $email_set = true;
                    } elseif (isset($item['params']['WORK']) && !empty($this->map['workEmail']) && (!isset($hash['workEmail']) || isset($item['params']['PREF']))) {
                        $e = Horde_Icalendar_Vcard::getBareEmail($item['value']);
                        $hash['workEmail'] = $e ? $e : '';
                        $email_set = true;
                    } elseif (isset($item['params']['TYPE'])) {
                        if (!is_array($item['params']['TYPE'])) {
                            $item['params']['TYPE'] = array($item['params']['TYPE']);
                        }
                        foreach ($item['params']['TYPE'] as &$type) {
                            $type = Horde_String::upper($type);
                        }
                        if (in_array('HOME', $item['params']['TYPE']) && !empty($this->map['homeEmail']) && (!isset($hash['homeEmail']) || in_array('PREF', $item['params']['TYPE']))) {
                            $e = Horde_Icalendar_Vcard::getBareEmail($item['value']);
                            $hash['homeEmail'] = $e ? $e : '';
                            $email_set = true;
                        } elseif (in_array('WORK', $item['params']['TYPE']) && !empty($this->map['workEmail']) && (!isset($hash['workEmail']) || in_array('PREF', $item['params']['TYPE']))) {
                            $e = Horde_Icalendar_Vcard::getBareEmail($item['value']);
                            $hash['workEmail'] = $e ? $e : '';
                            $email_set = true;
                        }
                    }
                    if (!$email_set && (!isset($hash['email']) || isset($item['params']['PREF']) || !empty($item['params']['TYPE']) && is_array($item['params']['TYPE']) && in_array('PREF', $item['params']['TYPE']))) {
                        $e = Horde_Icalendar_Vcard::getBareEmail($item['value']);
                        $hash['email'] = $e ? $e : '';
                    }
                    if (!isset($hash['emails'])) {
                        $hash['emails'] = '';
                    }
                    if ($e = Horde_Icalendar_Vcard::getBareEmail($item['value'])) {
                        if (strlen($hash['emails'])) {
                            $hash['emails'] .= ',';
                        }
                        $hash['emails'] .= $e;
                    }
                    break;
                case 'TITLE':
                    $hash['title'] = $item['value'];
                    break;
                case 'ROLE':
                    $hash['role'] = $item['value'];
                    break;
                case 'ORG':
                    // The VCARD 2.1 specification requires the presence of two
                    // SEMI-COLON separated fields: Organizational Name and
                    // Organizational Unit. Additional fields are optional.
                    $hash['company'] = !empty($item['values'][0]) ? $item['values'][0] : '';
                    $hash['department'] = !empty($item['values'][1]) ? $item['values'][1] : '';
                    break;
                case 'NOTE':
                    $hash['notes'] = $item['value'];
                    break;
                case 'CATEGORIES':
                    $hash['businessCategory'] = $item['value'];
                    $hash['__tags'] = $item['values'];
                    break;
                case 'URL':
                    if (isset($item['params']['HOME']) && !isset($hash['homeWebsite'])) {
                        $hash['homeWebsite'] = $item['value'];
                    } elseif (isset($item['params']['WORK']) && !isset($hash['workWebsite'])) {
                        $hash['workWebsite'] = $item['value'];
                    } elseif (!isset($hash['website'])) {
                        $hash['website'] = $item['value'];
                    }
                    break;
                case 'BDAY':
                    if (empty($item['value'])) {
                        $hash['birthday'] = null;
                    } else {
                        $hash['birthday'] = $item['value']['year'] . '-' . $item['value']['month'] . '-' . $item['value']['mday'];
                    }
                    break;
                case 'PHOTO':
                case 'LOGO':
                    if (isset($item['params']['VALUE']) && Horde_String::lower($item['params']['VALUE']) == 'uri') {
                        // No support for URIs yet.
                        break;
                    }
                    if (!isset($item['params']['ENCODING']) || Horde_String::lower($item['params']['ENCODING']) != 'b' && Horde_String::upper($item['params']['ENCODING']) != 'BASE64') {
                        // Invalid property.
                        break;
                    }
                    $type = Horde_String::lower($item['name']);
                    $type_key = $type . (!empty($this->map[$type . '_orig']) ? '_orig' : '');
                    $hash[$type_key] = base64_decode($item['value']);
                    if (isset($item['params']['TYPE'])) {
                        $hash[$type . 'type'] = $item['params']['TYPE'];
                    }
                    break;
                case 'X-SIP':
                    if (isset($item['params']['POC']) && !isset($hash['ptt'])) {
                        $hash['ptt'] = $item['value'];
                    } elseif (isset($item['params']['VOIP']) && !isset($hash['voip'])) {
                        $hash['voip'] = $item['value'];
                    } elseif (isset($item['params']['SWIS']) && !isset($hash['shareView'])) {
                        $hash['shareView'] = $item['value'];
                    } elseif (!isset($hash['sip'])) {
                        $hash['sip'] = $item['value'];
                    }
                    break;
                case 'X-WV-ID':
                    $hash['imaddress'] = $item['value'];
                    break;
                case 'X-ANNIVERSARY':
                    if (empty($item['value'])) {
                        $hash['anniversary'] = null;
                    } else {
                        $hash['anniversary'] = $item['value']['year'] . '-' . $item['value']['month'] . '-' . $item['value']['mday'];
                    }
                    break;
                case 'X-CHILDREN':
                    $hash['children'] = $item['value'];
                    break;
                case 'X-SPOUSE':
                    $hash['spouse'] = $item['value'];
                    break;
            }
        }
        /* Ensure we have a valid name field. */
        if (empty($hash['name'])) {
            /* If name is a composite field, it won't be present in the
             * $this->fields array, so check for that as well. */
            if (isset($this->map['name']) && is_array($this->map['name']) && !empty($this->map['name']['attribute'])) {
                $fieldarray = array();
                foreach ($this->map['name']['fields'] as $mapfields) {
                    $fieldarray[] = isset($hash[$mapfields]) ? $hash[$mapfields] : '';
                }
                $hash['name'] = Turba::formatCompositeField($this->map['name']['format'], $fieldarray);
            } else {
                $hash['name'] = isset($hash['firstname']) ? $hash['firstname'] : '';
                if (!empty($hash['lastname'])) {
                    $hash['name'] .= ' ' . $hash['lastname'];
                }
                $hash['name'] = trim($hash['name']);
            }
        }
        return $hash;
    }

Usage Example

Beispiel #1
0
 protected function toHash($vcard, $map = array())
 {
     $driver = new Turba_Driver();
     foreach ($map as $field => $config) {
         $driver->map[$field] = $config;
     }
     $ical = new Horde_Icalendar();
     $ical->parsevCalendar($vcard);
     return $driver->toHash($ical->getComponent(0));
 }