Renamed ifSqlHasZeroNums() to ifSqlHasZeroNumRows() and improved some queries.
[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 - 2015 by Mailer Developer Team                   *
20  * For more information visit: http://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_id')) {
51         // Init SQL queries
52         initSqls();
53
54         if (isGetRequestElementSet('activate')) {
55                 // Activate / deactivate
56                 switch (getRequestElement('activate')) {
57                         case '1': // Activate
58                                 addSql(sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_rallye_data` SET `is_active`='Y' WHERE `id`=%s AND `is_active`='N' LIMIT 1",
59                                         array(bigintval(getRequestElement('rallye_id'))), __FILE__, __LINE__, FALSE)
60                                 );
61                                 break;
62
63                         case '0': // Deactivate
64                                 addSql(sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_rallye_data` SET `is_active`='N' WHERE `id`=%s AND `is_active`='Y' LIMIT 1",
65                                         array(bigintval(getRequestElement('rallye_id'))), __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(sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_rallye_data` SET `send_notify`='Y' WHERE `id`=%s AND `send_notify`='N' LIMIT 1",
74                                         array(bigintval(getRequestElement('rallye_id'))), __FILE__, __LINE__, FALSE)
75                                 );
76                                 break;
77
78                         case '0': // Deactivate
79                                 addSql(sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_rallye_data` SET `send_notify`='N' WHERE `id`=%s AND `send_notify`='Y' LIMIT 1",
80                                         array(bigintval(getRequestElement('rallye_id'))), __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(sqlQueryEscaped("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_id'))), __FILE__, __LINE__, FALSE)
90                                 );
91                                 break;
92
93                         case '0': // Deactivate
94                                 addSql(sqlQueryEscaped("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_id'))), __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(sqlQueryEscaped("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_rallye_data` WHERE `id`=%s LIMIT 1",
113                                 array(bigintval($id)), __FILE__, __LINE__, FALSE));
114                         addSql(sqlQueryEscaped("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_rallye_prices` WHERE `rallye_id`=%s",
115                                 array(bigintval($id)), __FILE__, __LINE__, FALSE));
116                         addSql(sqlQueryEscaped("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 = '{--ADMIN_RALLYE_DELETED--}';
125         } else {
126                 // No rallye selected to delete!
127                 $message = '{--ADMIN_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_time_ho', $id), postRequestElement('start_time_mi', $id), 0, postRequestElement('start_time_mo', $id), postRequestElement('start_time_da', $id), postRequestElement('start_time_ye', $id));
142                         $END   = mktime(postRequestElement('end_time_ho', $id)  , postRequestElement('end_time_mi', $id)  , 0, postRequestElement('end_time_mo', $id)  , postRequestElement('end_time_da', $id)  , postRequestElement('end_time_ye', $id)  );
143
144                         // Update entry
145                         addSql(sqlQueryEscaped("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 = '{--ADMIN_RALLYE_CHANGED--}';
175         }
176 }
177
178 // edit/delete are both handled the same way
179 if ((isFormSent('edit')) || (isFormSent('delete'))) {
180         // Default is 'edit'
181         $mode = 'edit';
182
183         // Is delete button used?
184         if (isFormSent('delete')) {
185                 // Then set it
186                 $mode = 'delete';
187         } // END - if
188
189         // Check for selections
190         if (ifPostContainsSelections()) {
191                 // Make all selected and deactivated rallyes editable
192                 $OUT = '';
193                 foreach (postRequestElement('sel') as $id => $selected) {
194                         // Load rallye basic data
195                         $result = sqlQueryEscaped("SELECT
196         `title`,
197         `descr`,
198         `template`,
199         `start_time`,
200         `end_time`,
201         `min_users`,
202         `min_prices`
203 FROM
204         `{?_MYSQL_PREFIX?}_rallye_data`
205 WHERE
206         `id`=%s
207 LIMIT 1",
208                                 array(bigintval($id)), __FILE__, __LINE__);
209
210                         // Fetch an array
211                         $content = sqlFetchArray($result);
212
213                         // Free result
214                         sqlFreeResult($result);
215
216                         // Only for editing ...
217                         if ($mode == 'edit') {
218                                 // Starting day
219                                 $content['s_min']   = addSelectionBox('mi', getMinute($content['start_time'])   , 'start_time', $id);
220                                 $content['s_hour']  = addSelectionBox('ho', getShortHour($content['start_time']), 'start_time', $id);
221                                 $content['s_day']   = addSelectionBox('da', getDay($content['start_time'])      , 'start_time', $id);
222                                 $content['s_month'] = addSelectionBox('mo', getMonth($content['start_time'])    , 'start_time', $id);
223                                 $content['s_year']  = addSelectionBox('ye', getYear($content['start_time'])     , 'start_time', $id);
224
225                                 // Ending day
226                                 $content['e_min']   = addSelectionBox('mi', getMinute($content['end_time'])     , 'end_time'  , $id);
227                                 $content['e_hour']  = addSelectionBox('ho', getShortHour($content['end_time'])  , 'end_time'  , $id);
228                                 $content['e_day']   = addSelectionBox('da', getDay($content['end_time'])        , 'end_time'  , $id);
229                                 $content['e_month'] = addSelectionBox('mo', getMonth($content['end_time'])      , 'end_time'  , $id);
230                                 $content['e_year']  = addSelectionBox('ye', getYear($content['end_time'])       , 'end_time'  , $id);
231
232                                 // Remember other values
233                                 $content['templ']      = addReferralRallyeTemplateSelection('templ[' . $id . ']', $content['template']);
234                         } else {
235                                 // Convert timestamps into human-readable
236                                 $content['start_time'] = generateDateTime($content['start_time'], '2');
237                                 $content['end_time']   = generateDateTime($content['end_time']  , '2');
238                         }
239
240                         // Remember id number
241                         $content['rallye_id']         = bigintval($id);
242
243                         // Output row
244                         $OUT .= loadTemplate('admin_' . $mode . '_rallyes_row', TRUE, $content);
245                 } // END - foreach
246
247                 // Load final template
248                 loadTemplate('admin_' . $mode . '_rallyes', FALSE, $OUT);
249         } else {
250                 // Nothing selected to edit
251                 displayMessage('{--ADMIN_RALLYE_NO_RALLYES_SELECTED--}');
252         }
253 } elseif ((getRequestElement('sub') == 'users') && (getRequestElement('rallye_id') > 0)) {
254         // List users and their refs before start and current
255         $result = sqlQueryEscaped("SELECT
256         `userid`,
257         `refs`,
258         `curr_points`
259 FROM
260         `{?_MYSQL_PREFIX?}_rallye_users`
261 WHERE
262         `rallye_id`=%s
263 ORDER BY
264         `userid` ASC",
265                 array(bigintval(getRequestElement('rallye_id'))), __FILE__, __LINE__);
266         if (!ifSqlHasZeroNumRows($result)) {
267                 $OUT = '';
268                 $content['rallye_id'] = bigintval(getRequestElement('rallye_id'));
269                 while ($row = sqlFetchArray($result)) {
270                         // Check for referral count
271                         $count = getReferralRallyeRefsCount($row['userid'], $row['refs']);
272
273                         // Init variables
274                         $bl = '';
275                         $br = '';
276
277                         // Output row
278                         if (($row['curr_points'] > 0) && ($count > 0)) {
279                                 $bl = '<strong>';
280                                 $br = '</strong>';
281                         } // END - if
282
283                         // Get user points
284                         $points = countSumTotalData($row['userid'], 'user_points', 'points', 'userid', FALSE, " AND `ref_depth`=1");
285                         //* DEBUG: */ debugOutput(basename(__FILE__).':userid='.$row['userid'].',points='.$points.',opoints='.$row['curr_points']);
286
287                         // Prepare content
288                         $row = array(
289                                 'userid'  => $row['userid'],
290                                 'bold_l'  => $bl ,
291                                 'bold_r'  => $br ,
292                                 'refs'    => $row['refs'],
293                                 'count'   => $count,
294                                 'opoints' => ($points - $row['curr_points']),
295                         );
296
297                         // Load row template and switch color
298                         $OUT .= loadTemplate('admin_list_rallye_usr_row', TRUE, $row);
299                 } // END - while
300
301                 // Remember rows
302                 $content['rows']      = $OUT;
303                 $content['rallye_id'] = bigintval(getRequestElement('rallye_id'));
304
305                 // Free memory
306                 sqlFreeResult($result);
307
308                 // Load template
309                 loadTemplate('admin_list_rallye_usr', FALSE, $content);
310         } else {
311                 // No entries found?
312                 displayMessage('{--ADMIN_RALLYE_USERS_404--}');
313         }
314 } else {
315         // Start listing rallyes
316         $result = sqlQuery("SELECT
317         `id` AS `rallye_id`,
318         `admin_id`,
319         `title`,
320         `descr`,
321         `template`,
322         `start_time`,
323         `end_time`,
324         `auto_add_new_user`,
325         `is_active`,
326         `send_notify`,
327         `notified`,
328         `min_users`,
329         `min_prices`
330 FROM
331         `{?_MYSQL_PREFIX?}_rallye_data`
332 ORDER BY
333         `start_time` DESC", __FILE__, __LINE__);
334
335         // Records found?
336         if (!ifSqlHasZeroNumRows($result)) {
337                 // List found rallyes
338                 $OUT = '';
339                 while ($data = sqlFetchArray($result)) {
340                         // Count joined userids
341                         $users = countSumTotalData($data['rallye_id'], 'rallye_users', 'rallye_id', 'rallye_id', TRUE);
342
343                         // Did some users joined this rallye?
344                         if ($users > 0) {
345                                 // List joined users
346                                 $users = '<a href="{%url=modules.php?module=admin&amp;what=list_rallyes&amp;sub=users&amp;rallye_id=' . $data['rallye_id'] . '%}" title="{--ADMIN_LIST_RALLYE_USERS_TITLE--}">{%pipe,translateComma=' . $users . '%}</a>';
347                         } // END - if
348
349                         // Transfer data into array for the template
350                         $row = array(
351                                 'select'            => '<input type="checkbox" name="sel[' . $data['rallye_id'] . ']" class="form_field" value="1" />',
352                                 'admin_id'          => $data['admin_id'],
353                                 'rallye_id'         => $data['rallye_id'],
354                                 'title'             => $data['title'],
355                                 'template'          => $data['template'],
356                                 'users_content'     => $users,
357                                 'start_date'        => generateDateTime($data['start_time'], 2),
358                                 'end_date'          => generateDateTime($data['end_time']  , 2),
359                                 'is_active'         => $data['is_active'],
360                                 'send_notify'       => $data['send_notify'],
361                                 'auto_add_new_user' => $data['auto_add_new_user'],
362                                 'notified'          => $data['notified'],
363                                 'prices_cnt'        => countSumTotalData($data['rallye_id'], 'rallye_prices', 'rallye_id', 'rallye_id', TRUE),
364                                 'descr'             => $data['descr'],
365                                 'min_users'         => $data['min_users'],
366                                 'min_prices'        => $data['min_prices'],
367                         );
368
369                         // Is the rallye active or not?
370                         switch ($data['is_active']) {
371                                 case 'Y':
372                                         // Rallye is active so do not edit it!
373                                         $row['select']       = '<div class="big">' . $row['rallye_id'] . '</div>';
374                                         $row['active_title'] = '{--ADMIN_RALLYE_DEACTIVATE_NOW--}';
375                                         $row['active']       = '0';
376                                         break;
377
378                                 case 'N':
379                                         $row['active_title'] = '{--ADMIN_RALLYE_ACTIVATE_NOW--}';
380                                         $row['active']       = 1;
381                                         break;
382                         } // END - switch
383
384                         // Notification to members?
385                         switch ($data['send_notify']) {
386                                 case 'Y':
387                                         $row['notify_title'] = '{--ADMIN_RALLYE_STOP_NOTIFY_NOW--}';
388                                         $row['notify']       = '0';
389                                         break;
390
391                                 case 'N':
392                                         $row['notify_title'] = '{--ADMIN_RALLYE_START_NOTIFY_NOW--}';
393                                         $row['notify']       = 1;
394                                         break;
395                         } // END - switch
396
397                         // Auto-add of new joined members?
398                         switch ($data['auto_add_new_user']) {
399                                 case 'Y':
400                                         $row['auto_title'] = '{--ADMIN_RALLYE_STOP_AUTO_ADD_NOW--}';
401                                         $row['auto']       = '0';
402                                         break;
403
404                                 case 'N':
405                                         $row['auto_title'] = '{--ADMIN_RALLYE_START_AUTO_ADD_NOW--}';
406                                         $row['auto']       = 1;
407                                         break;
408                         } // END - switch
409
410                         // Output row
411                         $OUT .= loadTemplate('admin_list_rallyes_row', TRUE, $row);
412                 } // END - while
413
414                 // Free memory
415                 sqlFreeResult($result);
416
417                 // Remember rows in array
418                 $content['rows'] = $OUT;
419
420                 // Load template
421                 loadTemplate('admin_list_rallyes', FALSE, $content);
422         } else {
423                 // No rallyes setup so far
424                 displayMessage('{--ADMIN_RALLYE_NO_RALLYES_SETUP--}');
425         }
426 }
427
428 // [EOF]
429 ?>