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