Update due to changes to profile_display's schema.
[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 static function sortByGradYear($line1, $line2) {
88 $a = (int) $line1['grad_year'];
89 $b = (int) $line2['grad_year'];
90 if ($a == $b) {
91 return 0;
92 }
93 return ($a < $b) ? -1 : 1;
94 }
95
96 public function value(ProfilePage &$page, $field, $value, &$success)
97 {
98 $success = true;
99 if (is_null($value) || !is_array($value)) {
100 $value = array();
101 $res = XDB::iterator("SELECT eduid, degreeid, fieldid, grad_year, program
102 FROM profile_education
103 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)
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 }
123 usort($value, array("ProfileEdu", "sortByGradYear"));
124 }
125 return $value;
126 }
127
128 public function save(ProfilePage &$page, $field, $value)
129 {
130 XDB::execute("DELETE FROM profile_education
131 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)",
132 S::i('uid'));
133 foreach ($value as $eduid=>&$edu) {
134 if ($edu['eduid'] != '') {
135 XDB::execute("INSERT INTO profile_education
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']);
140 }
141 }
142 }
143 }
144
145 class ProfileEmailDirectory implements ProfileSetting
146 {
147 public function __construct(){}
148 public function save(ProfilePage &$page, $field, $value){}
149
150 public function value(ProfilePage &$page, $field, $value, &$success)
151 {
152 $p = Platal::page();
153
154 $success = true;
155 if (!is_null($value)) {
156 $email_stripped = strtolower(trim($value));
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');
161 $success = false;
162 } else {
163 $p->assign('email_error', '0');
164 }
165 }
166 return $value;
167 }
168 }
169
170 class ProfileNetworking implements ProfileSetting
171 {
172 private $email;
173 private $pub;
174 private $web;
175 private $number;
176
177 public function __construct()
178 {
179 $this->email = new ProfileEmail();
180 $this->pub = new ProfilePub();
181 $this->web = new ProfileWeb();
182 $this->number = new ProfileNumber();
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 }
201 $res = XDB::iterator("SELECT filter, network_type AS type
202 FROM profile_networking_enum;");
203 $filters = array();
204 while($filter = $res->next()) {
205 $filters[$filter['type']] = $filter['filter'];
206 }
207 $success = true;
208 foreach($value as $i=>&$network) {
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 }
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
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);
258 $this->settings['nom'] = $this->settings['prenom']
259 = new ProfileNom();
260 $this->settings['naissance'] = new ProfileDate();
261 $this->settings['freetext_pub']
262 = $this->settings['photo_pub']
263 = new ProfilePub();
264 $this->settings['freetext']
265 = $this->settings['nationalite']
266 = $this->settings['nationalite2']
267 = $this->settings['nationalite3']
268 = $this->settings['nick']
269 = $this->settings['yourself']
270 = $this->settings['display_name']
271 = $this->settings['sort_name']
272 = $this->settings['tooltip_name']
273 = $this->settings['promo_display']
274 = null;
275 $this->settings['synchro_ax']
276 = new ProfileBool();
277 $this->settings['email_directory']
278 = new ProfileEmail();
279 $this->settings['email_directory_new']
280 = new ProfileEmailDirectory();
281 $this->settings['networking'] = new ProfileNetworking();
282 $this->settings['tels'] = new ProfilePhones('user', 0);
283 $this->settings['edus'] = new ProfileEdu();
284 $this->watched= array('nom' => true, 'freetext' => true, 'tels' => true,
285 'networking' => true, 'edus' => true,
286 'nationalite' => true, 'nationalite2' => true,
287 'nationalite3' => true, 'nick' => true);
288 }
289
290 protected function _fetchData()
291 {
292 // Checkout all data...
293 $res = XDB::query("SELECT p.promo AS 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,
295 t.display_tel as mobile, t.pub as mobile_pub,
296 d.email_directory as email_directory,
297 q.profile_freetext as freetext, q.profile_freetext_pub as freetext_pub,
298 q.profile_nick as nick, q.profile_from_ax as synchro_ax, u.matricule_ax,
299 n.yourself, n.display AS display_name, n.sort AS sort_name,
300 n.tooltip AS tooltip_name
301 FROM auth_user_md5 AS u
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.pid = 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)
308 WHERE u.user_id = {?}", S::v('uid', -1));
309 $this->values = $res->fetchOneAssoc();
310
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();
322
323 // Retreive search names info
324 $this->values['search_names'] = XDB::iterator("
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",
329 S::v('uid'));
330
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 }
337 }
338
339 protected function _saveData()
340 {
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
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'));
363 }
364 if ($this->changed['nick'] || $this->changed['freetext'] || $this->changed['freetext_pub'] || $this->changed['synchro_ax']) {
365 XDB::execute("UPDATE auth_user_quick
366 SET profile_nick= {?},
367 profile_freetext={?},
368 profile_freetext_pub={?}, profile_from_ax = {?}
369 WHERE user_id = {?}",
370 $this->values['nick'],
371 $this->values['freetext'], $this->values['freetext_pub'],
372 $this->values['synchro_ax'], S::v('uid'));
373 }
374 if ($this->changed['email_directory']) {
375 $new_email = ($this->values['email_directory'] == "new@example.org") ?
376 $this->values['email_directory_new'] : $this->values['email_directory'];
377 if ($new_email == "") {
378 $new_email = NULL;
379 }
380 XDB::execute("REPLACE INTO profile_directory (uid, email_directory)
381 VALUES ({?}, {?})",
382 S::v('uid'), $new_email);
383 }
384 if ($this->changed['nick']) {
385 require_once('user.func.inc.php');
386 user_reindex(S::v('uid'));
387 }
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 }
394 if ($this->changed['yourself'] || $this->changed['sort_name'] ||
395 $this-> changed['display_name'] || $this->changed['tooltip_name']) {
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 = {?}
411 WHERE pid = {?}",
412 $this->values['promo_display'], S::v('uid'));
413 }
414 }
415
416 public function _prepare(PlPage &$page, $id)
417 {
418 require_once "education.func.inc.php";
419
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
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");
431 $page->assign('network_list', $res->fetchAllAssoc());
432 }
433 }
434
435 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
436 ?>