Merge commit 'origin/platal-0.10.0'
[platal.git] / include / vcard.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2009 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 require_once('user.func.inc.php');
23
24 class VCard extends PlVCard
25 {
26 private $user_list = array();
27 private $count = 0;
28 private $freetext = null;
29 private $photos = true;
30
31 public function __construct($photos = true, $freetext = null)
32 {
33 PlVCard::$folding = false;
34 $this->freetext = $freetext;
35 $this->photos = $photos;
36 }
37
38 public function addUser($user)
39 {
40 $user = User::getSilent($user);
41 if ($user) {
42 $this->user_list[] = $user;
43 $this->count++;
44 }
45 }
46
47 public function addUsers(array $users) {
48 foreach ($users as $user) {
49 $this->addUser($user);
50 }
51 }
52
53 protected function fetch()
54 {
55 return PlIteratorUtils::fromArray($this->user_list);
56 }
57
58 protected function buildEntry($entry)
59 {
60 global $globals;
61 $login = $entry['value'];
62 $user = get_user_details($login->login());
63
64 if (empty($user['nom_usage'])) {
65 $entry = new PlVCardEntry($user['prenom'], $user['nom'], null, null, @$user['nickname']);
66 } else {
67 $entry = new PlVCardEntry($user['prenom'], array($user['nom'], $user['nom_usage']), null, null, @$user['nickname']);
68 }
69
70 // Free text
71 $freetext = '(' . $user['promo'] . ')';
72 if ($this->freetext) {
73 $freetext .= "\n" . $this->freetext;
74 }
75 if (strlen(trim($user['freetext']))) {
76 $freetext .= "\n" . MiniWiki::WikiToText($user['freetext']);
77 }
78 $entry->set('NOTE', $freetext);
79
80 // Mobile
81 if (!empty($user['mobile'])) {
82 $entry->addTel(null, $user['mobile'], false, true, true, false, true, true);
83 }
84
85 // Emails
86 // TODO: this logic is not hruid-compatible; replace it.
87 $entry->addMail(null, $user['bestalias'] . '@' . $globals->mail->domain, true);
88 $entry->addMail(null, $user['bestalias'] . '@' . $globals->mail->domain2);
89 if ($user['bestalias'] != $user['forlife']) {
90 $entry->addMail(null, $user['forlife'] . '@' . $globals->mail->domain);
91 $entry->addMail(null, $user['forlife'] . '@' . $globals->mail->domain2);
92 }
93
94 // Homes
95 foreach ($user['adr'] as $adr) {
96 $street = array($adr['adr1']);
97 if (!empty($adr['adr2'])) {
98 $street[] = $adr['adr2'];
99 }
100 if (!empty($adr['adr3'])) {
101 $street[] = $adr['adr3'];
102 }
103 $group = $entry->addHome($street, null, null, $adr['postcode'], $adr['city'], $adr['region'], @$adr['country'],
104 $adr['active'], $adr['courier'], $adr['courier']);
105 if (!empty($adr['tels'])) {
106 foreach ($adr['tels'] as $tel) {
107 $fax = $tel['tel_type'] == 'Fax';
108 $entry->addTel($group, $tel['tel'], $fax, !$fax, !$fax, false, false, !$fax && $adr['active'] && empty($user['mobile']));
109 }
110 }
111 }
112
113 // Pro
114 foreach ($user['adr_pro'] as $pro) {
115 $street = array($adr['adr1']);
116 if (!empty($pro['adr2'])) {
117 $street[] = $pro['adr2'];
118 }
119 if (!empty($pro['adr3'])) {
120 $street[] = $pro['adr3'];
121 }
122 $group = $entry->addWork($pro['entreprise'], null, $pro['poste'], $pro['fonction'],
123 $street, null, null, $pro['postcode'], $pro['city'], $pro['region'], @$pro['country']);
124 if (!empty($pro['tel'])) {
125 $entry->addTel($group, $pro['tel']);
126 }
127 if (!empty($pro['fax'])) {
128 $entry->addTel($group, $pro['fax'], true);
129 }
130 if (!empty($pro['email'])) {
131 $entry->addMail($group, $pro['email']);
132 }
133 }
134
135 // Melix
136 $res = XDB::query(
137 "SELECT alias
138 FROM virtual
139 INNER JOIN virtual_redirect USING(vid)
140 INNER JOIN auth_user_quick ON ( user_id = {?} AND emails_alias_pub = 'public' )
141 WHERE ( redirect={?} OR redirect={?} )
142 AND alias LIKE '%@{$globals->mail->alias_dom}'",
143 $user['user_id'],
144 $user['forlife'].'@'.$globals->mail->domain,
145 $user['forlife'].'@'.$globals->mail->domain2);
146 if ($res->numRows()) {
147 $entry->addMail(null, $res->fetchOneCell());
148 }
149
150 // Custom fields
151 if (count($user['gpxs_name'])) {
152 $entry->set('X-GROUPS', join(', ', $user['gpxs_name']));
153 }
154 if (count($user['binets'])) {
155 $entry->set('X-BINETS', join(', ', $user['binets']));
156 }
157 if (!empty($user['section'])) {
158 $entry->set('X-SECTION', $user['section']);
159 }
160
161 // Photo
162 if ($this->photos) {
163 $res = XDB::query(
164 "SELECT attach, attachmime
165 FROM photo AS p
166 WHERE p.uid = {?}", $login->id());
167 if ($res->numRows()) {
168 list($data, $type) = $res->fetchOneRow();
169 $entry->setPhoto($data, strtoupper($type));
170 }
171 }
172 return $entry;
173 }
174 }
175
176 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
177 ?>