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