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