2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 define('SIZE_MAX', 32768);
25 require_once $globals->spoolroot
. '/core/classes/xdb.php';
28 * Iterator class, that lists objects through the database
30 class ValidateIterator
extends XOrgDBIterator
34 public function __construct()
36 parent
::__construct('SELECT data, DATE_FORMAT(stamp, "%Y%m%d%H%i%s")
38 ORDER BY stamp', MYSQL_NUM
);
42 // {{{ function next()
44 public function next()
46 if (list($result, $stamp) = parent
::next()) {
47 $result = Validate
::unserialize($result);
48 $result->stamp
= $stamp;
58 /** Virtual class to adapt for every possible implementation.
60 abstract class Validate
68 // Enable the refuse button.
69 public $refuse = true
;
72 public $comments = Array();
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.';
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.
84 public function __construct(User
&$_user, $_unique, $_type)
86 $this->user
= &$_user;
87 $this->stamp
= date('YmdHis');
88 $this->unique
= $_unique;
90 $this->promo
= $this->user
->promo();
94 // {{{ function submit()
96 /** Sends data to validation.
97 * It also deletes multiple requests for a couple (profile, type)
98 * when $this->unique is set to true.
100 public function submit()
103 XDB
::execute('DELETE FROM requests
104 WHERE uid = {?} AND type = {?}',
105 $this->user
->id(), $this->type
);
108 $this->stamp
= date('YmdHis');
109 XDB
::execute('INSERT INTO requests (uid, type, data, stamp)
110 VALUES ({?}, {?}, {?}, {?})',
111 $this->user
->id(), $this->type
, $this, $this->stamp
);
114 $globals->updateNbValid();
119 // {{{ function update()
121 protected function update()
123 XDB
::execute('UPDATE requests
124 SET data = {?}, stamp = stamp
125 WHERE uid = {?} AND type = {?} AND stamp = {?}',
126 $this, $this->user
->id(), $this->type
, $this->stamp
);
131 // {{{ function clean()
133 /** Deletes request from 'requests' table.
134 * If $this->unique is set, it deletes every requests of this type.
136 public function clean()
141 $success = XDB
::execute('DELETE FROM requests
142 WHERE uid = {?} AND type = {?}',
143 $this->user
->id(), $this->type
);
145 $success = XDB
::execute('DELETE FROM requests
146 WHERE uid = {?} AND type = {?} AND stamp = {?}',
147 $this->user
->id(), $this->type
, $this->stamp
);
149 $globals->updateNbValid();
154 // {{{ function handle_formu()
156 /** Handles form validation.
158 public function handle_formu()
160 if (Env
::has('delete')) {
162 $this->trigSuccess('Requête supprimée.');
167 if (Env
::has('edit')) {
168 if ($this->handle_editor()) {
170 $this->trigSuccess('Requête mise à jour.');
177 if (Env
::has('hold') && Env
::has('comm')) {
178 $formid = Env
::i('formid');
179 foreach ($this->comments
as $comment) {
180 if ($comment[2] === $formid) {
184 if (!strlen(trim(Env
::v('comm')))) {
187 $this->comments
[] = array(S
::user()->login(), Env
::v('comm'), $formid);
189 // Sends email to our hotline.
191 $mailer = new PlMailer();
192 $mailer->setSubject("Commentaires de validation {$this->type}");
193 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
194 $mailer->addTo($globals->core
->admin_email
);
196 $body = "Validation {$this->type} pour {$this->user->login()}\n\n"
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";
201 $mailer->setTxtBody(wordwrap($body));
205 $this->trigSuccess('Commentaire ajouté.');
209 if (Env
::has('accept')) {
210 if ($this->commit()) {
211 $this->sendmail(true
);
213 $this->trigSuccess('Email de validation envoyé');
216 $this->trigError('Erreur lors de la validation');
221 if (Env
::has('refuse')) {
222 if (Env
::v('comm')) {
223 $this->sendmail(false
);
225 $this->trigSuccess('Email de refus envoyé.');
228 $this->trigError('Pas de motivation pour le refus !!!');
236 // {{{ function sendmail
238 protected function sendmail($isok)
241 $mailer = new PlMailer();
242 $mailer->setSubject($this->_mail_subj());
243 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
244 $mailer->addTo("\"{$this->user->fullName()}\" <{$this->user->bestEmail()}>");
245 $mailer->addCc("validation+{$this->type}@{$globals->mail->domain}");
247 $body = ($this->user
->isFemale() ?
"Chère camarade,\n\n" : "Cher camarade,\n\n")
248 . $this->_mail_body($isok)
249 . (Env
::has('comm') ?
"\n\n" . Env
::v('comm') : '')
250 . "\n\nCordialement,\n-- \nL'équipe de Polytechnique.org\n"
251 . $this->_mail_ps($isok);
253 $mailer->setTxtBody(wordwrap($body));
258 // {{{ function trig()
260 protected function trigError($msg)
262 Platal
::page()->trigError($msg);
265 protected function trigWarning($msg)
267 Platal
::page()->trigWarning($msg);
270 protected function trigSuccess($msg)
272 Platal
::page()->trigSuccess($msg);
276 // {{{ function get_typed_request()
279 * @param $pid: profile's pid
280 * @param $type: request's type
281 * @param $stamp: request's timestamp
283 * Should only be used to retrieve an object in the databse with Validate::get_typed_request(...)
285 static public function get_typed_request($uid, $type, $stamp = -1)
288 $res = XDB
::query('SELECT data
290 WHERE uid = {?} and type = {?}',
293 $res = XDB
::query('SELECT data, DATE_FORMAT(stamp, "%Y%m%d%H%i%s")
295 WHERE uid = {?} AND type = {?} and stamp = {?}',
296 $uid, $type, $stamp);
298 if ($result = $res->fetchOneCell()) {
299 $result = Validate
::unserialize($result);
307 // {{{ function get_request_by_id()
309 static public function get_request_by_id($id)
311 list($uid, $type, $stamp) = explode('_', $id, 3);
312 return Validate
::get_typed_request($uid, $type, $stamp);
316 // {{{ function get_typed_requests()
318 /** Same as get_typed_request() but return an array of objects.
320 static public function get_typed_requests($uid, $type)
322 $res = XDB
::iterRow('SELECT data
324 WHERE uid = {?} and type = {?}',
327 while (list($data) = $res->next()) {
328 $array[] = Validate
::unserialize($data);
334 // {{{ function get_typed_requests_count()
336 /** Same as get_typed_requests() but return the count of available requests.
338 static public function get_typed_requests_count($uid, $type)
340 $res = XDB
::query('SELECT COUNT(data)
342 WHERE uid = {?} and type = {?}',
344 return $res->fetchOneCell();
348 // {{{ function _mail_body
350 abstract protected function _mail_body($isok);
353 // {{{ function _mail_subj
355 abstract protected function _mail_subj();
358 // {{{ function _mail_ps
360 protected function _mail_ps($isok)
366 // {{{ function commit()
368 /** Inserts data in database.
370 abstract public function commit();
373 // {{{ function formu()
375 /** Retunrs the name of the form's template. */
376 abstract public function formu();
379 // {{{ function editor()
381 /** Returns the name of the edition form's template. */
382 public function editor()
388 // {{{ function answers()
390 /** Automatic answers table for this type of validation. */
391 public function answers()
393 static $answers_table;
394 if (!isset($answers_table[$this->type
])) {
395 $r = XDB
::query('SELECT id, title, answer
396 FROM requests_answers
397 WHERE category = {?}',
399 $answers_table[$this->type
] = $r->fetchAllAssoc();
401 return $answers_table[$this->type
];
409 return $this->user
->id() . '_' . $this->type
. '_' . $this->stamp
;
413 // {{{ function ruleText()
415 public function ruleText()
417 return str_replace('\'', '\\\'', $this->rules
);
421 // {{{ function unserialize()
423 public static function unserialize($data)
425 return unserialize($data);
431 /** Virtual class for profile related validation.
433 abstract class ProfileValidate
extends Validate
438 public $profileOwner;
439 public $userIsProfileOwner;
440 public $ownerIsRegistered;
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.
452 public function __construct(User
&$_user, Profile
&$_profile, $_unique, $_type)
454 parent
::__construct($_user, $_unique, $_type);
455 $this->profile
= &$_profile;
456 $this->profileOwner
= $this->profile
->owner();
457 $this->userIsProfileOwner
= (!is_null($this->profileOwner
)
458 && $this->profileOwner
->id() == $this->user
->id());
459 $this->ownerIsRegistered
= $this->profile
->isActive();
463 // {{{ function submit()
465 /** Sends data to validation.
466 * It also deletes multiple requests for a couple (profile, type)
467 * when $this->unique is set to true.
469 public function submit()
472 XDB
::execute('DELETE FROM requests
473 WHERE pid = {?} AND type = {?}',
474 $this->profile
->id(), $this->type
);
477 $this->stamp
= date('YmdHis');
478 XDB
::execute('INSERT INTO requests (uid, pid, type, data, stamp)
479 VALUES ({?}, {?}, {?}, {?}, {?})',
480 $this->user
->id(), $this->profile
->id(), $this->type
, $this, $this->stamp
);
483 $globals->updateNbValid();
488 // {{{ function update()
490 protected function update()
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
);
500 // {{{ function clean()
502 /** Deletes request from 'requests' table.
503 * If $this->unique is set, it deletes every requests of this type.
505 public function clean()
510 $success = XDB
::execute('DELETE FROM requests
511 WHERE pid = {?} AND type = {?}',
512 $this->profile
->id(), $this->type
);
514 $success = XDB
::execute('DELETE FROM requests
515 WHERE pid = {?} AND type = {?} AND stamp = {?}',
516 $this->profile
->id(), $this->type
, $this->stamp
);
518 $globals->updateNbValid();
523 // {{{ function sendmail
525 protected function sendmail($isok)
527 // Only sends email if the profile's owner exists and is registered.
528 if ($this->ownerIsRegistered
) {
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));
547 // {{{ function get_typed_request()
550 * @param $pid: profile's pid
551 * @param $type: request's type
552 * @param $stamp: request's timestamp
554 * Should only be used to retrieve an object in the databse with Validate::get_typed_request(...)
556 static public function get_typed_request($pid, $type, $stamp = -1)
559 $res = XDB
::query('SELECT data
561 WHERE pid = {?} and type = {?}',
564 $res = XDB
::query('SELECT data, DATE_FORMAT(stamp, "%Y%m%d%H%i%s")
566 WHERE pid = {?} AND type = {?} and stamp = {?}',
567 $pid, $type, $stamp);
569 if ($result = $res->fetchOneCell()) {
570 $result = Validate
::unserialize($result);
578 // {{{ function get_request_by_id()
580 static public function get_request_by_id($id)
582 list($pid, $type, $stamp) = explode('_', $id, 3);
583 return Validate
::get_typed_request($pid, $type, $stamp);
587 // {{{ function get_typed_requests()
589 /** Same as get_typed_request() but return an array of objects.
591 static public function get_typed_requests($pid, $type)
593 $res = XDB
::iterRow('SELECT data
595 WHERE pid = {?} and type = {?}',
598 while (list($data) = $res->next()) {
599 $array[] = Validate
::unserialize($data);
605 // {{{ function get_typed_requests_count()
607 /** Same as get_typed_requests() but returns the count of available requests.
609 static public function get_typed_requests_count($pid, $type)
611 $res = XDB
::query('SELECT COUNT(data)
613 WHERE pid = {?} and type = {?}',
615 return $res->fetchOneCell();
623 return $this->profile
->id() . '_' . $this->type
. '_' . $this->stamp
;
629 foreach (glob(dirname(__FILE__
) . '/validations/*.inc.php') as $file) {
633 /* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker enc=utf-8: */