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 |
22 | define('SIZE_MAX', 32768); |
23 | |
fdc77a17 |
24 | require_once dirname(__FILE__) . '/../classes/xdb.php'; |
2f151d5f |
25 | |
0337d704 |
26 | /** |
27 | * Iterator class, that lists objects through the database |
28 | */ |
29 | class 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 | */ |
57 | class 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; |
1e33266a |
185 | $mailer = new PlMailer; |
0337d704 |
186 | $mailer->setSubject("Commentaires de validation {$this->type}"); |
187 | $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}"); |
c33efb7c |
188 | $mailer->addTo("hotliners@staff.polytechnique.org"); |
0337d704 |
189 | |
190 | $body = "Validation {$this->type} pour {$this->prenom} {$this->nom}\n\n" |
cab08090 |
191 | . S::v('bestalias')." a ajouté le commentaire :\n\n" |
5e2307dc |
192 | . Env::v('comm')."\n\n" |
8c7809b5 |
193 | . "cf la discussion sur : ".$globals->baseurl."/admin/validate"; |
0337d704 |
194 | |
195 | $mailer->setTxtBody(wordwrap($body)); |
196 | $mailer->send(); |
197 | |
198 | $this->update(); |
199 | $this->trig('commentaire ajouté'); |
200 | return true; |
201 | } |
202 | |
203 | if (Env::has('accept')) { |
204 | if ($this->commit()) { |
205 | $this->sendmail(true); |
206 | $this->clean(); |
207 | $this->trig('mail envoyé'); |
208 | return true; |
209 | } else { |
210 | $this->trig('erreur lors de la validation'); |
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(); |
219 | $this->trig('mail envoyé'); |
220 | return true; |
221 | } else { |
222 | $this->trig('pas de motivation pour le refus !!!'); |
223 | } |
224 | } |
225 | |
226 | return false; |
227 | } |
228 | |
229 | // }}} |
230 | // {{{ function sendmail |
231 | |
232 | function sendmail($isok) |
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}"); |
238 | $mailer->addTo("\"{$this->prenom} {$this->nom}\" <{$this->bestalias}@{$globals->mail->domain}>"); |
239 | $mailer->addCc("validation+{$this->type}@{$globals->mail->domain}"); |
240 | |
69b7b6e9 |
241 | $body = ($this->sexe ? "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') : '') |
0337d704 |
244 | . "\n\nCordialement,\nL'équipe Polytechnique.org\n"; |
245 | |
246 | $mailer->setTxtBody(wordwrap($body)); |
247 | $mailer->send(); |
248 | } |
249 | |
250 | // }}} |
251 | // {{{ function trig() |
252 | |
253 | function trig($msg) { |
254 | global $page; |
255 | $page->trig($msg); |
256 | } |
257 | |
258 | // }}} |
20d7932b |
259 | // {{{ function get_typed_request() |
0337d704 |
260 | |
261 | /** fonction statique qui renvoie la requête de l'utilisateur d'id $uidau timestamp $t |
262 | * @param $uid l'id de l'utilisateur concerné |
263 | * @param $type le type de la requête |
264 | * @param $stamp le timestamp de la requête |
265 | * |
266 | * XXX fonction "statique" XXX |
20d7932b |
267 | * à utiliser uniquement pour récupérer un objet dans la BD avec Validate::get_typed_request(...) |
0337d704 |
268 | */ |
ae2b4eac |
269 | static function get_typed_request($uid, $type, $stamp = -1) |
0337d704 |
270 | { |
0337d704 |
271 | if ($stamp == -1) { |
08cce2ff |
272 | $res = XDB::query('SELECT data FROM requests WHERE user_id={?} and type={?}', $uid, $type); |
0337d704 |
273 | } else { |
08cce2ff |
274 | $res = XDB::query("SELECT data, stamp FROM requests WHERE user_id={?} AND type={?} and stamp={?}", $uid, $type, $stamp); |
0337d704 |
275 | } |
276 | if ($result = $res->fetchOneCell()) { |
277 | $result = unserialize($result); |
278 | } else { |
279 | $result = false; |
280 | } |
281 | return($result); |
282 | } |
283 | |
284 | // }}} |
285 | // {{{ function _mail_body |
286 | |
287 | function _mail_body($isok) |
288 | { |
289 | } |
290 | |
291 | // }}} |
292 | // {{{ function _mail_subj |
293 | |
294 | function _mail_subj() |
295 | { |
296 | } |
297 | |
298 | // }}} |
299 | // {{{ function commit() |
300 | |
301 | /** fonction à utiliser pour insérer les données dans x4dat |
302 | * XXX la fonction est "virtuelle" XXX |
303 | */ |
304 | function commit () |
305 | { } |
306 | |
307 | // }}} |
308 | // {{{ function formu() |
309 | |
310 | /** nom du template qui contient le formulaire */ |
311 | function formu() |
312 | { return null; } |
313 | |
314 | // }}} |
6aa01fed |
315 | // {{{ function editor() |
316 | |
317 | /** nom du formulaire d'édition */ |
318 | function editor() |
319 | { return null; } |
320 | |
321 | // }}} |
e18888f4 |
322 | // {{{ function answers() |
323 | |
324 | /** automatic answers table for this type of validation */ |
325 | function answers() { |
326 | static $answers_table; |
327 | if (!isset($answers_table[$this->type])) { |
328 | $r = XDB::query("SELECT id, title, answer FROM requests_answers WHERE category = {?}", $this->type); |
329 | $answers_table[$this->type] = $r->fetchAllAssoc($r); |
330 | } |
331 | return $answers_table[$this->type]; |
332 | } |
333 | |
334 | // }}} |
ed5b9703 |
335 | // {{{ function id() |
336 | |
337 | function id() |
338 | { |
339 | return $this->uid . '_' . $this->type . '_' . $this->stamp; |
340 | } |
341 | |
342 | // }}} |
0337d704 |
343 | } |
344 | |
0337d704 |
345 | foreach (glob(dirname(__FILE__).'/validations/*.inc.php') as $file) { |
346 | require_once($file); |
347 | } |
348 | |
0337d704 |
349 | /* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker: */ |
350 | ?> |