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