Interface change to prefer link in Wiki documentation to static overlib tooltips
[platal.git] / modules / profile / general.inc.php
CommitLineData
fd38b30e
FB
1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 Polytechnique.org *
fd38b30e
FB
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
22class ProfileNom implements ProfileSetting
23{
24 private function matchWord($old, $new, $newLen) {
93553cea 25 return ($i = strpos($old, $new)) !== false
fd38b30e
FB
26 && ($i == 0 || $old{$i-1} == ' ')
27 && ($i + $newLen == strlen($old) || $old{$i + $newLen} == ' ');
28 }
29
30 private function prepareField($value)
31 {
32 $value = strtoupper(replace_accent($value));
33 return preg_replace('/[^A-Z]/', ' ', $value);
34 }
35
36 public function value(ProfilePage &$page, $field, $value, &$success)
37 {
38 $success = true;
39 $current = S::v($field);
40 $init = S::v($field . '_ini');
41 if (is_null($value)) {
42 return $current;
43 }
44 if ($value == $current || $value == $init) {
45 return $value;
46 }
47 $ini = $this->prepareField($init);
48 $old = $this->prepareField($current);
49 $new = $this->prepareField($value);
50 $newLen = strlen($new);
51 $success = $this->matchWord($old, $new, $newLen)
3f89a071
FB
52 || $this->matchWord($ini, $new, $newLen)
53 || ($field == 'nom' && $new == 'DE ' . $old);
fd38b30e
FB
54 if (!$success) {
55 global $page;
a7d35093 56 $page->trigError("Le $field que tu as choisi ($value) est trop loin de ton $field initial ($init)"
fd38b30e
FB
57 . (($init == $current)? "" : " et de ton prénom précédent ($current)"));
58 }
59 return $success ? $value : $current;
60 }
61
62 public function save(ProfilePage &$page, $field, $new_value)
63 {
64 $_SESSION[$field] = $new_value;
65 }
66}
67
b04882ff
PC
68class ProfileSearchName implements ProfileSetting
69{
70
71 public function __construct()
72 {
73 }
74
75 public function value(ProfilePage &$page, $field, $value, &$success)
76 {
77 }
78
79 public function save(ProfilePage &$page, $field, $new_value)
80 {
81 }
82}
83
576777d7
FB
84class ProfileAppli implements ProfileSetting
85{
86 public function value(ProfilePage &$page, $field, $value, &$success)
87 {
88 $success = true;
89 if (is_null($value)) {
90 return $page->values[$field];
91 }
92 return $value;
93 }
94
95 public function save(ProfilePage &$page, $field, $new_value)
96 {
97 $index = ($field == 'appli1' ? 0 : 1);
98 if ($new_value['id'] > 0) {
99 XDB::execute("REPLACE INTO applis_ins
100 SET uid = {?}, aid = {?}, type = {?}, ordre = {?}",
101 S::i('uid'), $new_value['id'], $new_value['type'], $index);
102 } else {
103 XDB::execute("DELETE FROM applis_ins
104 WHERE uid = {?} AND ordre = {?}",
105 S::i('uid'), $index);
106 }
107 }
108}
109
d1a2252a
GB
110class ProfileNetworking implements ProfileSetting
111{
112 private $email;
113 private $pub;
114 private $web;
115
116 public function __construct()
117 {
118 $this->email = new ProfileEmail();
119 $this->pub = new ProfilePub();
120 $this->web = new ProfileWeb();
121 }
122
123 public function value(ProfilePage &$page, $field, $value, &$success)
124 {
125 if (is_null($value)) {
126 $value = array();
127 $res = XDB::iterator("SELECT n.address, n.network_type AS type, n.pub, m.name
128 FROM profile_networking AS n
129 INNER JOIN profile_networking_enum AS m ON (n.network_type = m.network_type)
130 WHERE n.uid = {?}",
131 S::i('uid'));
132 while($network = $res->next()) {
133 $value[] = $network;
134 }
135 }
136 if (!is_array($value)) {
137 $value = array();
138 }
139 $res = XDB::iterator("SELECT filter, network_type AS type
140 FROM profile_networking_enum;");
141 $filters = array();
142 while($filter = $res->next()) {
143 $filters[$filter['type']] = $filter['filter'];
144 }
145 $success = true;
146 foreach($value as $i=>&$network) {
147 if(!isset($network['pub'])) {
148 $network['pub'] = 'private';
149 }
150 $network['error'] = false;
151 $network['pub'] = $this->pub->value($page, 'pub', $network['pub'], $s);
152 $s = true;
153 if ($filters[$network['type']] == 'web') {
154 $network['address'] = $this->web->value($page, 'address', $network['address'], $s);
155 } elseif ($filters[$network['type']] == 'email') {
156 $network['address'] = $this->email->value($page, 'address', $network['address'], $s);
157 }
158 if (!$s) {
159 $success = false;
160 $network['error'] = true;
161 }
162 }
163 return $value;
164 }
165
166 public function save(ProfilePage &$page, $field, $value)
167 {
168 XDB::execute("DELETE FROM profile_networking
169 WHERE uid = {?}",
170 S::i('uid'));
171 if (!count($value)) {
172 return;
173 }
174 $insert = array();
175 foreach ($value as $id=>$network) {
176 XDB::execute("INSERT INTO profile_networking (uid, nwid, network_type, address, pub)
177 VALUES ({?}, {?}, {?}, {?}, {?})",
178 S::i('uid'), $id, $network['type'], $network['address'], $network['pub']);
179 }
180 }
181}
182
fd38b30e
FB
183class ProfileGeneral extends ProfilePage
184{
185 protected $pg_template = 'profile/general.tpl';
186
187 public function __construct(PlWizard &$wiz)
188 {
189 parent::__construct($wiz);
190 $this->settings['nom'] = $this->settings['prenom']
191 = new ProfileNom();
7bff4cb0 192 $this->settings['naissance'] = new ProfileDate();
93553cea 193 $this->settings['mobile_pub']
93553cea 194 = $this->settings['freetext_pub']
576777d7 195 = $this->settings['photo_pub']
93553cea
FB
196 = new ProfilePub();
197 $this->settings['freetext']
576777d7 198 = $this->settings['nationalite']
93553cea 199 = $this->settings['nick']
b04882ff
PC
200 = $this->settings['yourself']
201 = $this->settings['display_name']
202 = $this->settings['sort_name']
203 = $this->settings['tooltip_name']
93553cea 204 = null;
576777d7
FB
205 $this->settings['synchro_ax']
206 = new ProfileBool();
93553cea 207 $this->settings['mobile'] = new ProfileTel();
d1a2252a 208 $this->settings['networking'] = new ProfileNetworking();
576777d7
FB
209 $this->settings['appli1']
210 = $this->settings['appli2']
211 = new ProfileAppli();
1052148d 212 $this->watched= array('nom' => true, 'freetext' => true, 'mobile' => true, 'networking' => true,
a2a1c2f2 213 'appli1' => true, 'appli2' => true, 'nationalite' => true, 'nick' => true);
93553cea
FB
214 }
215
7c2e0f0d 216 protected function _fetchData()
93553cea 217 {
576777d7 218 // Checkout all data...
7bff4cb0 219 $res = XDB::query("SELECT u.promo, u.promo_sortie, u.nom_usage, u.nationalite, u.naissance,
93553cea 220 q.profile_mobile as mobile, q.profile_mobile_pub as mobile_pub,
93553cea 221 q.profile_freetext as freetext, q.profile_freetext_pub as freetext_pub,
576777d7 222 q.profile_nick as nick, q.profile_from_ax as synchro_ax, u.matricule_ax,
93553cea 223 IF(a1.aid IS NULL, -1, a1.aid) as appli_id1, a1.type as appli_type1,
b04882ff
PC
224 IF(a2.aid IS NULL, -1, a2.aid) as appli_id2, a2.type as appli_type2,
225 n.yourself, n.display AS display_name, n.sort AS sort_name,
226 n.tooltip AS tooltip_name
93553cea 227 FROM auth_user_md5 AS u
b04882ff
PC
228 INNER JOIN auth_user_quick AS q ON(u.user_id = q.user_id)
229 INNER JOIN profile_names_display AS n ON(n.user_id = u.user_id)
93553cea
FB
230 LEFT JOIN applis_ins AS a1 ON(a1.uid = u.user_id and a1.ordre = 0)
231 LEFT JOIN applis_ins AS a2 ON(a2.uid = u.user_id and a2.ordre = 1)
232 WHERE u.user_id = {?}", S::v('uid', -1));
233 $this->values = $res->fetchOneAssoc();
576777d7
FB
234
235 // Reformat formation data
236 $this->values['appli1'] = array('id' => $this->values['appli_id1'],
237 'type' => $this->values['appli_type1']);
238 unset($this->values['appli_id1']);
239 unset($this->values['appli_type1']);
240 $this->values['appli2'] = array('id' => $this->values['appli_id2'],
241 'type' => $this->values['appli_type2']);
242 unset($this->values['appli_id2']);
243 unset($this->values['appli_type2']);
244
245 // Retreive photo informations
246 $res = XDB::query("SELECT pub
247 FROM photo
248 WHERE uid = {?}", S::v('uid'));
249 $this->values['photo_pub'] = $res->fetchOneCell();
250
251 $res = XDB::query("SELECT COUNT(*)
252 FROM requests
253 WHERE type='photo' AND user_id = {?}",
254 S::v('uid'));
255 $this->values['nouvellephoto'] = $res->fetchOneCell();
b04882ff
PC
256
257 // Retreive search names info
258 $this->values['search_names'] = XDB::iterator("
259 SELECT sn.search_name, sn.name_type, sn.pub, sn.sn_id
260 FROM profile_names_search AS sn
261 WHERE sn.user_id = {?}
262 ORDER BY sn.name_type, search_score, search_name",
263 S::v('uid'));
93553cea
FB
264 }
265
7c2e0f0d 266 protected function _saveData()
93553cea 267 {
7bff4cb0
FB
268 if ($this->changed['nationalite'] || $this->changed['nom'] || $this->changed['prenom']
269 || $this->changed['naissance']) {
576777d7 270 XDB::execute("UPDATE auth_user_md5
7bff4cb0 271 SET nationalite = {?}, nom={?}, prenom={?}, naissance={?}
a7c28fff 272 WHERE user_id = {?}",
7bff4cb0
FB
273 $this->values['nationalite'], $this->values['nom'], $this->values['prenom'],
274 preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $this->values['naissance']),
275 S::v('uid'));
576777d7
FB
276 }
277 if ($this->changed['nick'] || $this->changed['mobile'] || $this->changed['mobile_pub']
1052148d 278 || $this->changed['freetext']
576777d7
FB
279 || $this->changed['freetext_pub'] || $this->changed['synchro_ax']) {
280 XDB::execute("UPDATE auth_user_quick
281 SET profile_nick= {?}, profile_mobile={?}, profile_mobile_pub={?},
1052148d 282 profile_freetext={?},
576777d7
FB
283 profile_freetext_pub={?}, profile_from_ax = {?}
284 WHERE user_id = {?}",
285 $this->values['nick'], $this->values['mobile'], $this->values['mobile_pub'],
576777d7
FB
286 $this->values['freetext'], $this->values['freetext_pub'],
287 $this->values['synchro_ax'], S::v('uid'));
288 }
289 if ($this->changed['nick']) {
290 require_once('user.func.inc.php');
291 user_reindex(S::v('uid'));
292 }
a7c28fff
FB
293 if ($this->changed['photo_pub']) {
294 XDB::execute("UPDATE photo
295 SET pub = {?}
296 WHERE uid = {?}",
297 $this->values['photo_pub'], S::v('uid'));
298 }
b04882ff
PC
299 if ($this->changed['yourself'] || $this->changed['sort_name'] ||
300 $this-> changed['display_name'] || $this->changed['tooltip_name']) {
301 XDB::execute("UPDATE profile_names_display AS n
302 SET n.yourself = {?},
303 n.sort = {?}, ". // SET
304 "n.display = {?}, ". // SET
305 "n.tooltip = {?} ". // SET
306 "WHERE n.user_id = {?}",
307 $this->values['yourself'],
308 $this->values['sort_name'],
309 $this->values['display_name'],
310 $this->values['tooltip_name'],
311 S::v('uid'));
312 }
fd38b30e
FB
313 }
314
7c2e0f0d 315 public function _prepare(PlatalPage &$page, $id)
fd38b30e 316 {
fd38b30e 317 require_once "applis.func.inc.php";
d1a2252a
GB
318
319 $res = XDB::iterator("SELECT nw.network_type AS type, nw.name
320 FROM profile_networking_enum AS nw
321 ORDER BY name");
322 $page->assign('network_list', $res->fetchAllAssoc());
fd38b30e
FB
323 }
324}
325
326// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
327?>