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