Deletes all references to sectors as they have been replaced by jobterms.
[platal.git] / include / validations.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
9f5bd98e 3 * Copyright (C) 2003-2010 Polytechnique.org *
0337d704 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
0337d704 22define('SIZE_MAX', 32768);
23
ce8ca505
FB
24global $globals;
25require_once $globals->spoolroot . '/core/classes/xdb.php';
2f151d5f 26
0337d704 27/**
28 * Iterator class, that lists objects through the database
29 */
30class ValidateIterator extends XOrgDBIterator
31{
32 // {{{ constuctor
d71befc4 33
c67b874f 34 public function __construct()
0337d704 35 {
616918d0
SJ
36 parent::__construct('SELECT data, DATE_FORMAT(stamp, "%Y%m%d%H%i%s")
37 FROM requests
38 ORDER BY stamp', MYSQL_NUM);
0337d704 39 }
40
41 // }}}
42 // {{{ function next()
43
616918d0 44 public function next()
0337d704 45 {
46 if (list($result, $stamp) = parent::next()) {
33b47675 47 $result = Validate::unserialize($result);
0337d704 48 $result->stamp = $stamp;
616918d0 49 return $result;
0337d704 50 }
616918d0
SJ
51
52 return null;
0337d704 53 }
54
55 // }}}
56}
57
616918d0 58/** Virtual class to adapt for every possible implementation.
0337d704 59 */
612a2d8a 60abstract class Validate
0337d704 61{
62 // {{{ properties
eaf30d86 63
5daf68f6 64 public $user;
612a2d8a 65
66 public $stamp;
67 public $unique;
616918d0 68 // Enable the refuse button.
612a2d8a 69 public $refuse = true;
eaf30d86 70
612a2d8a 71 public $type;
72 public $comments = Array();
616918d0
SJ
73 // Validations rules: comments for administrators.
74 public $rules = 'Mieux vaut laisser une demande de validation à un autre administrateur que de valider une requête illégale ou que de refuser une demande légitime.';
0337d704 75
76 // }}}
77 // {{{ constructor
eaf30d86 78
616918d0
SJ
79 /** Constructor
80 * @param $_user: user object that required the validation.
81 * @param $_unique: set to false if a profile can have multiple requests of this type.
82 * @param $_type: request's type.
0337d704 83 */
532c06cf 84 public function __construct(User &$_user, $_unique, $_type)
0337d704 85 {
532c06cf 86 $this->user = &$_user;
0337d704 87 $this->stamp = date('YmdHis');
88 $this->unique = $_unique;
89 $this->type = $_type;
8f2104cb 90 $this->promo = $this->user->promo();
0337d704 91 }
eaf30d86 92
0337d704 93 // }}}
94 // {{{ function submit()
95
616918d0
SJ
96 /** Sends data to validation.
97 * It also deletes multiple requests for a couple (profile, type)
98 * when $this->unique is set to true.
0337d704 99 */
612a2d8a 100 public function submit()
0337d704 101 {
0337d704 102 if ($this->unique) {
1bf36cd1
SJ
103 XDB::execute('DELETE FROM requests
104 WHERE uid = {?} AND type = {?}',
105 $this->user->id(), $this->type);
0337d704 106 }
eaf30d86 107
0337d704 108 $this->stamp = date('YmdHis');
1bf36cd1
SJ
109 XDB::execute('INSERT INTO requests (uid, type, data, stamp)
110 VALUES ({?}, {?}, {?}, {?})',
616918d0 111 $this->user->id(), $this->type, $this, $this->stamp);
0337d704 112
84868ee9 113 global $globals;
ebfdf077 114 $globals->updateNbValid();
0337d704 115 return true;
116 }
117
118 // }}}
119 // {{{ function update()
120
612a2d8a 121 protected function update()
0337d704 122 {
1bf36cd1
SJ
123 XDB::execute('UPDATE requests
124 SET data = {?}, stamp = stamp
125 WHERE uid = {?} AND type = {?} AND stamp = {?}',
5daf68f6 126 $this, $this->user->id(), $this->type, $this->stamp);
0337d704 127 return true;
128 }
129
130 // }}}
131 // {{{ function clean()
eaf30d86 132
616918d0
SJ
133 /** Deletes request from 'requests' table.
134 * If $this->unique is set, it deletes every requests of this type.
0337d704 135 */
d17761d8 136 public function clean()
0337d704 137 {
95e36b0f
SJ
138 global $globals;
139
0337d704 140 if ($this->unique) {
1bf36cd1
SJ
141 $success = XDB::execute('DELETE FROM requests
142 WHERE uid = {?} AND type = {?}',
5daf68f6 143 $this->user->id(), $this->type);
0337d704 144 } else {
1bf36cd1
SJ
145 $success = XDB::execute('DELETE FROM requests
146 WHERE uid = {?} AND type = {?} AND stamp = {?}',
5daf68f6 147 $this->user->id(), $this->type, $this->stamp);
0337d704 148 }
ebfdf077 149 $globals->updateNbValid();
84868ee9 150 return $success;
0337d704 151 }
152
153 // }}}
154 // {{{ function handle_formu()
eaf30d86 155
616918d0 156 /** Handles form validation.
0337d704 157 */
612a2d8a 158 public function handle_formu()
0337d704 159 {
160 if (Env::has('delete')) {
161 $this->clean();
616918d0 162 $this->trigSuccess('Requête supprimée.');
0337d704 163 return true;
164 }
165
616918d0 166 // Data updates.
6aa01fed 167 if (Env::has('edit')) {
168 if ($this->handle_editor()) {
169 $this->update();
616918d0 170 $this->trigSuccess('Requête mise à jour.');
6aa01fed 171 return true;
172 }
173 return false;
174 }
175
616918d0 176 // Comment addition.
0337d704 177 if (Env::has('hold') && Env::has('comm')) {
4791ff77 178 $formid = Env::i('formid');
179 foreach ($this->comments as $comment) {
180 if ($comment[2] === $formid) {
181 return true;
182 }
183 }
90608d68 184 if (!strlen(trim(Env::v('comm')))) {
185 return true;
186 }
616918d0 187 $this->comments[] = array(S::user()->login(), Env::v('comm'), $formid);
0337d704 188
616918d0 189 // Sends email to our hotline.
0337d704 190 global $globals;
b9c53090 191 $mailer = new PlMailer();
0337d704 192 $mailer->setSubject("Commentaires de validation {$this->type}");
193 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
b9c53090 194 $mailer->addTo($globals->core->admin_email);
0337d704 195
53092def 196 $body = "Validation {$this->type} pour {$this->user->login()}\n\n"
5daf68f6
VZ
197 . S::user()->login() . " a ajouté le commentaire :\n\n"
198 . Env::v('comm') . "\n\n"
199 . "cf la discussion sur : " . $globals->baseurl . "/admin/validate";
0337d704 200
201 $mailer->setTxtBody(wordwrap($body));
202 $mailer->send();
203
204 $this->update();
616918d0 205 $this->trigSuccess('Commentaire ajouté.');
0337d704 206 return true;
207 }
208
209 if (Env::has('accept')) {
210 if ($this->commit()) {
211 $this->sendmail(true);
212 $this->clean();
faefdbb7 213 $this->trigSuccess('Email de validation envoyé');
0337d704 214 return true;
215 } else {
a7d35093 216 $this->trigError('Erreur lors de la validation');
0337d704 217 return false;
218 }
219 }
220
221 if (Env::has('refuse')) {
5e2307dc 222 if (Env::v('comm')) {
0337d704 223 $this->sendmail(false);
224 $this->clean();
616918d0 225 $this->trigSuccess('Email de refus envoyé.');
0337d704 226 return true;
227 } else {
6bb2f79a 228 $this->trigError('Pas de motivation pour le refus&nbsp;!!!');
0337d704 229 }
230 }
231
232 return false;
233 }
234
235 // }}}
236 // {{{ function sendmail
237
612a2d8a 238 protected function sendmail($isok)
0337d704 239 {
240 global $globals;
1e33266a 241 $mailer = new PlMailer();
0337d704 242 $mailer->setSubject($this->_mail_subj());
243 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
5daf68f6 244 $mailer->addTo("\"{$this->user->fullName()}\" <{$this->user->bestEmail()}>");
0337d704 245 $mailer->addCc("validation+{$this->type}@{$globals->mail->domain}");
246
5daf68f6 247 $body = ($this->user->isFemale() ? "Chère camarade,\n\n" : "Cher camarade,\n\n")
0337d704 248 . $this->_mail_body($isok)
9be4a981 249 . (Env::has('comm') ? "\n\n" . Env::v('comm') : '')
5353e51e
SJ
250 . "\n\nCordialement,\n-- \nL'équipe de Polytechnique.org\n"
251 . $this->_mail_ps($isok);
0337d704 252
253 $mailer->setTxtBody(wordwrap($body));
254 $mailer->send();
255 }
256
257 // }}}
258 // {{{ function trig()
eaf30d86 259
a7d35093 260 protected function trigError($msg)
612a2d8a 261 {
d7610c35 262 Platal::page()->trigError($msg);
a7d35093
FB
263 }
264
265 protected function trigWarning($msg)
266 {
d7610c35 267 Platal::page()->trigWarning($msg);
a7d35093
FB
268 }
269
270 protected function trigSuccess($msg)
271 {
d7610c35 272 Platal::page()->trigSuccess($msg);
0337d704 273 }
eaf30d86 274
0337d704 275 // }}}
20d7932b 276 // {{{ function get_typed_request()
0337d704 277
616918d0
SJ
278 /**
279 * @param $pid: profile's pid
280 * @param $type: request's type
281 * @param $stamp: request's timestamp
0337d704 282 *
616918d0 283 * Should only be used to retrieve an object in the databse with Validate::get_typed_request(...)
0337d704 284 */
612a2d8a 285 static public function get_typed_request($uid, $type, $stamp = -1)
0337d704 286 {
0337d704 287 if ($stamp == -1) {
1bf36cd1
SJ
288 $res = XDB::query('SELECT data
289 FROM requests
290 WHERE uid = {?} and type = {?}',
291 $uid, $type);
0337d704 292 } else {
1bf36cd1
SJ
293 $res = XDB::query('SELECT data, DATE_FORMAT(stamp, "%Y%m%d%H%i%s")
294 FROM requests
295 WHERE uid = {?} AND type = {?} and stamp = {?}',
296 $uid, $type, $stamp);
0337d704 297 }
298 if ($result = $res->fetchOneCell()) {
33b47675 299 $result = Validate::unserialize($result);
0337d704 300 } else {
301 $result = false;
302 }
303 return($result);
304 }
305
306 // }}}
02838718 307 // {{{ function get_request_by_id()
308
309 static public function get_request_by_id($id)
310 {
311 list($uid, $type, $stamp) = explode('_', $id, 3);
312 return Validate::get_typed_request($uid, $type, $stamp);
313 }
314
315 // }}}
5b0dc389 316 // {{{ function get_typed_requests()
317
616918d0 318 /** Same as get_typed_request() but return an array of objects.
5b0dc389 319 */
612a2d8a 320 static public function get_typed_requests($uid, $type)
5b0dc389 321 {
1bf36cd1
SJ
322 $res = XDB::iterRow('SELECT data
323 FROM requests
324 WHERE uid = {?} and type = {?}',
325 $uid, $type);
5b0dc389 326 $array = array();
327 while (list($data) = $res->next()) {
33b47675 328 $array[] = Validate::unserialize($data);
5b0dc389 329 }
330 return $array;
331 }
332
333 // }}}
bb0727ea
VZ
334 // {{{ function get_typed_requests_count()
335
616918d0 336 /** Same as get_typed_requests() but return the count of available requests.
bb0727ea
VZ
337 */
338 static public function get_typed_requests_count($uid, $type)
339 {
1bf36cd1
SJ
340 $res = XDB::query('SELECT COUNT(data)
341 FROM requests
342 WHERE uid = {?} and type = {?}',
343 $uid, $type);
bb0727ea
VZ
344 return $res->fetchOneCell();
345 }
346
347 // }}}
0337d704 348 // {{{ function _mail_body
349
612a2d8a 350 abstract protected function _mail_body($isok);
eaf30d86 351
0337d704 352 // }}}
353 // {{{ function _mail_subj
354
612a2d8a 355 abstract protected function _mail_subj();
eaf30d86 356
0337d704 357 // }}}
9be4a981
SJ
358 // {{{ function _mail_ps
359
360 protected function _mail_ps($isok)
361 {
5353e51e 362 return '';
9be4a981
SJ
363 }
364
365 // }}}
0337d704 366 // {{{ function commit()
eaf30d86 367
616918d0 368 /** Inserts data in database.
0337d704 369 */
612a2d8a 370 abstract public function commit();
0337d704 371
372 // }}}
373 // {{{ function formu()
eaf30d86 374
616918d0 375 /** Retunrs the name of the form's template. */
612a2d8a 376 abstract public function formu();
0337d704 377
378 // }}}
6aa01fed 379 // {{{ function editor()
380
616918d0 381 /** Returns the name of the edition form's template. */
612a2d8a 382 public function editor()
383 {
384 return null;
385 }
6aa01fed 386
387 // }}}
e18888f4 388 // {{{ function answers()
389
616918d0 390 /** Automatic answers table for this type of validation. */
612a2d8a 391 public function answers()
392 {
e18888f4 393 static $answers_table;
394 if (!isset($answers_table[$this->type])) {
616918d0
SJ
395 $r = XDB::query('SELECT id, title, answer
396 FROM requests_answers
397 WHERE category = {?}',
398 $this->type);
745539c0 399 $answers_table[$this->type] = $r->fetchAllAssoc();
e18888f4 400 }
401 return $answers_table[$this->type];
402 }
403
404 // }}}
616918d0 405 // {{{ function id()
ed5b9703 406
612a2d8a 407 public function id()
ed5b9703 408 {
5daf68f6 409 return $this->user->id() . '_' . $this->type . '_' . $this->stamp;
ed5b9703 410 }
411
412 // }}}
fba760d2 413 // {{{ function ruleText()
414
415 public function ruleText()
416 {
417 return str_replace('\'', '\\\'', $this->rules);
418 }
419
420 // }}}
33b47675 421 // {{{ function unserialize()
4407871a 422
33b47675
FB
423 public static function unserialize($data)
424 {
616918d0 425 return unserialize($data);
33b47675 426 }
4407871a
SJ
427
428 // }}}
429}
430
431/** Virtual class for profile related validation.
432 */
433abstract class ProfileValidate extends Validate
434{
435 // {{{ properties
436
437 public $profile;
438 public $profileOwner;
439 public $userIsProfileOwner;
a0fce0c6 440 public $ownerIsRegistered;
4407871a
SJ
441
442 // }}}
443 // {{{ constructor
444
616918d0 445 /** Constructor
4407871a
SJ
446 * @param $_user: user object that required the validation.
447 * @param $_profile: profile object that is to be modified,
448 * its owner (if exists) can differ from $_user.
449 * @param $_unique: set to false if a profile can have multiple requests of this type.
450 * @param $_type: request's type.
451 */
452 public function __construct(User &$_user, Profile &$_profile, $_unique, $_type)
453 {
454 parent::__construct($_user, $_unique, $_type);
455 $this->profile = &$_profile;
456 $this->profileOwner = $this->profile->owner();
a0fce0c6
SJ
457 $this->userIsProfileOwner = (!is_null($this->profileOwner)
458 && $this->profileOwner->id() == $this->user->id());
459 $this->ownerIsRegistered = $this->profile->isActive();
4407871a
SJ
460 }
461
462 // }}}
463 // {{{ function submit()
464
465 /** Sends data to validation.
466 * It also deletes multiple requests for a couple (profile, type)
467 * when $this->unique is set to true.
468 */
469 public function submit()
470 {
471 if ($this->unique) {
472 XDB::execute('DELETE FROM requests
473 WHERE pid = {?} AND type = {?}',
474 $this->profile->id(), $this->type);
475 }
476
477 $this->stamp = date('YmdHis');
478 XDB::execute('INSERT INTO requests (uid, pid, type, data, stamp)
d84b97ba 479 VALUES ({?}, {?}, {?}, {?}, {?})',
4407871a
SJ
480 $this->user->id(), $this->profile->id(), $this->type, $this, $this->stamp);
481
482 global $globals;
483 $globals->updateNbValid();
484 return true;
485 }
486
487 // }}}
488 // {{{ function update()
489
490 protected function update()
491 {
492 XDB::execute('UPDATE requests
493 SET data = {?}, stamp = stamp
494 WHERE pid = {?} AND type = {?} AND stamp = {?}',
495 $this, $this->profile->id(), $this->type, $this->stamp);
496 return true;
497 }
498
499 // }}}
500 // {{{ function clean()
501
502 /** Deletes request from 'requests' table.
503 * If $this->unique is set, it deletes every requests of this type.
504 */
505 public function clean()
506 {
507 global $globals;
508
509 if ($this->unique) {
510 $success = XDB::execute('DELETE FROM requests
511 WHERE pid = {?} AND type = {?}',
512 $this->profile->id(), $this->type);
513 } else {
514 $success = XDB::execute('DELETE FROM requests
515 WHERE pid = {?} AND type = {?} AND stamp = {?}',
516 $this->profile->id(), $this->type, $this->stamp);
517 }
518 $globals->updateNbValid();
519 return $success;
520 }
521
522 // }}}
523 // {{{ function sendmail
524
525 protected function sendmail($isok)
526 {
a0fce0c6
SJ
527 // Only sends email if the profile's owner exists and is registered.
528 if ($this->ownerIsRegistered) {
529 global $globals;
4407871a 530
a0fce0c6
SJ
531 $mailer = new PlMailer();
532 $mailer->setSubject($this->_mail_subj());
533 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
534 $mailer->addTo("\"{$this->profile->fullName()}\" <{$this->profileOwner->bestEmail()}>");
535 $mailer->addCc("validation+{$this->type}@{$globals->mail->domain}");
536 $body = ($this->profile->isFemale() ? "Chère camarade,\n\n" : "Cher camarade,\n\n")
537 . $this->_mail_body($isok)
538 . (Env::has('comm') ? "\n\n" . Env::v('comm') : '')
539 . "\n\nCordialement,\n-- \nL'équipe de Polytechnique.org\n"
540 . $this->_mail_ps($isok);
541 $mailer->setTxtBody(wordwrap($body));
542 $mailer->send();
543 }
4407871a
SJ
544 }
545
546 // }}}
547 // {{{ function get_typed_request()
548
549 /**
550 * @param $pid: profile's pid
551 * @param $type: request's type
552 * @param $stamp: request's timestamp
553 *
554 * Should only be used to retrieve an object in the databse with Validate::get_typed_request(...)
555 */
556 static public function get_typed_request($pid, $type, $stamp = -1)
557 {
558 if ($stamp == -1) {
559 $res = XDB::query('SELECT data
560 FROM requests
561 WHERE pid = {?} and type = {?}',
562 $pid, $type);
563 } else {
564 $res = XDB::query('SELECT data, DATE_FORMAT(stamp, "%Y%m%d%H%i%s")
565 FROM requests
566 WHERE pid = {?} AND type = {?} and stamp = {?}',
567 $pid, $type, $stamp);
568 }
569 if ($result = $res->fetchOneCell()) {
570 $result = Validate::unserialize($result);
571 } else {
572 $result = false;
573 }
574 return $result;
575 }
576
577 // }}}
578 // {{{ function get_request_by_id()
579
580 static public function get_request_by_id($id)
581 {
582 list($pid, $type, $stamp) = explode('_', $id, 3);
583 return Validate::get_typed_request($pid, $type, $stamp);
584 }
585
586 // }}}
587 // {{{ function get_typed_requests()
588
589 /** Same as get_typed_request() but return an array of objects.
590 */
591 static public function get_typed_requests($pid, $type)
592 {
593 $res = XDB::iterRow('SELECT data
594 FROM requests
595 WHERE pid = {?} and type = {?}',
596 $pid, $type);
597 $array = array();
598 while (list($data) = $res->next()) {
599 $array[] = Validate::unserialize($data);
600 }
601 return $array;
602 }
603
604 // }}}
605 // {{{ function get_typed_requests_count()
606
607 /** Same as get_typed_requests() but returns the count of available requests.
608 */
609 static public function get_typed_requests_count($pid, $type)
610 {
611 $res = XDB::query('SELECT COUNT(data)
612 FROM requests
613 WHERE pid = {?} and type = {?}',
614 $pid, $type);
615 return $res->fetchOneCell();
616 }
617
618 // }}}
616918d0 619 // {{{ function id()
4407871a
SJ
620
621 public function id()
622 {
d134ddda 623 return $this->profile->id() . '_' . $this->type . '_' . $this->stamp;
4407871a
SJ
624 }
625
626 // }}}
0337d704 627}
628
4407871a
SJ
629foreach (glob(dirname(__FILE__) . '/validations/*.inc.php') as $file) {
630 require_once $file;
0337d704 631}
632
a7de4ef7 633/* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker enc=utf-8: */
0337d704 634?>