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