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