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