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