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