| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2010 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 ProfileSettingSearchNames implements ProfileSetting |
| 23 | { |
| 24 | private $private_name_end; |
| 25 | private $search_names; |
| 26 | |
| 27 | private function matchWord($old, $new, $newLen) |
| 28 | { |
| 29 | return ($i = strpos($old, $new)) !== false |
| 30 | && ($i == 0 || $old{$i-1} == ' ') |
| 31 | && ($i + $newLen == strlen($old) || $old{$i + $newLen} == ' '); |
| 32 | } |
| 33 | |
| 34 | private function prepareField($value) |
| 35 | { |
| 36 | $value = mb_strtoupper($value); |
| 37 | return preg_replace('/[^A-Z]/', ' ', $value); |
| 38 | } |
| 39 | |
| 40 | private function prepare(ProfilePage &$page, $field, $value, $init, &$success) |
| 41 | { |
| 42 | $success = true; |
| 43 | $ini = $this->prepareField($init); |
| 44 | $new = $this->prepareField($value); |
| 45 | $newLen = strlen($new); |
| 46 | $success = $this->matchWord($ini, $new, $newLen) |
| 47 | || ($field == 'lastname' && $new == 'DE ' . $ini); |
| 48 | if (!$success) { |
| 49 | $field = strtolower($field); |
| 50 | Platal::page()->trigError("Le " . $field . " que tu as choisi (" . $value . |
| 51 | ") est trop loin de ton " . $field . " initial (" . $init . ")."); |
| 52 | } |
| 53 | return $success ? $value : $init; |
| 54 | } |
| 55 | |
| 56 | public function value(ProfilePage &$page, $field, $value, &$success) |
| 57 | { |
| 58 | $success = true; |
| 59 | $success_tmp = true; |
| 60 | |
| 61 | if (is_null($value)) { |
| 62 | $sn_all = XDB::iterator("SELECT CONCAT(sn.particle, sn.name) AS name, |
| 63 | sn.particle, sn.typeid, e.type, e.name AS type_name, |
| 64 | FIND_IN_SET('has_particle', e.flags) AS has_particle, |
| 65 | FIND_IN_SET('always_displayed', e.flags) AS always_displayed, |
| 66 | FIND_IN_SET('public', e.flags) AS pub |
| 67 | FROM profile_name AS sn |
| 68 | INNER JOIN profile_name_enum AS e ON (e.id = sn.typeid) |
| 69 | WHERE sn.pid = {?} AND NOT FIND_IN_SET('not_displayed', e.flags) |
| 70 | ORDER BY NOT FIND_IN_SET('always_displayed', e.flags), e.id, sn.name", |
| 71 | $page->pid()); |
| 72 | |
| 73 | $sn_types = XDB::iterator("SELECT id, type, name, |
| 74 | FIND_IN_SET('has_particle', flags) AS has_particle |
| 75 | FROM profile_name_enum |
| 76 | WHERE NOT FIND_IN_SET('not_displayed', flags) |
| 77 | AND FIND_IN_SET('always_displayed', flags) |
| 78 | ORDER BY id"); |
| 79 | |
| 80 | $value = array(); |
| 81 | $sn = $sn_all->next(); |
| 82 | while ($sn_type = $sn_types->next()) { |
| 83 | if ($sn_type['id'] == $sn['typeid']) { |
| 84 | $value[] = $sn; |
| 85 | if ($sn) { |
| 86 | $sn = $sn_all->next(); |
| 87 | } |
| 88 | } else { |
| 89 | $value[] = array('name' => '', |
| 90 | 'particle' => '', |
| 91 | 'typeid' => $sn_type['id'], |
| 92 | 'type' => $sn_type['type'], |
| 93 | 'type_name' => $sn_type['name'], |
| 94 | 'has_particle' => $sn_type['has_particle'], |
| 95 | 'always_displayed' => 1, |
| 96 | 'pub' => 1); |
| 97 | } |
| 98 | } |
| 99 | if ($sn) { |
| 100 | do { |
| 101 | $value[] = $sn; |
| 102 | } while ($sn = $sn_all->next()); |
| 103 | } |
| 104 | } else { |
| 105 | require_once 'name.func.inc.php'; |
| 106 | |
| 107 | $res = XDB::query("SELECT s.particle, s.name |
| 108 | FROM profile_name AS s |
| 109 | INNER JOIN profile_name_enum AS e ON (e.id = s.typeid) |
| 110 | WHERE s.pid = {?} AND e.type LIKE '%ini' |
| 111 | ORDER BY e.type = 'firstname_ini'", |
| 112 | $page->pid()); |
| 113 | $res = $res->fetchAllAssoc(); |
| 114 | $initial = array(); |
| 115 | $initial['lastname'] = $res[0]['particle'] . $res[0]['name']; |
| 116 | $initial['firstname'] = $res[1]['name']; |
| 117 | $sn_types = build_types(); |
| 118 | $this->search_names = array(); |
| 119 | foreach ($value as &$sn) { |
| 120 | $sn['name'] = trim($sn['name']); |
| 121 | if ($sn['type'] == 'firstname' || $sn['type'] == 'lastname') { |
| 122 | $sn['name'] = $this->prepare($page, $sn['type'], $sn['name'], |
| 123 | $initial[$sn['type']], $success_tmp); |
| 124 | $success = $success && $success_tmp; |
| 125 | } |
| 126 | if ($sn['pub']) { |
| 127 | if (isset($sn['particle']) && ($sn['particle'] != '')) { |
| 128 | list($particle, $name) = explode(' ', $sn['name'], 2); |
| 129 | $particle = trim($particle) . ' '; |
| 130 | if (!$name) { |
| 131 | list($particle, $name) = explode('\'', $sn['name'], 2); |
| 132 | $particle = trim($particle); |
| 133 | } |
| 134 | } else { |
| 135 | $particle = ''; |
| 136 | $name = $sn['name']; |
| 137 | } |
| 138 | } |
| 139 | if ($sn['name'] != '') { |
| 140 | if ($sn['pub']) { |
| 141 | $this->search_names[$sn['typeid']] = array('fullname' => $sn['name'], |
| 142 | 'name' => $name, |
| 143 | 'particle' => $particle, |
| 144 | 'pub' => $sn['pub']); |
| 145 | } else { |
| 146 | if (isset($this->search_names[$sn['typeid']])) { |
| 147 | $this->search_names[$sn['typeid']][] = $sn['name']; |
| 148 | } else { |
| 149 | $this->search_names[$sn['typeid']] = array('fullname' => $sn['name']); |
| 150 | } |
| 151 | $sn['type_name'] = $sn_types[$sn['typeid']]; |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | $res = XDB::query("SELECT public_name, private_name |
| 156 | FROM profile_display |
| 157 | WHERE pid = {?}", |
| 158 | S::v('uid')); |
| 159 | list($public_name, $private_name) = $res->fetchOneRow(); |
| 160 | if ($success) { |
| 161 | $sn_types_private = build_types('private'); |
| 162 | $this->private_name_end = build_private_name($this->search_names, $sn_types_private); |
| 163 | $private_name = $public_name . $this->private_name_end; |
| 164 | } |
| 165 | Platal::page()->assign('public_name', $public_name); |
| 166 | Platal::page()->assign('private_name', $private_name); |
| 167 | } |
| 168 | |
| 169 | return $value; |
| 170 | } |
| 171 | |
| 172 | public function save(ProfilePage &$page, $field, $value) |
| 173 | { |
| 174 | require_once 'name.func.inc.php'; |
| 175 | $sn_old = build_sn_pub(); |
| 176 | XDB::execute("DELETE FROM s |
| 177 | USING profile_name AS s |
| 178 | INNER JOIN profile_name_enum AS e ON (s.typeid = e.id) |
| 179 | WHERE s.pid = {?} AND NOT FIND_IN_SET('not_displayed', e.flags)", |
| 180 | $page->pid()); |
| 181 | $has_new = set_alias_names($this->search_names, $sn_old); |
| 182 | |
| 183 | // Only requires validation if modification in public names |
| 184 | if ($has_new) { |
| 185 | $new_names = new NamesReq(S::user(), $this->search_names, $this->private_name_end); |
| 186 | $new_names->submit(); |
| 187 | Platal::page()->trigWarning("La demande de modification de tes noms a bien été prise en compte." . |
| 188 | " Tu recevras un email dès que ces changements auront été effectués."); |
| 189 | } else { |
| 190 | $display_names = array(); |
| 191 | build_display_names($display_names, $this->search_names, $this->private_name_end); |
| 192 | set_profile_display($display_names); |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | class ProfileSettingEdu implements ProfileSetting |
| 198 | { |
| 199 | public function __construct() { |
| 200 | } |
| 201 | |
| 202 | static function sortByGradYear($line1, $line2) { |
| 203 | $a = (int) $line1['grad_year']; |
| 204 | $b = (int) $line2['grad_year']; |
| 205 | if ($a == $b) { |
| 206 | return 0; |
| 207 | } |
| 208 | return ($a < $b) ? -1 : 1; |
| 209 | } |
| 210 | |
| 211 | public function value(ProfilePage &$page, $field, $value, &$success) |
| 212 | { |
| 213 | $success = true; |
| 214 | if (is_null($value) || !is_array($value)) { |
| 215 | $value = array(); |
| 216 | $value = XDB::fetchAllAssoc("SELECT eduid, degreeid, fieldid, grad_year, program |
| 217 | FROM profile_education |
| 218 | WHERE pid = {?} AND !FIND_IN_SET('primary', flags) |
| 219 | ORDER BY id", |
| 220 | $page->pid()); |
| 221 | } else { |
| 222 | $i = 0; |
| 223 | foreach ($value as $key=>&$edu) { |
| 224 | if (($edu['grad_year'] < 1921) || ($edu['grad_year'] > (date('Y') + 4))) { |
| 225 | Platal::page()->trigError('L\'année d\'obtention du diplôme est mal renseignée, elle doit être du type : 2004.'); |
| 226 | $edu['error'] = true; |
| 227 | $success = false; |
| 228 | } |
| 229 | if ($key != $i) { |
| 230 | $value[$i] = $edu; |
| 231 | unset($value[$key]); |
| 232 | } |
| 233 | $i++; |
| 234 | } |
| 235 | usort($value, array("ProfileSettingEdu", "sortByGradYear")); |
| 236 | } |
| 237 | return $value; |
| 238 | } |
| 239 | |
| 240 | public function save(ProfilePage &$page, $field, $value) |
| 241 | { |
| 242 | XDB::execute("DELETE FROM profile_education |
| 243 | WHERE pid = {?} AND !FIND_IN_SET('primary', flags)", |
| 244 | $page->pid()); |
| 245 | foreach ($value as $eduid=>&$edu) { |
| 246 | if ($edu['eduid'] != '') { |
| 247 | XDB::execute("INSERT INTO profile_education |
| 248 | SET id = {?}, pid = {?}, eduid = {?}, degreeid = {?}, |
| 249 | fieldid = {?}, grad_year = {?}, program = {?}", |
| 250 | $eduid, $page->pid(), $edu['eduid'], $edu['degreeid'], |
| 251 | $edu['fieldid'], $edu['grad_year'], $edu['program']); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | class ProfileSettingEmailDirectory implements ProfileSetting |
| 258 | { |
| 259 | public function __construct(){} |
| 260 | public function save(ProfilePage &$page, $field, $value){} |
| 261 | |
| 262 | public function value(ProfilePage &$page, $field, $value, &$success) |
| 263 | { |
| 264 | $p = Platal::page(); |
| 265 | |
| 266 | $success = true; |
| 267 | if (!is_null($value)) { |
| 268 | $email_stripped = strtolower(trim($value)); |
| 269 | if ((!isvalid_email($email_stripped)) && ($email_stripped) && ($page->values['email_directory'] == "new@example.org")) { |
| 270 | $p->assign('email_error', '1'); |
| 271 | $p->assign('email_directory_error', $email_stripped); |
| 272 | $p->trigError('Adresse Email invalide'); |
| 273 | $success = false; |
| 274 | } else { |
| 275 | $p->assign('email_error', '0'); |
| 276 | } |
| 277 | } |
| 278 | return $value; |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | class ProfileSettingNetworking implements ProfileSetting |
| 283 | { |
| 284 | private $email; |
| 285 | private $pub; |
| 286 | private $web; |
| 287 | private $number; |
| 288 | |
| 289 | public function __construct() |
| 290 | { |
| 291 | $this->email = new ProfileSettingEmail(); |
| 292 | $this->pub = new ProfileSettingPub(); |
| 293 | $this->web = new ProfileSettingWeb(); |
| 294 | $this->number = new ProfileSettingNumber(); |
| 295 | } |
| 296 | |
| 297 | public function value(ProfilePage &$page, $field, $value, &$success) |
| 298 | { |
| 299 | if (is_null($value)) { |
| 300 | $value = XDB::fetchAllAssoc("SELECT n.address, n.network_type AS type, n.pub, m.name |
| 301 | FROM profile_networking AS n |
| 302 | INNER JOIN profile_networking_enum AS m ON (n.network_type = m.network_type) |
| 303 | WHERE n.pid = {?}", |
| 304 | $page->pid()); |
| 305 | } |
| 306 | if (!is_array($value)) { |
| 307 | $value = array(); |
| 308 | } |
| 309 | $filters = XDB::fetchAllAssoc('type', 'SELECT filter, network_type AS type |
| 310 | FROM profile_networking_enum;'); |
| 311 | $success = true; |
| 312 | foreach($value as $i=>&$network) { |
| 313 | if (!trim($network['address'])) { |
| 314 | unset($value[$i]); |
| 315 | } else { |
| 316 | if (!isset($network['pub'])) { |
| 317 | $network['pub'] = 'private'; |
| 318 | } |
| 319 | $network['error'] = false; |
| 320 | $network['pub'] = $this->pub->value($page, 'pub', $network['pub'], $s); |
| 321 | $s = true; |
| 322 | if ($filters[$network['type']] == 'web') { |
| 323 | $network['address'] = $this->web->value($page, 'address', $network['address'], $s); |
| 324 | } elseif ($filters[$network['type']] == 'email') { |
| 325 | $network['address'] = $this->email->value($page, 'address', $network['address'], $s); |
| 326 | } elseif ($filters[$network['type']] == 'number') { |
| 327 | $network['address'] = $this->number->value($page, 'address', $network['address'], $s); |
| 328 | } |
| 329 | if (!$s) { |
| 330 | $success = false; |
| 331 | $network['error'] = true; |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | return $value; |
| 336 | } |
| 337 | |
| 338 | public function save(ProfilePage &$page, $field, $value) |
| 339 | { |
| 340 | XDB::execute("DELETE FROM profile_networking |
| 341 | WHERE pid = {?}", |
| 342 | $page->pid()); |
| 343 | if (!count($value)) { |
| 344 | return; |
| 345 | } |
| 346 | $insert = array(); |
| 347 | foreach ($value as $id=>$network) { |
| 348 | XDB::execute("INSERT INTO profile_networking (pid, nwid, network_type, address, pub) |
| 349 | VALUES ({?}, {?}, {?}, {?}, {?})", |
| 350 | $page->pid(), $id, $network['type'], $network['address'], $network['pub']); |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | class ProfileSettingGeneral extends ProfilePage |
| 356 | { |
| 357 | protected $pg_template = 'profile/general.tpl'; |
| 358 | |
| 359 | public function __construct(PlWizard &$wiz) |
| 360 | { |
| 361 | parent::__construct($wiz); |
| 362 | $this->settings['search_names'] |
| 363 | = new ProfileSettingSearchNames(); |
| 364 | $this->settings['birthdate'] = new ProfileSettingDate(); |
| 365 | $this->settings['freetext_pub'] |
| 366 | = $this->settings['photo_pub'] |
| 367 | = new ProfileSettingPub(); |
| 368 | $this->settings['freetext'] |
| 369 | = $this->settings['nationality1'] |
| 370 | = $this->settings['nationality2'] |
| 371 | = $this->settings['nationality3'] |
| 372 | = $this->settings['yourself'] |
| 373 | = $this->settings['promo'] |
| 374 | = null; |
| 375 | $this->settings['email_directory'] |
| 376 | = new ProfileSettingEmail(); |
| 377 | $this->settings['email_directory_new'] |
| 378 | = new ProfileSettingEmailDirectory(); |
| 379 | $this->settings['networking'] = new ProfileSettingNetworking(); |
| 380 | $this->settings['tels'] = new ProfileSettingPhones('user', 0); |
| 381 | $this->settings['edus'] = new ProfileSettingEdu(); |
| 382 | $this->watched= array('freetext' => true, 'tels' => true, |
| 383 | 'networking' => true, 'edus' => true, |
| 384 | 'nationality1' => true, 'nationality2' => true, |
| 385 | 'nationality3' => true, 'search_names' => true); |
| 386 | } |
| 387 | |
| 388 | protected function _fetchData() |
| 389 | { |
| 390 | // Checkout all data... |
| 391 | $res = XDB::query("SELECT p.promo, e.entry_year AS entry_year, e.grad_year AS grad_year, |
| 392 | pr.nationality1, pr.nationality2, pr.nationality3, pr.birthdate, |
| 393 | t.display_tel as mobile, t.pub as mobile_pub, |
| 394 | pr.email_directory as email_directory, |
| 395 | pr.freetext, pr.freetext_pub, pr.ax_id AS matricule_ax, p.yourself |
| 396 | FROM profiles AS pr |
| 397 | INNER JOIN profile_display AS p ON (p.pid = pr.pid) |
| 398 | INNER JOIN profile_education AS e ON (e.pid = pr.pid AND FIND_IN_SET('primary', e.flags)) |
| 399 | LEFT JOIN profile_phones AS t ON (t.pid = pr.pid AND link_type = 'user') |
| 400 | WHERE pr.pid = {?}", $this->pid()); |
| 401 | $this->values = $res->fetchOneAssoc(); |
| 402 | if ($this->owner) { |
| 403 | $this->values['yourself'] = $this->owner->displayName(); |
| 404 | } |
| 405 | |
| 406 | // Retreive photo informations |
| 407 | $res = XDB::query("SELECT pub |
| 408 | FROM profile_photos |
| 409 | WHERE pid = {?}", $this->pid()); |
| 410 | $this->values['photo_pub'] = $res->fetchOneCell(); |
| 411 | |
| 412 | if ($this->owner) { |
| 413 | $res = XDB::query("SELECT COUNT(*) |
| 414 | FROM requests |
| 415 | WHERE type = 'photo' AND uid = {?}", |
| 416 | $this->owner->id()); |
| 417 | $this->values['nouvellephoto'] = $res->fetchOneCell(); |
| 418 | } else { |
| 419 | $this->values['nouvellephoto'] = 0; |
| 420 | } |
| 421 | |
| 422 | // Proposes choice for promotion |
| 423 | if ($this->values['entry_year'] != $this->values['grad_year'] - 3) { |
| 424 | for ($i = $this->values['entry_year']; $i < $this->values['grad_year'] - 2; $i++) { |
| 425 | $this->values['promo_choice'][] = "X" . $i; |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | protected function _saveData() |
| 431 | { |
| 432 | if ($this->changed['nationality1'] || $this->changed['nationality2'] || $this->changed['nationality3'] |
| 433 | || $this->changed['birthdate'] || $this->changed['freetext'] || $this->changed['freetext_pub'] |
| 434 | || $this->changed['email_directory']) { |
| 435 | if ($this->values['nationality3'] == "") { |
| 436 | $this->values['nationality3'] = NULL; |
| 437 | } |
| 438 | if ($this->values['nationality2'] == "") { |
| 439 | $this->values['nationality2'] = $this->values['nationality3']; |
| 440 | $this->values['nationality3'] = NULL; |
| 441 | } |
| 442 | if ($this->values['nationality1'] == "") { |
| 443 | $this->values['nationality1'] = $this->values['nationality2']; |
| 444 | $this->values['nationality2'] = $this->values['nationality3']; |
| 445 | $this->values['nationality3'] = NULL; |
| 446 | } |
| 447 | $new_email = ($this->values['email_directory'] == "new@example.org") ? |
| 448 | $this->values['email_directory_new'] : $this->values['email_directory']; |
| 449 | if ($new_email == "") { |
| 450 | $new_email = NULL; |
| 451 | } |
| 452 | |
| 453 | XDB::execute("UPDATE profiles |
| 454 | SET nationality1 = {?}, nationality2 = {?}, nationality3 = {?}, birthdate = {?}, |
| 455 | freetext = {?}, freetext_pub = {?}, email_directory = {?} |
| 456 | WHERE pid = {?}", |
| 457 | $this->values['nationality1'], $this->values['nationality2'], $this->values['nationality3'], |
| 458 | preg_replace('@(\d{2})/(\d{2})/(\d{4})@', '\3-\2-\1', $this->values['birthdate']), |
| 459 | $this->values['freetext'], $this->values['freetext_pub'], $new_email, $this->pid()); |
| 460 | } |
| 461 | if ($this->changed['photo_pub']) { |
| 462 | XDB::execute("UPDATE profile_photos |
| 463 | SET pub = {?} |
| 464 | WHERE pid = {?}", |
| 465 | $this->values['photo_pub'], $this->pid()); |
| 466 | } |
| 467 | if ($this->changed['yourself']) { |
| 468 | XDB::execute('UPDATE accounts |
| 469 | SET display_name = {?} |
| 470 | WHERE pid = {?}', $this->pid()); |
| 471 | } |
| 472 | if ($this->changed['promo']) { |
| 473 | XDB::execute("UPDATE profile_display |
| 474 | SET promo = {?} |
| 475 | WHERE pid = {?}", |
| 476 | $this->values['promo'], $this->pid()); |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | public function _prepare(PlPage &$page, $id) |
| 481 | { |
| 482 | require_once "education.func.inc.php"; |
| 483 | |
| 484 | $res = XDB::query("SELECT id, field |
| 485 | FROM profile_education_field_enum |
| 486 | ORDER BY field"); |
| 487 | $page->assign('edu_fields', $res->fetchAllAssoc()); |
| 488 | |
| 489 | require_once "emails.combobox.inc.php"; |
| 490 | fill_email_combobox($page, $this->owner, $this->profile); |
| 491 | |
| 492 | $res = XDB::query("SELECT nw.network_type AS type, nw.name |
| 493 | FROM profile_networking_enum AS nw |
| 494 | ORDER BY name"); |
| 495 | $page->assign('network_list', $res->fetchAllAssoc()); |
| 496 | |
| 497 | $res = XDB::query("SELECT public_name, private_name |
| 498 | FROM profile_display |
| 499 | WHERE pid = {?}", |
| 500 | $this->pid()); |
| 501 | $res = $res->fetchOneRow(); |
| 502 | $page->assign('public_name', $res[0]); |
| 503 | $page->assign('private_name', $res[1]); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: |
| 508 | ?> |