<?php
/* $Id: mgw_Contact_Vcard_Parse.php,v 1.4 2003/05/28 19:14:37 k-fish Exp $ */

require_once('Contact_Vcard_Parse.php');

class mgw_Contact_Vcard_Parse extends Contact_Vcard_Parse {
    
    /**
    *
    * Constructor.
    *
    * @access public
    * @return void
    *
    */
    
    function mgw_Contact_Vcard_Parse()
    {
    }

    // return all last names concatenated with
    // single space chars
    function getLastNames(&$card){
	return (isset($card['N'][0]['value'][0])) ? implode(' ', $card['N'][0]['value'][0]) : '';
    }

    // return all given names concatenated with
    // single space chars
    function getGivenNames(&$card){
	return (isset($card['N'][0]['value'][1])) ? implode(' ', $card['N'][0]['value'][1]) : '';
    }

    // return all additional names concatenated with
    // single space chars
    function getAddNames(&$card){
	return (isset($card['N'][0]['value'][2])) ? implode(' ', $card['N'][0]['value'][2]) : '';
    }

    // return all name prefixes concatenated with
    // single space chars
    function getNamePrefixes(&$card){
	return (isset($card['N'][0]['value'][3])) ? implode(' ', $card['N'][0]['value'][3]) : '';
    }

    // return all name suffixes concatenated with
    // single space chars
    function getNameSuffixes(&$card){
	return (isset($card['N'][0]['value'][4])) ? implode(' ', $card['N'][0]['value'][4]) : '';
    }

    // return all nicknames concatenated with
    // single space chars
    function getNicknames(&$card){
	return (isset($card['NICKNAME'][0]['value'])) ? implode(' ', $card['NICKNAME'][0]['value']) : '';
    }

    // return all POB parts concatenated with
    // single space chars
    function getPOBox(&$adr){
	return (isset($adr['value'][0])) ? implode(' ', $adr['value'][0]) : '';
    }

    // return all extended address parts concatenated with
    // single space chars
    function getExtAdr(&$adr){
	return (isset($adr['value'][1])) ? implode(' ', $adr['value'][1]) : '';
    }

    // return all street parts concatenated with
    // single space chars
    function getStreet(&$adr){
	return (isset($adr['value'][2])) ? implode(' ', $adr['value'][2]) : '';
    }

    // return all POB parts concatenated with
    // single space chars
    function getLocality(&$adr){
	return (isset($adr['value'][3])) ? implode(' ', $adr['value'][3]) : '';
    }

    // return all region parts concatenated with
    // single space chars
    function getRegion(&$adr){
	return (isset($adr['value'][4])) ? implode(' ', $adr['value'][4]) : '';
    }

    // return all ZIP (postcode)  parts concatenated with
    // single space chars
    function getZIP(&$adr){
	return (isset($adr['value'][5])) ? implode(' ', $adr['value'][5]) : '';
    }

    // return all ZIP (postcode)  parts concatenated with
    // single space chars
    function getCountry(&$adr){
	return (isset($adr['value'][6])) ? implode(' ', $adr['value'][6]) : '';
    }

    function getTitle(&$card){
	return (isset($card['TITLE'][0]['value'][0][0])) ? $card['TITLE'][0]['value'][0][0] : '';
    }

    function getNote(&$card){
	return (isset($card['NOTE'][0]['value'][0][0])) ? $card['NOTE'][0]['value'][0][0] : '';
    }

    // getOrg returns the first ORG entry
    function getOrg(&$card){
	return (isset($card['ORG'][0]['value'][0][0])) ? $card['ORG'][0]['value'][0][0] : '';
    }

    // getSubOrg returns the first ORG entry
    // there are only two name fields for companies,
    // so this is enough...
    function getSubOrg(&$card){
	return (isset($card['ORG'][0]['value'][0][1])) ? $card['ORG'][0]['value'][0][1] : '';
    }

    function getBirthday(&$card){
	if(!isset($card['BDAY'][0]['value'][0][0])) return '';

	if($card['BDAY'][0]['value'][0][0]{4} == '-')
	    return substr($card['BDAY'][0]['value'][0][0],0,10);
	else
	    return substr($card['BDAY'][0]['value'][0][0],0,4).'-'.
		substr($card['BDAY'][0]['value'][0][0],4,2).'-'.
		substr($card['BDAY'][0]['value'][0][0],6,2);
    }

    function getURL(&$card){
	reset($card);
	if(!isset($card['URL'])) return array();

	$rawurls = &$card['URL'];
	reset($rawurls);
	$urls = array();
	while($rawurl = current($rawurls)){
	    if(in_array('PREF', $rawurl['param']['TYPE']))
		array_unshift($urls, $rawurl['value'][0][0]);
	    else
		$urls[] = $rawurl['value'][0][0];
	    next($rawurls);
	}
	return $urls;
    }

    function getEMail(&$card){
	reset($card);
	if(!is_array($card['EMAIL'])) return array();

	$rawemails = &$card['EMAIL'];
	reset($rawemails);
	$emails = array();
	while($rawemail = current($rawemails)){
	    if(in_array('PREF', $rawemail['param']['TYPE']))
		array_unshift($emails, $rawemail['value'][0][0]);
	    else
		$emails[] = $rawemail['value'][0][0];
	    next($rawemails);
	}
	return $emails;
    }

    function getAddress(&$card, $type='WORK'){
	reset($card);
	if(!isset($card['ADR'])) return array();

	$rawadrs = &$card['ADR'];
	reset($rawadrs);
	$adrs = array();

	while($rawadr = current($rawadrs)){
	    // set TYPE defaults (intl,postal,parcel,work)
	    if(count($rawadr['param']['TYPE']) == 0){
		if(!in_array('INTL', $rawadr['param']['TYPE']))
		    $rawadr['param']['TYPE'][] = 'INTL';
		if(!in_array('POSTAL', $rawadr['param']['TYPE']))
		    $rawadr['param']['TYPE'][] = 'POSTAL';
		if(!in_array('PARCEL', $rawadr['param']['TYPE']))
		    $rawadr['param']['TYPE'][] = 'PARCEL';
		if(!in_array('WORK', $rawadr['param']['TYPE']))
		    $rawadr['param']['TYPE'][] = 'WORK';
	    }

	    if($this->typeCheck($type, $rawadr['param']['TYPE'])){
		if(in_array('PREF', $rawadr['param']['TYPE']))
		    array_unshift($adrs, $rawadr);
		else
		    $adrs[] = $rawadr;
	    }
	    next($rawadrs);
	}

	return $adrs;
    }

    function typeCheck($type,$param){
	$types = explode(',', $type);
	if(count($types)==1)
	    return in_array($types[0],$param);
	else{
	    $ret = true;
	    for($c=0;$c<count($types);$c++){
		$ret = $ret && in_array($types[$c],$param);
	    }
	    return $ret;
	}
    }

    function getPhone(&$card, $type='VOICE'){
	reset($card);
	if(!isset($card['TEL'])) return array();

	$rawnumbers = &$card['TEL'];
	reset($rawnumbers);
	$numbers = array();
	while($rawnumber = current($rawnumbers)){
	    // set TYPE defaults (voice)
	    if(!in_array('VOICE', $rawnumber['param']['TYPE']))
		$rawnumber['param']['TYPE'][] = 'VOICE';

	    if($this->typeCheck($type, $rawnumber['param']['TYPE'])){
		if(in_array('PREF', $rawnumber['param']['TYPE']))
		    array_unshift($numbers, $rawnumber['value'][0][0]);
		else
		    $numbers[] = $rawnumber['value'][0][0];
	    }
	    next($rawnumbers);
	}
	return $numbers;
    }

}

?>