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 | 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 |
67 | class 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 | ||
043bbacf | 83 | class ProfileEdu implements ProfileSetting |
576777d7 | 84 | { |
b814a8b8 | 85 | public function __construct(){} |
043bbacf | 86 | |
5ba8842e | 87 | static function sortByGradYear($line1, $line2) { |
22f0a0a8 SJ |
88 | $a = (int) $line1['grad_year']; |
89 | $b = (int) $line2['grad_year']; | |
90 | if ($a == $b) { | |
5ba8842e SJ |
91 | return 0; |
92 | } | |
22f0a0a8 | 93 | return ($a < $b) ? -1 : 1; |
5ba8842e SJ |
94 | } |
95 | ||
576777d7 FB |
96 | public function value(ProfilePage &$page, $field, $value, &$success) |
97 | { | |
98 | $success = true; | |
043bbacf SJ |
99 | if (is_null($value) || !is_array($value)) { |
100 | $value = array(); | |
1504ac45 | 101 | $res = XDB::iterator("SELECT eduid, degreeid, fieldid, grad_year, program |
043bbacf | 102 | FROM profile_education |
fb2c09c9 | 103 | WHERE uid = {?} AND !FIND_IN_SET('primary', flags) |
043bbacf SJ |
104 | ORDER BY id", |
105 | S::v('uid')); | |
106 | while($edu = $res->next()) { | |
107 | $value[] = $edu; | |
108 | } | |
109 | } else { | |
110 | $i = 0; | |
111 | foreach ($value as $key=>&$edu) { | |
112 | if (($edu['grad_year'] < 1921) || ($edu['grad_year'] > (date('Y') + 4))) { | |
113 | Platal::page()->trigError('L\'année d\'obtention du diplôme est mal renseignée, elle doit être du type : 2004.'); | |
114 | $edu['error'] = true; | |
115 | $success = false; | |
116 | } | |
117 | if ($key != $i) { | |
118 | $value[$i] = $edu; | |
119 | unset($value[$key]); | |
120 | } | |
121 | $i++; | |
122 | } | |
5ba8842e | 123 | usort($value, array("ProfileEdu", "sortByGradYear")); |
576777d7 FB |
124 | } |
125 | return $value; | |
126 | } | |
127 | ||
043bbacf | 128 | public function save(ProfilePage &$page, $field, $value) |
576777d7 | 129 | { |
043bbacf | 130 | XDB::execute("DELETE FROM profile_education |
fb2c09c9 | 131 | WHERE uid = {?} AND !FIND_IN_SET('primary', flags)", |
043bbacf SJ |
132 | S::i('uid')); |
133 | foreach ($value as $eduid=>&$edu) { | |
134 | if ($edu['eduid'] != '') { | |
135 | XDB::execute("INSERT INTO profile_education | |
1504ac45 SJ |
136 | SET id = {?}, uid = {?}, eduid = {?}, degreeid = {?}, |
137 | fieldid = {?}, grad_year = {?}, program = {?}", | |
138 | $eduid, S::i('uid'), $edu['eduid'], $edu['degreeid'], | |
139 | $edu['fieldid'], $edu['grad_year'], $edu['program']); | |
043bbacf | 140 | } |
576777d7 FB |
141 | } |
142 | } | |
143 | } | |
144 | ||
b715c1e1 SJ |
145 | class ProfileEmailDirectory implements ProfileSetting |
146 | { | |
b814a8b8 SJ |
147 | public function __construct(){} |
148 | public function save(ProfilePage &$page, $field, $value){} | |
b715c1e1 SJ |
149 | |
150 | public function value(ProfilePage &$page, $field, $value, &$success) | |
151 | { | |
ad3fee9d | 152 | $p = Platal::page(); |
b715c1e1 SJ |
153 | |
154 | $success = true; | |
155 | if (!is_null($value)) { | |
156 | $email_stripped = strtolower(trim($value)); | |
ad3fee9d SJ |
157 | if ((!isvalid_email($email_stripped)) && ($email_stripped) && ($page->values['email_directory'] == "new@example.org")) { |
158 | $p->assign('email_error', '1'); | |
159 | $p->assign('email_directory_error', $email_stripped); | |
160 | $p->trigError('Adresse Email invalide'); | |
b715c1e1 SJ |
161 | $success = false; |
162 | } else { | |
ad3fee9d | 163 | $p->assign('email_error', '0'); |
b715c1e1 SJ |
164 | } |
165 | } | |
166 | return $value; | |
167 | } | |
b715c1e1 SJ |
168 | } |
169 | ||
d1a2252a GB |
170 | class ProfileNetworking implements ProfileSetting |
171 | { | |
172 | private $email; | |
173 | private $pub; | |
174 | private $web; | |
92446a53 | 175 | private $number; |
d1a2252a GB |
176 | |
177 | public function __construct() | |
178 | { | |
179 | $this->email = new ProfileEmail(); | |
180 | $this->pub = new ProfilePub(); | |
181 | $this->web = new ProfileWeb(); | |
92446a53 | 182 | $this->number = new ProfileNumber(); |
d1a2252a GB |
183 | } |
184 | ||
185 | public function value(ProfilePage &$page, $field, $value, &$success) | |
186 | { | |
187 | if (is_null($value)) { | |
188 | $value = array(); | |
189 | $res = XDB::iterator("SELECT n.address, n.network_type AS type, n.pub, m.name | |
190 | FROM profile_networking AS n | |
191 | INNER JOIN profile_networking_enum AS m ON (n.network_type = m.network_type) | |
192 | WHERE n.uid = {?}", | |
193 | S::i('uid')); | |
194 | while($network = $res->next()) { | |
195 | $value[] = $network; | |
196 | } | |
197 | } | |
198 | if (!is_array($value)) { | |
199 | $value = array(); | |
200 | } | |
b715c1e1 SJ |
201 | $res = XDB::iterator("SELECT filter, network_type AS type |
202 | FROM profile_networking_enum;"); | |
d1a2252a GB |
203 | $filters = array(); |
204 | while($filter = $res->next()) { | |
205 | $filters[$filter['type']] = $filter['filter']; | |
206 | } | |
207 | $success = true; | |
208 | foreach($value as $i=>&$network) { | |
92c3f9e5 GB |
209 | if (!trim($network['address'])) { |
210 | unset($value[$i]); | |
211 | } else { | |
212 | if (!isset($network['pub'])) { | |
213 | $network['pub'] = 'private'; | |
214 | } | |
215 | $network['error'] = false; | |
216 | $network['pub'] = $this->pub->value($page, 'pub', $network['pub'], $s); | |
217 | $s = true; | |
218 | if ($filters[$network['type']] == 'web') { | |
219 | $network['address'] = $this->web->value($page, 'address', $network['address'], $s); | |
220 | } elseif ($filters[$network['type']] == 'email') { | |
221 | $network['address'] = $this->email->value($page, 'address', $network['address'], $s); | |
222 | } elseif ($filters[$network['type']] == 'number') { | |
223 | $network['address'] = $this->number->value($page, 'address', $network['address'], $s); | |
224 | } | |
225 | if (!$s) { | |
226 | $success = false; | |
227 | $network['error'] = true; | |
228 | } | |
d1a2252a GB |
229 | } |
230 | } | |
231 | return $value; | |
232 | } | |
233 | ||
234 | public function save(ProfilePage &$page, $field, $value) | |
235 | { | |
236 | XDB::execute("DELETE FROM profile_networking | |
237 | WHERE uid = {?}", | |
238 | S::i('uid')); | |
239 | if (!count($value)) { | |
240 | return; | |
241 | } | |
242 | $insert = array(); | |
243 | foreach ($value as $id=>$network) { | |
244 | XDB::execute("INSERT INTO profile_networking (uid, nwid, network_type, address, pub) | |
245 | VALUES ({?}, {?}, {?}, {?}, {?})", | |
246 | S::i('uid'), $id, $network['type'], $network['address'], $network['pub']); | |
247 | } | |
248 | } | |
249 | } | |
250 | ||
fd38b30e FB |
251 | class ProfileGeneral extends ProfilePage |
252 | { | |
253 | protected $pg_template = 'profile/general.tpl'; | |
254 | ||
255 | public function __construct(PlWizard &$wiz) | |
256 | { | |
257 | parent::__construct($wiz); | |
b715c1e1 SJ |
258 | $this->settings['nom'] = $this->settings['prenom'] |
259 | = new ProfileNom(); | |
7bff4cb0 | 260 | $this->settings['naissance'] = new ProfileDate(); |
bde2be3b | 261 | $this->settings['freetext_pub'] |
576777d7 | 262 | = $this->settings['photo_pub'] |
93553cea FB |
263 | = new ProfilePub(); |
264 | $this->settings['freetext'] | |
576777d7 | 265 | = $this->settings['nationalite'] |
8450c2aa SJ |
266 | = $this->settings['nationalite2'] |
267 | = $this->settings['nationalite3'] | |
93553cea | 268 | = $this->settings['nick'] |
b04882ff PC |
269 | = $this->settings['yourself'] |
270 | = $this->settings['display_name'] | |
271 | = $this->settings['sort_name'] | |
272 | = $this->settings['tooltip_name'] | |
fb2c09c9 | 273 | = $this->settings['promo_display'] |
93553cea | 274 | = null; |
576777d7 FB |
275 | $this->settings['synchro_ax'] |
276 | = new ProfileBool(); | |
b715c1e1 SJ |
277 | $this->settings['email_directory'] |
278 | = new ProfileEmail(); | |
279 | $this->settings['email_directory_new'] | |
280 | = new ProfileEmailDirectory(); | |
d1a2252a | 281 | $this->settings['networking'] = new ProfileNetworking(); |
043bbacf SJ |
282 | $this->settings['tels'] = new ProfilePhones('user', 0); |
283 | $this->settings['edus'] = new ProfileEdu(); | |
bde2be3b | 284 | $this->watched= array('nom' => true, 'freetext' => true, 'tels' => true, |
043bbacf | 285 | 'networking' => true, 'edus' => true, |
8450c2aa SJ |
286 | 'nationalite' => true, 'nationalite2' => true, |
287 | 'nationalite3' => true, 'nick' => true); | |
93553cea FB |
288 | } |
289 | ||
7c2e0f0d | 290 | protected function _fetchData() |
93553cea | 291 | { |
576777d7 | 292 | // Checkout all data... |
fb2c09c9 SJ |
293 | $res = XDB::query("SELECT p.promo_display, e.entry_year AS entry_year, e.grad_year AS grad_year, |
294 | u.nom_usage, u.nationalite, u.nationalite2, u.nationalite3, u.naissance, | |
5e4417a9 | 295 | t.display_tel as mobile, t.pub as mobile_pub, |
b715c1e1 | 296 | d.email_directory as email_directory, |
93553cea | 297 | q.profile_freetext as freetext, q.profile_freetext_pub as freetext_pub, |
576777d7 | 298 | q.profile_nick as nick, q.profile_from_ax as synchro_ax, u.matricule_ax, |
b04882ff PC |
299 | n.yourself, n.display AS display_name, n.sort AS sort_name, |
300 | n.tooltip AS tooltip_name | |
b715c1e1 | 301 | FROM auth_user_md5 AS u |
fb2c09c9 SJ |
302 | INNER JOIN auth_user_quick AS q ON (u.user_id = q.user_id) |
303 | INNER JOIN profile_names_display AS n ON (n.user_id = u.user_id) | |
304 | INNER JOIN profile_display AS p ON (p.uid = u.user_id) | |
305 | INNER JOIN profile_education AS e ON (e.uid = u.user_id AND FIND_IN_SET('primary', e.flags)) | |
306 | LEFT JOIN profile_phones AS t ON (u.user_id = t.uid AND link_type = 'user') | |
307 | LEFT JOIN profile_directory AS d ON (d.uid = u.user_id) | |
93553cea FB |
308 | WHERE u.user_id = {?}", S::v('uid', -1)); |
309 | $this->values = $res->fetchOneAssoc(); | |
576777d7 | 310 | |
576777d7 FB |
311 | // Retreive photo informations |
312 | $res = XDB::query("SELECT pub | |
313 | FROM photo | |
314 | WHERE uid = {?}", S::v('uid')); | |
315 | $this->values['photo_pub'] = $res->fetchOneCell(); | |
316 | ||
317 | $res = XDB::query("SELECT COUNT(*) | |
318 | FROM requests | |
319 | WHERE type='photo' AND user_id = {?}", | |
320 | S::v('uid')); | |
321 | $this->values['nouvellephoto'] = $res->fetchOneCell(); | |
b715c1e1 | 322 | |
b04882ff PC |
323 | // Retreive search names info |
324 | $this->values['search_names'] = XDB::iterator(" | |
b715c1e1 SJ |
325 | SELECT sn.search_name, sn.name_type, sn.pub, sn.sn_id |
326 | FROM profile_names_search AS sn | |
327 | WHERE sn.user_id = {?} | |
328 | ORDER BY sn.name_type, search_score, search_name", | |
b04882ff | 329 | S::v('uid')); |
bde2be3b | 330 | |
fb2c09c9 SJ |
331 | // Proposes choice for promo_display |
332 | if ($this->values['entry_year'] != $this->values['grad_year'] - 3) { | |
333 | for ($i = $this->values['entry_year']; $i < $this->values['grad_year'] - 2; $i++) { | |
334 | $this->values['promo_choice'][] = "X" . $i; | |
335 | } | |
336 | } | |
93553cea FB |
337 | } |
338 | ||
7c2e0f0d | 339 | protected function _saveData() |
93553cea | 340 | { |
8450c2aa SJ |
341 | if ($this->changed['nationalite'] || $this->changed['nationalite2'] || $this->changed['nationalite3'] |
342 | || $this->changed['nom'] || $this->changed['prenom'] || $this->changed['naissance']) { | |
343 | if ($this->values['nationalite3'] == "") { | |
344 | $this->values['nationalite3'] = NULL; | |
345 | } | |
346 | if ($this->values['nationalite2'] == "") { | |
347 | $this->values['nationalite2'] = $this->values['nationalite3']; | |
348 | $this->values['nationalite3'] = NULL; | |
349 | } | |
350 | if ($this->values['nationalite'] == "") { | |
351 | $this->values['nationalite'] = $this->values['nationalite2']; | |
352 | $this->values['nationalite2'] = $this->values['nationalite3']; | |
353 | $this->values['nationalite3'] = NULL; | |
354 | } | |
355 | ||
fb2c09c9 SJ |
356 | XDB::execute("UPDATE auth_user_md5 |
357 | SET nationalite = {?}, nationalite2 = {?}, nationalite3 = {?}, nom={?}, prenom={?}, naissance={?} | |
358 | WHERE user_id = {?}", | |
359 | $this->values['nationalite'], $this->values['nationalite2'], $this->values['nationalite3'], | |
360 | $this->values['nom'], $this->values['prenom'], | |
361 | preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $this->values['naissance']), | |
362 | S::v('uid')); | |
576777d7 | 363 | } |
5e4417a9 | 364 | if ($this->changed['nick'] || $this->changed['freetext'] || $this->changed['freetext_pub'] || $this->changed['synchro_ax']) { |
576777d7 | 365 | XDB::execute("UPDATE auth_user_quick |
5e4417a9 | 366 | SET profile_nick= {?}, |
1052148d | 367 | profile_freetext={?}, |
576777d7 FB |
368 | profile_freetext_pub={?}, profile_from_ax = {?} |
369 | WHERE user_id = {?}", | |
5e4417a9 | 370 | $this->values['nick'], |
576777d7 FB |
371 | $this->values['freetext'], $this->values['freetext_pub'], |
372 | $this->values['synchro_ax'], S::v('uid')); | |
373 | } | |
b715c1e1 | 374 | if ($this->changed['email_directory']) { |
6ea6d9c1 | 375 | $new_email = ($this->values['email_directory'] == "new@example.org") ? |
b715c1e1 | 376 | $this->values['email_directory_new'] : $this->values['email_directory']; |
039ed1a1 SJ |
377 | if ($new_email == "") { |
378 | $new_email = NULL; | |
379 | } | |
b715c1e1 SJ |
380 | XDB::execute("REPLACE INTO profile_directory (uid, email_directory) |
381 | VALUES ({?}, {?})", | |
382 | S::v('uid'), $new_email); | |
383 | } | |
576777d7 FB |
384 | if ($this->changed['nick']) { |
385 | require_once('user.func.inc.php'); | |
386 | user_reindex(S::v('uid')); | |
387 | } | |
a7c28fff FB |
388 | if ($this->changed['photo_pub']) { |
389 | XDB::execute("UPDATE photo | |
390 | SET pub = {?} | |
391 | WHERE uid = {?}", | |
392 | $this->values['photo_pub'], S::v('uid')); | |
393 | } | |
b04882ff PC |
394 | if ($this->changed['yourself'] || $this->changed['sort_name'] || |
395 | $this-> changed['display_name'] || $this->changed['tooltip_name']) { | |
fb2c09c9 SJ |
396 | XDB::execute("UPDATE profile_names_display AS n |
397 | SET n.yourself = {?}, | |
398 | n.sort = {?}, ". // SET | |
399 | "n.display = {?}, ". // SET | |
400 | "n.tooltip = {?} ". // SET | |
401 | "WHERE n.user_id = {?}", | |
402 | $this->values['yourself'], | |
403 | $this->values['sort_name'], | |
404 | $this->values['display_name'], | |
405 | $this->values['tooltip_name'], | |
406 | S::v('uid')); | |
407 | } | |
408 | if ($this->changed['promo_display']) { | |
409 | XDB::execute("UPDATE profile_display | |
410 | SET promo_display = {?} | |
411 | WHERE uid = {?}", | |
412 | $this->values['promo_display'], S::v('uid')); | |
b04882ff | 413 | } |
fd38b30e FB |
414 | } |
415 | ||
04334c61 | 416 | public function _prepare(PlPage &$page, $id) |
fd38b30e | 417 | { |
f711b03f | 418 | require_once "education.func.inc.php"; |
d1a2252a | 419 | |
043bbacf SJ |
420 | $res = XDB::iterator("SELECT id, field |
421 | FROM profile_education_field_enum | |
422 | ORDER BY field"); | |
423 | $page->assign('edu_fields', $res->fetchAllAssoc()); | |
424 | ||
b715c1e1 SJ |
425 | require_once "emails.combobox.inc.php"; |
426 | fill_email_combobox($page); | |
427 | ||
428 | $res = XDB::iterator("SELECT nw.network_type AS type, nw.name | |
429 | FROM profile_networking_enum AS nw | |
430 | ORDER BY name"); | |
d1a2252a | 431 | $page->assign('network_list', $res->fetchAllAssoc()); |
fd38b30e FB |
432 | } |
433 | } | |
434 | ||
435 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
436 | ?> |