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);
27 /** Virtual class to adapt for every possible implementation.
29 abstract class Validate
37 // Enable the refuse button.
38 public $refuse = true
;
41 public $comments = Array();
42 // Validations rules: comments for administrators.
43 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.';
49 * @param $_user: user object that required the validation.
50 * @param $_unique: set to false if a profile can have multiple requests of this type.
51 * @param $_type: request's type.
53 public function __construct(User
&$_user, $_unique, $_type)
55 $this->user
= &$_user;
56 $this->stamp
= date('YmdHis');
57 $this->unique
= $_unique;
59 $this->promo
= $this->user
->promo();
63 // {{{ function submit()
65 /** Sends data to validation.
66 * It also deletes multiple requests for a couple (profile, type)
67 * when $this->unique is set to true.
69 public function submit()
72 XDB
::execute('DELETE FROM requests
73 WHERE uid = {?} AND type = {?}',
74 $this->user
->id(), $this->type
);
77 $this->stamp
= date('YmdHis');
78 XDB
::execute('INSERT INTO requests (uid, type, data, stamp)
79 VALUES ({?}, {?}, {?}, {?})',
80 $this->user
->id(), $this->type
, $this, $this->stamp
);
83 $globals->updateNbValid();
88 // {{{ function update()
90 protected function update()
92 XDB
::execute('UPDATE requests
93 SET data = {?}, stamp = stamp
94 WHERE uid = {?} AND type = {?} AND stamp = {?}',
95 $this, $this->user
->id(), $this->type
, $this->stamp
);
100 // {{{ function clean()
102 /** Deletes request from 'requests' table.
103 * If $this->unique is set, it deletes every requests of this type.
105 public function clean()
110 $success = XDB
::execute('DELETE FROM requests
111 WHERE uid = {?} AND type = {?}',
112 $this->user
->id(), $this->type
);
114 $success = XDB
::execute('DELETE FROM requests
115 WHERE uid = {?} AND type = {?} AND stamp = {?}',
116 $this->user
->id(), $this->type
, $this->stamp
);
118 $globals->updateNbValid();
123 // {{{ function handle_formu()
125 /** Handles form validation.
127 public function handle_formu()
129 if (Env
::has('delete')) {
131 $this->trigSuccess('Requête supprimée.');
136 if (Env
::has('edit')) {
137 if ($this->handle_editor()) {
139 $this->trigSuccess('Requête mise à jour.');
146 if (Env
::has('hold') && Env
::has('comm')) {
147 $formid = Env
::i('formid');
148 foreach ($this->comments
as $comment) {
149 if ($comment[2] === $formid) {
153 if (!strlen(trim(Env
::v('comm')))) {
156 $this->comments
[] = array(S
::user()->login(), Env
::v('comm'), $formid);
158 // Sends email to our hotline.
160 $mailer = new PlMailer();
161 $mailer->setSubject("Commentaires de validation {$this->type}");
162 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
163 $mailer->addTo($globals->core
->admin_email
);
165 $body = "Validation {$this->type} pour {$this->user->login()}\n\n"
166 . S
::user()->login() . " a ajouté le commentaire :\n\n"
167 . Env
::v('comm') . "\n\n"
168 . "cf la discussion sur : " . $globals->baseurl
. "/admin/validate";
170 $mailer->setTxtBody(wordwrap($body));
174 $this->trigSuccess('Commentaire ajouté.');
178 if (Env
::has('accept')) {
179 if ($this->commit()) {
180 $this->sendmail(true
);
182 $this->trigSuccess('Email de validation envoyé');
185 $this->trigError('Erreur lors de la validation');
190 if (Env
::has('refuse')) {
191 if (Env
::v('comm')) {
192 $this->sendmail(false
);
194 $this->trigSuccess('Email de refus envoyé.');
197 $this->trigError('Pas de motivation pour le refus !!!');
205 // {{{ function sendmail
207 protected function sendmail($isok)
210 $mailer = new PlMailer();
211 $mailer->setSubject($this->_mail_subj());
212 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
213 $mailer->addTo("\"{$this->user->fullName()}\" <{$this->user->bestEmail()}>");
214 $mailer->addCc("validation+{$this->type}@{$globals->mail->domain}");
216 $body = ($this->user
->isFemale() ?
"Chère camarade,\n\n" : "Cher camarade,\n\n")
217 . $this->_mail_body($isok)
218 . (Env
::has('comm') ?
"\n\n" . Env
::v('comm') : '')
219 . "\n\nCordialement,\n-- \nL'équipe de Polytechnique.org\n"
220 . $this->_mail_ps($isok);
222 $mailer->setTxtBody(wordwrap($body));
227 // {{{ function trig()
229 protected function trigError($msg)
231 Platal
::page()->trigError($msg);
234 protected function trigWarning($msg)
236 Platal
::page()->trigWarning($msg);
239 protected function trigSuccess($msg)
241 Platal
::page()->trigSuccess($msg);
245 // {{{ function get_typed_request()
248 * @param $pid: profile's pid
249 * @param $type: request's type
250 * @param $stamp: request's timestamp
252 * Should only be used to retrieve an object in the databse with Validate::get_typed_request(...)
254 static public function get_typed_request($uid, $type, $stamp = -1)
257 $res = XDB
::query('SELECT data
259 WHERE uid = {?} and type = {?}',
262 $res = XDB
::query('SELECT data, DATE_FORMAT(stamp, "%Y%m%d%H%i%s")
264 WHERE uid = {?} AND type = {?} and stamp = {?}',
265 $uid, $type, $stamp);
267 if ($result = $res->fetchOneCell()) {
268 $result = Validate
::unserialize($result);
276 // {{{ function get_request_by_id()
278 static public function get_request_by_id($id)
280 list($uid, $type, $stamp) = explode('_', $id, 3);
281 return Validate
::get_typed_request($uid, $type, $stamp);
285 // {{{ function get_typed_requests()
287 /** Same as get_typed_request() but return an array of objects.
289 static public function get_typed_requests($uid, $type)
291 $res = XDB
::iterRow('SELECT data
293 WHERE uid = {?} and type = {?}',
296 while (list($data) = $res->next()) {
297 $array[] = Validate
::unserialize($data);
303 // {{{ function get_typed_requests_count()
305 /** Same as get_typed_requests() but return the count of available requests.
307 static public function get_typed_requests_count($uid, $type)
309 $res = XDB
::query('SELECT COUNT(data)
311 WHERE uid = {?} and type = {?}',
313 return $res->fetchOneCell();
317 // {{{ function _mail_body
319 abstract protected function _mail_body($isok);
322 // {{{ function _mail_subj
324 abstract protected function _mail_subj();
327 // {{{ function _mail_ps
329 protected function _mail_ps($isok)
335 // {{{ function commit()
337 /** Inserts data in database.
339 abstract public function commit();
342 // {{{ function formu()
344 /** Retunrs the name of the form's template. */
345 abstract public function formu();
348 // {{{ function editor()
350 /** Returns the name of the edition form's template. */
351 public function editor()
357 // {{{ function answers()
359 /** Automatic answers table for this type of validation. */
360 public function answers()
362 static $answers_table;
363 if (!isset($answers_table[$this->type
])) {
364 $r = XDB
::query('SELECT id, title, answer
365 FROM requests_answers
366 WHERE category = {?}',
368 $answers_table[$this->type
] = $r->fetchAllAssoc();
370 return $answers_table[$this->type
];
378 return $this->user
->id() . '_' . $this->type
. '_' . $this->stamp
;
382 // {{{ function ruleText()
384 public function ruleText()
386 return str_replace('\'', '\\\'', $this->rules
);
390 // {{{ function unserialize()
392 public static function unserialize($data)
394 return unserialize($data);
399 /** Return an iterator over the validation concerning the given type
400 * and the given user.
402 * @param type The type of the validations to fetch, null mean "any type"
403 * @param applyTo A User or a Profile object the validation applies to.
405 public static function iterate($type = null
, $applyTo = null
)
407 function toValidation($elt)
409 list($result, $stamp) = $elt;
410 $result = Validate
::unserialize($result);
411 $result->stamp
= $stamp;
417 $where[] = XDB
::format('type = {?}', $type);
420 if ($applyTo instanceof User
) {
421 $where[] = XDB
::format('uid = {?}', $applyTo->id());
422 } else if ($applyTo instanceof Profile
) {
423 $where[] = XDB
::format('pid = {?}', $applyTo->id());
426 if (!empty($where)) {
427 $where = 'WHERE ' . implode('AND', $where);
429 $it = XDB
::iterRow('SELECT data, DATE_FORMAT(stamp, "%Y%m%d%H%i%s")
433 return PlIteratorUtils
::map($it, 'toValidation');
437 /** Virtual class for profile related validation.
439 abstract class ProfileValidate
extends Validate
444 public $profileOwner;
445 public $userIsProfileOwner;
446 public $ownerIsRegistered;
452 * @param $_user: user object that required the validation.
453 * @param $_profile: profile object that is to be modified,
454 * its owner (if exists) can differ from $_user.
455 * @param $_unique: set to false if a profile can have multiple requests of this type.
456 * @param $_type: request's type.
458 public function __construct(User
&$_user, Profile
&$_profile, $_unique, $_type)
460 parent
::__construct($_user, $_unique, $_type);
461 $this->profile
= &$_profile;
462 $this->profileOwner
= $this->profile
->owner();
463 $this->userIsProfileOwner
= (!is_null($this->profileOwner
)
464 && $this->profileOwner
->id() == $this->user
->id());
465 $this->ownerIsRegistered
= $this->profile
->isActive();
469 // {{{ function submit()
471 /** Sends data to validation.
472 * It also deletes multiple requests for a couple (profile, type)
473 * when $this->unique is set to true.
475 public function submit()
478 XDB
::execute('DELETE FROM requests
479 WHERE pid = {?} AND type = {?}',
480 $this->profile
->id(), $this->type
);
483 $this->stamp
= date('YmdHis');
484 XDB
::execute('INSERT INTO requests (uid, pid, type, data, stamp)
485 VALUES ({?}, {?}, {?}, {?}, {?})',
486 $this->user
->id(), $this->profile
->id(), $this->type
, $this, $this->stamp
);
489 $globals->updateNbValid();
494 // {{{ function update()
496 protected function update()
498 XDB
::execute('UPDATE requests
499 SET data = {?}, stamp = stamp
500 WHERE pid = {?} AND type = {?} AND stamp = {?}',
501 $this, $this->profile
->id(), $this->type
, $this->stamp
);
506 // {{{ function clean()
508 /** Deletes request from 'requests' table.
509 * If $this->unique is set, it deletes every requests of this type.
511 public function clean()
516 $success = XDB
::execute('DELETE FROM requests
517 WHERE pid = {?} AND type = {?}',
518 $this->profile
->id(), $this->type
);
520 $success = XDB
::execute('DELETE FROM requests
521 WHERE pid = {?} AND type = {?} AND stamp = {?}',
522 $this->profile
->id(), $this->type
, $this->stamp
);
524 $globals->updateNbValid();
529 // {{{ function sendmail
531 protected function sendmail($isok)
533 // Only sends email if the profile's owner exists and is registered.
534 if ($this->ownerIsRegistered
) {
537 $mailer = new PlMailer();
538 $mailer->setSubject($this->_mail_subj());
539 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
540 $mailer->addTo("\"{$this->profile->fullName()}\" <{$this->profileOwner->bestEmail()}>");
541 $mailer->addCc("validation+{$this->type}@{$globals->mail->domain}");
542 $body = ($this->profile
->isFemale() ?
"Chère camarade,\n\n" : "Cher camarade,\n\n")
543 . $this->_mail_body($isok)
544 . (Env
::has('comm') ?
"\n\n" . Env
::v('comm') : '')
545 . "\n\nCordialement,\n-- \nL'équipe de Polytechnique.org\n"
546 . $this->_mail_ps($isok);
547 $mailer->setTxtBody(wordwrap($body));
553 // {{{ function get_typed_request()
556 * @param $pid: profile's pid
557 * @param $type: request's type
558 * @param $stamp: request's timestamp
560 * Should only be used to retrieve an object in the databse with Validate::get_typed_request(...)
562 static public function get_typed_request($pid, $type, $stamp = -1)
565 $res = XDB
::query('SELECT data
567 WHERE pid = {?} and type = {?}',
570 $res = XDB
::query('SELECT data, DATE_FORMAT(stamp, "%Y%m%d%H%i%s")
572 WHERE pid = {?} AND type = {?} and stamp = {?}',
573 $pid, $type, $stamp);
575 if ($result = $res->fetchOneCell()) {
576 $result = Validate
::unserialize($result);
584 // {{{ function get_request_by_id()
586 static public function get_request_by_id($id)
588 list($pid, $type, $stamp) = explode('_', $id, 3);
589 return Validate
::get_typed_request($pid, $type, $stamp);
593 // {{{ function get_typed_requests()
595 /** Same as get_typed_request() but return an array of objects.
597 static public function get_typed_requests($pid, $type)
599 $res = XDB
::iterRow('SELECT data
601 WHERE pid = {?} and type = {?}',
604 while (list($data) = $res->next()) {
605 $array[] = Validate
::unserialize($data);
611 // {{{ function get_typed_requests_count()
613 /** Same as get_typed_requests() but returns the count of available requests.
615 static public function get_typed_requests_count($pid, $type)
617 $res = XDB
::query('SELECT COUNT(data)
619 WHERE pid = {?} and type = {?}',
621 return $res->fetchOneCell();
629 return $this->profile
->id() . '_' . $this->type
. '_' . $this->stamp
;
635 foreach (glob(dirname(__FILE__
) . '/validations/*.inc.php') as $file) {
639 /* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker enc=utf-8: */