1<?php
2/* $Id: mgw_Contact_Vcard_Parse.php,v 1.4 2003/05/28 19:14:37 k-fish Exp $ */
3
4require_once('Contact_Vcard_Parse.php');
5
6class mgw_Contact_Vcard_Parse extends Contact_Vcard_Parse {
7
8    /**
9    *
10    * Constructor.
11    *
12    * @access public
13    * @return void
14    *
15    */
16
17    function mgw_Contact_Vcard_Parse()
18    {
19    }
20
21    // return all last names concatenated with
22    // single space chars
23    function getLastNames(&$card){
24	return (isset($card['N'][0]['value'][0])) ? implode(' ', $card['N'][0]['value'][0]) : '';
25    }
26
27    // return all given names concatenated with
28    // single space chars
29    function getGivenNames(&$card){
30	return (isset($card['N'][0]['value'][1])) ? implode(' ', $card['N'][0]['value'][1]) : '';
31    }
32
33    // return all additional names concatenated with
34    // single space chars
35    function getAddNames(&$card){
36	return (isset($card['N'][0]['value'][2])) ? implode(' ', $card['N'][0]['value'][2]) : '';
37    }
38
39    // return all name prefixes concatenated with
40    // single space chars
41    function getNamePrefixes(&$card){
42	return (isset($card['N'][0]['value'][3])) ? implode(' ', $card['N'][0]['value'][3]) : '';
43    }
44
45    // return all name suffixes concatenated with
46    // single space chars
47    function getNameSuffixes(&$card){
48	return (isset($card['N'][0]['value'][4])) ? implode(' ', $card['N'][0]['value'][4]) : '';
49    }
50
51    // return all nicknames concatenated with
52    // single space chars
53    function getNicknames(&$card){
54	return (isset($card['NICKNAME'][0]['value'])) ? implode(' ', $card['NICKNAME'][0]['value']) : '';
55    }
56
57    // return all POB parts concatenated with
58    // single space chars
59    function getPOBox(&$adr){
60	return (isset($adr['value'][0])) ? implode(' ', $adr['value'][0]) : '';
61    }
62
63    // return all extended address parts concatenated with
64    // single space chars
65    function getExtAdr(&$adr){
66	return (isset($adr['value'][1])) ? implode(' ', $adr['value'][1]) : '';
67    }
68
69    // return all street parts concatenated with
70    // single space chars
71    function getStreet(&$adr){
72	return (isset($adr['value'][2])) ? implode(' ', $adr['value'][2]) : '';
73    }
74
75    // return all POB parts concatenated with
76    // single space chars
77    function getLocality(&$adr){
78	return (isset($adr['value'][3])) ? implode(' ', $adr['value'][3]) : '';
79    }
80
81    // return all region parts concatenated with
82    // single space chars
83    function getRegion(&$adr){
84	return (isset($adr['value'][4])) ? implode(' ', $adr['value'][4]) : '';
85    }
86
87    // return all ZIP (postcode)  parts concatenated with
88    // single space chars
89    function getZIP(&$adr){
90	return (isset($adr['value'][5])) ? implode(' ', $adr['value'][5]) : '';
91    }
92
93    // return all ZIP (postcode)  parts concatenated with
94    // single space chars
95    function getCountry(&$adr){
96	return (isset($adr['value'][6])) ? implode(' ', $adr['value'][6]) : '';
97    }
98
99    function getTitle(&$card){
100	return (isset($card['TITLE'][0]['value'][0][0])) ? $card['TITLE'][0]['value'][0][0] : '';
101    }
102
103    function getNote(&$card){
104	return (isset($card['NOTE'][0]['value'][0][0])) ? $card['NOTE'][0]['value'][0][0] : '';
105    }
106
107    // getOrg returns the first ORG entry
108    function getOrg(&$card){
109	return (isset($card['ORG'][0]['value'][0][0])) ? $card['ORG'][0]['value'][0][0] : '';
110    }
111
112    // getSubOrg returns the first ORG entry
113    // there are only two name fields for companies,
114    // so this is enough...
115    function getSubOrg(&$card){
116	return (isset($card['ORG'][0]['value'][0][1])) ? $card['ORG'][0]['value'][0][1] : '';
117    }
118
119    function getBirthday(&$card){
120	if(!isset($card['BDAY'][0]['value'][0][0])) return '';
121
122	if($card['BDAY'][0]['value'][0][0]{4} == '-')
123	    return substr($card['BDAY'][0]['value'][0][0],0,10);
124	else
125	    return substr($card['BDAY'][0]['value'][0][0],0,4).'-'.
126		substr($card['BDAY'][0]['value'][0][0],4,2).'-'.
127		substr($card['BDAY'][0]['value'][0][0],6,2);
128    }
129
130    function getURL(&$card){
131	reset($card);
132	if(!isset($card['URL'])) return array();
133
134	$rawurls = &$card['URL'];
135	reset($rawurls);
136	$urls = array();
137	while($rawurl = current($rawurls)){
138	    if(in_array('PREF', $rawurl['param']['TYPE']))
139		array_unshift($urls, $rawurl['value'][0][0]);
140	    else
141		$urls[] = $rawurl['value'][0][0];
142	    next($rawurls);
143	}
144	return $urls;
145    }
146
147    function getEMail(&$card){
148	reset($card);
149	if(!is_array($card['EMAIL'])) return array();
150
151	$rawemails = &$card['EMAIL'];
152	reset($rawemails);
153	$emails = array();
154	while($rawemail = current($rawemails)){
155	    if(in_array('PREF', $rawemail['param']['TYPE']))
156		array_unshift($emails, $rawemail['value'][0][0]);
157	    else
158		$emails[] = $rawemail['value'][0][0];
159	    next($rawemails);
160	}
161	return $emails;
162    }
163
164    function getAddress(&$card, $type='WORK'){
165	reset($card);
166	if(!isset($card['ADR'])) return array();
167
168	$rawadrs = &$card['ADR'];
169	reset($rawadrs);
170	$adrs = array();
171
172	while($rawadr = current($rawadrs)){
173	    // set TYPE defaults (intl,postal,parcel,work)
174	    if(count($rawadr['param']['TYPE']) == 0){
175		if(!in_array('INTL', $rawadr['param']['TYPE']))
176		    $rawadr['param']['TYPE'][] = 'INTL';
177		if(!in_array('POSTAL', $rawadr['param']['TYPE']))
178		    $rawadr['param']['TYPE'][] = 'POSTAL';
179		if(!in_array('PARCEL', $rawadr['param']['TYPE']))
180		    $rawadr['param']['TYPE'][] = 'PARCEL';
181		if(!in_array('WORK', $rawadr['param']['TYPE']))
182		    $rawadr['param']['TYPE'][] = 'WORK';
183	    }
184
185	    if($this->typeCheck($type, $rawadr['param']['TYPE'])){
186		if(in_array('PREF', $rawadr['param']['TYPE']))
187		    array_unshift($adrs, $rawadr);
188		else
189		    $adrs[] = $rawadr;
190	    }
191	    next($rawadrs);
192	}
193
194	return $adrs;
195    }
196
197    function typeCheck($type,$param){
198	$types = explode(',', $type);
199	if(count($types)==1)
200	    return in_array($types[0],$param);
201	else{
202	    $ret = true;
203	    for($c=0;$c<count($types);$c++){
204		$ret = $ret && in_array($types[$c],$param);
205	    }
206	    return $ret;
207	}
208    }
209
210    function getPhone(&$card, $type='VOICE'){
211	reset($card);
212	if(!isset($card['TEL'])) return array();
213
214	$rawnumbers = &$card['TEL'];
215	reset($rawnumbers);
216	$numbers = array();
217	while($rawnumber = current($rawnumbers)){
218	    // set TYPE defaults (voice)
219	    if(!in_array('VOICE', $rawnumber['param']['TYPE']))
220		$rawnumber['param']['TYPE'][] = 'VOICE';
221
222	    if($this->typeCheck($type, $rawnumber['param']['TYPE'])){
223		if(in_array('PREF', $rawnumber['param']['TYPE']))
224		    array_unshift($numbers, $rawnumber['value'][0][0]);
225		else
226		    $numbers[] = $rawnumber['value'][0][0];
227	    }
228	    next($rawnumbers);
229	}
230	return $numbers;
231    }
232
233}
234
235?>