2b49451865ec50b8f0c662637ada69a4c9dab3cd
[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 {
26 return ($i = strpos($old, $new)) !== false
27 && ($i == 0 || $old{$i-1} == ' ')
28 && ($i + $newLen == strlen($old) || $old{$i + $newLen} == ' ');
29 }
30
31 private function prepareField($value)
32 {
33 $value = strtoupper(replace_accent($value));
34 return preg_replace('/[^A-Z]/', ' ', $value);
35 }
36
37 public function value(ProfilePage &$page, $field, $value, &$success)
38 {
39 $success = true;
40 $current = S::v($field);
41 $init = S::v($field . '_ini');
42 if (is_null($value)) {
43 return $current;
44 }
45 if ($value == $current || $value == $init) {
46 return $value;
47 }
48 $ini = $this->prepareField($init);
49 $old = $this->prepareField($current);
50 $new = $this->prepareField($value);
51 $newLen = strlen($new);
52 $success = $this->matchWord($old, $new, $newLen)
53 || $this->matchWord($ini, $new, $newLen)
54 || ($field == 'nom' && $new == 'DE ' . $old);
55 if (!$success) {
56 Platal::page()->trigError("Le $field que tu as choisi ($value) est trop loin de ton $field initial ($init)"
57 . (($init == $current)? "" : " et de ton prénom précédent ($current)"));
58 }
59 return $success ? $value : $current;
60 }
61
62 public function save(ProfilePage &$page, $field, $new_value)
63 {
64 $_SESSION[$field] = $new_value;
65 }
66 }
67
68 class ProfileSearchName implements ProfileSetting
69 {
70
71 public function __construct()
72 {
73 }
74
75 public function value(ProfilePage &$page, $field, $value, &$success)
76 {
77 }
78
79 public function save(ProfilePage &$page, $field, $new_value)
80 {
81 }
82 }
83
84 class ProfileEdu implements ProfileSetting
85 {
86 public function __construct() {
87 }
88
89 static function sortByGradYear($line1, $line2) {
90 $a = (int) $line1['grad_year'];
91 $b = (int) $line2['grad_year'];
92 if ($a == $b) {
93 return 0;
94 }
95 return ($a < $b) ? -1 : 1;
96 }
97
98 public function value(ProfilePage &$page, $field, $value, &$success)
99 {
100 $success = true;
101 if (is_null($value) || !is_array($value)) {
102 $value = array();
103 $res = XDB::iterator("SELECT eduid, degreeid, fieldid, grad_year, program
104 FROM profile_education
105 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)
106 ORDER BY id",
107 $page->pid());
108 while($edu = $res->next()) {
109 $value[] = $edu;
110 }
111 } else {
112 $i = 0;
113 foreach ($value as $key=>&$edu) {
114 if (($edu['grad_year'] < 1921) || ($edu['grad_year'] > (date('Y') + 4))) {
115 Platal::page()->trigError('L\'année d\'obtention du diplôme est mal renseignée, elle doit être du type : 2004.');
116 $edu['error'] = true;
117 $success = false;
118 }
119 if ($key != $i) {
120 $value[$i] = $edu;
121 unset($value[$key]);
122 }
123 $i++;
124 }
125 usort($value, array("ProfileEdu", "sortByGradYear"));
126 }
127 return $value;
128 }
129
130 public function save(ProfilePage &$page, $field, $value)
131 {
132 XDB::execute("DELETE FROM profile_education
133 WHERE uid = {?} AND !FIND_IN_SET('primary', flags)",
134 $page->pid());
135 foreach ($value as $eduid=>&$edu) {
136 if ($edu['eduid'] != '') {
137 XDB::execute("INSERT INTO profile_education
138 SET id = {?}, uid = {?}, eduid = {?}, degreeid = {?},
139 fieldid = {?}, grad_year = {?}, program = {?}",
140 $eduid, $page->pid(), $edu['eduid'], $edu['degreeid'],
141 $edu['fieldid'], $edu['grad_year'], $edu['program']);
142 }
143 }
144 }
145 }
146
147 class ProfileEmailDirectory implements ProfileSetting
148 {
149 public function __construct(){}
150 public function save(ProfilePage &$page, $field, $value){}
151
152 public function value(ProfilePage &$page, $field, $value, &$success)
153 {
154 $p = Platal::page();
155
156 $success = true;
157 if (!is_null($value)) {
158 $email_stripped = strtolower(trim($value));
159 if ((!isvalid_email($email_stripped)) && ($email_stripped) && ($page->values['email_directory'] == "new@example.org")) {
160 $p->assign('email_error', '1');
161 $p->assign('email_directory_error', $email_stripped);
162 $p->trigError('Adresse Email invalide');
163 $success = false;
164 } else {
165 $p->assign('email_error', '0');
166 }
167 }
168 return $value;
169 }
170 }
171
172 class ProfileNetworking implements ProfileSetting
173 {
174 private $email;
175 private $pub;
176 private $web;
177 private $number;
178
179 public function __construct()
180 {
181 $this->email = new ProfileEmail();
182 $this->pub = new ProfilePub();
183 $this->web = new ProfileWeb();
184 $this->number = new ProfileNumber();
185 }
186
187 public function value(ProfilePage &$page, $field, $value, &$success)
188 {
189 if (is_null($value)) {
190 $value = array();
191 $res = XDB::iterator("SELECT n.address, n.network_type AS type, n.pub, m.name
192 FROM profile_networking AS n
193 INNER JOIN profile_networking_enum AS m ON (n.network_type = m.network_type)
194 WHERE n.uid = {?}",
195 $page->pid());
196 while($network = $res->next()) {
197 $value[] = $network;
198 }
199 }
200 if (!is_array($value)) {
201 $value = array();
202 }
203 $res = XDB::iterator("SELECT filter, network_type AS type
204 FROM profile_networking_enum;");
205 $filters = array();
206 while($filter = $res->next()) {
207 $filters[$filter['type']] = $filter['filter'];
208 }
209 $success = true;
210 foreach($value as $i=>&$network) {
211 if (!trim($network['address'])) {
212 unset($value[$i]);
213 } else {
214 if (!isset($network['pub'])) {
215 $network['pub'] = 'private';
216 }
217 $network['error'] = false;
218 $network['pub'] = $this->pub->value($page, 'pub', $network['pub'], $s);
219 $s = true;
220 if ($filters[$network['type']] == 'web') {
221 $network['address'] = $this->web->value($page, 'address', $network['address'], $s);
222 } elseif ($filters[$network['type']] == 'email') {
223 $network['address'] = $this->email->value($page, 'address', $network['address'], $s);
224 } elseif ($filters[$network['type']] == 'number') {
225 $network['address'] = $this->number->value($page, 'address', $network['address'], $s);
226 }
227 if (!$s) {
228 $success = false;
229 $network['error'] = true;
230 }
231 }
232 }
233 return $value;
234 }
235
236 public function save(ProfilePage &$page, $field, $value)
237 {
238 XDB::execute("DELETE FROM profile_networking
239 WHERE uid = {?}",
240 $page->pid());
241 if (!count($value)) {
242 return;
243 }
244 $insert = array();
245 foreach ($value as $id=>$network) {
246 XDB::execute("INSERT INTO profile_networking (uid, nwid, network_type, address, pub)
247 VALUES ({?}, {?}, {?}, {?}, {?})",
248 $page->pid(), $id, $network['type'], $network['address'], $network['pub']);
249 }
250 }
251 }
252
253 class ProfileGeneral extends ProfilePage
254 {
255 protected $pg_template = 'profile/general.tpl';
256
257 public function __construct(PlWizard &$wiz)
258 {
259 parent::__construct($wiz);
260 $this->settings['nom'] = $this->settings['prenom']
261 = new ProfileNom();
262 $this->settings['birthdate'] = new ProfileDate();
263 $this->settings['freetext_pub']
264 = $this->settings['photo_pub']
265 = new ProfilePub();
266 $this->settings['freetext']
267 = $this->settings['nationality1']
268 = $this->settings['nationality2']
269 = $this->settings['nationality3']
270 = $this->settings['nick']
271 = $this->settings['yourself']
272 = $this->settings['display_name']
273 = $this->settings['sort_name']
274 = $this->settings['tooltip_name']
275 = $this->settings['promo_display']
276 = null;
277 $this->settings['synchro_ax']
278 = new ProfileBool();
279 $this->settings['email_directory']
280 = new ProfileEmail();
281 $this->settings['email_directory_new']
282 = new ProfileEmailDirectory();
283 $this->settings['networking'] = new ProfileNetworking();
284 $this->settings['tels'] = new ProfilePhones('user', 0);
285 $this->settings['edus'] = new ProfileEdu();
286 $this->watched= array('nom' => true, 'freetext' => true, 'tels' => true,
287 'networking' => true, 'edus' => true,
288 'nationality1' => true, 'nationality2' => true,
289 'nationality3' => true, 'nick' => true);
290 }
291
292 protected function _fetchData()
293 {
294 // Checkout all data...
295 $res = XDB::query("SELECT p.promo AS promo_display, e.entry_year AS entry_year, e.grad_year AS grad_year,
296 pr.nationality1, pr.nationality2, pr.nationality3, pr.birthdate,
297 t.display_tel as mobile, t.pub as mobile_pub,
298 d.email_directory as email_directory,
299 pr.freetext, pr.freetext_pub as freetext_pub,
300 n.yourself, n.display AS display_name, n.sort AS sort_name,
301 n.tooltip AS tooltip_name
302 FROM profiles AS pr
303 INNER JOIN profile_names_display AS n ON (n.user_id = pr.pid)
304 INNER JOIN profile_display AS p ON (p.pid = pr.pid)
305 INNER JOIN profile_education AS e ON (e.uid = pr.pid AND FIND_IN_SET('primary', e.flags))
306 LEFT JOIN profile_phones AS t ON (t.uid = pr.pid AND link_type = 'user')
307 LEFT JOIN profile_directory AS d ON (d.uid = pr.pid)
308 WHERE pr.pid = {?}", $this->pid());
309 $this->values = $res->fetchOneAssoc();
310
311 // Retreive photo informations
312 $res = XDB::query("SELECT pub
313 FROM photo
314 WHERE uid = {?}", $this->pid());
315 $this->values['photo_pub'] = $res->fetchOneCell();
316
317 if ($this->owner) {
318 $res = XDB::query("SELECT COUNT(*)
319 FROM requests
320 WHERE type='photo' AND user_id = {?}",
321 $this->owner->id());
322 $this->values['nouvellephoto'] = $res->fetchOneCell();
323 } else {
324 $this->values['nouvellephoto'] = 0;
325 }
326
327 // Retreive search names info
328 $this->values['search_names'] = XDB::iterator("
329 SELECT sn.search_name, sn.name_type, sn.pub, sn.sn_id
330 FROM profile_names_search AS sn
331 WHERE sn.user_id = {?}
332 ORDER BY sn.name_type, search_score, search_name",
333 $this->pid());
334
335 // Proposes choice for promo_display
336 if ($this->values['entry_year'] != $this->values['grad_year'] - 3) {
337 for ($i = $this->values['entry_year']; $i < $this->values['grad_year'] - 2; $i++) {
338 $this->values['promo_choice'][] = "X" . $i;
339 }
340 }
341 }
342
343 protected function _saveData()
344 {
345 if ($this->changed['nationality1'] || $this->changed['nationality2'] || $this->changed['nationality3']
346 || $this->changed['nom'] || $this->changed['prenom'] || $this->changed['naissance']
347 || $this->changed['freetext'] || $this->changed['freetext_pub']) {
348 if ($this->values['nationality3'] == "") {
349 $this->values['nationality3'] = NULL;
350 }
351 if ($this->values['nationality2'] == "") {
352 $this->values['nationality2'] = $this->values['nationality3'];
353 $this->values['nationality3'] = NULL;
354 }
355 if ($this->values['nationality1'] == "") {
356 $this->values['nationality1'] = $this->values['nationality2'];
357 $this->values['nationality2'] = $this->values['nationality3'];
358 $this->values['nationality3'] = NULL;
359 }
360
361 XDB::execute("UPDATE profiles
362 SET nationality1 = {?}, nationality2 = {?}, nationality3 = {?}, birthdate = {?},
363 freetext = {?}, freetext_pub = {?}
364 WHERE user_id = {?}",
365 $this->values['nationality1'], $this->values['nationality2'], $this->values['nationality3'],
366 preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $this->values['birthdate']),
367 $this->values['freetext'], $this->values['freetext_pub'],
368 $this->pid());
369 }
370 if ($this->changed['email_directory']) {
371 $new_email = ($this->values['email_directory'] == "new@example.org") ?
372 $this->values['email_directory_new'] : $this->values['email_directory'];
373 if ($new_email == "") {
374 $new_email = NULL;
375 }
376 XDB::execute("REPLACE INTO profile_directory (uid, email_directory)
377 VALUES ({?}, {?})",
378 $this->pid(), $new_email);
379 }
380 if ($this->changed['nick']) {
381 require_once('user.func.inc.php');
382 user_reindex(S::v('uid'));
383 }
384 if ($this->changed['photo_pub']) {
385 XDB::execute("UPDATE photo
386 SET pub = {?}
387 WHERE uid = {?}",
388 $this->values['photo_pub'], $this->pid());
389 }
390 if ($this->changed['yourself'] || $this->changed['sort_name'] ||
391 $this-> changed['display_name'] || $this->changed['tooltip_name']) {
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 = {?}",
398 $this->values['yourself'],
399 $this->values['sort_name'],
400 $this->values['display_name'],
401 $this->values['tooltip_name'],
402 $this->pid());
403 }
404 if ($this->changed['promo_display']) {
405 XDB::execute("UPDATE profile_display
406 SET promo = {?}
407 WHERE pid = {?}",
408 $this->values['promo_display'], $this->pid());
409 }
410 }
411
412 public function _prepare(PlPage &$page, $id)
413 {
414 require_once "education.func.inc.php";
415
416 $res = XDB::iterator("SELECT id, field
417 FROM profile_education_field_enum
418 ORDER BY field");
419 $page->assign('edu_fields', $res->fetchAllAssoc());
420
421 require_once "emails.combobox.inc.php";
422 fill_email_combobox($page, $this->owner, $this->profile);
423
424 $res = XDB::iterator("SELECT nw.network_type AS type, nw.name
425 FROM profile_networking_enum AS nw
426 ORDER BY name");
427 $page->assign('network_list', $res->fetchAllAssoc());
428 }
429 }
430
431 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
432 ?>