Merge commit 'origin/master' into fusionax
[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 54 if (!$success) {
d7610c35
FB
55 Platal::page()->trigError("Le $field que tu as choisi ($value) est trop loin de ton $field initial ($init)"
56 . (($init == $current)? "" : " et de ton prénom précédent ($current)"));
fd38b30e
FB
57 }
58 return $success ? $value : $current;
59 }
60
61 public function save(ProfilePage &$page, $field, $new_value)
62 {
63 $_SESSION[$field] = $new_value;
64 }
65}
66
b04882ff
PC
67class ProfileSearchName implements ProfileSetting
68{
69
70 public function __construct()
71 {
72 }
73
74 public function value(ProfilePage &$page, $field, $value, &$success)
75 {
76 }
77
78 public function save(ProfilePage &$page, $field, $new_value)
79 {
80 }
81}
82
576777d7
FB
83class ProfileAppli implements ProfileSetting
84{
85 public function value(ProfilePage &$page, $field, $value, &$success)
86 {
87 $success = true;
88 if (is_null($value)) {
89 return $page->values[$field];
90 }
91 return $value;
92 }
93
94 public function save(ProfilePage &$page, $field, $new_value)
95 {
4887be78 96 $index = ($field == 'edu_0' ? 0 : 1);
576777d7
FB
97 if ($new_value['id'] > 0) {
98 XDB::execute("REPLACE INTO applis_ins
99 SET uid = {?}, aid = {?}, type = {?}, ordre = {?}",
100 S::i('uid'), $new_value['id'], $new_value['type'], $index);
101 } else {
102 XDB::execute("DELETE FROM applis_ins
103 WHERE uid = {?} AND ordre = {?}",
104 S::i('uid'), $index);
105 }
106 }
107}
108
b715c1e1
SJ
109class ProfileEmailDirectory implements ProfileSetting
110{
111 private $email;
112
113 public function __construct()
114 {
115 }
116
117 public function value(ProfilePage &$page, $field, $value, &$success)
118 {
ad3fee9d 119 $p = Platal::page();
b715c1e1
SJ
120
121 $success = true;
122 if (!is_null($value)) {
123 $email_stripped = strtolower(trim($value));
ad3fee9d
SJ
124 if ((!isvalid_email($email_stripped)) && ($email_stripped) && ($page->values['email_directory'] == "new@example.org")) {
125 $p->assign('email_error', '1');
126 $p->assign('email_directory_error', $email_stripped);
127 $p->trigError('Adresse Email invalide');
b715c1e1
SJ
128 $success = false;
129 } else {
ad3fee9d 130 $p->assign('email_error', '0');
b715c1e1
SJ
131 }
132 }
133 return $value;
134 }
135
136 public function save(ProfilePage &$page, $field, $value)
137 {
138 }
139}
140
d1a2252a
GB
141class ProfileNetworking implements ProfileSetting
142{
143 private $email;
144 private $pub;
145 private $web;
92446a53 146 private $number;
d1a2252a
GB
147
148 public function __construct()
149 {
150 $this->email = new ProfileEmail();
151 $this->pub = new ProfilePub();
152 $this->web = new ProfileWeb();
92446a53 153 $this->number = new ProfileNumber();
d1a2252a
GB
154 }
155
156 public function value(ProfilePage &$page, $field, $value, &$success)
157 {
158 if (is_null($value)) {
159 $value = array();
160 $res = XDB::iterator("SELECT n.address, n.network_type AS type, n.pub, m.name
161 FROM profile_networking AS n
162 INNER JOIN profile_networking_enum AS m ON (n.network_type = m.network_type)
163 WHERE n.uid = {?}",
164 S::i('uid'));
165 while($network = $res->next()) {
166 $value[] = $network;
167 }
168 }
169 if (!is_array($value)) {
170 $value = array();
171 }
b715c1e1
SJ
172 $res = XDB::iterator("SELECT filter, network_type AS type
173 FROM profile_networking_enum;");
d1a2252a
GB
174 $filters = array();
175 while($filter = $res->next()) {
176 $filters[$filter['type']] = $filter['filter'];
177 }
178 $success = true;
179 foreach($value as $i=>&$network) {
92c3f9e5
GB
180 if (!trim($network['address'])) {
181 unset($value[$i]);
182 } else {
183 if (!isset($network['pub'])) {
184 $network['pub'] = 'private';
185 }
186 $network['error'] = false;
187 $network['pub'] = $this->pub->value($page, 'pub', $network['pub'], $s);
188 $s = true;
189 if ($filters[$network['type']] == 'web') {
190 $network['address'] = $this->web->value($page, 'address', $network['address'], $s);
191 } elseif ($filters[$network['type']] == 'email') {
192 $network['address'] = $this->email->value($page, 'address', $network['address'], $s);
193 } elseif ($filters[$network['type']] == 'number') {
194 $network['address'] = $this->number->value($page, 'address', $network['address'], $s);
195 }
196 if (!$s) {
197 $success = false;
198 $network['error'] = true;
199 }
d1a2252a
GB
200 }
201 }
202 return $value;
203 }
204
205 public function save(ProfilePage &$page, $field, $value)
206 {
207 XDB::execute("DELETE FROM profile_networking
208 WHERE uid = {?}",
209 S::i('uid'));
210 if (!count($value)) {
211 return;
212 }
213 $insert = array();
214 foreach ($value as $id=>$network) {
215 XDB::execute("INSERT INTO profile_networking (uid, nwid, network_type, address, pub)
216 VALUES ({?}, {?}, {?}, {?}, {?})",
217 S::i('uid'), $id, $network['type'], $network['address'], $network['pub']);
218 }
219 }
220}
221
fd38b30e
FB
222class ProfileGeneral extends ProfilePage
223{
224 protected $pg_template = 'profile/general.tpl';
225
226 public function __construct(PlWizard &$wiz)
227 {
228 parent::__construct($wiz);
b715c1e1
SJ
229 $this->settings['nom'] = $this->settings['prenom']
230 = new ProfileNom();
7bff4cb0 231 $this->settings['naissance'] = new ProfileDate();
bde2be3b 232 $this->settings['freetext_pub']
576777d7 233 = $this->settings['photo_pub']
93553cea
FB
234 = new ProfilePub();
235 $this->settings['freetext']
576777d7 236 = $this->settings['nationalite']
8450c2aa
SJ
237 = $this->settings['nationalite2']
238 = $this->settings['nationalite3']
93553cea 239 = $this->settings['nick']
b04882ff
PC
240 = $this->settings['yourself']
241 = $this->settings['display_name']
242 = $this->settings['sort_name']
243 = $this->settings['tooltip_name']
93553cea 244 = null;
576777d7
FB
245 $this->settings['synchro_ax']
246 = new ProfileBool();
b715c1e1
SJ
247 $this->settings['email_directory']
248 = new ProfileEmail();
249 $this->settings['email_directory_new']
250 = new ProfileEmailDirectory();
d1a2252a 251 $this->settings['networking'] = new ProfileNetworking();
bde2be3b 252 $this->settings['tels'] = new ProfilePhones('user', 0);
4887be78
SJ
253 $this->settings['edu_0']
254 = $this->settings['edu_1']
576777d7 255 = new ProfileAppli();
bde2be3b 256 $this->watched= array('nom' => true, 'freetext' => true, 'tels' => true,
4887be78 257 'networking' => true, 'edu_0' => true, 'edu_1' => true,
8450c2aa
SJ
258 'nationalite' => true, 'nationalite2' => true,
259 'nationalite3' => true, 'nick' => true);
93553cea
FB
260 }
261
7c2e0f0d 262 protected function _fetchData()
93553cea 263 {
576777d7 264 // Checkout all data...
8450c2aa
SJ
265 $res = XDB::query("SELECT u.promo, u.promo_sortie, u.nom_usage, u.nationalite,
266 u.nationalite2, u.nationalite3, u.naissance,
5e4417a9 267 t.display_tel as mobile, t.pub as mobile_pub,
b715c1e1 268 d.email_directory as email_directory,
93553cea 269 q.profile_freetext as freetext, q.profile_freetext_pub as freetext_pub,
576777d7 270 q.profile_nick as nick, q.profile_from_ax as synchro_ax, u.matricule_ax,
4887be78
SJ
271 IF(a1.aid IS NULL, -1, a1.aid) as edu_id1, a1.type as edu_type1,
272 IF(a2.aid IS NULL, -1, a2.aid) as edu_id2, a2.type as edu_type2,
b04882ff
PC
273 n.yourself, n.display AS display_name, n.sort AS sort_name,
274 n.tooltip AS tooltip_name
b715c1e1
SJ
275 FROM auth_user_md5 AS u
276 INNER JOIN auth_user_quick AS q ON(u.user_id = q.user_id)
277 INNER JOIN profile_names_display AS n ON(n.user_id = u.user_id)
b235d980 278 LEFT JOIN profile_phones AS t ON(u.user_id = t.uid AND link_type = 'user')
b715c1e1
SJ
279 LEFT JOIN profile_directory AS d ON(d.uid = u.user_id)
280 LEFT JOIN applis_ins AS a1 ON(a1.uid = u.user_id and a1.ordre = 0)
281 LEFT JOIN applis_ins AS a2 ON(a2.uid = u.user_id and a2.ordre = 1)
93553cea
FB
282 WHERE u.user_id = {?}", S::v('uid', -1));
283 $this->values = $res->fetchOneAssoc();
576777d7
FB
284
285 // Reformat formation data
4887be78
SJ
286 $this->values['edu_0'] = array('id' => $this->values['edu_id1'],
287 'type' => $this->values['edu_type1']);
288 unset($this->values['edu_id1']);
289 unset($this->values['edu_type1']);
290 $this->values['edu_1'] = array('id' => $this->values['edu_id2'],
291 'type' => $this->values['edu_type2']);
292 unset($this->values['edu_id2']);
293 unset($this->values['edu_type2']);
576777d7
FB
294
295 // Retreive photo informations
296 $res = XDB::query("SELECT pub
297 FROM photo
298 WHERE uid = {?}", S::v('uid'));
299 $this->values['photo_pub'] = $res->fetchOneCell();
300
301 $res = XDB::query("SELECT COUNT(*)
302 FROM requests
303 WHERE type='photo' AND user_id = {?}",
304 S::v('uid'));
305 $this->values['nouvellephoto'] = $res->fetchOneCell();
b715c1e1 306
b04882ff
PC
307 // Retreive search names info
308 $this->values['search_names'] = XDB::iterator("
b715c1e1
SJ
309 SELECT sn.search_name, sn.name_type, sn.pub, sn.sn_id
310 FROM profile_names_search AS sn
311 WHERE sn.user_id = {?}
312 ORDER BY sn.name_type, search_score, search_name",
b04882ff 313 S::v('uid'));
bde2be3b
GB
314
315 // Retreive phones
316 $res = XDB::iterator("SELECT t.display_tel AS tel, t.tel_type AS type, t.pub, t.comment
317 FROM profile_phones AS t
318 WHERE t.uid = {?} AND t.link_type = 'user'
319 ORDER BY t.tel_id",
320 S::v('uid'));
321 $this->values['tels'] = $res->fetchAllAssoc();
93553cea
FB
322 }
323
7c2e0f0d 324 protected function _saveData()
93553cea 325 {
8450c2aa
SJ
326 if ($this->changed['nationalite'] || $this->changed['nationalite2'] || $this->changed['nationalite3']
327 || $this->changed['nom'] || $this->changed['prenom'] || $this->changed['naissance']) {
328 if ($this->values['nationalite3'] == "") {
329 $this->values['nationalite3'] = NULL;
330 }
331 if ($this->values['nationalite2'] == "") {
332 $this->values['nationalite2'] = $this->values['nationalite3'];
333 $this->values['nationalite3'] = NULL;
334 }
335 if ($this->values['nationalite'] == "") {
336 $this->values['nationalite'] = $this->values['nationalite2'];
337 $this->values['nationalite2'] = $this->values['nationalite3'];
338 $this->values['nationalite3'] = NULL;
339 }
340
576777d7 341 XDB::execute("UPDATE auth_user_md5
8450c2aa 342 SET nationalite = {?}, nationalite2 = {?}, nationalite3 = {?}, nom={?}, prenom={?}, naissance={?}
a7c28fff 343 WHERE user_id = {?}",
8450c2aa
SJ
344 $this->values['nationalite'], $this->values['nationalite2'], $this->values['nationalite3'],
345 $this->values['nom'], $this->values['prenom'],
7bff4cb0
FB
346 preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $this->values['naissance']),
347 S::v('uid'));
576777d7 348 }
5e4417a9 349 if ($this->changed['nick'] || $this->changed['freetext'] || $this->changed['freetext_pub'] || $this->changed['synchro_ax']) {
576777d7 350 XDB::execute("UPDATE auth_user_quick
5e4417a9 351 SET profile_nick= {?},
1052148d 352 profile_freetext={?},
576777d7
FB
353 profile_freetext_pub={?}, profile_from_ax = {?}
354 WHERE user_id = {?}",
5e4417a9 355 $this->values['nick'],
576777d7
FB
356 $this->values['freetext'], $this->values['freetext_pub'],
357 $this->values['synchro_ax'], S::v('uid'));
358 }
b715c1e1 359 if ($this->changed['email_directory']) {
6ea6d9c1 360 $new_email = ($this->values['email_directory'] == "new@example.org") ?
b715c1e1 361 $this->values['email_directory_new'] : $this->values['email_directory'];
039ed1a1
SJ
362 if ($new_email == "") {
363 $new_email = NULL;
364 }
b715c1e1
SJ
365 XDB::execute("REPLACE INTO profile_directory (uid, email_directory)
366 VALUES ({?}, {?})",
367 S::v('uid'), $new_email);
368 }
576777d7
FB
369 if ($this->changed['nick']) {
370 require_once('user.func.inc.php');
371 user_reindex(S::v('uid'));
372 }
a7c28fff
FB
373 if ($this->changed['photo_pub']) {
374 XDB::execute("UPDATE photo
375 SET pub = {?}
376 WHERE uid = {?}",
377 $this->values['photo_pub'], S::v('uid'));
378 }
b04882ff
PC
379 if ($this->changed['yourself'] || $this->changed['sort_name'] ||
380 $this-> changed['display_name'] || $this->changed['tooltip_name']) {
b715c1e1
SJ
381 XDB::execute("UPDATE profile_names_display AS n
382 SET n.yourself = {?},
383 n.sort = {?}, ". // SET
384 "n.display = {?}, ". // SET
385 "n.tooltip = {?} ". // SET
386 "WHERE n.user_id = {?}",
b04882ff
PC
387 $this->values['yourself'],
388 $this->values['sort_name'],
389 $this->values['display_name'],
390 $this->values['tooltip_name'],
391 S::v('uid'));
392 }
fd38b30e
FB
393 }
394
04334c61 395 public function _prepare(PlPage &$page, $id)
fd38b30e 396 {
fd38b30e 397 require_once "applis.func.inc.php";
d1a2252a 398
b715c1e1
SJ
399 require_once "emails.combobox.inc.php";
400 fill_email_combobox($page);
401
402 $res = XDB::iterator("SELECT nw.network_type AS type, nw.name
403 FROM profile_networking_enum AS nw
404 ORDER BY name");
d1a2252a 405 $page->assign('network_list', $res->fetchAllAssoc());
fd38b30e
FB
406 }
407}
408
409// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
410?>