Moving to GitHub.
[platal.git] / include / vcard.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2014 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
22 class VCard extends PlVCard
23 {
24 private $profile_list = array();
25 private $count = 0;
26 private $freetext = null;
27 private $photos = true;
28 private $visibility;
29
30 public function __construct($photos = true, $freetext = null)
31 {
32 PlVCard::$folding = false;
33 $this->visibility = Visibility::defaultForRead(Visibility::VIEW_PRIVATE);
34 $this->freetext = $freetext;
35 $this->photos = $photos;
36 }
37
38 public function addProfile($profile)
39 {
40 $profile = Profile::get($profile, Profile::FETCH_ALL, $this->visibility);
41 if ($profile) {
42 $this->profile_list[] = $profile;
43 $this->count++;
44 }
45 }
46
47 public function addProfiles(array $profiles) {
48 foreach ($profiles as $profile) {
49 $this->addProfile($profile);
50 }
51 }
52
53 protected function fetch()
54 {
55 return PlIteratorUtils::fromArray($this->profile_list);
56 }
57
58 protected function buildEntry($pf)
59 {
60 global $globals;
61 $pf = $pf['value'];
62
63 $entry = new PlVCardEntry($pf->firstNames(), $pf->lastNames(), null, null, $pf->nickname);
64
65 $user = $pf->owner();
66
67 // Free text
68 $freetext = '(' . $pf->promo . ')';
69 if ($this->freetext) {
70 $freetext .= "\n" . $this->freetext;
71 }
72 $entry->set('NOTE', $freetext);
73 if ($pf->mobile) {
74 $entry->addTel(null, $pf->mobile, false, true, true, false, true, true);
75 }
76
77 // Emails
78 if (!is_null($user)) {
79 $entry->addMail(null, $user->bestalias, true);
80 if ($user->forlife != $user->bestalias) {
81 $entry->addMail(null, $user->forlife);
82 }
83 if ($user->forlife_alternate != $user->bestalias) {
84 $entry->addMail(null, $user->forlife_alternate);
85 }
86 }
87
88 // Homes
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'));
95 } else {
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'));
99 }
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);
106 }
107 }
108 }
109
110 // Pro
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;
116 }
117 $terms = implode(', ', $terms_array);
118 if ($job->address) {
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);
123 } else {
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);
128 }
129 } else {
130 $group = $entry->addWork($job->company->name, null, $job->description, $terms,
131 null, null, null, null,
132 null, null, null);
133 }
134 if ($job->user_email) {
135 $entry->addMail($group, $job->user_email);
136 }
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);
142 } else {
143 $entry->addTel($group, $phone->display, false, true, true);
144 }
145 }
146 }
147
148 // Melix
149 if (!is_null($user)) {
150 $alias = $user->emailAlias();
151 if (!is_null($alias) && $pf->alias_pub == 'public') {
152 $entry->addMail(null, $alias);
153 }
154 }
155
156 // Custom fields
157 if (!is_null($user)) {
158 $groups = $user->groups(true, true);
159 if (count($groups)) {
160 $gn = DirEnum::getOptions(DirEnum::GROUPESX);
161 $gns = array();
162 foreach (array_keys($groups) as $gid) {
163 $gns[$gid] = $gn[$gid];
164 }
165 $entry->set('X-GROUPS', join(', ', $gns));
166 }
167 }
168
169 $binets = $pf->getBinets();
170
171 if (count($binets)) {
172 $bn = DirEnum::getOptions(DirEnum::BINETS);
173 $bns = array();
174 foreach ($binets as $bid) {
175 $bns[$bid] = $bn[$bid];
176 }
177 $entry->set('X-BINETS', join(', ', $bns));
178 }
179 if (!empty($pf->section)) {
180 $entry->set('X-SECTION', $pf->section);
181 }
182
183 // Photo
184 if ($this->photos) {
185 $res = XDB::query(
186 "SELECT attach, attachmime
187 FROM profile_photos
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));
193 }
194 }
195 return $entry;
196 }
197 }
198
199 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
200 ?>