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