Merge commit 'origin/fusionax' into account
[platal.git] / include / validations.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
179afa7f 3 * Copyright (C) 2003-2008 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
612a2d8a 34 public function __construct ()
0337d704 35 {
96b00435 36 parent::__construct('SELECT data, DATE_FORMAT(stamp, "%Y%m%d%H%i%s") FROM requests ORDER BY stamp', MYSQL_NUM);
0337d704 37 }
38
39 // }}}
40 // {{{ function next()
41
612a2d8a 42 public function next ()
0337d704 43 {
44 if (list($result, $stamp) = parent::next()) {
33b47675 45 $result = Validate::unserialize($result);
0337d704 46 $result->stamp = $stamp;
47 return($result);
48 } else {
49 return null;
50 }
51 }
52
53 // }}}
54}
55
a7de4ef7 56/** classe "virtuelle" à dériver pour chaque nouvelle implémentation
0337d704 57 */
612a2d8a 58abstract class Validate
0337d704 59{
60 // {{{ properties
eaf30d86 61
5daf68f6 62 public $user;
612a2d8a 63
64 public $stamp;
65 public $unique;
0337d704 66 // enable the refuse button
612a2d8a 67 public $refuse = true;
eaf30d86 68
612a2d8a 69 public $type;
70 public $comments = Array();
0337d704 71 // the validations rules : comments for admins
612a2d8a 72 public $rules = "Mieux vaut laisser une demande de validation à un autre admin que de valider une requête illégale ou que de refuser une demande légitime";
0337d704 73
74 // }}}
75 // {{{ constructor
eaf30d86 76
0337d704 77 /** constructeur
5daf68f6 78 * @param $_user user object
a7de4ef7 79 * @param $_unique requête pouvant être multiple ou non
80 * @param $_type type de la donnée comme dans le champ type de x4dat.requests
0337d704 81 */
532c06cf 82 public function __construct(User &$_user, $_unique, $_type)
0337d704 83 {
532c06cf 84 $this->user = &$_user;
0337d704 85 $this->stamp = date('YmdHis');
86 $this->unique = $_unique;
87 $this->type = $_type;
d1319488 88 $res = XDB::query("SELECT promo
fb2c09c9 89 FROM profile_display
d1319488 90 WHERE pid={?}", $this->user->id());
fb2c09c9 91 $this->promo = $res->fetchOneCell();
0337d704 92 }
eaf30d86 93
0337d704 94 // }}}
95 // {{{ function submit()
96
a7de4ef7 97 /** fonction à utiliser pour envoyer les données à la modération
0337d704 98 * cette fonction supprimme les doublons sur un couple ($user,$type) si $this->unique est vrai
99 */
612a2d8a 100 public function submit()
0337d704 101 {
0337d704 102 if ($this->unique) {
5daf68f6 103 XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?}', $this->user->id(), $this->type);
0337d704 104 }
eaf30d86 105
0337d704 106 $this->stamp = date('YmdHis');
08cce2ff 107 XDB::execute('INSERT INTO requests (user_id, type, data, stamp) VALUES ({?}, {?}, {?}, {?})',
5daf68f6 108 $this->user->id(), $this->type, $this, $this->stamp);
0337d704 109
84868ee9 110 global $globals;
ebfdf077 111 $globals->updateNbValid();
0337d704 112 return true;
113 }
114
115 // }}}
116 // {{{ function update()
117
612a2d8a 118 protected function update()
0337d704 119 {
08cce2ff 120 XDB::execute('UPDATE requests SET data={?}, stamp=stamp
612a2d8a 121 WHERE user_id={?} AND type={?} AND stamp={?}',
5daf68f6 122 $this, $this->user->id(), $this->type, $this->stamp);
0337d704 123 return true;
124 }
125
126 // }}}
127 // {{{ function clean()
eaf30d86 128
a7de4ef7 129 /** fonction à utiliser pour nettoyer l'entrée de la requête dans la table requests
130 * attention, tout est supprimé si c'est un unique
0337d704 131 */
d17761d8 132 public function clean()
0337d704 133 {
95e36b0f
SJ
134 global $globals;
135
0337d704 136 if ($this->unique) {
84868ee9 137 $success = XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?}',
5daf68f6 138 $this->user->id(), $this->type);
0337d704 139 } else {
84868ee9 140 $success = XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?} AND stamp={?}',
5daf68f6 141 $this->user->id(), $this->type, $this->stamp);
0337d704 142 }
ebfdf077 143 $globals->updateNbValid();
84868ee9 144 return $success;
0337d704 145 }
146
147 // }}}
148 // {{{ function handle_formu()
eaf30d86 149
cb04af2c 150 /** fonction à réaliser en cas de validation du formulaire
0337d704 151 */
612a2d8a 152 public function handle_formu()
0337d704 153 {
154 if (Env::has('delete')) {
155 $this->clean();
a7d35093 156 $this->trigSuccess('Requête supprimée');
0337d704 157 return true;
158 }
159
a7de4ef7 160 // mise à jour des informations
6aa01fed 161 if (Env::has('edit')) {
162 if ($this->handle_editor()) {
163 $this->update();
a7d35093 164 $this->trigSuccess('Requête mise à jour');
6aa01fed 165 return true;
166 }
167 return false;
168 }
169
0337d704 170 // ajout d'un commentaire
171 if (Env::has('hold') && Env::has('comm')) {
4791ff77 172 $formid = Env::i('formid');
173 foreach ($this->comments as $comment) {
174 if ($comment[2] === $formid) {
175 return true;
176 }
177 }
90608d68 178 if (!strlen(trim(Env::v('comm')))) {
179 return true;
180 }
5daf68f6 181 $this->comments[] = Array(S::user()->login(), Env::v('comm'), $formid);
0337d704 182
a7de4ef7 183 // envoi d'un mail à hotliners
0337d704 184 global $globals;
b9c53090 185 $mailer = new PlMailer();
0337d704 186 $mailer->setSubject("Commentaires de validation {$this->type}");
187 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
b9c53090 188 $mailer->addTo($globals->core->admin_email);
0337d704 189
53092def 190 $body = "Validation {$this->type} pour {$this->user->login()}\n\n"
5daf68f6
VZ
191 . S::user()->login() . " a ajouté le commentaire :\n\n"
192 . Env::v('comm') . "\n\n"
193 . "cf la discussion sur : " . $globals->baseurl . "/admin/validate";
0337d704 194
195 $mailer->setTxtBody(wordwrap($body));
196 $mailer->send();
197
198 $this->update();
a7d35093 199 $this->trigSuccess('Commentaire ajouté');
0337d704 200 return true;
201 }
202
203 if (Env::has('accept')) {
204 if ($this->commit()) {
205 $this->sendmail(true);
206 $this->clean();
faefdbb7 207 $this->trigSuccess('Email de validation envoyé');
0337d704 208 return true;
209 } else {
a7d35093 210 $this->trigError('Erreur lors de la validation');
0337d704 211 return false;
212 }
213 }
214
215 if (Env::has('refuse')) {
5e2307dc 216 if (Env::v('comm')) {
0337d704 217 $this->sendmail(false);
218 $this->clean();
faefdbb7 219 $this->trigSuccess('Email de refus envoyé');
0337d704 220 return true;
221 } else {
a7d35093 222 $this->trigError('pas de motivation pour le refus !!!');
0337d704 223 }
224 }
225
226 return false;
227 }
228
229 // }}}
230 // {{{ function sendmail
231
612a2d8a 232 protected function sendmail($isok)
0337d704 233 {
234 global $globals;
1e33266a 235 $mailer = new PlMailer();
0337d704 236 $mailer->setSubject($this->_mail_subj());
237 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
5daf68f6 238 $mailer->addTo("\"{$this->user->fullName()}\" <{$this->user->bestEmail()}>");
0337d704 239 $mailer->addCc("validation+{$this->type}@{$globals->mail->domain}");
240
5daf68f6 241 $body = ($this->user->isFemale() ? "Chère camarade,\n\n" : "Cher camarade,\n\n")
0337d704 242 . $this->_mail_body($isok)
5e2307dc 243 . (Env::has('comm') ? "\n\n".Env::v('comm') : '')
780bc68d 244 . "\n\nCordialement,\n\n-- \nL'équipe de Polytechnique.org\n";
0337d704 245
246 $mailer->setTxtBody(wordwrap($body));
247 $mailer->send();
248 }
249
250 // }}}
251 // {{{ function trig()
eaf30d86 252
a7d35093 253 protected function trigError($msg)
612a2d8a 254 {
d7610c35 255 Platal::page()->trigError($msg);
a7d35093
FB
256 }
257
258 protected function trigWarning($msg)
259 {
d7610c35 260 Platal::page()->trigWarning($msg);
a7d35093
FB
261 }
262
263 protected function trigSuccess($msg)
264 {
d7610c35 265 Platal::page()->trigSuccess($msg);
0337d704 266 }
eaf30d86 267
0337d704 268 // }}}
20d7932b 269 // {{{ function get_typed_request()
0337d704 270
a7de4ef7 271 /** fonction statique qui renvoie la requête de l'utilisateur d'id $uidau timestamp $t
272 * @param $uid l'id de l'utilisateur concerné
273 * @param $type le type de la requête
274 * @param $stamp le timestamp de la requête
0337d704 275 *
276 * XXX fonction "statique" XXX
a7de4ef7 277 * à utiliser uniquement pour récupérer un objet dans la BD avec Validate::get_typed_request(...)
0337d704 278 */
612a2d8a 279 static public function get_typed_request($uid, $type, $stamp = -1)
0337d704 280 {
0337d704 281 if ($stamp == -1) {
08cce2ff 282 $res = XDB::query('SELECT data FROM requests WHERE user_id={?} and type={?}', $uid, $type);
0337d704 283 } else {
96b00435 284 $res = XDB::query('SELECT data, DATE_FORMAT(stamp, "%Y%m%d%H%i%s") FROM requests WHERE user_id={?} AND type={?} and stamp={?}', $uid, $type, $stamp);
0337d704 285 }
286 if ($result = $res->fetchOneCell()) {
33b47675 287 $result = Validate::unserialize($result);
0337d704 288 } else {
289 $result = false;
290 }
291 return($result);
292 }
293
294 // }}}
02838718 295 // {{{ function get_request_by_id()
296
297 static public function get_request_by_id($id)
298 {
299 list($uid, $type, $stamp) = explode('_', $id, 3);
300 return Validate::get_typed_request($uid, $type, $stamp);
301 }
302
303 // }}}
5b0dc389 304 // {{{ function get_typed_requests()
305
306 /** same as get_typed_request() but return an array of objects
307 */
612a2d8a 308 static public function get_typed_requests($uid, $type)
5b0dc389 309 {
310 $res = XDB::iterRow('SELECT data FROM requests WHERE user_id={?} and type={?}', $uid, $type);
311 $array = array();
312 while (list($data) = $res->next()) {
33b47675 313 $array[] = Validate::unserialize($data);
5b0dc389 314 }
315 return $array;
316 }
317
318 // }}}
bb0727ea
VZ
319 // {{{ function get_typed_requests_count()
320
321 /** same as get_typed_requests() but return the count of available requests.
322 */
323 static public function get_typed_requests_count($uid, $type)
324 {
325 $res = XDB::query('SELECT COUNT(data) FROM requests WHERE user_id={?} and type={?}', $uid, $type);
326 return $res->fetchOneCell();
327 }
328
329 // }}}
0337d704 330 // {{{ function _mail_body
331
612a2d8a 332 abstract protected function _mail_body($isok);
eaf30d86 333
0337d704 334 // }}}
335 // {{{ function _mail_subj
336
612a2d8a 337 abstract protected function _mail_subj();
eaf30d86 338
0337d704 339 // }}}
340 // {{{ function commit()
eaf30d86 341
a7de4ef7 342 /** fonction à utiliser pour insérer les données dans x4dat
0337d704 343 */
612a2d8a 344 abstract public function commit();
0337d704 345
346 // }}}
347 // {{{ function formu()
eaf30d86 348
0337d704 349 /** nom du template qui contient le formulaire */
612a2d8a 350 abstract public function formu();
0337d704 351
352 // }}}
6aa01fed 353 // {{{ function editor()
354
a7de4ef7 355 /** nom du formulaire d'édition */
612a2d8a 356 public function editor()
357 {
358 return null;
359 }
6aa01fed 360
361 // }}}
e18888f4 362 // {{{ function answers()
363
364 /** automatic answers table for this type of validation */
612a2d8a 365 public function answers()
366 {
e18888f4 367 static $answers_table;
368 if (!isset($answers_table[$this->type])) {
369 $r = XDB::query("SELECT id, title, answer FROM requests_answers WHERE category = {?}", $this->type);
370 $answers_table[$this->type] = $r->fetchAllAssoc($r);
371 }
372 return $answers_table[$this->type];
373 }
374
375 // }}}
a7de4ef7 376 // {{{ function id()
ed5b9703 377
612a2d8a 378 public function id()
ed5b9703 379 {
5daf68f6 380 return $this->user->id() . '_' . $this->type . '_' . $this->stamp;
ed5b9703 381 }
382
383 // }}}
fba760d2 384 // {{{ function ruleText()
385
386 public function ruleText()
387 {
388 return str_replace('\'', '\\\'', $this->rules);
389 }
390
391 // }}}
33b47675
FB
392 // {{{ function unserialize()
393 public static function unserialize($data)
394 {
395 $obj = unserialize($data);
396 /* XXX: Temporary for hruid migration */
397 if (!isset($obj->user) || !is_object($obj)) {
398 $obj->user =& User::get($obj->forlife);
399 }
400 /* XXX: End temporary block */
401 return $obj;
402 }
0337d704 403}
404
0337d704 405foreach (glob(dirname(__FILE__).'/validations/*.inc.php') as $file) {
406 require_once($file);
407}
408
a7de4ef7 409/* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker enc=utf-8: */
0337d704 410?>