Merge branch 'xorg/maint' into xorg/master
[platal.git] / include / vcard.inc.php
... / ...
CommitLineData
1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2011 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
5 * *
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. *
10 * *
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. *
15 * *
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 *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22class VCard extends PlVCard
23{
24 private $profile_list = array();
25 private $count = 0;
26 private $freetext = null;
27 private $photos = true;
28
29 public function __construct($photos = true, $freetext = null)
30 {
31 PlVCard::$folding = false;
32 $this->freetext = $freetext;
33 $this->photos = $photos;
34 }
35
36 public function addProfile($profile)
37 {
38 $profile = Profile::get($profile, Profile::FETCH_ALL);
39 if ($profile) {
40 $this->profile_list[] = $profile;
41 $this->count++;
42 }
43 }
44
45 public function addProfiles(array $profiles) {
46 foreach ($profiles as $profile) {
47 $this->addProfile($profile);
48 }
49 }
50
51 protected function fetch()
52 {
53 return PlIteratorUtils::fromArray($this->profile_list);
54 }
55
56 protected function buildEntry($pf)
57 {
58 global $globals;
59 $pf = $pf['value'];
60
61 $entry = new PlVCardEntry($pf->firstNames(), $pf->lastNames(), null, null, $pf->nickname);
62
63 $user = $pf->owner();
64
65 // Free text
66 $freetext = '(' . $pf->promo . ')';
67 if ($this->freetext) {
68 $freetext .= "\n" . $this->freetext;
69 }
70 $entry->set('NOTE', $freetext);
71 if ($pf->mobile) {
72 $entry->addTel(null, $pf->mobile, false, true, true, false, true, true);
73 }
74
75 // Emails
76 if (!is_null($user)) {
77 $entry->addMail(null, $user->bestalias, true);
78 if ($user->forlife != $user->bestalias) {
79 $entry->addMail(null, $user->forlife);
80 }
81 if ($user->forlife_alternate != $user->bestalias) {
82 $entry->addMail(null, $user->forlife_alternate);
83 }
84 }
85
86 // Homes
87 $adrs = $pf->iterAddresses(Profile::ADDRESS_PERSO);
88 while ($adr = $adrs->next()) {
89 if (!$adr->postalCode || !$adr->locality || !$adr->country) {
90 $group = $entry->addHome($adr->text, null, null, null,
91 null, $adr->administrativeArea, null,
92 $adr->hasFlag('current'), $adr->hasFlag('mail'), $adr->hasFlag('mail'));
93 } else {
94 $group = $entry->addHome(trim(Geocoder::getFirstLines($adr->text, $adr->postalCode, 4)), null, null, $adr->postalCode,
95 $adr->locality, $adr->administrativeArea, $adr->country,
96 $adr->hasFlag('current'), $adr->hasFlag('mail'), $adr->hasFlag('mail'));
97 }
98 foreach ($adr->phones() as $phone) {
99 if ($phone->link_type == Phone::TYPE_FIXED) {
100 $entry->addTel($group, $phone->display, false, true, true, false, false,
101 $adr->hasFlag('current') && empty($pf->mobile));
102 } else if ($phone->link_type == Phone::TYPE_FAX) {
103 $entry->addTel($group, $phone->display, true, false, false, false, false, false);
104 }
105 }
106 }
107
108 // Pro
109 $jobs = $pf->getJobs();
110 foreach ($jobs as $job) {
111 $terms_array = array();
112 foreach ($job->terms as $term) {
113 $terms_array[] = $term->full_name;
114 }
115 $terms = implode(', ', $terms_array);
116 if ($job->address) {
117 if (!$job->address->postalCode || !$job->address->locality || !$job->address->country) {
118 $group = $entry->addWork($job->company->name, null, $job->description, $terms,
119 $job->address->text, null, null, null,
120 null, $job->address->administrativeArea, null);
121 } else {
122 $group = $entry->addWork($job->company->name, null, $job->description, $terms,
123 trim(Geocoder::getFirstLines($job->address->text, $job->address->postalCode, 4)),
124 null, null, $job->address->postalCode,
125 $job->address->locality, $job->address->administrativeArea, $job->address->country);
126 }
127 } else {
128 $group = $entry->addWork($job->company->name, null, $job->description, $terms,
129 null, null, null, null,
130 null, null, null);
131 }
132 if ($job->user_email) {
133 $entry->addMail($group, $job->user_email);
134 }
135 foreach ($job->phones as $phone) {
136 if ($phone->type == Phone::TYPE_MOBILE) {
137 $entry->addTel($group, $phone->display, false, true, true, false, true);
138 } else if ($phone->type == Phone::TYPE_FAX) {
139 $entry->addTel($group, $phone->display, true);
140 } else {
141 $entry->addTel($group, $phone->display, false, true, true);
142 }
143 }
144 }
145
146 // Melix
147 if (!is_null($user)) {
148 $alias = $user->emailAlias();
149 if (!is_null($alias) && $pf->alias_pub == 'public') {
150 $entry->addMail(null, $alias);
151 }
152 }
153
154 // Custom fields
155 if (!is_null($user)) {
156 $groups = $user->groups(true, true);
157 if (count($groups)) {
158 $gn = DirEnum::getOptions(DirEnum::GROUPESX);
159 $gns = array();
160 foreach (array_keys($groups) as $gid) {
161 $gns[$gid] = $gn[$gid];
162 }
163 $entry->set('X-GROUPS', join(', ', $gns));
164 }
165 }
166
167 $binets = $pf->getBinets();
168
169 if (count($binets)) {
170 $bn = DirEnum::getOptions(DirEnum::BINETS);
171 $bns = array();
172 foreach ($binets as $bid) {
173 $bns[$bid] = $bn[$bid];
174 }
175 $entry->set('X-BINETS', join(', ', $bns));
176 }
177 if (!empty($pf->section)) {
178 $entry->set('X-SECTION', $pf->section);
179 }
180
181 // Photo
182 if ($this->photos) {
183 $res = XDB::query(
184 "SELECT attach, attachmime
185 FROM profile_photos AS p
186 WHERE p.pid = {?}", $pf->id());
187 if ($res->numRows()) {
188 list($data, $type) = $res->fetchOneRow();
189 $entry->setPhoto($data, strtoupper($type));
190 }
191 }
192 return $entry;
193 }
194}
195
196// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
197?>