Implement X.net ergonomy suggestions by BdB
[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()) {
45 $result = unserialize($result);
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
612a2d8a 62 public $uid;
63 public $prenom;
64 public $nom;
65 public $promo;
66 public $sexe;
67 public $bestalias;
68 public $forlife;
69
70 public $stamp;
71 public $unique;
0337d704 72 // enable the refuse button
612a2d8a 73 public $refuse = true;
eaf30d86 74
612a2d8a 75 public $type;
76 public $comments = Array();
0337d704 77 // the validations rules : comments for admins
612a2d8a 78 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 79
80 // }}}
81 // {{{ constructor
eaf30d86 82
0337d704 83 /** constructeur
84 * @param $_uid user id
a7de4ef7 85 * @param $_unique requête pouvant être multiple ou non
86 * @param $_type type de la donnée comme dans le champ type de x4dat.requests
0337d704 87 */
612a2d8a 88 public function __construct($_uid, $_unique, $_type)
0337d704 89 {
0337d704 90 $this->uid = $_uid;
91 $this->stamp = date('YmdHis');
92 $this->unique = $_unique;
93 $this->type = $_type;
08cce2ff 94 $res = XDB::query(
69b7b6e9 95 "SELECT u.prenom, u.nom, u.promo, FIND_IN_SET('femme', u.flags) AS sexe, a.alias, b.alias
0337d704 96 FROM auth_user_md5 AS u
97 INNER JOIN aliases AS a ON ( u.user_id=a.id AND a.type='a_vie' )
98 INNER JOIN aliases AS b ON ( u.user_id=b.id AND b.type!='homonyme' AND FIND_IN_SET('bestalias', b.flags) )
99 WHERE u.user_id={?}", $_uid);
69b7b6e9 100 list($this->prenom, $this->nom, $this->promo, $this->sexe, $this->forlife, $this->bestalias) = $res->fetchOneRow();
0337d704 101 }
eaf30d86 102
0337d704 103 // }}}
104 // {{{ function submit()
105
a7de4ef7 106 /** fonction à utiliser pour envoyer les données à la modération
0337d704 107 * cette fonction supprimme les doublons sur un couple ($user,$type) si $this->unique est vrai
108 */
612a2d8a 109 public function submit()
0337d704 110 {
0337d704 111 if ($this->unique) {
08cce2ff 112 XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?}', $this->uid, $this->type);
0337d704 113 }
eaf30d86 114
0337d704 115 $this->stamp = date('YmdHis');
08cce2ff 116 XDB::execute('INSERT INTO requests (user_id, type, data, stamp) VALUES ({?}, {?}, {?}, {?})',
0337d704 117 $this->uid, $this->type, $this, $this->stamp);
118
84868ee9 119 global $globals;
ebfdf077 120 $globals->updateNbValid();
0337d704 121 return true;
122 }
123
124 // }}}
125 // {{{ function update()
126
612a2d8a 127 protected function update()
0337d704 128 {
08cce2ff 129 XDB::execute('UPDATE requests SET data={?}, stamp=stamp
612a2d8a 130 WHERE user_id={?} AND type={?} AND stamp={?}',
131 $this, $this->uid, $this->type, $this->stamp);
0337d704 132 return true;
133 }
134
135 // }}}
136 // {{{ function clean()
eaf30d86 137
a7de4ef7 138 /** fonction à utiliser pour nettoyer l'entrée de la requête dans la table requests
139 * attention, tout est supprimé si c'est un unique
0337d704 140 */
d17761d8 141 public function clean()
0337d704 142 {
95e36b0f
SJ
143 global $globals;
144
0337d704 145 if ($this->unique) {
84868ee9
FB
146 $success = XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?}',
147 $this->uid, $this->type);
0337d704 148 } else {
84868ee9
FB
149 $success = XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?} AND stamp={?}',
150 $this->uid, $this->type, $this->stamp);
0337d704 151 }
ebfdf077 152 $globals->updateNbValid();
84868ee9 153 return $success;
0337d704 154 }
155
156 // }}}
157 // {{{ function handle_formu()
eaf30d86 158
cb04af2c 159 /** fonction à réaliser en cas de validation du formulaire
0337d704 160 */
612a2d8a 161 public function handle_formu()
0337d704 162 {
163 if (Env::has('delete')) {
164 $this->clean();
a7d35093 165 $this->trigSuccess('Requête supprimée');
0337d704 166 return true;
167 }
168
a7de4ef7 169 // mise à jour des informations
6aa01fed 170 if (Env::has('edit')) {
171 if ($this->handle_editor()) {
172 $this->update();
a7d35093 173 $this->trigSuccess('Requête mise à jour');
6aa01fed 174 return true;
175 }
176 return false;
177 }
178
0337d704 179 // ajout d'un commentaire
180 if (Env::has('hold') && Env::has('comm')) {
4791ff77 181 $formid = Env::i('formid');
182 foreach ($this->comments as $comment) {
183 if ($comment[2] === $formid) {
184 return true;
185 }
186 }
90608d68 187 if (!strlen(trim(Env::v('comm')))) {
188 return true;
189 }
4791ff77 190 $this->comments[] = Array(S::v('bestalias'), Env::v('comm'), $formid);
0337d704 191
a7de4ef7 192 // envoi d'un mail à hotliners
0337d704 193 global $globals;
b9c53090 194 $mailer = new PlMailer();
0337d704 195 $mailer->setSubject("Commentaires de validation {$this->type}");
196 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
b9c53090 197 $mailer->addTo($globals->core->admin_email);
0337d704 198
199 $body = "Validation {$this->type} pour {$this->prenom} {$this->nom}\n\n"
eaf30d86 200 . S::v('bestalias')." a ajouté le commentaire :\n\n"
5e2307dc 201 . Env::v('comm')."\n\n"
8c7809b5 202 . "cf la discussion sur : ".$globals->baseurl."/admin/validate";
0337d704 203
204 $mailer->setTxtBody(wordwrap($body));
205 $mailer->send();
206
207 $this->update();
a7d35093 208 $this->trigSuccess('Commentaire ajouté');
0337d704 209 return true;
210 }
211
212 if (Env::has('accept')) {
213 if ($this->commit()) {
214 $this->sendmail(true);
215 $this->clean();
faefdbb7 216 $this->trigSuccess('Email de validation envoyé');
0337d704 217 return true;
218 } else {
a7d35093 219 $this->trigError('Erreur lors de la validation');
0337d704 220 return false;
221 }
222 }
223
224 if (Env::has('refuse')) {
5e2307dc 225 if (Env::v('comm')) {
0337d704 226 $this->sendmail(false);
227 $this->clean();
faefdbb7 228 $this->trigSuccess('Email de refus envoyé');
0337d704 229 return true;
230 } else {
a7d35093 231 $this->trigError('pas de motivation pour le refus !!!');
0337d704 232 }
233 }
234
235 return false;
236 }
237
238 // }}}
239 // {{{ function sendmail
240
612a2d8a 241 protected function sendmail($isok)
0337d704 242 {
243 global $globals;
1e33266a 244 $mailer = new PlMailer();
0337d704 245 $mailer->setSubject($this->_mail_subj());
246 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
247 $mailer->addTo("\"{$this->prenom} {$this->nom}\" <{$this->bestalias}@{$globals->mail->domain}>");
248 $mailer->addCc("validation+{$this->type}@{$globals->mail->domain}");
249
a7de4ef7 250 $body = ($this->sexe ? "Chère camarade,\n\n" : "Cher camarade,\n\n")
0337d704 251 . $this->_mail_body($isok)
5e2307dc 252 . (Env::has('comm') ? "\n\n".Env::v('comm') : '')
780bc68d 253 . "\n\nCordialement,\n\n-- \nL'équipe de Polytechnique.org\n";
0337d704 254
255 $mailer->setTxtBody(wordwrap($body));
256 $mailer->send();
257 }
258
259 // }}}
260 // {{{ function trig()
eaf30d86 261
a7d35093 262 protected function trigError($msg)
612a2d8a 263 {
d7610c35 264 Platal::page()->trigError($msg);
a7d35093
FB
265 }
266
267 protected function trigWarning($msg)
268 {
d7610c35 269 Platal::page()->trigWarning($msg);
a7d35093
FB
270 }
271
272 protected function trigSuccess($msg)
273 {
d7610c35 274 Platal::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?>