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 | |
0337d704 |
24 | /** |
25 | * Iterator class, that lists objects through the database |
26 | */ |
27 | class ValidateIterator extends XOrgDBIterator |
28 | { |
29 | // {{{ constuctor |
d71befc4 |
30 | |
0337d704 |
31 | function ValidateIterator () |
32 | { |
33 | parent::XOrgDBIterator('SELECT data,stamp FROM requests ORDER BY stamp', MYSQL_NUM); |
34 | } |
35 | |
36 | // }}} |
37 | // {{{ function next() |
38 | |
39 | function next () |
40 | { |
41 | if (list($result, $stamp) = parent::next()) { |
42 | $result = unserialize($result); |
43 | $result->stamp = $stamp; |
44 | return($result); |
45 | } else { |
46 | return null; |
47 | } |
48 | } |
49 | |
50 | // }}} |
51 | } |
52 | |
0337d704 |
53 | /** classe "virtuelle" à dériver pour chaque nouvelle implémentation |
54 | */ |
55 | class Validate |
56 | { |
57 | // {{{ properties |
58 | |
59 | var $uid; |
60 | var $prenom; |
61 | var $nom; |
62 | var $promo; |
63 | var $bestalias; |
64 | var $forlife; |
65 | |
66 | var $stamp; |
67 | var $unique; |
68 | // enable the refuse button |
69 | var $refuse = true; |
6aa01fed |
70 | |
0337d704 |
71 | var $type; |
72 | var $comments = Array(); |
73 | // the validations rules : comments for admins |
74 | 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"; |
75 | |
76 | // }}} |
77 | // {{{ constructor |
78 | |
79 | /** constructeur |
80 | * @param $_uid user id |
81 | * @param $_unique requête pouvant être multiple ou non |
82 | * @param $_type type de la donnée comme dans le champ type de x4dat.requests |
83 | */ |
84 | function Validate($_uid, $_unique, $_type) |
85 | { |
0337d704 |
86 | $this->uid = $_uid; |
87 | $this->stamp = date('YmdHis'); |
88 | $this->unique = $_unique; |
89 | $this->type = $_type; |
08cce2ff |
90 | $res = XDB::query( |
0337d704 |
91 | "SELECT u.prenom, u.nom, u.promo, a.alias, b.alias |
92 | FROM auth_user_md5 AS u |
93 | INNER JOIN aliases AS a ON ( u.user_id=a.id AND a.type='a_vie' ) |
94 | INNER JOIN aliases AS b ON ( u.user_id=b.id AND b.type!='homonyme' AND FIND_IN_SET('bestalias', b.flags) ) |
95 | WHERE u.user_id={?}", $_uid); |
96 | list($this->prenom, $this->nom, $this->promo, $this->forlife, $this->bestalias) = $res->fetchOneRow(); |
97 | } |
98 | |
99 | // }}} |
100 | // {{{ function submit() |
101 | |
102 | /** fonction à utiliser pour envoyer les données à la modération |
103 | * cette fonction supprimme les doublons sur un couple ($user,$type) si $this->unique est vrai |
104 | */ |
105 | function submit () |
106 | { |
0337d704 |
107 | if ($this->unique) { |
08cce2ff |
108 | XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?}', $this->uid, $this->type); |
0337d704 |
109 | } |
110 | |
111 | $this->stamp = date('YmdHis'); |
08cce2ff |
112 | XDB::execute('INSERT INTO requests (user_id, type, data, stamp) VALUES ({?}, {?}, {?}, {?})', |
0337d704 |
113 | $this->uid, $this->type, $this, $this->stamp); |
114 | |
115 | return true; |
116 | } |
117 | |
118 | // }}} |
119 | // {{{ function update() |
120 | |
121 | function update () |
122 | { |
08cce2ff |
123 | XDB::execute('UPDATE requests SET data={?}, stamp=stamp |
0337d704 |
124 | WHERE user_id={?} AND type={?} AND stamp={?}', |
125 | $this, $this->uid, $this->type, $this->stamp); |
126 | |
127 | return true; |
128 | } |
129 | |
130 | // }}} |
131 | // {{{ function clean() |
132 | |
133 | /** fonction à utiliser pour nettoyer l'entrée de la requête dans la table requests |
134 | * attention, tout est supprimé si c'est un unique |
135 | */ |
136 | function clean () |
137 | { |
0337d704 |
138 | if ($this->unique) { |
08cce2ff |
139 | return XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?}', |
0337d704 |
140 | $this->uid, $this->type); |
141 | } else { |
08cce2ff |
142 | return XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?} AND stamp={?}', |
0337d704 |
143 | $this->uid, $this->type, $this->stamp); |
144 | } |
145 | } |
146 | |
147 | // }}} |
148 | // {{{ function handle_formu() |
149 | |
150 | /** fonction à réaliser en cas de valistion du formulaire |
151 | */ |
152 | function handle_formu() |
153 | { |
154 | if (Env::has('delete')) { |
155 | $this->clean(); |
156 | $this->trig('requete supprimée'); |
157 | return true; |
158 | } |
159 | |
6aa01fed |
160 | // mise à jour des informations |
161 | if (Env::has('edit')) { |
162 | if ($this->handle_editor()) { |
163 | $this->update(); |
164 | $this->trig('requête mise à jour'); |
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 | } |
178 | $this->comments[] = Array(S::v('bestalias'), Env::v('comm'), $formid); |
0337d704 |
179 | |
180 | // envoi d'un mail à hotliners |
181 | global $globals; |
182 | require_once('diogenes/diogenes.hermes.inc.php'); |
183 | $mailer = new HermesMailer; |
184 | $mailer->setSubject("Commentaires de validation {$this->type}"); |
185 | $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}"); |
c33efb7c |
186 | $mailer->addTo("hotliners@staff.polytechnique.org"); |
0337d704 |
187 | |
188 | $body = "Validation {$this->type} pour {$this->prenom} {$this->nom}\n\n" |
cab08090 |
189 | . S::v('bestalias')." a ajouté le commentaire :\n\n" |
5e2307dc |
190 | . Env::v('comm')."\n\n" |
8c7809b5 |
191 | . "cf la discussion sur : ".$globals->baseurl."/admin/validate"; |
0337d704 |
192 | |
193 | $mailer->setTxtBody(wordwrap($body)); |
194 | $mailer->send(); |
195 | |
196 | $this->update(); |
197 | $this->trig('commentaire ajouté'); |
198 | return true; |
199 | } |
200 | |
201 | if (Env::has('accept')) { |
202 | if ($this->commit()) { |
203 | $this->sendmail(true); |
204 | $this->clean(); |
205 | $this->trig('mail envoyé'); |
206 | return true; |
207 | } else { |
208 | $this->trig('erreur lors de la validation'); |
209 | return false; |
210 | } |
211 | } |
212 | |
213 | if (Env::has('refuse')) { |
5e2307dc |
214 | if (Env::v('comm')) { |
0337d704 |
215 | $this->sendmail(false); |
216 | $this->clean(); |
217 | $this->trig('mail envoyé'); |
218 | return true; |
219 | } else { |
220 | $this->trig('pas de motivation pour le refus !!!'); |
221 | } |
222 | } |
223 | |
224 | return false; |
225 | } |
226 | |
227 | // }}} |
228 | // {{{ function sendmail |
229 | |
230 | function sendmail($isok) |
231 | { |
232 | global $globals; |
233 | require_once('diogenes/diogenes.hermes.inc.php'); |
234 | $mailer = new HermesMailer; |
235 | $mailer->setSubject($this->_mail_subj()); |
236 | $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}"); |
237 | $mailer->addTo("\"{$this->prenom} {$this->nom}\" <{$this->bestalias}@{$globals->mail->domain}>"); |
238 | $mailer->addCc("validation+{$this->type}@{$globals->mail->domain}"); |
239 | |
240 | $body = "Cher(e) camarade,\n\n" |
241 | . $this->_mail_body($isok) |
5e2307dc |
242 | . (Env::has('comm') ? "\n\n".Env::v('comm') : '') |
0337d704 |
243 | . "\n\nCordialement,\nL'équipe Polytechnique.org\n"; |
244 | |
245 | $mailer->setTxtBody(wordwrap($body)); |
246 | $mailer->send(); |
247 | } |
248 | |
249 | // }}} |
250 | // {{{ function trig() |
251 | |
252 | function trig($msg) { |
253 | global $page; |
254 | $page->trig($msg); |
255 | } |
256 | |
257 | // }}} |
258 | // {{{ function get_request() |
259 | |
260 | /** fonction statique qui renvoie la requête de l'utilisateur d'id $uidau timestamp $t |
261 | * @param $uid l'id de l'utilisateur concerné |
262 | * @param $type le type de la requête |
263 | * @param $stamp le timestamp de la requête |
264 | * |
265 | * XXX fonction "statique" XXX |
266 | * à utiliser uniquement pour récupérer un objet dans la BD avec Validate::get_request(...) |
267 | */ |
268 | function get_request($uid, $type, $stamp = -1) |
269 | { |
0337d704 |
270 | if ($stamp == -1) { |
08cce2ff |
271 | $res = XDB::query('SELECT data FROM requests WHERE user_id={?} and type={?}', $uid, $type); |
0337d704 |
272 | } else { |
08cce2ff |
273 | $res = XDB::query("SELECT data, stamp FROM requests WHERE user_id={?} AND type={?} and stamp={?}", $uid, $type, $stamp); |
0337d704 |
274 | } |
275 | if ($result = $res->fetchOneCell()) { |
276 | $result = unserialize($result); |
277 | } else { |
278 | $result = false; |
279 | } |
280 | return($result); |
281 | } |
282 | |
283 | // }}} |
284 | // {{{ function _mail_body |
285 | |
286 | function _mail_body($isok) |
287 | { |
288 | } |
289 | |
290 | // }}} |
291 | // {{{ function _mail_subj |
292 | |
293 | function _mail_subj() |
294 | { |
295 | } |
296 | |
297 | // }}} |
298 | // {{{ function commit() |
299 | |
300 | /** fonction à utiliser pour insérer les données dans x4dat |
301 | * XXX la fonction est "virtuelle" XXX |
302 | */ |
303 | function commit () |
304 | { } |
305 | |
306 | // }}} |
307 | // {{{ function formu() |
308 | |
309 | /** nom du template qui contient le formulaire */ |
310 | function formu() |
311 | { return null; } |
312 | |
313 | // }}} |
6aa01fed |
314 | // {{{ function editor() |
315 | |
316 | /** nom du formulaire d'édition */ |
317 | function editor() |
318 | { return null; } |
319 | |
320 | // }}} |
e18888f4 |
321 | // {{{ function answers() |
322 | |
323 | /** automatic answers table for this type of validation */ |
324 | function answers() { |
325 | static $answers_table; |
326 | if (!isset($answers_table[$this->type])) { |
327 | $r = XDB::query("SELECT id, title, answer FROM requests_answers WHERE category = {?}", $this->type); |
328 | $answers_table[$this->type] = $r->fetchAllAssoc($r); |
329 | } |
330 | return $answers_table[$this->type]; |
331 | } |
332 | |
333 | // }}} |
0337d704 |
334 | } |
335 | |
0337d704 |
336 | foreach (glob(dirname(__FILE__).'/validations/*.inc.php') as $file) { |
337 | require_once($file); |
338 | } |
339 | |
0337d704 |
340 | /* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker: */ |
341 | ?> |