Uses 'liste de diffusion' instead of 'mailing list' for the site is in French.
[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
fdc77a17 24require_once dirname(__FILE__) . '/../classes/xdb.php';
2f151d5f 25
0337d704 26/**
27 * Iterator class, that lists objects through the database
28 */
29class ValidateIterator extends XOrgDBIterator
30{
31 // {{{ constuctor
d71befc4 32
612a2d8a 33 public function __construct ()
0337d704 34 {
96b00435 35 parent::__construct('SELECT data, DATE_FORMAT(stamp, "%Y%m%d%H%i%s") FROM requests ORDER BY stamp', MYSQL_NUM);
0337d704 36 }
37
38 // }}}
39 // {{{ function next()
40
612a2d8a 41 public function next ()
0337d704 42 {
43 if (list($result, $stamp) = parent::next()) {
44 $result = unserialize($result);
45 $result->stamp = $stamp;
46 return($result);
47 } else {
48 return null;
49 }
50 }
51
52 // }}}
53}
54
a7de4ef7 55/** classe "virtuelle" à dériver pour chaque nouvelle implémentation
0337d704 56 */
612a2d8a 57abstract class Validate
0337d704 58{
59 // {{{ properties
eaf30d86 60
612a2d8a 61 public $uid;
62 public $prenom;
63 public $nom;
64 public $promo;
65 public $sexe;
66 public $bestalias;
67 public $forlife;
68
69 public $stamp;
70 public $unique;
0337d704 71 // enable the refuse button
612a2d8a 72 public $refuse = true;
eaf30d86 73
612a2d8a 74 public $type;
75 public $comments = Array();
0337d704 76 // the validations rules : comments for admins
612a2d8a 77 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 78
79 // }}}
80 // {{{ constructor
eaf30d86 81
0337d704 82 /** constructeur
83 * @param $_uid user id
a7de4ef7 84 * @param $_unique requête pouvant être multiple ou non
85 * @param $_type type de la donnée comme dans le champ type de x4dat.requests
0337d704 86 */
612a2d8a 87 public function __construct($_uid, $_unique, $_type)
0337d704 88 {
0337d704 89 $this->uid = $_uid;
90 $this->stamp = date('YmdHis');
91 $this->unique = $_unique;
92 $this->type = $_type;
08cce2ff 93 $res = XDB::query(
69b7b6e9 94 "SELECT u.prenom, u.nom, u.promo, FIND_IN_SET('femme', u.flags) AS sexe, a.alias, b.alias
0337d704 95 FROM auth_user_md5 AS u
96 INNER JOIN aliases AS a ON ( u.user_id=a.id AND a.type='a_vie' )
97 INNER JOIN aliases AS b ON ( u.user_id=b.id AND b.type!='homonyme' AND FIND_IN_SET('bestalias', b.flags) )
98 WHERE u.user_id={?}", $_uid);
69b7b6e9 99 list($this->prenom, $this->nom, $this->promo, $this->sexe, $this->forlife, $this->bestalias) = $res->fetchOneRow();
0337d704 100 }
eaf30d86 101
0337d704 102 // }}}
103 // {{{ function submit()
104
a7de4ef7 105 /** fonction à utiliser pour envoyer les données à la modération
0337d704 106 * cette fonction supprimme les doublons sur un couple ($user,$type) si $this->unique est vrai
107 */
612a2d8a 108 public function submit()
0337d704 109 {
0337d704 110 if ($this->unique) {
08cce2ff 111 XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?}', $this->uid, $this->type);
0337d704 112 }
eaf30d86 113
0337d704 114 $this->stamp = date('YmdHis');
08cce2ff 115 XDB::execute('INSERT INTO requests (user_id, type, data, stamp) VALUES ({?}, {?}, {?}, {?})',
0337d704 116 $this->uid, $this->type, $this, $this->stamp);
117
84868ee9
FB
118 global $globals;
119 update_NbValid();
0337d704 120 return true;
121 }
122
123 // }}}
124 // {{{ function update()
125
612a2d8a 126 protected function update()
0337d704 127 {
08cce2ff 128 XDB::execute('UPDATE requests SET data={?}, stamp=stamp
612a2d8a 129 WHERE user_id={?} AND type={?} AND stamp={?}',
130 $this, $this->uid, $this->type, $this->stamp);
0337d704 131 return true;
132 }
133
134 // }}}
135 // {{{ function clean()
eaf30d86 136
a7de4ef7 137 /** fonction à utiliser pour nettoyer l'entrée de la requête dans la table requests
138 * attention, tout est supprimé si c'est un unique
0337d704 139 */
d17761d8 140 public function clean()
0337d704 141 {
0337d704 142 if ($this->unique) {
84868ee9
FB
143 $success = XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?}',
144 $this->uid, $this->type);
0337d704 145 } else {
84868ee9
FB
146 $success = XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?} AND stamp={?}',
147 $this->uid, $this->type, $this->stamp);
0337d704 148 }
84868ee9
FB
149 update_NbValid();
150 return $success;
0337d704 151 }
152
153 // }}}
154 // {{{ function handle_formu()
eaf30d86 155
cb04af2c 156 /** fonction à réaliser en cas de validation du formulaire
0337d704 157 */
612a2d8a 158 public function handle_formu()
0337d704 159 {
160 if (Env::has('delete')) {
161 $this->clean();
a7d35093 162 $this->trigSuccess('Requête supprimée');
0337d704 163 return true;
164 }
165
a7de4ef7 166 // mise à jour des informations
6aa01fed 167 if (Env::has('edit')) {
168 if ($this->handle_editor()) {
169 $this->update();
a7d35093 170 $this->trigSuccess('Requête mise à jour');
6aa01fed 171 return true;
172 }
173 return false;
174 }
175
0337d704 176 // ajout d'un commentaire
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 }
4791ff77 187 $this->comments[] = Array(S::v('bestalias'), Env::v('comm'), $formid);
0337d704 188
a7de4ef7 189 // envoi d'un mail à hotliners
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
196 $body = "Validation {$this->type} pour {$this->prenom} {$this->nom}\n\n"
eaf30d86 197 . S::v('bestalias')." a ajouté le commentaire :\n\n"
5e2307dc 198 . Env::v('comm')."\n\n"
8c7809b5 199 . "cf la discussion sur : ".$globals->baseurl."/admin/validate";
0337d704 200
201 $mailer->setTxtBody(wordwrap($body));
202 $mailer->send();
203
204 $this->update();
a7d35093 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();
a7d35093 213 $this->trigSuccess('Mail 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();
a7d35093 225 $this->trigSuccess('Mail de refus envoyé');
0337d704 226 return true;
227 } else {
a7d35093 228 $this->trigError('pas de motivation pour le refus !!!');
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}");
244 $mailer->addTo("\"{$this->prenom} {$this->nom}\" <{$this->bestalias}@{$globals->mail->domain}>");
245 $mailer->addCc("validation+{$this->type}@{$globals->mail->domain}");
246
a7de4ef7 247 $body = ($this->sexe ? "Chère camarade,\n\n" : "Cher camarade,\n\n")
0337d704 248 . $this->_mail_body($isok)
5e2307dc 249 . (Env::has('comm') ? "\n\n".Env::v('comm') : '')
780bc68d 250 . "\n\nCordialement,\n\n-- \nL'équipe de Polytechnique.org\n";
0337d704 251
252 $mailer->setTxtBody(wordwrap($body));
253 $mailer->send();
254 }
255
256 // }}}
257 // {{{ function trig()
eaf30d86 258
a7d35093 259 protected function trigError($msg)
612a2d8a 260 {
0337d704 261 global $page;
a7d35093
FB
262 $page->trigError($msg);
263 }
264
265 protected function trigWarning($msg)
266 {
267 global $page;
268 $page->trigWarning($msg);
269 }
270
271 protected function trigSuccess($msg)
272 {
273 global $page;
274 $page->trigSuccess($msg);
0337d704 275 }
eaf30d86 276
0337d704 277 // }}}
20d7932b 278 // {{{ function get_typed_request()
0337d704 279
a7de4ef7 280 /** fonction statique qui renvoie la requête de l'utilisateur d'id $uidau timestamp $t
281 * @param $uid l'id de l'utilisateur concerné
282 * @param $type le type de la requête
283 * @param $stamp le timestamp de la requête
0337d704 284 *
285 * XXX fonction "statique" XXX
a7de4ef7 286 * à utiliser uniquement pour récupérer un objet dans la BD avec Validate::get_typed_request(...)
0337d704 287 */
612a2d8a 288 static public function get_typed_request($uid, $type, $stamp = -1)
0337d704 289 {
0337d704 290 if ($stamp == -1) {
08cce2ff 291 $res = XDB::query('SELECT data FROM requests WHERE user_id={?} and type={?}', $uid, $type);
0337d704 292 } else {
96b00435 293 $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 294 }
295 if ($result = $res->fetchOneCell()) {
296 $result = unserialize($result);
297 } else {
298 $result = false;
299 }
300 return($result);
301 }
302
303 // }}}
02838718 304 // {{{ function get_request_by_id()
305
306 static public function get_request_by_id($id)
307 {
308 list($uid, $type, $stamp) = explode('_', $id, 3);
309 return Validate::get_typed_request($uid, $type, $stamp);
310 }
311
312 // }}}
5b0dc389 313 // {{{ function get_typed_requests()
314
315 /** same as get_typed_request() but return an array of objects
316 */
612a2d8a 317 static public function get_typed_requests($uid, $type)
5b0dc389 318 {
319 $res = XDB::iterRow('SELECT data FROM requests WHERE user_id={?} and type={?}', $uid, $type);
320 $array = array();
321 while (list($data) = $res->next()) {
322 $array[] = unserialize($data);
323 }
324 return $array;
325 }
326
327 // }}}
bb0727ea
VZ
328 // {{{ function get_typed_requests_count()
329
330 /** same as get_typed_requests() but return the count of available requests.
331 */
332 static public function get_typed_requests_count($uid, $type)
333 {
334 $res = XDB::query('SELECT COUNT(data) FROM requests WHERE user_id={?} and type={?}', $uid, $type);
335 return $res->fetchOneCell();
336 }
337
338 // }}}
0337d704 339 // {{{ function _mail_body
340
612a2d8a 341 abstract protected function _mail_body($isok);
eaf30d86 342
0337d704 343 // }}}
344 // {{{ function _mail_subj
345
612a2d8a 346 abstract protected function _mail_subj();
eaf30d86 347
0337d704 348 // }}}
349 // {{{ function commit()
eaf30d86 350
a7de4ef7 351 /** fonction à utiliser pour insérer les données dans x4dat
0337d704 352 */
612a2d8a 353 abstract public function commit();
0337d704 354
355 // }}}
356 // {{{ function formu()
eaf30d86 357
0337d704 358 /** nom du template qui contient le formulaire */
612a2d8a 359 abstract public function formu();
0337d704 360
361 // }}}
6aa01fed 362 // {{{ function editor()
363
a7de4ef7 364 /** nom du formulaire d'édition */
612a2d8a 365 public function editor()
366 {
367 return null;
368 }
6aa01fed 369
370 // }}}
e18888f4 371 // {{{ function answers()
372
373 /** automatic answers table for this type of validation */
612a2d8a 374 public function answers()
375 {
e18888f4 376 static $answers_table;
377 if (!isset($answers_table[$this->type])) {
378 $r = XDB::query("SELECT id, title, answer FROM requests_answers WHERE category = {?}", $this->type);
379 $answers_table[$this->type] = $r->fetchAllAssoc($r);
380 }
381 return $answers_table[$this->type];
382 }
383
384 // }}}
a7de4ef7 385 // {{{ function id()
ed5b9703 386
612a2d8a 387 public function id()
ed5b9703 388 {
389 return $this->uid . '_' . $this->type . '_' . $this->stamp;
390 }
391
392 // }}}
fba760d2 393 // {{{ function ruleText()
394
395 public function ruleText()
396 {
397 return str_replace('\'', '\\\'', $this->rules);
398 }
399
400 // }}}
0337d704 401}
402
0337d704 403foreach (glob(dirname(__FILE__).'/validations/*.inc.php') as $file) {
404 require_once($file);
405}
406
a7de4ef7 407/* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker enc=utf-8: */
0337d704 408?>