Moving to GitHub.
[platal.git] / include / vcard.inc.php
CommitLineData
89460d9c 1<?php
2/***************************************************************************
c441aabe 3 * Copyright (C) 2003-2014 Polytechnique.org *
89460d9c 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
5d42c993 22class VCard extends PlVCard
89460d9c 23{
6713fc49 24 private $profile_list = array();
29b12e6e 25 private $count = 0;
26 private $freetext = null;
27 private $photos = true;
91107522 28 private $visibility;
89460d9c 29
5d42c993 30 public function __construct($photos = true, $freetext = null)
89460d9c 31 {
5d42c993 32 PlVCard::$folding = false;
22771578 33 $this->visibility = Visibility::defaultForRead(Visibility::VIEW_PRIVATE);
29b12e6e 34 $this->freetext = $freetext;
35 $this->photos = $photos;
89460d9c 36 }
37
6713fc49 38 public function addProfile($profile)
89460d9c 39 {
91107522 40 $profile = Profile::get($profile, Profile::FETCH_ALL, $this->visibility);
6713fc49
RB
41 if ($profile) {
42 $this->profile_list[] = $profile;
3ce06e37
FB
43 $this->count++;
44 }
89460d9c 45 }
46
6713fc49
RB
47 public function addProfiles(array $profiles) {
48 foreach ($profiles as $profile) {
49 $this->addProfile($profile);
5d42c993 50 }
89460d9c 51 }
52
5d42c993 53 protected function fetch()
89460d9c 54 {
6713fc49 55 return PlIteratorUtils::fromArray($this->profile_list);
29b12e6e 56 }
89460d9c 57
6713fc49 58 protected function buildEntry($pf)
29b12e6e 59 {
29b12e6e 60 global $globals;
3e6cd673 61 $pf = $pf['value'];
89460d9c 62
3e6cd673 63 $entry = new PlVCardEntry($pf->firstNames(), $pf->lastNames(), null, null, $pf->nickname);
6713fc49
RB
64
65 $user = $pf->owner();
5d42c993
FB
66
67 // Free text
6713fc49 68 $freetext = '(' . $pf->promo . ')';
29b12e6e 69 if ($this->freetext) {
5d42c993
FB
70 $freetext .= "\n" . $this->freetext;
71 }
5d42c993 72 $entry->set('NOTE', $freetext);
79dceea9 73 if ($pf->mobile) {
6713fc49 74 $entry->addTel(null, $pf->mobile, false, true, true, false, true, true);
5d42c993
FB
75 }
76
77 // Emails
6713fc49
RB
78 if (!is_null($user)) {
79 $entry->addMail(null, $user->bestalias, true);
6713fc49
RB
80 if ($user->forlife != $user->bestalias) {
81 $entry->addMail(null, $user->forlife);
9f3f87bc
SJ
82 }
83 if ($user->forlife_alternate != $user->bestalias) {
6713fc49
RB
84 $entry->addMail(null, $user->forlife_alternate);
85 }
5d42c993
FB
86 }
87
88 // Homes
598c57f6 89 $adrs = $pf->iterAddresses(Profile::ADDRESS_PERSO);
6713fc49 90 while ($adr = $adrs->next()) {
79dceea9
FB
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 {
e7fb8e06 96 $group = $entry->addHome(trim(Geocoder::getFirstLines($adr->text, $adr->postalCode, 4)), null, null, $adr->postalCode,
79dceea9
FB
97 $adr->locality, $adr->administrativeArea, $adr->country,
98 $adr->hasFlag('current'), $adr->hasFlag('mail'), $adr->hasFlag('mail'));
99 }
3e6cd673 100 foreach ($adr->phones() as $phone) {
0b53817f 101 if ($phone->link_type == Phone::TYPE_FIXED) {
3e6cd673
FB
102 $entry->addTel($group, $phone->display, false, true, true, false, false,
103 $adr->hasFlag('current') && empty($pf->mobile));
0b53817f 104 } else if ($phone->link_type == Phone::TYPE_FAX) {
3e6cd673
FB
105 $entry->addTel($group, $phone->display, true, false, false, false, false, false);
106 }
5d42c993
FB
107 }
108 }
109
110 // Pro
801adeec
SJ
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 }
79dceea9 129 } else {
801adeec
SJ
130 $group = $entry->addWork($job->company->name, null, $job->description, $terms,
131 null, null, null, null,
132 null, null, null);
79dceea9 133 }
801adeec
SJ
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) {
3e6cd673 141 $entry->addTel($group, $phone->display, true);
801adeec
SJ
142 } else {
143 $entry->addTel($group, $phone->display, false, true, true);
a86b9767 144 }
3e6cd673 145 }
89460d9c 146 }
147
5d42c993 148 // Melix
6713fc49 149 if (!is_null($user)) {
0592ff62 150 $alias = $user->emailAlias();
fab7a90a 151 if (!is_null($alias) && $pf->alias_pub == 'public') {
6713fc49
RB
152 $entry->addMail(null, $alias);
153 }
5d42c993
FB
154 }
155
156 // Custom fields
6713fc49 157 if (!is_null($user)) {
01c8c98b 158 $groups = $user->groups(true, true);
6713fc49 159 if (count($groups)) {
2998edf1 160 $gn = DirEnum::getOptions(DirEnum::GROUPESX);
6713fc49
RB
161 $gns = array();
162 foreach (array_keys($groups) as $gid) {
163 $gns[$gid] = $gn[$gid];
164 }
165 $entry->set('X-GROUPS', join(', ', $gns));
166 }
5d42c993 167 }
6713fc49
RB
168
169 $binets = $pf->getBinets();
170
171 if (count($binets)) {
2998edf1 172 $bn = DirEnum::getOptions(DirEnum::BINETS);
6713fc49
RB
173 $bns = array();
174 foreach ($binets as $bid) {
175 $bns[$bid] = $bn[$bid];
176 }
3e6cd673 177 $entry->set('X-BINETS', join(', ', $bns));
5d42c993 178 }
6713fc49 179 if (!empty($pf->section)) {
17717c87 180 $entry->set('X-SECTION', $pf->section);
5d42c993 181 }
eaf30d86 182
5d42c993 183 // Photo
917c4d11 184 if ($this->photos) {
185 $res = XDB::query(
a104d709 186 "SELECT attach, attachmime
91107522
SJ
187 FROM profile_photos
188 WHERE pid = {?} AND pub IN ('public', {?})",
189 $pf->id(), $this->visibility->level());
917c4d11 190 if ($res->numRows()) {
5d42c993
FB
191 list($data, $type) = $res->fetchOneRow();
192 $entry->setPhoto($data, strtoupper($type));
29b12e6e 193 }
29b12e6e 194 }
5d42c993 195 return $entry;
89460d9c 196 }
89460d9c 197}
198
448c8cdc 199// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
89460d9c 200?>