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