3ed7e88846d1a60151216f3661b229f1455134fe
[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 ((!defined('__SECURITY')) || (!IS_ADMIN())) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // Add description as navigation point
41 ADD_DESCR("admin", __FILE__);
42
43 if (!REQUEST_ISSET_GET(('sub'))) REQUEST_SET_GET('sub', "");
44 $MSG = "";
45
46 // Quick actions on a rallye
47 if (REQUEST_ISSET_GET(('rallye'))) {
48         // Init SQL queries
49         INIT_SQLS();
50
51         if (REQUEST_ISSET_GET(('activate'))) {
52                 // Activate / deactivate
53                 switch (REQUEST_GET('activate'))
54                 {
55                 case "1": // Activate
56                         ADD_SQL("UPDATE `{!_MYSQL_PREFIX!}_rallye_data` SET is_active='Y' WHERE id=%s AND is_active='N' LIMIT 1");
57                         break;
58
59                 case "0": // Deactivate
60                         ADD_SQL("UPDATE `{!_MYSQL_PREFIX!}_rallye_data` SET is_active='N' WHERE id=%s AND is_active='Y' LIMIT 1");
61                         break;
62                 }
63         } elseif (REQUEST_ISSET_GET(('notify'))) {
64                 // Automatic notification
65                 switch (REQUEST_GET('notify'))
66                 {
67                 case "1": // Activate
68                         ADD_SQL("UPDATE `{!_MYSQL_PREFIX!}_rallye_data` SET send_notify='Y' WHERE id=%s AND send_notify='N' LIMIT 1");
69                         break;
70
71                 case "0": // Deactivate
72                         ADD_SQL("UPDATE `{!_MYSQL_PREFIX!}_rallye_data` SET send_notify='N' WHERE id=%s AND send_notify='Y' LIMIT 1");
73                         break;
74                 }
75         } elseif (REQUEST_ISSET_GET(('auto'))) {
76                 // Automatic adding of new members
77                 switch (REQUEST_GET('auto'))
78                 {
79                 case "1": // Activate
80                         ADD_SQL("UPDATE `{!_MYSQL_PREFIX!}_rallye_data` SET auto_add_new_user='Y' WHERE id=%s AND auto_add_new_user='N' LIMIT 1");
81                         break;
82
83                 case "0": // Deactivate
84                         ADD_SQL("UPDATE `{!_MYSQL_PREFIX!}_rallye_data` SET auto_add_new_user='N' WHERE id=%s AND auto_add_new_user='Y' LIMIT 1");
85                         break;
86                 }
87         }
88
89         // Run SQL command
90         runFilterChain('run_sqls');
91 } elseif (REQUEST_ISSET_POST(('remove'))) {
92         // Delete rallyes
93         $SEL = SELECTION_COUNT(REQUEST_POST('sel'));
94         if ($SEL > 0) {
95                 // Delete selected rallyes and all it's data
96                 foreach (REQUEST_POST('sel') as $id => $selected) {
97                         // Remove selected rallye entirely...
98                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_rallye_data` WHERE id=%s LIMIT 1",
99                                 array(bigintval($id)), __FILE__, __LINE__);
100                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_rallye_prices` WHERE rallye_id=%s",
101                                 array(bigintval($id)), __FILE__, __LINE__);
102                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_rallye_users` WHERE rallye_id=%s",
103                                 array(bigintval($id)), __FILE__, __LINE__);
104                 }
105
106                 // Output message
107                 $MSG = getMessage('RALLYE_DELETED');
108         } else {
109                 // No rallye selected to delete!
110                 $MSG = getMessage('RALLYE_DELETE_NOTHING_SELECTED');
111         }
112 } elseif (REQUEST_ISSET_POST(('change'))) {
113         // Change rallye
114         $SEL = SELECTION_COUNT(REQUEST_POST('title'));
115         if ($SEL > 0) {
116                 // Change selected rallyes and all it's data
117                 foreach (REQUEST_POST('title') as $id => $title) {
118                         // Secure ID number
119                         $id = bigintval($id);
120
121                         // Generate timestamps
122                         $START = mktime(REQUEST_POST('start_hour', $id), REQUEST_POST('start_min', $id), REQUEST_POST('start_sec', $id), REQUEST_POST('start_month', $id), REQUEST_POST('start_day', $id), REQUEST_POST('start_year', $id));
123                         $END   = mktime(REQUEST_POST('end_hour', $id)  , REQUEST_POST('end_min', $id)  , REQUEST_POST('end_sec', $id)  , REQUEST_POST('end_month', $id)  , REQUEST_POST('end_day', $id)  , REQUEST_POST('end_year', $id)  );
124
125                         // Update entry
126                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_rallye_data` SET
127 title='%s',
128 descr='%s',
129 template='%s',
130 start_time='%s',
131 end_time='%s',
132 min_users='%s',
133 min_prices='%s'
134 WHERE id='".$id."' LIMIT 1",
135                                 array(
136                                         $title,
137                                         REQUEST_POST('descr', $id),
138                                         REQUEST_POST('templ', $id),
139                                         bigintval($START),
140                                         bigintval($END),
141                                         bigintval(REQUEST_POST('min_users', $id)),
142                                         bigintval(REQUEST_POST('min_prices', $id)),
143                                         $id
144                                 ), __FILE__, __LINE__);
145                 }
146
147                 // Output message
148                 $MSG = getMessage('RALLYE_CHANGED');
149         }
150 }
151
152 if (REQUEST_ISSET_POST(('edit'))) {
153         // Check for selections
154         $SEL = SELECTION_COUNT(REQUEST_POST('sel'));
155         if ($SEL > 0) {
156                 // Make all selected and deactivated rallyes editable
157                 $SW = 2; $OUT = "";
158                 foreach (REQUEST_POST('sel') as $id => $selected) {
159                         // Load rallye basic data
160                         $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",
161                                 array(bigintval($id)), __FILE__, __LINE__);
162                         list($title, $descr, $templ, $start, $end, $min_users, $min_prices) = SQL_FETCHROW($result);
163                         SQL_FREERESULT($result);
164
165                         // Starting day
166                         $content['s_sec']   = ADD_SELECTION("sec"  , date("s", $start), "start", $id);
167                         $content['s_min']   = ADD_SELECTION("min"  , date("i", $start), "start", $id);
168                         $content['s_hour']  = ADD_SELECTION("hour" , date("G", $start), "start", $id);
169                         $content['s_day']   = ADD_SELECTION("day"  , date("d", $start), "start", $id);
170                         $content['s_month'] = ADD_SELECTION("month", date("m", $start), "start", $id);
171                         $content['s_year']  = ADD_SELECTION("year" , date('Y', $start), "start", $id);
172
173                         // Ending day
174                         $content['e_sec']   = ADD_SELECTION("sec"  , date("s", $end)  , "end"  , $id);
175                         $content['e_min']   = ADD_SELECTION("min"  , date("i", $end)  , "end"  , $id);
176                         $content['e_hour']  = ADD_SELECTION("hour" , date("G", $end)  , "end"  , $id);
177                         $content['e_day']   = ADD_SELECTION("day"  , date("d", $end)  , "end"  , $id);
178                         $content['e_month'] = ADD_SELECTION("month", date("m", $end)  , "end"  , $id);
179                         $content['e_year']  = ADD_SELECTION("year" , date('Y', $end)  , "end"  , $id);
180
181                         // Remember over values
182                         $content['templ']      = RALLYE_TEMPLATE_SELECTION("templ[".$id."]", $templ);
183                         $content['sw']         = $SW;
184                         $content['id']         = $id;
185                         $content['title']      = $title;
186                         $content['descr']      = $descr;
187                         $content['min_users']  = $min_users;
188                         $content['min_prices'] = $min_prices;
189
190                         // Output row
191                         $OUT .= LOAD_TEMPLATE("admin_edit_rallyes_row", true, $content);
192
193                         // Color switching
194                         $SW = 3 - $SW;
195                 }
196
197                 // Remember rows in constant
198                 define('__RALLYE_ROWS', $OUT);
199
200                 // Load final template
201                 LOAD_TEMPLATE("admin_edit_rallyes");
202         } else {
203                 // Nothing selected to edit
204                 LOAD_TEMPLATE("admin_settings_saved", false, LOAD_TEMPLATE("admin_list_rallye_noselect", true));
205         }
206 } elseif ((REQUEST_GET('sub') == "users") && (REQUEST_GET('rallye') > 0)) {
207         // List users and their refs before start and current
208         $result = SQL_QUERY_ESC("SELECT userid, refs, curr_points FROM `{!_MYSQL_PREFIX!}_rallye_users` WHERE rallye_id=%s ORDER BY userid",
209                 array(bigintval(REQUEST_GET('rallye'))), __FILE__, __LINE__);
210         if (SQL_NUMROWS($result) > 0) {
211                 $SW = 2; $OUT = "";
212                 define('__RALLYE_VALUE', REQUEST_GET('rallye'));
213                 while (list($uid, $old, $opoints) = SQL_FETCHROW($result)) {
214                         // Check for referal count
215                         $cnt = RALLYE_GET_REFCOUNT($uid, $old);
216
217                         // Output row
218                         $Bl = ""; $Br = "";
219                         if (($opoints > 0) && ($cnt > 0)) { $Bl = "<strong>"; $Br = "</strong>"; }
220                         if (($old > 0) || ($cnt > 0)) {
221                                 // Insert link to referal list
222                                 //* DEBUG: */ echo "-".$uid."/".$cnt."/".$old."-<br />";
223                                 $cnt = ADMIN_USER_PROFILE_LINK($uid, $cnt, "list_refs");
224                                 $old = ADMIN_USER_PROFILE_LINK($uid, $old, "list_refs");
225                         }
226
227                         // Get user points
228                         $points = GET_TOTAL_DATA($uid, "user_points", "points", "userid", false, " AND ref_depth=1 LIMIT 1");
229                         //* DEBUG: */ echo basename(__FILE__).":uid={$uid},points={$points},opoints={$opoints}<br />\n";
230                         $content = array(
231                                 'sw'      => $SW ,
232                                 'uid'     => $uid,
233                                 'bold_l'  => $Bl ,
234                                 'bold_r'  => $Br ,
235                                 'old'     => $old,
236                                 'cnt'     => $cnt,
237                                 'opoints' => TRANSLATE_COMMA($points - $opoints),
238                         );
239                         $OUT .= LOAD_TEMPLATE("admin_list_rallye_usr_row", true, $content);
240                         $SW = 3 - $SW;
241                 }
242
243                 // Free memory
244                 SQL_FREERESULT($result);
245                 define('__RALLYE_USER_ROWS', $OUT);
246
247                 // Load template
248                 LOAD_TEMPLATE("admin_list_rallye_usr");
249         } else {
250                 // No entries found?
251                 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('RALLYE_ADMIN_USERS_404'));
252         }
253 } else {
254         // Start listing rallyes
255         $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
256 FROM `{!_MYSQL_PREFIX!}_rallye_data`
257 ORDER BY start_time DESC",
258  __FILE__, __LINE__);
259         if (SQL_NUMROWS($result) > 0) {
260                 // List found rallyes
261                 $SW = 2; $OUT = "";
262                 while (list($id, $aid, $title, $descr, $templ, $start, $end, $auto_add, $active, $notify, $notified, $min_users, $min_prices) = SQL_FETCHROW($result)) {
263                         // Load admin login
264                         $alogin = GET_ADMIN_LOGIN($aid);
265
266                         // Count assigned prices
267                         $result_prices = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_rallye_prices` WHERE rallye_id=%s",
268                                 array(bigintval($id)), __FILE__, __LINE__);
269
270                         // Count joined userids
271                         $result_user = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_rallye_users` WHERE rallye_id=%s",
272                                 array($id), __FILE__, __LINE__);
273                         $joined = SQL_NUMROWS($result_user);
274
275                         // Did some users joined this rallye?
276                         if ($joined > 0) {
277                                 // List joined users
278                                 $joined = "<a href=\"{!URL!}/modules.php?module=admin&amp;what=list_rallyes&amp;sub=users&amp;rallye=".$id."\" title=\"".RALLYE_LIST_USERS."\">".$joined."</a>";
279                         }
280
281                         // Alter some variables
282                         if (empty($alogin)) $alogin = "???";
283                         if (empty($templ))  $templ  = "---";
284                         if (empty($descr))  $descr  = "---";
285
286                         // Transfer data into array for the template
287                         $content = array(
288                                 'select'     => "<input type=\"checkbox\" name=\"sel[".$id."]\" class=\"admin_normal\" value=\"1\" />",
289                                 'aid'        => $aid,
290                                 'email_link' => CREATE_EMAIL_LINK($aid),
291                                 'alogin'     => $alogin,
292                                 'id'         => $id,
293                                 'sw'         => $SW,
294                                 'title'      => $title,
295                                 'template'   => $templ,
296                                 'joined'     => $joined,
297                                 'start_date' => MAKE_DATETIME($start, "2"),
298                                 'end_date'   => MAKE_DATETIME($end  , "2"),
299                                 'active_lnk' => TRANSLATE_YESNO($active),
300                                 'notify_lnk' => TRANSLATE_YESNO($notify),
301                                 'auto_lnk'   => TRANSLATE_YESNO($auto_add),
302                                 'notified'   => TRANSLATE_YESNO($notified),
303                                 'prices_cnt' => SQL_NUMROWS($result_prices),
304                                 'descr'      => COMPILE_CODE($descr),
305                                 'min_users'  => $min_users,
306                                 'min_prices' => $min_prices,
307                         );
308
309                         // Free memory
310                         SQL_FREERESULT($result_prices);
311                         SQL_FREERESULT($result_user);
312
313                         // Is the rallye active or not?
314                         switch ($active)
315                         {
316                         case "Y":
317                                 // Rallye is active so do not edit it!
318                                 $content['select'] = "<div class=\"big\">".$id."</strong>";
319                                 $content['active_title'] = getMessage('RALLYE_DEACTIVATE_NOW');
320                                 $content['active'] = 0;
321                                 break;
322
323                         case "N":
324                                 $content['active_title'] = getMessage('RALLYE_ACTIVATE_NOW');
325                                 $content['active'] = "1";
326                                 break;
327                         }
328
329                         // Notification to members?
330                         switch ($notify)
331                         {
332                         case "Y":
333                                 $content['notify_title'] = getMessage('RALLYE_STOP_NOTIFY_NOW');
334                                 $content['notify'] = 0;
335                                 break;
336
337                         case "N":
338                                 $content['notify_title'] = getMessage('RALLYE_START_NOTIFY_NOW');
339                                 $content['notify'] = "1";
340                                 break;
341                         }
342
343                         // Auto-add of new joined members?
344                         switch ($auto_add)
345                         {
346                         case "Y":
347                                 $content['auto_title'] = getMessage('RALLYE_STOP_AUTO_ADD_NOW');
348                                 $content['auto'] = 0;
349                                 break;
350
351                         case "N":
352                                 $content['auto_title'] = getMessage('RALLYE_START_AUTO_ADD_NOW');
353                                 $content['auto'] = "1";
354                                 break;
355                         }
356
357                         // Output row
358                         $OUT .= LOAD_TEMPLATE("admin_list_rallyes_row", true, $content);
359                         $SW = 3 - $SW;
360                 }
361
362                 // Free memory
363                 SQL_FREERESULT($result);
364                 define('__RALLYE_ROWS', $OUT);
365
366                 // Load template
367                 LOAD_TEMPLATE("admin_list_rallyes");
368         } else {
369                 // No rallyes setup so far
370                 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('RALLYE_NO_RALLYES_SETUP'));
371         }
372 }
373
374 //
375 ?>