Merge branch 'xorg/1.0.2/master' into xorg/master
[platal.git] / include / vcard.inc.php
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
22 class 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);
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 $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 }
83 }
84
85 // Homes
86 $adrs = $pf->iterAddresses(Profile::ADDRESS_PERSO);
87 while ($adr = $adrs->next()) {
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 {
93 $group = $entry->addHome(trim(Geocoder::getFirstLines($adr->text, $adr->postalCode, 4)), null, null, $adr->postalCode,
94 $adr->locality, $adr->administrativeArea, $adr->country,
95 $adr->hasFlag('current'), $adr->hasFlag('mail'), $adr->hasFlag('mail'));
96 }
97 foreach ($adr->phones() as $phone) {
98 if ($phone->link_type == Phone::TYPE_FIXED) {
99 $entry->addTel($group, $phone->display, false, true, true, false, false,
100 $adr->hasFlag('current') && empty($pf->mobile));
101 } else if ($phone->link_type == Phone::TYPE_FAX) {
102 $entry->addTel($group, $phone->display, true, false, false, false, false, false);
103 }
104 }
105 }
106
107 // Pro
108 $adrs = $pf->iterAddresses(Profile::ADDRESS_PRO);
109 while ($adr = $adrs->next()) {
110 if (!$adr->postalCode || !$adr->locality || !$adr->country) {
111 $group = $entry->addWork(null, null, null, null,
112 $adr->text, null, null, null,
113 null, $adr->administrativeArea, null);
114 } else {
115 // TODO : link address to company
116 $group = $entry->addWork(null, null, null, null,
117 trim(Geocoder::getFirstLines($adr->text, $adr->postalCode, 4)), null, null, $adr->postalCode,
118 $adr->locality, $adr->administrativeArea, $adr->country);
119 }
120 foreach ($adr->phones() as $phone) {
121 if ($phone->link_type == Phone::TYPE_FIXED) {
122 $entry->addTel($group, $phone->display);
123 } else if ($phone->link_type == Phone::TYPE_FAX) {
124 $entry->addTel($group, $phone->display, true);
125 }
126 }
127 }
128
129 // Melix
130 if (!is_null($user)) {
131 $alias = $user->emailAlias();
132 if (!is_null($alias) && $pf->alias_pub == 'public') {
133 $entry->addMail(null, $alias);
134 }
135 }
136
137 // Custom fields
138 if (!is_null($user)) {
139 $groups = $user->groups(true, true);
140 if (count($groups)) {
141 $gn = DirEnum::getOptions(DirEnum::GROUPESX);
142 $gns = array();
143 foreach (array_keys($groups) as $gid) {
144 $gns[$gid] = $gn[$gid];
145 }
146 $entry->set('X-GROUPS', join(', ', $gns));
147 }
148 }
149
150 $binets = $pf->getBinets();
151
152 if (count($binets)) {
153 $bn = DirEnum::getOptions(DirEnum::BINETS);
154 $bns = array();
155 foreach ($binets as $bid) {
156 $bns[$bid] = $bn[$bid];
157 }
158 $entry->set('X-BINETS', join(', ', $bns));
159 }
160 if (!empty($pf->section)) {
161 $entry->set('X-SECTION', $pf->section);
162 }
163
164 // Photo
165 if ($this->photos) {
166 $res = XDB::query(
167 "SELECT attach, attachmime
168 FROM profile_photos AS p
169 WHERE p.pid = {?}", $pf->id());
170 if ($res->numRows()) {
171 list($data, $type) = $res->fetchOneRow();
172 $entry->setPhoto($data, strtoupper($type));
173 }
174 }
175 return $entry;
176 }
177 }
178
179 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
180 ?>