Merge remote branch 'origin/platal-0.10.2'
[platal.git] / include / validations.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
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
22 define('SIZE_MAX', 32768);
23
24 global $globals;
25 require_once $globals->spoolroot . '/core/classes/xdb.php';
26
27 /**
28 * Iterator class, that lists objects through the database
29 */
30 class ValidateIterator extends XOrgDBIterator
31 {
32 // {{{ constuctor
33
34 public function __construct ()
35 {
36 parent::__construct('SELECT data, DATE_FORMAT(stamp, "%Y%m%d%H%i%s") FROM requests ORDER BY stamp', MYSQL_NUM);
37 }
38
39 // }}}
40 // {{{ function next()
41
42 public function next ()
43 {
44 if (list($result, $stamp) = parent::next()) {
45 $result = Validate::unserialize($result);
46 $result->stamp = $stamp;
47 return($result);
48 } else {
49 return null;
50 }
51 }
52
53 // }}}
54 }
55
56 /** classe "virtuelle" à dériver pour chaque nouvelle implémentation
57 */
58 abstract class Validate
59 {
60 // {{{ properties
61
62 public $user;
63
64 public $stamp;
65 public $unique;
66 // enable the refuse button
67 public $refuse = true;
68
69 public $type;
70 public $comments = Array();
71 // the validations rules : comments for admins
72 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";
73
74 // }}}
75 // {{{ constructor
76
77 /** constructeur
78 * @param $_user user object
79 * @param $_unique requête pouvant être multiple ou non
80 * @param $_type type de la donnée comme dans le champ type de x4dat.requests
81 */
82 public function __construct(User &$_user, $_unique, $_type)
83 {
84 $this->user = &$_user;
85 $this->stamp = date('YmdHis');
86 $this->unique = $_unique;
87 $this->type = $_type;
88 $this->promo = $this->user->promo();
89 }
90
91 // }}}
92 // {{{ function submit()
93
94 /** fonction à utiliser pour envoyer les données à la modération
95 * cette fonction supprimme les doublons sur un couple ($user,$type) si $this->unique est vrai
96 */
97 public function submit()
98 {
99 if ($this->unique) {
100 XDB::execute('DELETE FROM requests
101 WHERE uid = {?} AND type = {?}',
102 $this->user->id(), $this->type);
103 }
104
105 $this->stamp = date('YmdHis');
106 XDB::execute('INSERT INTO requests (uid, type, data, stamp)
107 VALUES ({?}, {?}, {?}, {?})',
108 $this->user->id(), $this->type, $this, $this->stamp);
109
110 global $globals;
111 $globals->updateNbValid();
112 return true;
113 }
114
115 // }}}
116 // {{{ function update()
117
118 protected function update()
119 {
120 XDB::execute('UPDATE requests
121 SET data = {?}, stamp = stamp
122 WHERE uid = {?} AND type = {?} AND stamp = {?}',
123 $this, $this->user->id(), $this->type, $this->stamp);
124 return true;
125 }
126
127 // }}}
128 // {{{ function clean()
129
130 /** fonction à utiliser pour nettoyer l'entrée de la requête dans la table requests
131 * attention, tout est supprimé si c'est un unique
132 */
133 public function clean()
134 {
135 global $globals;
136
137 if ($this->unique) {
138 $success = XDB::execute('DELETE FROM requests
139 WHERE uid = {?} AND type = {?}',
140 $this->user->id(), $this->type);
141 } else {
142 $success = XDB::execute('DELETE FROM requests
143 WHERE uid = {?} AND type = {?} AND stamp = {?}',
144 $this->user->id(), $this->type, $this->stamp);
145 }
146 $globals->updateNbValid();
147 return $success;
148 }
149
150 // }}}
151 // {{{ function handle_formu()
152
153 /** fonction à réaliser en cas de validation du formulaire
154 */
155 public function handle_formu()
156 {
157 if (Env::has('delete')) {
158 $this->clean();
159 $this->trigSuccess('Requête supprimée');
160 return true;
161 }
162
163 // mise à jour des informations
164 if (Env::has('edit')) {
165 if ($this->handle_editor()) {
166 $this->update();
167 $this->trigSuccess('Requête mise à jour');
168 return true;
169 }
170 return false;
171 }
172
173 // ajout d'un commentaire
174 if (Env::has('hold') && Env::has('comm')) {
175 $formid = Env::i('formid');
176 foreach ($this->comments as $comment) {
177 if ($comment[2] === $formid) {
178 return true;
179 }
180 }
181 if (!strlen(trim(Env::v('comm')))) {
182 return true;
183 }
184 $this->comments[] = Array(S::user()->login(), Env::v('comm'), $formid);
185
186 // envoi d'un mail à hotliners
187 global $globals;
188 $mailer = new PlMailer();
189 $mailer->setSubject("Commentaires de validation {$this->type}");
190 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
191 $mailer->addTo($globals->core->admin_email);
192
193 $body = "Validation {$this->type} pour {$this->user->login()}\n\n"
194 . S::user()->login() . " a ajouté le commentaire :\n\n"
195 . Env::v('comm') . "\n\n"
196 . "cf la discussion sur : " . $globals->baseurl . "/admin/validate";
197
198 $mailer->setTxtBody(wordwrap($body));
199 $mailer->send();
200
201 $this->update();
202 $this->trigSuccess('Commentaire ajouté');
203 return true;
204 }
205
206 if (Env::has('accept')) {
207 if ($this->commit()) {
208 $this->sendmail(true);
209 $this->clean();
210 $this->trigSuccess('Email de validation envoyé');
211 return true;
212 } else {
213 $this->trigError('Erreur lors de la validation');
214 return false;
215 }
216 }
217
218 if (Env::has('refuse')) {
219 if (Env::v('comm')) {
220 $this->sendmail(false);
221 $this->clean();
222 $this->trigSuccess('Email de refus envoyé');
223 return true;
224 } else {
225 $this->trigError('Pas de motivation pour le refus&nbsp;!!!');
226 }
227 }
228
229 return false;
230 }
231
232 // }}}
233 // {{{ function sendmail
234
235 protected function sendmail($isok)
236 {
237 global $globals;
238 $mailer = new PlMailer();
239 $mailer->setSubject($this->_mail_subj());
240 $mailer->setFrom("validation+{$this->type}@{$globals->mail->domain}");
241 $mailer->addTo("\"{$this->user->fullName()}\" <{$this->user->bestEmail()}>");
242 $mailer->addCc("validation+{$this->type}@{$globals->mail->domain}");
243
244 $body = ($this->user->isFemale() ? "Chère camarade,\n\n" : "Cher camarade,\n\n")
245 . $this->_mail_body($isok)
246 . (Env::has('comm') ? "\n\n" . Env::v('comm') : '')
247 . "\n\nCordialement,\n-- \nL'équipe de Polytechnique.org\n"
248 . $this->_mail_ps($isok);
249
250 $mailer->setTxtBody(wordwrap($body));
251 $mailer->send();
252 }
253
254 // }}}
255 // {{{ function trig()
256
257 protected function trigError($msg)
258 {
259 Platal::page()->trigError($msg);
260 }
261
262 protected function trigWarning($msg)
263 {
264 Platal::page()->trigWarning($msg);
265 }
266
267 protected function trigSuccess($msg)
268 {
269 Platal::page()->trigSuccess($msg);
270 }
271
272 // }}}
273 // {{{ function get_typed_request()
274
275 /** fonction statique qui renvoie la requête de l'utilisateur d'id $uidau timestamp $t
276 * @param $uid l'id de l'utilisateur concerné
277 * @param $type le type de la requête
278 * @param $stamp le timestamp de la requête
279 *
280 * XXX fonction "statique" XXX
281 * à utiliser uniquement pour récupérer un objet dans la BD avec Validate::get_typed_request(...)
282 */
283 static public function get_typed_request($uid, $type, $stamp = -1)
284 {
285 if ($stamp == -1) {
286 $res = XDB::query('SELECT data
287 FROM requests
288 WHERE uid = {?} and type = {?}',
289 $uid, $type);
290 } else {
291 $res = XDB::query('SELECT data, DATE_FORMAT(stamp, "%Y%m%d%H%i%s")
292 FROM requests
293 WHERE uid = {?} AND type = {?} and stamp = {?}',
294 $uid, $type, $stamp);
295 }
296 if ($result = $res->fetchOneCell()) {
297 $result = Validate::unserialize($result);
298 } else {
299 $result = false;
300 }
301 return($result);
302 }
303
304 // }}}
305 // {{{ function get_request_by_id()
306
307 static public function get_request_by_id($id)
308 {
309 list($uid, $type, $stamp) = explode('_', $id, 3);
310 return Validate::get_typed_request($uid, $type, $stamp);
311 }
312
313 // }}}
314 // {{{ function get_typed_requests()
315
316 /** same as get_typed_request() but return an array of objects
317 */
318 static public function get_typed_requests($uid, $type)
319 {
320 $res = XDB::iterRow('SELECT data
321 FROM requests
322 WHERE uid = {?} and type = {?}',
323 $uid, $type);
324 $array = array();
325 while (list($data) = $res->next()) {
326 $array[] = Validate::unserialize($data);
327 }
328 return $array;
329 }
330
331 // }}}
332 // {{{ function get_typed_requests_count()
333
334 /** same as get_typed_requests() but return the count of available requests.
335 */
336 static public function get_typed_requests_count($uid, $type)
337 {
338 $res = XDB::query('SELECT COUNT(data)
339 FROM requests
340 WHERE uid = {?} and type = {?}',
341 $uid, $type);
342 return $res->fetchOneCell();
343 }
344
345 // }}}
346 // {{{ function _mail_body
347
348 abstract protected function _mail_body($isok);
349
350 // }}}
351 // {{{ function _mail_subj
352
353 abstract protected function _mail_subj();
354
355 // }}}
356 // {{{ function _mail_ps
357
358 protected function _mail_ps($isok)
359 {
360 return '';
361 }
362
363 // }}}
364 // {{{ function commit()
365
366 /** fonction à utiliser pour insérer les données dans x4dat
367 */
368 abstract public function commit();
369
370 // }}}
371 // {{{ function formu()
372
373 /** nom du template qui contient le formulaire */
374 abstract public function formu();
375
376 // }}}
377 // {{{ function editor()
378
379 /** nom du formulaire d'édition */
380 public function editor()
381 {
382 return null;
383 }
384
385 // }}}
386 // {{{ function answers()
387
388 /** automatic answers table for this type of validation */
389 public function answers()
390 {
391 static $answers_table;
392 if (!isset($answers_table[$this->type])) {
393 $r = XDB::query("SELECT id, title, answer FROM requests_answers WHERE category = {?}", $this->type);
394 $answers_table[$this->type] = $r->fetchAllAssoc();
395 }
396 return $answers_table[$this->type];
397 }
398
399 // }}}
400 // {{{ function id()
401
402 public function id()
403 {
404 return $this->user->id() . '_' . $this->type . '_' . $this->stamp;
405 }
406
407 // }}}
408 // {{{ function ruleText()
409
410 public function ruleText()
411 {
412 return str_replace('\'', '\\\'', $this->rules);
413 }
414
415 // }}}
416 // {{{ function unserialize()
417 public static function unserialize($data)
418 {
419 $obj = unserialize($data);
420 /* XXX: Temporary for hruid migration */
421 if (!isset($obj->user) || !is_object($obj)) {
422 $obj->user =& User::get($obj->forlife);
423 }
424 /* XXX: End temporary block */
425 return $obj;
426 }
427 }
428
429 foreach (glob(dirname(__FILE__).'/validations/*.inc.php') as $file) {
430 require_once($file);
431 }
432
433 /* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker enc=utf-8: */
434 ?>