Merge branch 'fusionax' of /home/git/platal into fusionax
[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 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
109 class 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@example.org")) {
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
142 class ProfileNetworking implements ProfileSetting
143 {
144 private $email;
145 private $pub;
146 private $web;
147 private $number;
148
149 public function __construct()
150 {
151 $this->email = new ProfileEmail();
152 $this->pub = new ProfilePub();
153 $this->web = new ProfileWeb();
154 $this->number = new ProfileNumber();
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 }
173 $res = XDB::iterator("SELECT filter, network_type AS type
174 FROM profile_networking_enum;");
175 $filters = array();
176 while($filter = $res->next()) {
177 $filters[$filter['type']] = $filter['filter'];
178 }
179 $success = true;
180 foreach($value as $i=>&$network) {
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 }
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
223 class ProfileGeneral extends ProfilePage
224 {
225 protected $pg_template = 'profile/general.tpl';
226
227 public function __construct(PlWizard &$wiz)
228 {
229 parent::__construct($wiz);
230 $this->settings['nom'] = $this->settings['prenom']
231 = new ProfileNom();
232 $this->settings['naissance'] = new ProfileDate();
233 $this->settings['freetext_pub']
234 = $this->settings['photo_pub']
235 = new ProfilePub();
236 $this->settings['freetext']
237 = $this->settings['nationalite']
238 = $this->settings['nationalite2']
239 = $this->settings['nationalite3']
240 = $this->settings['nick']
241 = $this->settings['yourself']
242 = $this->settings['display_name']
243 = $this->settings['sort_name']
244 = $this->settings['tooltip_name']
245 = null;
246 $this->settings['synchro_ax']
247 = new ProfileBool();
248 $this->settings['email_directory']
249 = new ProfileEmail();
250 $this->settings['email_directory_new']
251 = new ProfileEmailDirectory();
252 $this->settings['networking'] = new ProfileNetworking();
253 $this->settings['tels'] = new ProfilePhones('user', 0);
254 $this->settings['appli1']
255 = $this->settings['appli2']
256 = new ProfileAppli();
257 $this->watched= array('nom' => true, 'freetext' => true, 'tels' => true,
258 'networking' => true, 'appli1' => true, 'appli2' => true,
259 'nationalite' => true, 'nationalite2' => true,
260 'nationalite3' => true, 'nick' => true);
261 }
262
263 protected function _fetchData()
264 {
265 // Checkout all data...
266 $res = XDB::query("SELECT u.promo, u.promo_sortie, u.nom_usage, u.nationalite,
267 u.nationalite2, u.nationalite3, u.naissance,
268 t.display_tel as mobile, t.pub as mobile_pub,
269 d.email_directory as email_directory,
270 q.profile_freetext as freetext, q.profile_freetext_pub as freetext_pub,
271 q.profile_nick as nick, q.profile_from_ax as synchro_ax, u.matricule_ax,
272 IF(a1.aid IS NULL, -1, a1.aid) as appli_id1, a1.type as appli_type1,
273 IF(a2.aid IS NULL, -1, a2.aid) as appli_id2, a2.type as appli_type2,
274 n.yourself, n.display AS display_name, n.sort AS sort_name,
275 n.tooltip AS tooltip_name
276 FROM auth_user_md5 AS u
277 INNER JOIN auth_user_quick AS q ON(u.user_id = q.user_id)
278 INNER JOIN profile_names_display AS n ON(n.user_id = u.user_id)
279 LEFT JOIN profile_phones AS t ON(u.user_id = t.uid AND link_type = 'user')
280 LEFT JOIN profile_directory AS d ON(d.uid = u.user_id)
281 LEFT JOIN applis_ins AS a1 ON(a1.uid = u.user_id and a1.ordre = 0)
282 LEFT JOIN applis_ins AS a2 ON(a2.uid = u.user_id and a2.ordre = 1)
283 WHERE u.user_id = {?}", S::v('uid', -1));
284 $this->values = $res->fetchOneAssoc();
285
286 // Reformat formation data
287 $this->values['appli1'] = array('id' => $this->values['appli_id1'],
288 'type' => $this->values['appli_type1']);
289 unset($this->values['appli_id1']);
290 unset($this->values['appli_type1']);
291 $this->values['appli2'] = array('id' => $this->values['appli_id2'],
292 'type' => $this->values['appli_type2']);
293 unset($this->values['appli_id2']);
294 unset($this->values['appli_type2']);
295
296 // Retreive photo informations
297 $res = XDB::query("SELECT pub
298 FROM photo
299 WHERE uid = {?}", S::v('uid'));
300 $this->values['photo_pub'] = $res->fetchOneCell();
301
302 $res = XDB::query("SELECT COUNT(*)
303 FROM requests
304 WHERE type='photo' AND user_id = {?}",
305 S::v('uid'));
306 $this->values['nouvellephoto'] = $res->fetchOneCell();
307
308 // Retreive search names info
309 $this->values['search_names'] = XDB::iterator("
310 SELECT sn.search_name, sn.name_type, sn.pub, sn.sn_id
311 FROM profile_names_search AS sn
312 WHERE sn.user_id = {?}
313 ORDER BY sn.name_type, search_score, search_name",
314 S::v('uid'));
315
316 // Retreive phones
317 $res = XDB::iterator("SELECT t.display_tel AS tel, t.tel_type AS type, t.pub, t.comment
318 FROM profile_phones AS t
319 WHERE t.uid = {?} AND t.link_type = 'user'
320 ORDER BY t.tel_id",
321 S::v('uid'));
322 $this->values['tels'] = $res->fetchAllAssoc();
323 }
324
325 protected function _saveData()
326 {
327 if ($this->changed['nationalite'] || $this->changed['nationalite2'] || $this->changed['nationalite3']
328 || $this->changed['nom'] || $this->changed['prenom'] || $this->changed['naissance']) {
329 if ($this->values['nationalite3'] == "") {
330 $this->values['nationalite3'] = NULL;
331 }
332 if ($this->values['nationalite2'] == "") {
333 $this->values['nationalite2'] = $this->values['nationalite3'];
334 $this->values['nationalite3'] = NULL;
335 }
336 if ($this->values['nationalite'] == "") {
337 $this->values['nationalite'] = $this->values['nationalite2'];
338 $this->values['nationalite2'] = $this->values['nationalite3'];
339 $this->values['nationalite3'] = NULL;
340 }
341
342 XDB::execute("UPDATE auth_user_md5
343 SET nationalite = {?}, nationalite2 = {?}, nationalite3 = {?}, nom={?}, prenom={?}, naissance={?}
344 WHERE user_id = {?}",
345 $this->values['nationalite'], $this->values['nationalite2'], $this->values['nationalite3'],
346 $this->values['nom'], $this->values['prenom'],
347 preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $this->values['naissance']),
348 S::v('uid'));
349 }
350 if ($this->changed['nick'] || $this->changed['freetext'] || $this->changed['freetext_pub'] || $this->changed['synchro_ax']) {
351 XDB::execute("UPDATE auth_user_quick
352 SET profile_nick= {?},
353 profile_freetext={?},
354 profile_freetext_pub={?}, profile_from_ax = {?}
355 WHERE user_id = {?}",
356 $this->values['nick'],
357 $this->values['freetext'], $this->values['freetext_pub'],
358 $this->values['synchro_ax'], S::v('uid'));
359 }
360 if ($this->changed['email_directory']) {
361 $new_email = ($this->values['email_directory'] == "new@example.org") ?
362 $this->values['email_directory_new'] : $this->values['email_directory'];
363 if ($new_email == "") {
364 $new_email = NULL;
365 }
366 XDB::execute("REPLACE INTO profile_directory (uid, email_directory)
367 VALUES ({?}, {?})",
368 S::v('uid'), $new_email);
369 }
370 if ($this->changed['nick']) {
371 require_once('user.func.inc.php');
372 user_reindex(S::v('uid'));
373 }
374 if ($this->changed['photo_pub']) {
375 XDB::execute("UPDATE photo
376 SET pub = {?}
377 WHERE uid = {?}",
378 $this->values['photo_pub'], S::v('uid'));
379 }
380 if ($this->changed['yourself'] || $this->changed['sort_name'] ||
381 $this-> changed['display_name'] || $this->changed['tooltip_name']) {
382 XDB::execute("UPDATE profile_names_display AS n
383 SET n.yourself = {?},
384 n.sort = {?}, ". // SET
385 "n.display = {?}, ". // SET
386 "n.tooltip = {?} ". // SET
387 "WHERE n.user_id = {?}",
388 $this->values['yourself'],
389 $this->values['sort_name'],
390 $this->values['display_name'],
391 $this->values['tooltip_name'],
392 S::v('uid'));
393 }
394 }
395
396 public function _prepare(PlPage &$page, $id)
397 {
398 require_once "applis.func.inc.php";
399
400 require_once "emails.combobox.inc.php";
401 fill_email_combobox($page);
402
403 $res = XDB::iterator("SELECT nw.network_type AS type, nw.name
404 FROM profile_networking_enum AS nw
405 ORDER BY name");
406 $page->assign('network_list', $res->fetchAllAssoc());
407 }
408 }
409
410 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
411 ?>