3 * StatusNet, the distributed open-source microblogging tool
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @author Zach Copley <zach@status.net>
26 * @copyright 2010 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
37 const NS = 'http://portablecontacts.net/spec/1.0';
39 const USERNAME = 'preferredUsername';
40 const DISPLAYNAME = 'displayName';
43 public $preferredUsername;
47 public $urls = array();
49 function __construct($element = null)
51 if (empty($element)) {
55 $this->preferredUsername = ActivityUtils::childContent(
61 $this->displayName = ActivityUtils::childContent(
67 $this->note = ActivityUtils::childContent(
73 $this->address = $this->_getAddress($element);
74 $this->urls = $this->_getURLs($element);
77 private function _getURLs($element)
79 $urlEls = $element->getElementsByTagnameNS(self::NS, PoCoURL::URLS);
82 foreach ($urlEls as $urlEl) {
84 $type = ActivityUtils::childContent(
90 $value = ActivityUtils::childContent(
96 $primary = ActivityUtils::childContent(
104 if (isset($primary) && $primary == 'true') {
108 // @todo check to make sure a primary hasn't already been added
110 array_push($urls, new PoCoURL($type, $value, $isPrimary));
115 private function _getAddress($element)
117 $addressEl = ActivityUtils::child(
119 PoCoAddress::ADDRESS,
123 if (!empty($addressEl)) {
124 $formatted = ActivityUtils::childContent(
126 PoCoAddress::FORMATTED,
130 if (!empty($formatted)) {
131 $address = new PoCoAddress();
132 $address->formatted = $formatted;
140 static function fromProfile(Profile $profile)
144 $poco->preferredUsername = $profile->nickname;
145 $poco->displayName = $profile->getBestName();
147 $poco->note = $profile->bio;
149 $paddy = new PoCoAddress();
150 $paddy->formatted = $profile->location;
151 $poco->address = $paddy;
153 if (!empty($profile->homepage)) {
167 static function fromGroup(User_group $group)
171 $poco->preferredUsername = $group->nickname;
172 $poco->displayName = $group->getBestName();
174 $poco->note = $group->description;
176 $paddy = new PoCoAddress();
177 $paddy->formatted = $group->location;
178 $poco->address = $paddy;
180 if (!empty($group->homepage)) {
194 function getPrimaryURL()
196 foreach ($this->urls as $url) {
205 $xs = new XMLStringer(true);
206 $this->outputTo($xs);
207 return $xs->getString();
210 function outputTo($xo)
213 'poco:preferredUsername',
215 $this->preferredUsername
224 if (!empty($this->note)) {
225 $xo->element('poco:note', null, common_xml_safe_str($this->note));
228 if (!empty($this->address)) {
229 $this->address->outputTo($xo);
232 foreach ($this->urls as $url) {
238 * Output a Portable Contact as an array suitable for serializing
241 * @return $array the PoCo array
248 $poco['preferredUsername'] = $this->preferredUsername;
249 $poco['displayName'] = $this->displayName;
251 if (!empty($this->note)) {
252 $poco['note'] = $this->note;
255 if (!empty($this->address)) {
256 $poco['addresses'] = $this->address->asArray();
259 if (!empty($this->urls)) {
263 foreach ($this->urls as $url) {
264 $urls[] = $url->asArray();
267 $poco['urls'] = $urls;