c331916d2b31b9f15f8cd0d201a89e832b97d05b
[platal.git] / htdocs.net / groupe / evt-modif.php
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
23 require 'xnet.inc.php';
24
25 new_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
35 if (!may_update())
36 redirect("evenements.php");
37
38 if ($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
49 if (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');
126 $evt['short_name'] = $short_name;
127 $evt['deadline_inscription'] = (Post::get('deadline', 'off')=='on')?null:(Post::get('inscr_Year')."-".Post::get('inscr_Month')."-".Post::get('inscr_Day'));
128
129 // Store the modifications in the database
130 $globals->xdb->execute("REPLACE INTO groupex.evenements
131 SET eid={?}, asso_id={?}, organisateur_uid={?}, intitule={?},
132 paiement_id = {?}, descriptif = {?},
133 debut = {?}, fin = {?},
134 membres_only = {?}, advertise = {?}, show_participants = {?},
135 short_name = {?}, deadline_inscription = {?}",
136 $evt['eid'], $evt['asso_id'], $evt['organisateur_uid'], $evt['intitule']
137 , $evt['paiement_id'], $evt['descriptif'],
138 $evt['debut'], $evt['fin'],
139 $evt['membres_only'], $evt['advertise'], $evt['show_participants'],
140 $evt['short_name'], $evt['deadline_inscription']);
141
142 // if new event, get its id
143 if (!$eid) {
144 $res = $globals->xdb->query("SELECT LAST_INSERT_ID()");
145 $eid = $res->fetchOneCell();
146 $evt['eid'] = $eid;
147 }
148
149 $nb_moments = 0;
150 $money_defaut = 0;
151 foreach ($moments as $i)
152 {
153 if (Post::get('titre'.$i)) {
154 $nb_moments++;
155 if (!($money_defaut > 0))
156 $money_defaut = strtr(Post::get('montant'.$i), ',', '.');
157 $globals->xdb->execute("
158 REPLACE INTO groupex.evenements_items VALUES (
159 {?}, {?},
160 {?}, {?}, {?})",
161 $eid, $i,
162 Post::get('titre'.$i), Post::get('details'.$i), strtr(Post::get('montant'.$i), ',', '.'));
163 }
164 else
165 {
166 $globals->xdb->execute("DELETE FROM groupex.evenements_items WHERE eid = {?} AND item_id = {?}", $eid, $i);
167 }
168 }
169
170 // request for a new payment
171 if (Post::get('paiement_id') == -1 && $money_defaut >= 0) {
172 require_once ('validations.inc.php');
173 $p = new PayReq(
174 Session::get('uid'),
175 Post::get('intitule')." - ".$globals->asso('nom'),
176 Post::get('site'),
177 $money_defaut,
178 Post::get('confirmation'),
179 0,
180 999,
181 $globals->asso('id'),
182 $eid);
183 $p->submit();
184 }
185
186 // events with no sub-event: add a sub-event with no name
187 if ($nb_moments == 0)
188 $globals->xdb->execute("INSERT INTO groupex.evenements_items VALUES ({?}, {?}, '', '', 0)", $eid, 1);
189 }
190
191 if (Env::has('sup') && $eid) {
192 // deletes the event
193 $globals->xdb->execute("DELETE FROM groupex.evenements WHERE eid = {?} AND asso_id = {?}", $eid, $globals->asso('id'));
194 // deletes the event items
195 $globals->xdb->execute("DELETE FROM groupex.evenements_items WHERE eid = {?}", $eid);
196 // deletes the event participants
197 $globals->xdb->execute("DELETE FROM groupex.evenements_participants WHERE eid = {?}", $eid);
198 // deletes the event mailing aliases
199 if ($infos['short_name'])
200 $globals->xdb->execute("DELETE FROM virtual WHERE type = 'evt' AND alias LIKE {?}", $infos['short_name']."-%");
201 // delete the requests for payments
202 require_once('validations.inc.php');
203 $globals->xdb->execute("DELETE FROM requests WHERE type = 'paiements' AND data LIKE {?}", PayReq::same_event($eid, $globals->asso('id')));
204 header("Location: evenements.php");
205 die();
206 }
207
208 if (!$get_form)
209 redirect("evenements.php");
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();
215 while ($a = $res->next()) $paiements[$a['id']] = $a['text'];
216 $page->assign('paiements', $paiements);
217
218 // when modifying an old event retreive the old datas
219 if ($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 ?>