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