fix path to image
[platal.git] / include / validations.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
50a40a33 3 * Copyright (C) 2003-2006 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
0337d704 33 function ValidateIterator ()
34 {
917c4d11 35 parent::__construct('SELECT data,stamp FROM requests ORDER BY stamp', MYSQL_NUM);
0337d704 36 }
37
38 // }}}
39 // {{{ function next()
40
41 function next ()
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
0337d704 55/** classe "virtuelle" à dériver pour chaque nouvelle implémentation
56 */
57class Validate
58{
59 // {{{ properties
60
61 var $uid;
62 var $prenom;
63 var $nom;
64 var $promo;
69b7b6e9 65 var $sexe;
0337d704 66 var $bestalias;
67 var $forlife;
68
69 var $stamp;
70 var $unique;
71 // enable the refuse button
72 var $refuse = true;
6aa01fed 73
0337d704 74 var $type;
75 var $comments = Array();
76 // the validations rules : comments for admins
77 var $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";
78
79 // }}}
80 // {{{ constructor
81
82 /** constructeur
83 * @param $_uid user id
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
86 */
87 function Validate($_uid, $_unique, $_type)
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 }
101
102 // }}}
103 // {{{ function submit()
104
105 /** fonction à utiliser pour envoyer les données à la modération
106 * cette fonction supprimme les doublons sur un couple ($user,$type) si $this->unique est vrai
107 */
108 function submit ()
109 {
0337d704 110 if ($this->unique) {
08cce2ff 111 XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?}', $this->uid, $this->type);
0337d704 112 }
113
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
118 return true;
119 }
120
121 // }}}
122 // {{{ function update()
123
124 function update ()
125 {
08cce2ff 126 XDB::execute('UPDATE requests SET data={?}, stamp=stamp
0337d704 127 WHERE user_id={?} AND type={?} AND stamp={?}',
128 $this, $this->uid, $this->type, $this->stamp);
129
130 return true;
131 }
132
133 // }}}
134 // {{{ function clean()
135
136 /** fonction à utiliser pour nettoyer l'entrée de la requête dans la table requests
137 * attention, tout est supprimé si c'est un unique
138 */
139 function clean ()
140 {
0337d704 141 if ($this->unique) {
08cce2ff 142 return XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?}',
0337d704 143 $this->uid, $this->type);
144 } else {
08cce2ff 145 return XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?} AND stamp={?}',
0337d704 146 $this->uid, $this->type, $this->stamp);
147 }
148 }
149
150 // }}}
151 // {{{ function handle_formu()
152
153 /** fonction à réaliser en cas de valistion du formulaire
154 */
155 function handle_formu()
156 {
157 if (Env::has('delete')) {
158 $this->clean();
159 $this->trig('requete supprimée');
160 return true;
161 }
162
6aa01fed 163 // mise à jour des informations
164 if (Env::has('edit')) {
165 if ($this->handle_editor()) {
166 $this->update();
167 $this->trig('requête mise à jour');
168 return true;
169 }
170 return false;
171 }
172
0337d704 173 // ajout d'un commentaire
174 if (Env::has('hold') && Env::has('comm')) {
4791ff77 175 $formid = Env::i('formid');
176 foreach ($this->comments as $comment) {
177 if ($comment[2] === $formid) {
178 return true;
179 }
180 }
181 $this->comments[] = Array(S::v('bestalias'), Env::v('comm'), $formid);
0337d704 182
183 // envoi d'un mail à hotliners
184 global $globals;
185 require_once('diogenes/diogenes.hermes.inc.php');
186 $mailer = new HermesMailer;
187 $mailer->setSubject("Commentaires de validation {$this->type}");
188 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
c33efb7c 189 $mailer->addTo("hotliners@staff.polytechnique.org");
0337d704 190
191 $body = "Validation {$this->type} pour {$this->prenom} {$this->nom}\n\n"
cab08090 192 . S::v('bestalias')." a ajouté le commentaire :\n\n"
5e2307dc 193 . Env::v('comm')."\n\n"
8c7809b5 194 . "cf la discussion sur : ".$globals->baseurl."/admin/validate";
0337d704 195
196 $mailer->setTxtBody(wordwrap($body));
197 $mailer->send();
198
199 $this->update();
200 $this->trig('commentaire ajouté');
201 return true;
202 }
203
204 if (Env::has('accept')) {
205 if ($this->commit()) {
206 $this->sendmail(true);
207 $this->clean();
208 $this->trig('mail envoyé');
209 return true;
210 } else {
211 $this->trig('erreur lors de la validation');
212 return false;
213 }
214 }
215
216 if (Env::has('refuse')) {
5e2307dc 217 if (Env::v('comm')) {
0337d704 218 $this->sendmail(false);
219 $this->clean();
220 $this->trig('mail envoyé');
221 return true;
222 } else {
223 $this->trig('pas de motivation pour le refus !!!');
224 }
225 }
226
227 return false;
228 }
229
230 // }}}
231 // {{{ function sendmail
232
233 function sendmail($isok)
234 {
235 global $globals;
236 require_once('diogenes/diogenes.hermes.inc.php');
237 $mailer = new HermesMailer;
238 $mailer->setSubject($this->_mail_subj());
239 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
240 $mailer->addTo("\"{$this->prenom} {$this->nom}\" <{$this->bestalias}@{$globals->mail->domain}>");
241 $mailer->addCc("validation+{$this->type}@{$globals->mail->domain}");
242
69b7b6e9 243 $body = ($this->sexe ? "Chère camarade,\n\n" : "Cher camarade,\n\n")
0337d704 244 . $this->_mail_body($isok)
5e2307dc 245 . (Env::has('comm') ? "\n\n".Env::v('comm') : '')
0337d704 246 . "\n\nCordialement,\nL'équipe Polytechnique.org\n";
247
248 $mailer->setTxtBody(wordwrap($body));
249 $mailer->send();
250 }
251
252 // }}}
253 // {{{ function trig()
254
255 function trig($msg) {
256 global $page;
257 $page->trig($msg);
258 }
259
260 // }}}
20d7932b 261 // {{{ function get_typed_request()
0337d704 262
263 /** fonction statique qui renvoie la requête de l'utilisateur d'id $uidau timestamp $t
264 * @param $uid l'id de l'utilisateur concerné
265 * @param $type le type de la requête
266 * @param $stamp le timestamp de la requête
267 *
268 * XXX fonction "statique" XXX
20d7932b 269 * à utiliser uniquement pour récupérer un objet dans la BD avec Validate::get_typed_request(...)
0337d704 270 */
ae2b4eac 271 static function get_typed_request($uid, $type, $stamp = -1)
0337d704 272 {
0337d704 273 if ($stamp == -1) {
08cce2ff 274 $res = XDB::query('SELECT data FROM requests WHERE user_id={?} and type={?}', $uid, $type);
0337d704 275 } else {
08cce2ff 276 $res = XDB::query("SELECT data, stamp FROM requests WHERE user_id={?} AND type={?} and stamp={?}", $uid, $type, $stamp);
0337d704 277 }
278 if ($result = $res->fetchOneCell()) {
279 $result = unserialize($result);
280 } else {
281 $result = false;
282 }
283 return($result);
284 }
285
286 // }}}
287 // {{{ function _mail_body
288
289 function _mail_body($isok)
290 {
291 }
292
293 // }}}
294 // {{{ function _mail_subj
295
296 function _mail_subj()
297 {
298 }
299
300 // }}}
301 // {{{ function commit()
302
303 /** fonction à utiliser pour insérer les données dans x4dat
304 * XXX la fonction est "virtuelle" XXX
305 */
306 function commit ()
307 { }
308
309 // }}}
310 // {{{ function formu()
311
312 /** nom du template qui contient le formulaire */
313 function formu()
314 { return null; }
315
316 // }}}
6aa01fed 317 // {{{ function editor()
318
319 /** nom du formulaire d'édition */
320 function editor()
321 { return null; }
322
323 // }}}
e18888f4 324 // {{{ function answers()
325
326 /** automatic answers table for this type of validation */
327 function answers() {
328 static $answers_table;
329 if (!isset($answers_table[$this->type])) {
330 $r = XDB::query("SELECT id, title, answer FROM requests_answers WHERE category = {?}", $this->type);
331 $answers_table[$this->type] = $r->fetchAllAssoc($r);
332 }
333 return $answers_table[$this->type];
334 }
335
336 // }}}
ed5b9703 337 // {{{ function id()
338
339 function id()
340 {
341 return $this->uid . '_' . $this->type . '_' . $this->stamp;
342 }
343
344 // }}}
0337d704 345}
346
0337d704 347foreach (glob(dirname(__FILE__).'/validations/*.inc.php') as $file) {
348 require_once($file);
349}
350
0337d704 351/* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker: */
352?>