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