Reset rewritten, SQL fixed, zeros are now numeric
[mailer.git] / inc / modules / admin / what-list_rallyes.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 06/29/2004 *
4  * ================                             Last change: 02/11/2005 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-list_rallyes.php                            *
8  * -------------------------------------------------------------------- *
9  * Short description : List existing rallyes                            *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Bestehende Rallyes auflisten                     *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if ((ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) || (!IS_ADMIN()))
36 {
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
38         require($INC);
39 }
40
41 // Add description as navigation point
42 ADD_DESCR("admin", basename(__FILE__));
43
44 if (empty($_GET['sub'])) $_GET['sub'] = "";
45 $MSG = "";
46
47 // Quick actions on a rallye
48 if (isset($_GET['rallye']))
49 {
50         // Activate / deactivate
51         $SQL = "";
52         if (isset($_GET['activate']))
53         {
54                 switch ($_GET['activate'])
55                 {
56                 case "1": // Activate
57                         $SQL = "UPDATE "._MYSQL_PREFIX."_rallye_data SET is_active='Y' WHERE id=%s AND is_active='N' LIMIT 1";
58                         break;
59
60                 case "0": // Deactivate
61                         $SQL = "UPDATE "._MYSQL_PREFIX."_rallye_data SET is_active='N' WHERE id=%s AND is_active='Y' LIMIT 1";
62                         break;
63                 }
64         }
65
66         // Automatic notification
67         if (isset($_GET['notify']))
68         {
69                 switch ($_GET['notify'])
70                 {
71                 case "1": // Activate
72                         $SQL = "UPDATE "._MYSQL_PREFIX."_rallye_data SET send_notify='Y' WHERE id=%s AND send_notify='N' LIMIT 1";
73                         break;
74
75                 case "0": // Deactivate
76                         $SQL = "UPDATE "._MYSQL_PREFIX."_rallye_data SET send_notify='N' WHERE id=%s AND send_notify='Y' LIMIT 1";
77                         break;
78                 }
79         }
80
81         // Automatic adding of new members
82         if (isset($_GET['auto']))
83         {
84                 switch ($_GET['auto'])
85                 {
86                 case "1": // Activate
87                         $SQL = "UPDATE "._MYSQL_PREFIX."_rallye_data SET auto_add_new_user='Y' WHERE id=%s AND auto_add_new_user='N' LIMIT 1";
88                         break;
89
90                 case "0": // Deactivate
91                         $SQL = "UPDATE "._MYSQL_PREFIX."_rallye_data SET auto_add_new_user='N' WHERE id=%s AND auto_add_new_user='Y' LIMIT 1";
92                         break;
93                 }
94         }
95
96         // Run SQL command
97         if (!empty($SQL))
98         {
99                 $result = SQL_QUERY_ESC($SQL, array(bigintval($_GET['rallye'])), __FILE__, __LINE__);
100         }
101 }
102  elseif (isset($_POST['remove']))
103 {
104         // Delete rallyes
105         $SEL = SELECTION_COUNT($_POST['sel']);
106         if ($SEL > 0)
107         {
108                 // Delete selected rallyes and all it's data
109                 foreach ($_POST['sel'] as $id => $sel)
110                 {
111                         // Remove selected rallye entirely...
112                         $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_rallye_data WHERE id=%s LIMIT 1",
113                          array(bigintval($id)), __FILE__, __LINE__);
114                         $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_rallye_prices WHERE rallye_id=%s",
115                          array(bigintval($id)), __FILE__, __LINE__);
116                         $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_rallye_users WHERE rallye_id=%s",
117                          array(bigintval($id)), __FILE__, __LINE__);
118                 }
119
120                 // Output message
121                 $MSG = RALLYE_DELETED;
122         }
123          else
124         {
125                 // No rallye selected to delete!
126                 $MSG = RALLYE_DELETE_NOTHING_SELECTED;
127         }
128 }
129  elseif (isset($_POST['change']))
130 {
131         // Change rallye
132         $SEL = SELECTION_COUNT($_POST['title']);
133         if ($SEL > 0)
134         {
135                 // Change selected rallyes and all it's data
136                 foreach ($_POST['title'] as $id => $title)
137                 {
138                         // Secure ID number
139                         $id = bigintval($id);
140
141                         // Generate timestamps
142                         $START = mktime($_POST['start_hour'][$id], $_POST['start_min'][$id], $_POST['start_sec'][$id], $_POST['start_month'][$id], $_POST['start_day'][$id], $_POST['start_year'][$id]);
143                         $END   = mktime($_POST['end_hour'][$id]  , $_POST['end_min'][$id]  , $_POST['end_sec'][$id]  , $_POST['end_month'][$id]  , $_POST['end_day'][$id]  , $_POST['end_year'][$id]  );
144
145                         // Update entry
146                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_rallye_data SET
147 title='%s',
148 descr='%s',
149 template='%s',
150 start_time='%s',
151 end_time='%s',
152 min_users='%s',
153 min_prices='%s'
154 WHERE id='".$id."' LIMIT 1",
155  array($title, $_POST['descr'][$id], $_POST['templ'][$id], bigintval($START), bigintval($END), bigintval($_POST['min_users'][$id]), bigintval($_POST['min_prices'][$id]), $id),
156  __FILE__, __LINE__);
157                 }
158
159                 // Output message
160                 $MSG = RALLYE_CHANGED;
161         }
162 }
163
164 if (isset($_POST['edit']))
165 {
166         // Check for selections
167         $SEL = SELECTION_COUNT($_POST['sel']);
168         if ($SEL > 0)
169         {
170                 // Make all selected and deactivated rallyes editable
171                 $SW = 2; $OUT = "";
172                 foreach ($_POST['sel'] as $id => $sel)
173                 {
174                         // Load rallye basic data
175                         $result = SQL_QUERY_ESC("SELECT title, descr, template, start_time, end_time, min_users, min_prices FROM "._MYSQL_PREFIX."_rallye_data WHERE id=%s LIMIT 1",
176                          array(bigintval($id)), __FILE__, __LINE__);
177                         list($title, $descr, $templ, $start, $end, $min_users, $min_prices) = SQL_FETCHROW($result);
178                         SQL_FREERESULT($result);
179
180                         // Starting day
181                         $content['s_sec']   = ADD_SELECTION("sec"  , date("s", $start), "start", $id);
182                         $content['s_min']   = ADD_SELECTION("min"  , date("i", $start), "start", $id);
183                         $content['s_hour']  = ADD_SELECTION("hour" , date("G", $start), "start", $id);
184                         $content['s_day']   = ADD_SELECTION("day"  , date("d", $start), "start", $id);
185                         $content['s_month'] = ADD_SELECTION("month", date("m", $start), "start", $id);
186                         $content['s_year']  = ADD_SELECTION("year" , date('Y', $start), "start", $id);
187
188                         // Ending day
189                         $content['e_sec']   = ADD_SELECTION("sec"  , date("s", $end)  , "end"  , $id);
190                         $content['e_min']   = ADD_SELECTION("min"  , date("i", $end)  , "end"  , $id);
191                         $content['e_hour']  = ADD_SELECTION("hour" , date("G", $end)  , "end"  , $id);
192                         $content['e_day']   = ADD_SELECTION("day"  , date("d", $end)  , "end"  , $id);
193                         $content['e_month'] = ADD_SELECTION("month", date("m", $end)  , "end"  , $id);
194                         $content['e_year']  = ADD_SELECTION("year" , date('Y', $end)  , "end"  , $id);
195
196                         // Remember over values
197                         $content['templ']      = RALLYE_TEMPLATE_SELECTION("templ[".$id."]", $templ);
198                         $content['sw']         = $SW;
199                         $content['id']         = $id;
200                         $content['title']      = $title;
201                         $content['descr']      = $descr;
202                         $content['min_users']  = $min_users;
203                         $content['min_prices'] = $min_prices;
204
205                         // Output row
206                         $OUT .= LOAD_TEMPLATE("admin_edit_rallyes_row", true, $content);
207
208                         // Color switching
209                         $SW = 3 - $SW;
210                 }
211                 // Remember rows in constant
212                 define('__RALLYE_ROWS', $OUT);
213
214                 // Load final template
215                 LOAD_TEMPLATE("admin_edit_rallyes");
216         }
217          else
218         {
219                 // Nothing selected to edit
220                 LOAD_TEMPLATE("admin_settings_saved", false, LOAD_TEMPLATE("admin_list_rallye_noselect", true));
221         }
222 }
223  elseif (($_GET['sub'] == "users") && ($_GET['rallye'] > 0))
224 {
225         // List users and their refs before start and current
226         $result = SQL_QUERY_ESC("SELECT userid, refs, curr_points FROM "._MYSQL_PREFIX."_rallye_users WHERE rallye_id=%s ORDER BY userid",
227          array(bigintval($_GET['rallye'])), __FILE__, __LINE__);
228         if (SQL_NUMROWS($result) > 0)
229         {
230                 $SW = 2; $OUT = "";
231                 define('__RALLYE_VALUE', $_GET['rallye']);
232                 while (list($uid, $old, $opoints) = SQL_FETCHROW($result))
233                 {
234                         // Check for referral count
235                         $cnt = RALLYE_GET_REFCOUNT($uid, $old);
236
237                         // Output row
238                         $Bl = ""; $Br = "";
239                         if (($opoints > 0) && ($cnt > 0)) { $Bl = "<STRONG>"; $Br = "</STRONG>"; }
240                         if (($old > 0) || ($cnt > 0))
241                         {
242                                 // Insert link to referral list
243                                 //* DEBUG: */ echo "-".$uid."/".$cnt."/".$old."-<br />";
244                                 $cnt = ADMIN_USER_PROFILE_LINK($uid, $cnt, "list_refs");
245                                 $old = ADMIN_USER_PROFILE_LINK($uid, $old, "list_refs");
246                         }
247                         $content = array(
248                                 'sw'      => $SW ,
249                                 'uid'     => $uid,
250                                 'bold_l'  => $Bl ,
251                                 'bold_r'  => $Br ,
252                                 'old'     => $old,
253                                 'cnt'     => $cnt,
254                                 'opoints' => TRANSLATE_COMMA($opoints),
255                         );
256                         $OUT .= LOAD_TEMPLATE("admin_list_rallye_usr_row", true, $content);
257                         $SW = 3 - $SW;
258                 }
259
260                 // Free memory
261                 SQL_FREERESULT($result);
262                 define('__RALLYE_USER_ROWS', $OUT);
263
264                 // Load template
265                 LOAD_TEMPLATE("admin_list_rallye_usr");
266         }
267          else
268         {
269                 // No entries found?
270                 LOAD_TEMPLATE("admin_settings_saved", false, RALLYE_ADMIN_USERS_404);
271         }
272 }
273  else
274 {
275         // Start listing rallyes
276         $result = SQL_QUERY("SELECT id, admin_id, title, descr, template, start_time, end_time, auto_add_new_user, is_active, send_notify, notified, min_users, min_prices
277 FROM "._MYSQL_PREFIX."_rallye_data
278 ORDER BY start_time DESC",
279  __FILE__, __LINE__);
280         if (SQL_NUMROWS($result) > 0)
281         {
282                 // List found rallyes
283                 $SW = 2; $OUT = "";
284                 while (list($id, $aid, $title, $descr, $templ, $start, $end, $auto_add, $active, $notify, $notified, $min_users, $min_prices) = SQL_FETCHROW($result))
285                 {
286                         // Load admin login
287                         $alogin = GET_ADMIN_LOGIN($aid);
288
289                         // Count assigned prices
290                         $result_prices = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_rallye_prices WHERE rallye_id=%s",
291                          array(bigintval($id)), __FILE__, __LINE__);
292
293                         // Count joined userids
294                         $result_user = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_rallye_users WHERE rallye_id=%s",
295                          array($id), __FILE__, __LINE__);
296                         $joined = SQL_NUMROWS($result_user);
297
298                         // Did some users joined this rallye?
299                         if ($joined > 0)
300                         {
301                                 // List joined users
302                                 $joined = "<A href=\"".URL."/modules.php?module=admin&amp;what=list_rallyes&amp;sub=users&amp;rallye=".$id."\" title=\"".RALLYE_LIST_USERS."\">".$joined."</A>";
303                         }
304
305                         // Alter some variables
306                         if (empty($alogin)) $alogin = "???";
307                         if (empty($templ))  $templ  = "---";
308                         if (empty($descr))  $descr  = "---";
309
310                         // Transfer data into array for the template
311                         $content = array(
312                                 'select'     => "<INPUT type=\"checkbox\" name=\"sel[".$id."]\" class=\"admin_normal\" value=\"1\">",
313                                 'aid'        => $aid,
314                                 'email_link' => CREATE_EMAIL_LINK($aid),
315                                 'alogin'     => $alogin,
316                                 'id'         => $id,
317                                 'sw'         => $SW,
318                                 'title'      => $title,
319                                 'template'   => $templ,
320                                 'joined'     => $joined,
321                                 'start_date' => MAKE_DATETIME($start, "2"),
322                                 'end_date'   => MAKE_DATETIME($end  , "2"),
323                                 'active_lnk' => TRANSLATE_YESNO($active),
324                                 'notify_lnk' => TRANSLATE_YESNO($notify),
325                                 'auto_lnk'   => TRANSLATE_YESNO($auto_add),
326                                 'notified'   => TRANSLATE_YESNO($notified),
327                                 'prices_cnt' => SQL_NUMROWS($result_prices),
328                                 'descr'      => COMPILE_CODE($descr),
329                                 'min_users'  => $min_users,
330                                 'min_prices' => $min_prices,
331                         );
332
333                         // Free memory
334                         SQL_FREERESULT($result_prices);
335                         SQL_FREERESULT($result_user);
336
337                         // Is the rallye active or not?
338                         switch ($active)
339                         {
340                         case 'Y':
341                                 // Rallye is active so do not edit it!
342                                 $content['select'] = "<STRONG class=\"big\">".$id."</STRONG>";
343                                 $content['active_title'] = RALLYE_DEACTIVATE_NOW;
344                                 $content['active'] = 0;
345                                 break;
346
347                         case 'N':
348                                 $content['active_title'] = RALLYE_ACTIVATE_NOW;
349                                 $content['active'] = "1";
350                                 break;
351                         }
352
353                         // Notification to members?
354                         switch ($notify)
355                         {
356                         case 'Y':
357                                 $content['notify_title'] = RALLYE_STOP_NOTIFY_NOW;
358                                 $content['notify'] = 0;
359                                 break;
360
361                         case 'N':
362                                 $content['notify_title'] = RALLYE_START_NOTIFY_NOW;
363                                 $content['notify'] = "1";
364                                 break;
365                         }
366
367                         // Auto-add of new joined members?
368                         switch ($auto_add)
369                         {
370                         case 'Y':
371                                 $content['auto_title'] = RALLYE_STOP_AUTO_ADD_NOW;
372                                 $content['auto'] = 0;
373                                 break;
374
375                         case 'N':
376                                 $content['auto_title'] = RALLYE_START_AUTO_ADD_NOW;
377                                 $content['auto'] = "1";
378                                 break;
379                         }
380
381                         // Output row
382                         $OUT .= LOAD_TEMPLATE("admin_list_rallyes_row", true, $content);
383                         $SW = 3 - $SW;
384                 }
385
386                 // Free memory
387                 SQL_FREERESULT($result);
388                 define('__RALLYE_ROWS', $OUT);
389
390                 // Load template
391                 LOAD_TEMPLATE("admin_list_rallyes");
392         }
393          else
394         {
395                 // No rallyes setup so far
396                 LOAD_TEMPLATE("admin_settings_saved", false, RALLYE_NO_RALLYES_SETUP);
397         }
398 }
399 //
400 ?>