Even more rewrites/fixes from EL branch (please report any broken part after you...
[mailer.git] / inc / modules / admin / what-list_rallyes.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                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  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if ((!defined('__SECURITY')) || (!isAdmin())) {
42         die();
43 } // END - if
44
45 // Add description as navigation point
46 addMenuDescription('admin', __FILE__);
47
48 // Init message
49 $message = '';
50
51 // Quick actions on a rallye
52 if (isGetRequestParameterSet('rallye')) {
53         // Init SQL queries
54         initSqls();
55
56         if (isGetRequestParameterSet('activate')) {
57                 // Activate / deactivate
58                 switch (getRequestParameter('activate')) {
59                         case '1': // Activate
60                                 addSql(SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_rallye_data` SET `is_active`='Y' WHERE `id`=%s AND `is_active`='N' LIMIT 1",
61                                         array(bigintval(getRequestParameter('rallye'))), __FILE__, __LINE__, false)
62                                 );
63                                 break;
64
65                         case '0': // Deactivate
66                                 addSql(SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_rallye_data` SET `is_active`='N' WHERE `id`=%s AND `is_active`='Y' LIMIT 1",
67                                         array(bigintval(getRequestParameter('rallye'))), __FILE__, __LINE__, false)
68                                 );
69                                 break;
70                 } // END - switch
71         } elseif (isGetRequestParameterSet('notify')) {
72                 // Automatic notification
73                 switch (getRequestParameter('notify')) {
74                         case '1': // Activate
75                                 addSql(SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_rallye_data` SET `send_notify`='Y' WHERE `id`=%s AND `send_notify`='N' LIMIT 1",
76                                         array(bigintval(getRequestParameter('rallye'))), __FILE__, __LINE__, false)
77                                 );
78                                 break;
79
80                         case '0': // Deactivate
81                                 addSql(SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_rallye_data` SET `send_notify`='N' WHERE `id`=%s AND `send_notify`='Y' LIMIT 1",
82                                         array(bigintval(getRequestParameter('rallye'))), __FILE__, __LINE__, false)
83                                 );
84                                 break;
85                 } // END - switch
86         } elseif (isGetRequestParameterSet('auto')) {
87                 // Automatic adding of new members
88                 switch (getRequestParameter('auto')) {
89                         case '1': // Activate
90                                 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",
91                                         array(bigintval(getRequestParameter('rallye'))), __FILE__, __LINE__, false)
92                                 );
93                                 break;
94
95                         case '0': // Deactivate
96                                 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",
97                                         array(bigintval(getRequestParameter('rallye'))), __FILE__, __LINE__, false)
98                                 );
99                                 break;
100                 } // END - switch
101         }
102
103         // Run SQL command
104         runFilterChain('run_sqls');
105 } elseif (isFormSent('remove')) {
106         // Delete rallyes
107         if (countPostSelection() > 0) {
108                 // Init SQLs
109                 initSqls();
110
111                 // Delete selected rallyes and all it's data
112                 foreach (postRequestParameter('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 (isFormSent('change')) {
132         // Change rallye
133         if (countSelection(postRequestParameter('title')) > 0) {
134                 // Init SQLs
135                 initSqls();
136
137                 // Change selected rallyes and all it's data
138                 foreach (postRequestParameter('title') as $id => $title) {
139                         // Secure id number
140                         $id = bigintval($id);
141
142                         // Generate timestamps
143                         $START = mktime(postRequestParameter('start_hour', $id), postRequestParameter('start_min', $id), postRequestParameter('start_sec', $id), postRequestParameter('start_month', $id), postRequestParameter('start_day', $id), postRequestParameter('start_year', $id));
144                         $END   = mktime(postRequestParameter('end_hour', $id)  , postRequestParameter('end_min', $id)  , postRequestParameter('end_sec', $id)  , postRequestParameter('end_month', $id)  , postRequestParameter('end_day', $id)  , postRequestParameter('end_year', $id)  );
145
146                         // Update entry
147                         addSql(SQL_QUERY_ESC("UPDATE
148         `{?_MYSQL_PREFIX?}_rallye_data`
149 SET
150         `title`='%s',
151         `descr`='%s',
152         `template`='%s',
153         `start_time`='%s',
154         `end_time`='%s',
155         `min_users`='%s',
156         `min_prices`='%s'
157 WHERE
158         `id`='%s'
159 LIMIT 1",
160                         array(
161                                 $title,
162                                 postRequestParameter('descr', $id),
163                                 postRequestParameter('templ', $id),
164                                 bigintval($START),
165                                 bigintval($END),
166                                 bigintval(postRequestParameter('min_users', $id)),
167                                 bigintval(postRequestParameter('min_prices', $id)),
168                                 $id
169                         ), __FILE__, __LINE__, false));
170                 }
171
172                 // Run SQLS
173                 runFilterChain('run_sqls');
174
175                 // Output message
176                 $message = getMessage('RALLYE_CHANGED');
177         }
178 }
179
180 if (isFormSent('edit')) {
181         // Check for selections
182         if (countPostSelection() > 0) {
183                 // Make all selected and deactivated rallyes editable
184                 $OUT = ''; $SW = 2;
185                 foreach (postRequestParameter('sel') as $id => $selected) {
186                         // Load rallye basic data
187                         $result = SQL_QUERY_ESC("SELECT
188         `title`, `descr`, `template`, `start_time`, `end_time`, `min_users`, `min_prices`
189 FROM
190         `{?_MYSQL_PREFIX?}_rallye_data`
191 WHERE
192         `id`=%s
193 LIMIT 1",
194                                 array(bigintval($id)), __FILE__, __LINE__);
195                         // Fetch an array
196                         $content = SQL_FETCHARRAY($result);
197
198                         // Free result
199                         SQL_FREERESULT($result);
200
201                         // Starting day
202                         $content['s_sec']   = addSelectionBox('sec'  , date('s', $content['start_time']), 'start', $id);
203                         $content['s_min']   = addSelectionBox('min'  , date('i', $content['start_time']), 'start', $id);
204                         $content['s_hour']  = addSelectionBox('hour' , date('G', $content['start_time']), 'start', $id);
205                         $content['s_day']   = addSelectionBox('day'  , date('d', $content['start_time']), 'start', $id);
206                         $content['s_month'] = addSelectionBox('month', date('m', $content['start_time']), 'start', $id);
207                         $content['s_year']  = addSelectionBox('year' , date('Y', $content['start_time']), 'start', $id);
208
209                         // Ending day
210                         $content['e_sec']   = addSelectionBox('sec'  , date('s', $content['end_time'])  , 'end'  , $id);
211                         $content['e_min']   = addSelectionBox('min'  , date('i', $content['end_time'])  , 'end'  , $id);
212                         $content['e_hour']  = addSelectionBox('hour' , date('G', $content['end_time'])  , 'end'  , $id);
213                         $content['e_day']   = addSelectionBox('day'  , date('d', $content['end_time'])  , 'end'  , $id);
214                         $content['e_month'] = addSelectionBox('month', date('m', $content['end_time'])  , 'end'  , $id);
215                         $content['e_year']  = addSelectionBox('year' , date('Y', $content['end_time'])  , 'end'  , $id);
216
217                         // Remember other values
218                         $content['templ']      = addReferalRallyeTemplateSelection('templ['.$id.']', $content['template']);
219                         $content['sw']         = $SW;
220                         $content['id']         = $id;
221
222                         // Output row
223                         $OUT .= loadTemplate('admin_edit_rallyes_row', true, $content);
224
225                         // Color switching
226                         $SW = 3 - $SW;
227                 }
228
229                 // Load final template
230                 loadTemplate('admin_edit_rallyes', false, $OUT);
231         } else {
232                 // Nothing selected to edit
233                 loadTemplate('admin_settings_saved', false, getMessage('RALLYE_NO_RALLYES_SELECTED'));
234         }
235 } elseif ((getRequestParameter('sub') == 'users') && (getRequestParameter('rallye') > 0)) {
236         // List users and their refs before start and current
237         $result = SQL_QUERY_ESC("SELECT `userid`, `refs`, `curr_points` FROM `{?_MYSQL_PREFIX?}_rallye_users` WHERE `rallye_id`=%s ORDER BY `userid` ASC",
238                 array(bigintval(getRequestParameter('rallye'))), __FILE__, __LINE__);
239         if (SQL_NUMROWS($result) > 0) {
240                 $OUT = ''; $SW = 2;
241                 $content['rallye'] = getRequestParameter('rallye');
242                 while ($row = SQL_FETCHARRAY($result)) {
243                         // Check for referal count
244                         $cnt = getReferalRallyeRefsCount($row['userid'], $row['refs']);
245
246                         // Init variables
247                         $bl = '';
248                         $br = '';
249
250                         // Output row
251                         if (($row['curr_points'] > 0) && ($cnt > 0)) { $bl = '<strong>'; $br = '</strong>'; }
252
253                         // Get user points
254                         $points = countSumTotalData($row['userid'], 'user_points', 'points', 'userid', false, " AND `ref_depth`=1 LIMIT 1");
255                         //* DEBUG: */ outputHtml(basename(__FILE__).":userid={$row['userid']},points={$points},opoints={$row['curr_points']}<br />");
256
257                         // Prepare content
258                         // @TODO Rewritings: opoints->curr_points,old->refs in template
259                         $row = array(
260                                 'sw'      => $SW ,
261                                 'userid'  => $row['userid'],
262                                 'bold_l'  => $bl ,
263                                 'bold_r'  => $br ,
264                                 'old'     => $row['refs'],
265                                 'cnt'     => $cnt,
266                                 'opoints' => translateComma($points - $row['curr_points']),
267                         );
268
269                         // Load row template and switch color
270                         $OUT .= loadTemplate('admin_list_rallye_usr_row', true, $row);
271                         $SW = 3 - $SW;
272                 } // END - while
273
274                 // Remember rows
275                 $content['rows']   = $OUT;
276                 $content['rallye'] = getRequestParameter('rallye');
277
278                 // Free memory
279                 SQL_FREERESULT($result);
280
281                 // Load template
282                 loadTemplate('admin_list_rallye_usr', false, $content);
283         } else {
284                 // No entries found?
285                 loadTemplate('admin_settings_saved', false, getMessage('RALLYE_ADMIN_USERS_404'));
286         }
287 } else {
288         // Start listing rallyes
289         $result = SQL_QUERY("SELECT
290         `id`, `admin_id`, `title`, `descr`, `template`, `start_time`, `end_time`, `auto_add_new_user`, `is_active`, `send_notify`, `notified`, `min_users`, `min_prices`
291 FROM
292         `{?_MYSQL_PREFIX?}_rallye_data`
293 ORDER BY
294         `start_time` DESC", __FILE__, __LINE__);
295
296         // Records found?
297         if (SQL_NUMROWS($result) > 0) {
298                 // List found rallyes
299                 $OUT = ''; $SW = 2;
300                 while ($data = SQL_FETCHARRAY($result)) {
301                         // Load admin login
302                         $data['login'] = getAdminLogin($data['admin_id']);
303
304                         // Count joined userids
305                         $joined = countSumTotalData($data['id'], 'rallye_users', 'id', 'rallye_id', true);
306
307                         // Did some users joined this rallye?
308                         if ($joined > 0) {
309                                 // List joined users
310                                 $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>';
311                         } // END - if
312
313                         // Alter some variables
314                         if (empty($data['login']))    $data['login']     = '???';
315                         if (empty($data['template'])) $data['template']  = '---';
316                         if (empty($data['descr']))    $data['descr']     = '---';
317
318                         // Transfer data into array for the template
319                         $row = array(
320                                 'select'     => '<input type="checkbox" name="sel[' . $data['id'] . ']" class="admin_normal" value="1" />',
321                                 'admin_id'   => $data['admin_id'],
322                                 'admin_link' => generateAdminLink($data['admin_id']),
323                                 'login'      => $data['login'],
324                                 'id'         => $data['id'],
325                                 'sw'         => $SW,
326                                 'title'      => $data['title'],
327                                 'template'   => $data['template'],
328                                 'joined'     => $joined,
329                                 'start_date' => generateDateTime($data['start_time'], 2),
330                                 'end_date'   => generateDateTime($data['end_time']  , 2),
331                                 'active_lnk' => translateYesNo($data['is_active']),
332                                 'notify_lnk' => translateYesNo($data['send_notify']),
333                                 'auto_lnk'   => translateYesNo($data['auto_add_new_user']),
334                                 'notified'   => translateYesNo($data['notified']),
335                                 'prices_cnt' => translateComma(countSumTotalData($data['id'], 'rallye_prices', 'id', 'rallye_id', true)),
336                                 'descr'      => $data['descr'],
337                                 'min_users'  => $data['min_users'],
338                                 'min_prices' => $data['min_prices'],
339                         );
340
341                         // Is the rallye active or not?
342                         switch ($data['is_active']) {
343                                 case 'Y':
344                                         // Rallye is active so do not edit it!
345                                         $row['select'] = '<div class="big">' . $row['id'] . '</div>';
346                                         $row['active_title'] = getMessage('RALLYE_DEACTIVATE_NOW');
347                                         $row['active'] = '0';
348                                         break;
349
350                                 case 'N':
351                                         $row['active_title'] = getMessage('RALLYE_ACTIVATE_NOW');
352                                         $row['active'] = 1;
353                                         break;
354                         }
355
356                         // Notification to members?
357                         switch ($data['send_notify']) {
358                                 case 'Y':
359                                         $row['notify_title'] = getMessage('RALLYE_STOP_NOTIFY_NOW');
360                                         $row['notify'] = '0';
361                                         break;
362
363                                 case 'N':
364                                         $row['notify_title'] = getMessage('RALLYE_START_NOTIFY_NOW');
365                                         $row['notify'] = 1;
366                                         break;
367                         }
368
369                         // Auto-add of new joined members?
370                         switch ($data['auto_add_new_user']) {
371                                 case 'Y':
372                                         $row['auto_title'] = getMessage('RALLYE_STOP_AUTO_ADD_NOW');
373                                         $row['auto'] = '0';
374                                         break;
375
376                                 case 'N':
377                                         $row['auto_title'] = getMessage('RALLYE_START_AUTO_ADD_NOW');
378                                         $row['auto'] = 1;
379                                         break;
380                         }
381
382                         // Output row
383                         $OUT .= loadTemplate('admin_list_rallyes_row', true, $row);
384                         $SW = 3 - $SW;
385                 } // END - while
386
387                 // Free memory
388                 SQL_FREERESULT($result);
389
390                 // Remember rows in array
391                 $content['rows'] = $OUT;
392
393                 // Load template
394                 loadTemplate('admin_list_rallyes', false, $content);
395         } else {
396                 // No rallyes setup so far
397                 loadTemplate('admin_settings_saved', false, getMessage('RALLYE_NO_RALLYES_SETUP'));
398         }
399 }
400
401 // [EOF]
402 ?>