2 /***************************************************************************
3 * Copyright (C) 2003-2013 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 class VCard
extends PlVCard
24 private $profile_list = array();
26 private $freetext = null
;
27 private $photos = true
;
30 public function __construct($photos = true
, $freetext = null
)
32 PlVCard
::$folding = false
;
33 $this->visibility
= Visibility
::defaultForRead(Visibility
::VIEW_PRIVATE
);
34 $this->freetext
= $freetext;
35 $this->photos
= $photos;
38 public function addProfile($profile)
40 $profile = Profile
::get($profile, Profile
::FETCH_ALL
, $this->visibility
);
42 $this->profile_list
[] = $profile;
47 public function addProfiles(array $profiles) {
48 foreach ($profiles as $profile) {
49 $this->addProfile($profile);
53 protected function fetch()
55 return PlIteratorUtils
::fromArray($this->profile_list
);
58 protected function buildEntry($pf)
63 $entry = new PlVCardEntry($pf->firstNames(), $pf->lastNames(), null
, null
, $pf->nickname
);
68 $freetext = '(' . $pf->promo
. ')';
69 if ($this->freetext
) {
70 $freetext .= "\n" . $this->freetext
;
72 $entry->set('NOTE', $freetext);
74 $entry->addTel(null
, $pf->mobile
, false
, true
, true
, false
, true
, true
);
78 if (!is_null($user)) {
79 $entry->addMail(null
, $user->bestalias
, true
);
80 if ($user->forlife
!= $user->bestalias
) {
81 $entry->addMail(null
, $user->forlife
);
83 if ($user->forlife_alternate
!= $user->bestalias
) {
84 $entry->addMail(null
, $user->forlife_alternate
);
89 $adrs = $pf->iterAddresses(Profile
::ADDRESS_PERSO
);
90 while ($adr = $adrs->next()) {
91 if (!$adr->postalCode ||
!$adr->locality ||
!$adr->country
) {
92 $group = $entry->addHome($adr->text
, null
, null
, null
,
93 null
, $adr->administrativeArea
, null
,
94 $adr->hasFlag('current'), $adr->hasFlag('mail'), $adr->hasFlag('mail'));
96 $group = $entry->addHome(trim(Geocoder
::getFirstLines($adr->text
, $adr->postalCode
, 4)), null
, null
, $adr->postalCode
,
97 $adr->locality
, $adr->administrativeArea
, $adr->country
,
98 $adr->hasFlag('current'), $adr->hasFlag('mail'), $adr->hasFlag('mail'));
100 foreach ($adr->phones() as $phone) {
101 if ($phone->link_type
== Phone
::TYPE_FIXED
) {
102 $entry->addTel($group, $phone->display
, false
, true
, true
, false
, false
,
103 $adr->hasFlag('current') && empty($pf->mobile
));
104 } else if ($phone->link_type
== Phone
::TYPE_FAX
) {
105 $entry->addTel($group, $phone->display
, true
, false
, false
, false
, false
, false
);
111 $jobs = $pf->getJobs();
112 foreach ($jobs as $job) {
113 $terms_array = array();
114 foreach ($job->terms
as $term) {
115 $terms_array[] = $term->full_name
;
117 $terms = implode(', ', $terms_array);
119 if (!$job->address
->postalCode ||
!$job->address
->locality ||
!$job->address
->country
) {
120 $group = $entry->addWork($job->company
->name
, null
, $job->description
, $terms,
121 $job->address
->text
, null
, null
, null
,
122 null
, $job->address
->administrativeArea
, null
);
124 $group = $entry->addWork($job->company
->name
, null
, $job->description
, $terms,
125 trim(Geocoder
::getFirstLines($job->address
->text
, $job->address
->postalCode
, 4)),
126 null
, null
, $job->address
->postalCode
,
127 $job->address
->locality
, $job->address
->administrativeArea
, $job->address
->country
);
130 $group = $entry->addWork($job->company
->name
, null
, $job->description
, $terms,
131 null
, null
, null
, null
,
134 if ($job->user_email
) {
135 $entry->addMail($group, $job->user_email
);
137 foreach ($job->phones
as $phone) {
138 if ($phone->type
== Phone
::TYPE_MOBILE
) {
139 $entry->addTel($group, $phone->display
, false
, true
, true
, false
, true
);
140 } else if ($phone->type
== Phone
::TYPE_FAX
) {
141 $entry->addTel($group, $phone->display
, true
);
143 $entry->addTel($group, $phone->display
, false
, true
, true
);
149 if (!is_null($user)) {
150 $alias = $user->emailAlias();
151 if (!is_null($alias) && $pf->alias_pub
== 'public') {
152 $entry->addMail(null
, $alias);
157 if (!is_null($user)) {
158 $groups = $user->groups(true
, true
);
159 if (count($groups)) {
160 $gn = DirEnum
::getOptions(DirEnum
::GROUPESX
);
162 foreach (array_keys($groups) as $gid) {
163 $gns[$gid] = $gn[$gid];
165 $entry->set('X-GROUPS', join(', ', $gns));
169 $binets = $pf->getBinets();
171 if (count($binets)) {
172 $bn = DirEnum
::getOptions(DirEnum
::BINETS
);
174 foreach ($binets as $bid) {
175 $bns[$bid] = $bn[$bid];
177 $entry->set('X-BINETS', join(', ', $bns));
179 if (!empty($pf->section
)) {
180 $entry->set('X-SECTION', $pf->section
);
186 "SELECT attach, attachmime
188 WHERE pid = {?} AND pub IN ('public', {?})",
189 $pf->id(), $this->visibility
->level());
190 if ($res->numRows()) {
191 list($data, $type) = $res->fetchOneRow();
192 $entry->setPhoto($data, strtoupper($type));
199 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: