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; |
70 | var $type; |
71 | var $comments = Array(); |
72 | // the validations rules : comments for admins |
73 | 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"; |
74 | |
75 | // }}} |
76 | // {{{ constructor |
77 | |
78 | /** constructeur |
79 | * @param $_uid user id |
80 | * @param $_unique requête pouvant être multiple ou non |
81 | * @param $_type type de la donnée comme dans le champ type de x4dat.requests |
82 | */ |
83 | function Validate($_uid, $_unique, $_type) |
84 | { |
0337d704 |
85 | $this->uid = $_uid; |
86 | $this->stamp = date('YmdHis'); |
87 | $this->unique = $_unique; |
88 | $this->type = $_type; |
08cce2ff |
89 | $res = XDB::query( |
0337d704 |
90 | "SELECT u.prenom, u.nom, u.promo, a.alias, b.alias |
91 | FROM auth_user_md5 AS u |
92 | INNER JOIN aliases AS a ON ( u.user_id=a.id AND a.type='a_vie' ) |
93 | INNER JOIN aliases AS b ON ( u.user_id=b.id AND b.type!='homonyme' AND FIND_IN_SET('bestalias', b.flags) ) |
94 | WHERE u.user_id={?}", $_uid); |
95 | list($this->prenom, $this->nom, $this->promo, $this->forlife, $this->bestalias) = $res->fetchOneRow(); |
96 | } |
97 | |
98 | // }}} |
99 | // {{{ function submit() |
100 | |
101 | /** fonction à utiliser pour envoyer les données à la modération |
102 | * cette fonction supprimme les doublons sur un couple ($user,$type) si $this->unique est vrai |
103 | */ |
104 | function submit () |
105 | { |
0337d704 |
106 | if ($this->unique) { |
08cce2ff |
107 | XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?}', $this->uid, $this->type); |
0337d704 |
108 | } |
109 | |
110 | $this->stamp = date('YmdHis'); |
08cce2ff |
111 | XDB::execute('INSERT INTO requests (user_id, type, data, stamp) VALUES ({?}, {?}, {?}, {?})', |
0337d704 |
112 | $this->uid, $this->type, $this, $this->stamp); |
113 | |
114 | return true; |
115 | } |
116 | |
117 | // }}} |
118 | // {{{ function update() |
119 | |
120 | function update () |
121 | { |
08cce2ff |
122 | XDB::execute('UPDATE requests SET data={?}, stamp=stamp |
0337d704 |
123 | WHERE user_id={?} AND type={?} AND stamp={?}', |
124 | $this, $this->uid, $this->type, $this->stamp); |
125 | |
126 | return true; |
127 | } |
128 | |
129 | // }}} |
130 | // {{{ function clean() |
131 | |
132 | /** fonction à utiliser pour nettoyer l'entrée de la requête dans la table requests |
133 | * attention, tout est supprimé si c'est un unique |
134 | */ |
135 | function clean () |
136 | { |
0337d704 |
137 | if ($this->unique) { |
08cce2ff |
138 | return XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?}', |
0337d704 |
139 | $this->uid, $this->type); |
140 | } else { |
08cce2ff |
141 | return XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?} AND stamp={?}', |
0337d704 |
142 | $this->uid, $this->type, $this->stamp); |
143 | } |
144 | } |
145 | |
146 | // }}} |
147 | // {{{ function handle_formu() |
148 | |
149 | /** fonction à réaliser en cas de valistion du formulaire |
150 | */ |
151 | function handle_formu() |
152 | { |
153 | if (Env::has('delete')) { |
154 | $this->clean(); |
155 | $this->trig('requete supprimée'); |
156 | return true; |
157 | } |
158 | |
159 | // ajout d'un commentaire |
160 | if (Env::has('hold') && Env::has('comm')) { |
5e2307dc |
161 | $this->comments[] = Array(S::v('bestalias'), Env::v('comm')); |
0337d704 |
162 | |
163 | // envoi d'un mail à hotliners |
164 | global $globals; |
165 | require_once('diogenes/diogenes.hermes.inc.php'); |
166 | $mailer = new HermesMailer; |
167 | $mailer->setSubject("Commentaires de validation {$this->type}"); |
168 | $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}"); |
169 | $mailer->addTo("hotliners@{$globals->mail->domain}"); |
170 | |
171 | $body = "Validation {$this->type} pour {$this->prenom} {$this->nom}\n\n" |
cab08090 |
172 | . S::v('bestalias')." a ajouté le commentaire :\n\n" |
5e2307dc |
173 | . Env::v('comm')."\n\n" |
0337d704 |
174 | . "cf la discussion sur : ".$globals->baseurl."/admin/valider.php"; |
175 | |
176 | $mailer->setTxtBody(wordwrap($body)); |
177 | $mailer->send(); |
178 | |
179 | $this->update(); |
180 | $this->trig('commentaire ajouté'); |
181 | return true; |
182 | } |
183 | |
184 | if (Env::has('accept')) { |
185 | if ($this->commit()) { |
186 | $this->sendmail(true); |
187 | $this->clean(); |
188 | $this->trig('mail envoyé'); |
189 | return true; |
190 | } else { |
191 | $this->trig('erreur lors de la validation'); |
192 | return false; |
193 | } |
194 | } |
195 | |
196 | if (Env::has('refuse')) { |
5e2307dc |
197 | if (Env::v('comm')) { |
0337d704 |
198 | $this->sendmail(false); |
199 | $this->clean(); |
200 | $this->trig('mail envoyé'); |
201 | return true; |
202 | } else { |
203 | $this->trig('pas de motivation pour le refus !!!'); |
204 | } |
205 | } |
206 | |
207 | return false; |
208 | } |
209 | |
210 | // }}} |
211 | // {{{ function sendmail |
212 | |
213 | function sendmail($isok) |
214 | { |
215 | global $globals; |
216 | require_once('diogenes/diogenes.hermes.inc.php'); |
217 | $mailer = new HermesMailer; |
218 | $mailer->setSubject($this->_mail_subj()); |
219 | $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}"); |
220 | $mailer->addTo("\"{$this->prenom} {$this->nom}\" <{$this->bestalias}@{$globals->mail->domain}>"); |
221 | $mailer->addCc("validation+{$this->type}@{$globals->mail->domain}"); |
222 | |
223 | $body = "Cher(e) camarade,\n\n" |
224 | . $this->_mail_body($isok) |
5e2307dc |
225 | . (Env::has('comm') ? "\n\n".Env::v('comm') : '') |
0337d704 |
226 | . "\n\nCordialement,\nL'équipe Polytechnique.org\n"; |
227 | |
228 | $mailer->setTxtBody(wordwrap($body)); |
229 | $mailer->send(); |
230 | } |
231 | |
232 | // }}} |
233 | // {{{ function trig() |
234 | |
235 | function trig($msg) { |
236 | global $page; |
237 | $page->trig($msg); |
238 | } |
239 | |
240 | // }}} |
241 | // {{{ function get_request() |
242 | |
243 | /** fonction statique qui renvoie la requête de l'utilisateur d'id $uidau timestamp $t |
244 | * @param $uid l'id de l'utilisateur concerné |
245 | * @param $type le type de la requête |
246 | * @param $stamp le timestamp de la requête |
247 | * |
248 | * XXX fonction "statique" XXX |
249 | * à utiliser uniquement pour récupérer un objet dans la BD avec Validate::get_request(...) |
250 | */ |
251 | function get_request($uid, $type, $stamp = -1) |
252 | { |
0337d704 |
253 | if ($stamp == -1) { |
08cce2ff |
254 | $res = XDB::query('SELECT data FROM requests WHERE user_id={?} and type={?}', $uid, $type); |
0337d704 |
255 | } else { |
08cce2ff |
256 | $res = XDB::query("SELECT data, stamp FROM requests WHERE user_id={?} AND type={?} and stamp={?}", $uid, $type, $stamp); |
0337d704 |
257 | } |
258 | if ($result = $res->fetchOneCell()) { |
259 | $result = unserialize($result); |
260 | } else { |
261 | $result = false; |
262 | } |
263 | return($result); |
264 | } |
265 | |
266 | // }}} |
267 | // {{{ function _mail_body |
268 | |
269 | function _mail_body($isok) |
270 | { |
271 | } |
272 | |
273 | // }}} |
274 | // {{{ function _mail_subj |
275 | |
276 | function _mail_subj() |
277 | { |
278 | } |
279 | |
280 | // }}} |
281 | // {{{ function commit() |
282 | |
283 | /** fonction à utiliser pour insérer les données dans x4dat |
284 | * XXX la fonction est "virtuelle" XXX |
285 | */ |
286 | function commit () |
287 | { } |
288 | |
289 | // }}} |
290 | // {{{ function formu() |
291 | |
292 | /** nom du template qui contient le formulaire */ |
293 | function formu() |
294 | { return null; } |
295 | |
296 | // }}} |
e18888f4 |
297 | // {{{ function answers() |
298 | |
299 | /** automatic answers table for this type of validation */ |
300 | function answers() { |
301 | static $answers_table; |
302 | if (!isset($answers_table[$this->type])) { |
303 | $r = XDB::query("SELECT id, title, answer FROM requests_answers WHERE category = {?}", $this->type); |
304 | $answers_table[$this->type] = $r->fetchAllAssoc($r); |
305 | } |
306 | return $answers_table[$this->type]; |
307 | } |
308 | |
309 | // }}} |
0337d704 |
310 | } |
311 | |
0337d704 |
312 | foreach (glob(dirname(__FILE__).'/validations/*.inc.php') as $file) { |
313 | require_once($file); |
314 | } |
315 | |
0337d704 |
316 | /* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker: */ |
317 | ?> |