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