Referal overview finished and rewritten for refback extension
[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", basename(__FILE__));
42
43 if (empty($_GET['sub'])) $_GET['sub'] = "";
44 $MSG = "";
45
46 // Quick actions on a rallye
47 if (isset($_GET['rallye']))
48 {
49         // Activate / deactivate
50         $SQL = "";
51         if (isset($_GET['activate']))
52         {
53                 switch ($_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 (isset($_GET['notify']))
67         {
68                 switch ($_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 (isset($_GET['auto']))
82         {
83                 switch ($_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($_GET['rallye'])), __FILE__, __LINE__);
99         }
100 }
101  elseif (isset($_POST['remove']))
102 {
103         // Delete rallyes
104         $SEL = SELECTION_COUNT($_POST['sel']);
105         if ($SEL > 0)
106         {
107                 // Delete selected rallyes and all it's data
108                 foreach ($_POST['sel'] as $id => $sel)
109                 {
110                         // Remove selected rallye entirely...
111                         $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_rallye_data WHERE id=%s LIMIT 1",
112                          array(bigintval($id)), __FILE__, __LINE__);
113                         $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_rallye_prices WHERE rallye_id=%s",
114                          array(bigintval($id)), __FILE__, __LINE__);
115                         $result = 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 (isset($_POST['change']))
129 {
130         // Change rallye
131         $SEL = SELECTION_COUNT($_POST['title']);
132         if ($SEL > 0)
133         {
134                 // Change selected rallyes and all it's data
135                 foreach ($_POST['title'] as $id => $title)
136                 {
137                         // Secure ID number
138                         $id = bigintval($id);
139
140                         // Generate timestamps
141                         $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]);
142                         $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]  );
143
144                         // Update entry
145                         $result = 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($title, $_POST['descr'][$id], $_POST['templ'][$id], bigintval($START), bigintval($END), bigintval($_POST['min_users'][$id]), bigintval($_POST['min_prices'][$id]), $id),
155  __FILE__, __LINE__);
156                 }
157
158                 // Output message
159                 $MSG = RALLYE_CHANGED;
160         }
161 }
162
163 if (isset($_POST['edit']))
164 {
165         // Check for selections
166         $SEL = SELECTION_COUNT($_POST['sel']);
167         if ($SEL > 0)
168         {
169                 // Make all selected and deactivated rallyes editable
170                 $SW = 2; $OUT = "";
171                 foreach ($_POST['sel'] as $id => $sel)
172                 {
173                         // Load rallye basic data
174                         $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",
175                          array(bigintval($id)), __FILE__, __LINE__);
176                         list($title, $descr, $templ, $start, $end, $min_users, $min_prices) = SQL_FETCHROW($result);
177                         SQL_FREERESULT($result);
178
179                         // Starting day
180                         $content['s_sec']   = ADD_SELECTION("sec"  , date("s", $start), "start", $id);
181                         $content['s_min']   = ADD_SELECTION("min"  , date("i", $start), "start", $id);
182                         $content['s_hour']  = ADD_SELECTION("hour" , date("G", $start), "start", $id);
183                         $content['s_day']   = ADD_SELECTION("day"  , date("d", $start), "start", $id);
184                         $content['s_month'] = ADD_SELECTION("month", date("m", $start), "start", $id);
185                         $content['s_year']  = ADD_SELECTION("year" , date('Y', $start), "start", $id);
186
187                         // Ending day
188                         $content['e_sec']   = ADD_SELECTION("sec"  , date("s", $end)  , "end"  , $id);
189                         $content['e_min']   = ADD_SELECTION("min"  , date("i", $end)  , "end"  , $id);
190                         $content['e_hour']  = ADD_SELECTION("hour" , date("G", $end)  , "end"  , $id);
191                         $content['e_day']   = ADD_SELECTION("day"  , date("d", $end)  , "end"  , $id);
192                         $content['e_month'] = ADD_SELECTION("month", date("m", $end)  , "end"  , $id);
193                         $content['e_year']  = ADD_SELECTION("year" , date('Y', $end)  , "end"  , $id);
194
195                         // Remember over values
196                         $content['templ']      = RALLYE_TEMPLATE_SELECTION("templ[".$id."]", $templ);
197                         $content['sw']         = $SW;
198                         $content['id']         = $id;
199                         $content['title']      = $title;
200                         $content['descr']      = $descr;
201                         $content['min_users']  = $min_users;
202                         $content['min_prices'] = $min_prices;
203
204                         // Output row
205                         $OUT .= LOAD_TEMPLATE("admin_edit_rallyes_row", true, $content);
206
207                         // Color switching
208                         $SW = 3 - $SW;
209                 }
210                 // Remember rows in constant
211                 define('__RALLYE_ROWS', $OUT);
212
213                 // Load final template
214                 LOAD_TEMPLATE("admin_edit_rallyes");
215         }
216          else
217         {
218                 // Nothing selected to edit
219                 LOAD_TEMPLATE("admin_settings_saved", false, LOAD_TEMPLATE("admin_list_rallye_noselect", true));
220         }
221 } elseif (($_GET['sub'] == "users") && ($_GET['rallye'] > 0)) {
222         // List users and their refs before start and current
223         $result = SQL_QUERY_ESC("SELECT userid, refs, curr_points FROM "._MYSQL_PREFIX."_rallye_users WHERE rallye_id=%s ORDER BY userid",
224          array(bigintval($_GET['rallye'])), __FILE__, __LINE__);
225         if (SQL_NUMROWS($result) > 0)
226         {
227                 $SW = 2; $OUT = "";
228                 define('__RALLYE_VALUE', $_GET['rallye']);
229                 while (list($uid, $old, $opoints) = SQL_FETCHROW($result))
230                 {
231                         // Check for referal count
232                         $cnt = RALLYE_GET_REFCOUNT($uid, $old);
233
234                         // Output row
235                         $Bl = ""; $Br = "";
236                         if (($opoints > 0) && ($cnt > 0)) { $Bl = "<STRONG>"; $Br = "</STRONG>"; }
237                         if (($old > 0) || ($cnt > 0))
238                         {
239                                 // Insert link to referal list
240                                 //* DEBUG: */ echo "-".$uid."/".$cnt."/".$old."-<br />";
241                                 $cnt = ADMIN_USER_PROFILE_LINK($uid, $cnt, "list_refs");
242                                 $old = ADMIN_USER_PROFILE_LINK($uid, $old, "list_refs");
243                         }
244
245                         // Get user points
246                         $points = GET_TOTAL_DATA($uid, "user_points", "points", "userid", false, " AND ref_depth=1 LIMIT 1");
247                         //* DEBUG: */ echo basename(__FILE__).":uid={$uid},points={$points},opoints={$opoints}<br />\n";
248                         $content = array(
249                                 'sw'      => $SW ,
250                                 'uid'     => $uid,
251                                 'bold_l'  => $Bl ,
252                                 'bold_r'  => $Br ,
253                                 'old'     => $old,
254                                 'cnt'     => $cnt,
255                                 'opoints' => TRANSLATE_COMMA($points - $opoints),
256                         );
257                         $OUT .= LOAD_TEMPLATE("admin_list_rallye_usr_row", true, $content);
258                         $SW = 3 - $SW;
259                 }
260
261                 // Free memory
262                 SQL_FREERESULT($result);
263                 define('__RALLYE_USER_ROWS', $OUT);
264
265                 // Load template
266                 LOAD_TEMPLATE("admin_list_rallye_usr");
267         }
268          else
269         {
270                 // No entries found?
271                 LOAD_TEMPLATE("admin_settings_saved", false, RALLYE_ADMIN_USERS_404);
272         }
273 }
274  else
275 {
276         // Start listing rallyes
277         $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
278 FROM "._MYSQL_PREFIX."_rallye_data
279 ORDER BY start_time DESC",
280  __FILE__, __LINE__);
281         if (SQL_NUMROWS($result) > 0)
282         {
283                 // List found rallyes
284                 $SW = 2; $OUT = "";
285                 while (list($id, $aid, $title, $descr, $templ, $start, $end, $auto_add, $active, $notify, $notified, $min_users, $min_prices) = SQL_FETCHROW($result))
286                 {
287                         // Load admin login
288                         $alogin = GET_ADMIN_LOGIN($aid);
289
290                         // Count assigned prices
291                         $result_prices = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_rallye_prices WHERE rallye_id=%s",
292                          array(bigintval($id)), __FILE__, __LINE__);
293
294                         // Count joined userids
295                         $result_user = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_rallye_users WHERE rallye_id=%s",
296                          array($id), __FILE__, __LINE__);
297                         $joined = SQL_NUMROWS($result_user);
298
299                         // Did some users joined this rallye?
300                         if ($joined > 0)
301                         {
302                                 // List joined users
303                                 $joined = "<A href=\"".URL."/modules.php?module=admin&amp;what=list_rallyes&amp;sub=users&amp;rallye=".$id."\" title=\"".RALLYE_LIST_USERS."\">".$joined."</A>";
304                         }
305
306                         // Alter some variables
307                         if (empty($alogin)) $alogin = "???";
308                         if (empty($templ))  $templ  = "---";
309                         if (empty($descr))  $descr  = "---";
310
311                         // Transfer data into array for the template
312                         $content = array(
313                                 'select'     => "<INPUT type=\"checkbox\" name=\"sel[".$id."]\" class=\"admin_normal\" value=\"1\">",
314                                 'aid'        => $aid,
315                                 'email_link' => CREATE_EMAIL_LINK($aid),
316                                 'alogin'     => $alogin,
317                                 'id'         => $id,
318                                 'sw'         => $SW,
319                                 'title'      => $title,
320                                 'template'   => $templ,
321                                 'joined'     => $joined,
322                                 'start_date' => MAKE_DATETIME($start, "2"),
323                                 'end_date'   => MAKE_DATETIME($end  , "2"),
324                                 'active_lnk' => TRANSLATE_YESNO($active),
325                                 'notify_lnk' => TRANSLATE_YESNO($notify),
326                                 'auto_lnk'   => TRANSLATE_YESNO($auto_add),
327                                 'notified'   => TRANSLATE_YESNO($notified),
328                                 'prices_cnt' => SQL_NUMROWS($result_prices),
329                                 'descr'      => COMPILE_CODE($descr),
330                                 'min_users'  => $min_users,
331                                 'min_prices' => $min_prices,
332                         );
333
334                         // Free memory
335                         SQL_FREERESULT($result_prices);
336                         SQL_FREERESULT($result_user);
337
338                         // Is the rallye active or not?
339                         switch ($active)
340                         {
341                         case 'Y':
342                                 // Rallye is active so do not edit it!
343                                 $content['select'] = "<STRONG class=\"big\">".$id."</STRONG>";
344                                 $content['active_title'] = RALLYE_DEACTIVATE_NOW;
345                                 $content['active'] = 0;
346                                 break;
347
348                         case 'N':
349                                 $content['active_title'] = RALLYE_ACTIVATE_NOW;
350                                 $content['active'] = "1";
351                                 break;
352                         }
353
354                         // Notification to members?
355                         switch ($notify)
356                         {
357                         case 'Y':
358                                 $content['notify_title'] = RALLYE_STOP_NOTIFY_NOW;
359                                 $content['notify'] = 0;
360                                 break;
361
362                         case 'N':
363                                 $content['notify_title'] = RALLYE_START_NOTIFY_NOW;
364                                 $content['notify'] = "1";
365                                 break;
366                         }
367
368                         // Auto-add of new joined members?
369                         switch ($auto_add)
370                         {
371                         case 'Y':
372                                 $content['auto_title'] = RALLYE_STOP_AUTO_ADD_NOW;
373                                 $content['auto'] = 0;
374                                 break;
375
376                         case 'N':
377                                 $content['auto_title'] = RALLYE_START_AUTO_ADD_NOW;
378                                 $content['auto'] = "1";
379                                 break;
380                         }
381
382                         // Output row
383                         $OUT .= LOAD_TEMPLATE("admin_list_rallyes_row", true, $content);
384                         $SW = 3 - $SW;
385                 }
386
387                 // Free memory
388                 SQL_FREERESULT($result);
389                 define('__RALLYE_ROWS', $OUT);
390
391                 // Load template
392                 LOAD_TEMPLATE("admin_list_rallyes");
393         }
394          else
395         {
396                 // No rallyes setup so far
397                 LOAD_TEMPLATE("admin_settings_saved", false, RALLYE_NO_RALLYES_SETUP);
398         }
399 }
400 //
401 ?>