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