Redefine DirEnum API, factor some code
[platal.git] / include / vcard.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 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 $profile_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 addProfile($profile)
39 {
40 $profile = Profile::get($profile);
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
62 $entry = new PlVCardEntry($pf->firstNames(), $pf->lastNames, null, null, $pf->nickname);
63
64 $user = $pf->owner();
65
66 // Free text
67 $freetext = '(' . $pf->promo . ')';
68 if ($this->freetext) {
69 $freetext .= "\n" . $this->freetext;
70 }
71 $entry->set('NOTE', $freetext);
72 // Mobile
73 if (!empty($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 $entry->addMail(null, $user->bestalias_alternate);
81 if ($user->forlife != $user->bestalias) {
82 $entry->addMail(null, $user->forlife);
83 $entry->addMail(null, $user->forlife_alternate);
84 }
85 }
86
87 // Homes
88 $adrs = $pf->getAddresses(Profile::ADDRESS_PERSO);
89 while ($adr = $adrs->next()) {
90 // TODO : find a way to fetch only the "street" part of the address
91 $group = $entry->addHome($adr['text'], null, null, $adr['postalCode'],
92 $adr['locality'], $adr['administrativeArea'], $adr['country'],
93 $adr['current'], $adr['mail'], $adr['mail']);
94 if (!empty($adr['fixed_tel'])) {
95 $entry->addTel($group, $adr['fixed_tel'], false, true, true, false, false, $adr['current'] && empty($pf->mobile));
96 }
97 if (!empty($adr['fax_tel'])) {
98 $entry->addTel($group, $adr['fax_tel'], true, false, false, false, false, false);
99 }
100 }
101
102 // Pro
103 $adrs = $pf->getAddresses(Profile::ADDRESS_PRO);
104 while ($adr = $adrs->next()) {
105 // TODO : link address to company
106 $group = $entry->addWork(null, null, null, null,
107 $adr['text'], null, null, $adr['postalCode'],
108 $adr['locality'], $adr['administrativeArea'], $adr['country']);
109 if (!empty($adr['fixed_tel'])) {
110 $entry->addTel($group, $adr['fixed_tel']);
111 }
112 if (!empty($adr['fax_tel'])) {
113 $entry->addTel($group, $adr['display_tel'], true);
114 }
115 /* TODO : fetch email for jobs, too
116 if (!empty($pro['email'])) {
117 $entry->addMail($group, $pro['email']);
118 }
119 */
120 }
121
122 // Melix
123 if (!is_null($user)) {
124 $alias = $user->emailAlias();
125 if (!is_null($alias)) {
126 $entry->addMail(null, $alias);
127 }
128 }
129
130 // Custom fields
131 if (!is_null($user)) {
132 $groups = $user->groups();
133 if (count($groups)) {
134 require_once "directory.enums.inc.php";
135 $gn = DirEnum::getOptions(DirEnum::GROUPESX);
136 $gns = array();
137 foreach (array_keys($groups) as $gid) {
138 $gns[$gid] = $gn[$gid];
139 }
140 $entry->set('X-GROUPS', join(', ', $gns));
141 }
142 }
143
144 $binets = $pf->getBinets();
145
146 if (count($binets)) {
147 require_once "directory.enums.inc.php";
148 $bn = DirEnum::getOptions(DirEnum::BINETS);
149 $bns = array();
150 foreach ($binets as $bid) {
151 $bns[$bid] = $bn[$bid];
152 }
153 $entry->set('X-BINETS', join(', ', $bid));
154 }
155 if (!empty($pf->section)) {
156 require_once "directory.enums.inc.php";
157 $sections = DirEnum::getOptions(DirEnum::SECTIONS);
158 $entry->set('X-SECTION', $sections[$pf->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 = {?}", $pf->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 ?>