bug 413, quand on creait un nouvel evenement. si on faisait une faute toutes les...
[platal.git] / htdocs.net / groupe / evt-modif.php
CommitLineData
258b9710 1<?php
2// vim:set et sw=4 sts=4 sws=4 foldmethod=marker:
3/***************************************************************************
4 * Copyright (C) 2003-2004 Polytechnique.org *
5 * http://opensource.polytechnique.org/ *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the Free Software *
19 * Foundation, Inc., *
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
21 ***************************************************************************/
22
23require 'xnet.inc.php';
24
25new_group_page('xnet/groupe/evt-modif.tpl');
26
27$page->assign('logged', logged());
28$page->assign('admin', may_update());
29
30$moments = range(1, 4);
31$page->assign('moments', $moments);
32
33$page->assign('eid', Env::get('eid'));
34
35if (!may_update())
36 redirect("evenements.php");
37
38if ($eid = Env::get('eid')) {
39 $res = $globals->xdb->query("SELECT asso_id, short_name FROM groupex.evenements WHERE eid = {?}", $eid);
40 $infos = $res->fetchOneAssoc();
41 if ($infos['asso_id'] != $globals->asso('id')) {
42 unset($eid);
43 unset($infos);
44 }
45}
46
47$get_form = true;
48
49if (Post::get('intitule')) {
50 $get_form = false;
51 $short_name = Env::get('short_name');
52 //Quelques vérifications sur l'alias (caractères spéciaux)
53 if ($short_name && !preg_match( "/^[a-zA-Z0-9\-.]{3,20}$/", $short_name))
54 {
55 $page->trig("Le raccourci demandé n'est pas valide.
56 Vérifie qu'il comporte entre 3 et 20 caractères
57 et qu'il ne contient que des lettres non accentuées,
58 des chiffres ou les caractères - et .");
59 $short_name = $infos['short_name'];
60 $get_form = true;
61 }
62 //vérifier que l'alias n'est pas déja pris
63 if ($short_name && $short_name != $infos['short_name']) {
64 $res = $globals->xdb->query('SELECT COUNT(*) FROM virtual WHERE alias LIKE {?}', $short_name."-%");
65 if ($res->fetchOneCell() > 0) {
66 $page->trig("Le raccourci demandé est déjà utilisé. Choisis en un autre.");
67 $short_name = $infos['short_name'];
68 $get_form = true;
69 }
70 }
71
72 // if had a previous shortname change the old lists
73 if ($short_name && $infos['short_name'] && $short_name != $infos['short_name']) {
74 $globals->xdb->execute("UPDATE virtual SET alias = REPLACE(alias, {?}, {?}) WHERE type = 'evt' AND alias LIKE {?}",
75 $infos['short_name'], $short_name, $infos['short_name']."-%");
76 }
77 // if we have a first new short_name create the lists
78 elseif ($short_name && !$infos['short_name'])
79 {
80 $globals->xdb->execute("INSERT INTO virtual SET type = 'evt', alias = {?}",
81 $short_name."-participants@".$globals->xnet->evts_domain);
82
83 $res = $globals->xdb->query("SELECT LAST_INSERT_ID()");
84 $globals->xdb->execute("INSERT INTO virtual_redirect (
85 SELECT {?} AS vid, IF(u.nom IS NULL, m.email, CONCAT(a.alias, {?})) AS redirect
86 FROM groupex.evenements_participants AS ep
87 LEFT JOIN groupex.membres AS m ON (ep.uid = m.uid)
88 LEFT JOIN auth_user_md5 AS u ON (u.user_id = ep.uid)
89 LEFT JOIN aliases AS a ON (a.id = ep.uid AND a.type = 'a_vie')
90 WHERE ep.eid = {?}
91 GROUP BY ep.uid)",
92 $res->fetchOneCell(), "@".$globals->mail->domain, $eid);
93
94 $globals->xdb->execute("INSERT INTO virtual SET type = 'evt', alias = {?}",
95 $short_name."-absents@".$globals->xnet->evts_domain);
96
97 $res = $globals->xdb->query("SELECT LAST_INSERT_ID()");
98 $globals->xdb->execute("INSERT INTO virtual_redirect (
99 SELECT {?} AS vid, IF(u.nom IS NULL, m.email, CONCAT(a.alias, {?})) AS redirect
100 FROM groupex.membres AS m
101 LEFT JOIN groupex.evenements_participants AS ep ON (ep.uid = m.uid)
102 LEFT JOIN auth_user_md5 AS u ON (u.user_id = m.uid)
103 LEFT JOIN aliases AS a ON (a.id = m.uid AND a.type = 'a_vie')
104 WHERE m.asso_id = {?} AND ep.uid IS NULL
105 GROUP BY m.uid)",
106 $res->fetchOneCell(), "@".$globals->mail->domain, $globals->asso('id'));
107 }
108 // if we delete the old short name, delete the lists
109 elseif (!$short_name && $infos['short_name']) {
110 $globals->xdb->execute("DELETE virtual, virtual_redirect FROM virtual LEFT JOIN virtual_redirect USING(vid) WHERE virtual.alias LIKE {?}",
111 $infos['short_name']."-%");
112 }
113
114 $evt = array();
115 $evt['eid'] = $eid;
116 $evt['asso_id'] = $globals->asso('id');
117 $evt['organisateur_uid'] = Session::get('uid');
118 $evt['intitule'] = Post::get('intitule');
119 $evt['paiement_id'] =(Post::get('paiement_id')>0)?Post::get('paiement_id'):null;
120 $evt['descriptif'] =Post::get('descriptif');
121 $evt['debut'] = Post::get('deb_Year')."-".Post::get('deb_Month')."-".Post::get('deb_Day')." ".Post::get('deb_Hour').":".Post::get('deb_Minute').":00";
122 $evt['fin'] = Post::get('fin_Year')."-".Post::get('fin_Month')."-".Post::get('fin_Day')." ".Post::get('fin_Hour').":".Post::get('fin_Minute').":00";
123 $evt['membres_only'] = Post::get('membres_only');
124 $evt['advertise'] = Post::get('advertise');
125 $evt['show_participants'] = Post::get('show_participants');
576208f3 126 if (!$short_name) $short_name = '';
258b9710 127 $evt['short_name'] = $short_name;
128 $evt['deadline_inscription'] = (Post::get('deadline', 'off')=='on')?null:(Post::get('inscr_Year')."-".Post::get('inscr_Month')."-".Post::get('inscr_Day'));
129
130 // Store the modifications in the database
131 $globals->xdb->execute("REPLACE INTO groupex.evenements
132 SET eid={?}, asso_id={?}, organisateur_uid={?}, intitule={?},
133 paiement_id = {?}, descriptif = {?},
134 debut = {?}, fin = {?},
135 membres_only = {?}, advertise = {?}, show_participants = {?},
136 short_name = {?}, deadline_inscription = {?}",
137 $evt['eid'], $evt['asso_id'], $evt['organisateur_uid'], $evt['intitule']
138 , $evt['paiement_id'], $evt['descriptif'],
139 $evt['debut'], $evt['fin'],
140 $evt['membres_only'], $evt['advertise'], $evt['show_participants'],
141 $evt['short_name'], $evt['deadline_inscription']);
142
143 // if new event, get its id
144 if (!$eid) {
145 $res = $globals->xdb->query("SELECT LAST_INSERT_ID()");
146 $eid = $res->fetchOneCell();
147 $evt['eid'] = $eid;
148 }
149
150 $nb_moments = 0;
151 $money_defaut = 0;
152 foreach ($moments as $i)
153 {
154 if (Post::get('titre'.$i)) {
155 $nb_moments++;
156 if (!($money_defaut > 0))
157 $money_defaut = strtr(Post::get('montant'.$i), ',', '.');
158 $globals->xdb->execute("
159 REPLACE INTO groupex.evenements_items VALUES (
160 {?}, {?},
161 {?}, {?}, {?})",
162 $eid, $i,
163 Post::get('titre'.$i), Post::get('details'.$i), strtr(Post::get('montant'.$i), ',', '.'));
164 }
165 else
166 {
167 $globals->xdb->execute("DELETE FROM groupex.evenements_items WHERE eid = {?} AND item_id = {?}", $eid, $i);
168 }
169 }
170
171 // request for a new payment
172 if (Post::get('paiement_id') == -1 && $money_defaut >= 0) {
576208f3 173 require_once ('validations.inc.php');
174 $p = new PayReq(
258b9710 175 Session::get('uid'),
176 Post::get('intitule')." - ".$globals->asso('nom'),
177 Post::get('site'),
178 $money_defaut,
179 Post::get('confirmation'),
180 0,
181 999,
182 $globals->asso('id'),
183 $eid);
576208f3 184 $p->submit();
258b9710 185 }
186
187 // events with no sub-event: add a sub-event with no name
188 if ($nb_moments == 0)
189 $globals->xdb->execute("INSERT INTO groupex.evenements_items VALUES ({?}, {?}, '', '', 0)", $eid, 1);
190}
191
192if (Env::has('sup') && $eid) {
193 // deletes the event
194 $globals->xdb->execute("DELETE FROM groupex.evenements WHERE eid = {?} AND asso_id = {?}", $eid, $globals->asso('id'));
195 // deletes the event items
196 $globals->xdb->execute("DELETE FROM groupex.evenements_items WHERE eid = {?}", $eid);
197 // deletes the event participants
198 $globals->xdb->execute("DELETE FROM groupex.evenements_participants WHERE eid = {?}", $eid);
199 // deletes the event mailing aliases
200 if ($infos['short_name'])
201 $globals->xdb->execute("DELETE FROM virtual WHERE type = 'evt' AND alias LIKE {?}", $infos['short_name']."-%");
202 // delete the requests for payments
203 require_once('validations.inc.php');
204 $globals->xdb->execute("DELETE FROM requests WHERE type = 'paiements' AND data LIKE {?}", PayReq::same_event($eid, $globals->asso('id')));
df8fe359 205 redirect("evenements.php");
258b9710 206}
207
208if (!$get_form)
be007d27 209 redirect("evenements.php");
258b9710 210
211// get a list of all the payment for this asso
212$res = $globals->xdb->iterator
213 ("SELECT id, text FROM {$globals->money->mpay_tprefix}paiements WHERE asso_id = {?}", $globals->asso('id'));
214$paiements = array();
215while ($a = $res->next()) $paiements[$a['id']] = $a['text'];
216 $page->assign('paiements', $paiements);
217
218// when modifying an old event retreive the old datas
219if ($eid) {
220 $res = $globals->xdb->query(
221 "SELECT eid, intitule, descriptif, debut, fin, membres_only, advertise, show_participants, paiement_id, short_name, deadline_inscription
222 FROM groupex.evenements
223 WHERE eid = {?}", $eid);
224 $evt = $res->fetchOneAssoc();
225 // find out if there is already a request for a payment for this event
226 require_once('validations.inc.php');
227 $res = $globals->xdb->query("SELECT stamp FROM requests WHERE type = 'paiements' AND data LIKE {?}", PayReq::same_event($eid, $globals->asso('id')));
228 $stamp = $res->fetchOneCell();
229 if ($stamp) {
230 $evt['paiement_id'] = -2;
231 $evt['paiement_req'] = $stamp;
232 }
233 $page->assign('evt', $evt);
234 // get all the different moments infos
235 $res = $globals->xdb->iterator(
236 "SELECT item_id, titre, details, montant
237 FROM groupex.evenements_items AS ei
238 INNER JOIN groupex.evenements AS e ON(e.eid = ei.eid)
239 WHERE e.eid = {?}
240 ORDER BY item_id", $eid);
241 $items = array();
242 while ($item = $res->next()) $items[$item['item_id']] = $item;
243 $page->assign('items', $items);
244}
245
246$page->run();
247
248?>