Fix bug in DirEnum for DE with "suboptions" (degree for a given school, ...)
[platal.git] / include / vcard.inc.php
CommitLineData
89460d9c 1<?php
2/***************************************************************************
9f5bd98e 3 * Copyright (C) 2003-2010 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
89460d9c 22require_once('user.func.inc.php');
23
5d42c993 24class VCard extends PlVCard
89460d9c 25{
6713fc49 26 private $profile_list = array();
29b12e6e 27 private $count = 0;
28 private $freetext = null;
29 private $photos = true;
89460d9c 30
5d42c993 31 public function __construct($photos = true, $freetext = null)
89460d9c 32 {
5d42c993 33 PlVCard::$folding = false;
29b12e6e 34 $this->freetext = $freetext;
35 $this->photos = $photos;
89460d9c 36 }
37
6713fc49 38 public function addProfile($profile)
89460d9c 39 {
6713fc49
RB
40 $profile = Profile::get($profile);
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;
89460d9c 61
6713fc49
RB
62 $entry = new PlVCardEntry($pf->firstNames(), $pf->lastNames, null, null, $pf->nickname);
63
64 $user = $pf->owner();
5d42c993
FB
65
66 // Free text
6713fc49 67 $freetext = '(' . $pf->promo . ')';
29b12e6e 68 if ($this->freetext) {
5d42c993
FB
69 $freetext .= "\n" . $this->freetext;
70 }
5d42c993 71 $entry->set('NOTE', $freetext);
5d42c993 72 // Mobile
6713fc49
RB
73 if (!empty($pf->mobile)) {
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);
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 }
5d42c993
FB
85 }
86
87 // Homes
6713fc49
RB
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));
5d42c993 96 }
6713fc49
RB
97 if (!empty($adr['fax_tel'])) {
98 $entry->addTel($group, $adr['fax_tel'], true, false, false, false, false, false);
5d42c993
FB
99 }
100 }
101
102 // Pro
6713fc49
RB
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
a86b9767
OLF
116 if (!empty($pro['email'])) {
117 $entry->addMail($group, $pro['email']);
118 }
6713fc49 119 */
89460d9c 120 }
121
5d42c993 122 // Melix
6713fc49
RB
123 if (!is_null($user)) {
124 $alias = $user->emailAlias();
125 if (!is_null($alias)) {
126 $entry->addMail(null, $alias);
127 }
5d42c993
FB
128 }
129
130 // Custom fields
6713fc49
RB
131 if (!is_null($user)) {
132 $groups = $user->groups();
133 if (count($groups)) {
134 require_once "directory.enums.inc.php";
135 $gn = DirEnum::getOptionsArray(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 }
5d42c993 142 }
6713fc49
RB
143
144 $binets = $pf->getBinets();
145
146 if (count($binets)) {
147 require_once "directory.enums.inc.php";
148 $bn = DirEnum::getOptionsArray(DirEnum::BINETS);
149 $bns = array();
150 foreach ($binets as $bid) {
151 $bns[$bid] = $bn[$bid];
152 }
153 $entry->set('X-BINETS', join(', ', $bid));
5d42c993 154 }
6713fc49
RB
155 if (!empty($pf->section)) {
156 require_once "directory.enums.inc.php";
157 $sections = DirEnum::getOptionsArray(DirEnum::SECTIONS);
158 $entry->set('X-SECTION', $sections[$pf->section]);
5d42c993 159 }
eaf30d86 160
5d42c993 161 // Photo
917c4d11 162 if ($this->photos) {
163 $res = XDB::query(
a104d709
VZ
164 "SELECT attach, attachmime
165 FROM photo AS p
6713fc49 166 WHERE p.uid = {?}", $pf->id());
917c4d11 167 if ($res->numRows()) {
5d42c993
FB
168 list($data, $type) = $res->fetchOneRow();
169 $entry->setPhoto($data, strtoupper($type));
29b12e6e 170 }
29b12e6e 171 }
5d42c993 172 return $entry;
89460d9c 173 }
89460d9c 174}
175
a7de4ef7 176// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
89460d9c 177?>