Wrapper getTurboRates() introduced
[mailer.git] / inc / libs / bonus_functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 11/10/2004 *
4  * ===================                          Last change: 03/18/2005 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : bonus_functions.php                              *
8  * -------------------------------------------------------------------- *
9  * Short description : Special functions for bonus extension            *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Spezielle Funktion fuer bonus-Erweiterung        *
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')) {
40         die();
41 } // END - if
42
43 // This function must be run *BEFORE* a link is removed from table 'mailer_user_links' !
44 function addTurboBonus ($mid, $userid, $type) {
45         // Shall we add bonus points?
46         if (!isBonusRallyeActive()) {
47                 return false;
48         } // END - if
49
50         // Init variables
51         $sql = ''; $bonus = '0'; $mail = '0'; $column = '';
52
53         // Select SQL command
54         switch ($type) {
55                 case 'bonusid':
56                         $column = 'bonus_id';
57                         $bonus = $mid;
58                         break;
59
60                 case 'mailid' :
61                         $column = 'mail_id';
62                         $mail = $mid;
63                         break;
64
65                 default:
66                         logDebugMessage(__FUNCTION__, __LINE__, sprintf("Invalid type %s detected.", $type));
67                         break;
68         } // END - switch
69
70         // Is a column name set?
71         if (empty($column)) {
72                 // No, then abort here
73                 return false;
74         } // END - if
75
76         // Check for entry
77         $rank = countSumTotalData($mid, 'bonus_turbo', 'id', $column, true) + 1;
78
79         // Which rank?
80         if ($rank == 1) {
81                 // First rank!
82                 $points = getConfig('turbo_bonus');
83         } else {
84                 // Anything else so let's explode all entered rank points
85                 $test = explode(';', getTurboRates());
86                 if (!empty($test[$rank - 2])) {
87                         // Level found
88                         $points = $test[$rank - 2];
89                 } else {
90                         // Level not found
91                         $points = '0.00000';
92                 }
93         }
94
95         // Add points to his account directly
96         SQL_QUERY_ESC("UPDATE `{?_MYSQL_PREFIX?}_user_data` SET `turbo_bonus`=`turbo_bonus`+%s WHERE `userid`=%s LIMIT 1",
97                 array(
98                         $points,
99                         bigintval($userid)
100                 ), __FUNCTION__, __LINE__);
101
102         // Rember this whole data for displaying ranking list
103         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_bonus_turbo` (`userid`,`mail_id`,`bonus_id`,`level`,`points`,`timemark`) VALUES (%s, %s, %s, %s, %s, UNIX_TIMESTAMP())",
104                 array(
105                         bigintval($userid),
106                         bigintval($mail),
107                         bigintval($bonus),
108                         $rank,
109                         $points
110                 ), __FUNCTION__, __LINE__);
111
112         if ((isExtensionInstalledAndNewer('bonus', '0.3.5')) && (getBonusMode() != 'ADD') && ($points > 0)) {
113                 handleBonusPoints($points, $userid);
114         } // END - if
115 }
116
117 //
118 function addBonusRanks ($data, $type, $userid) {
119         // Init variables
120         $self = false; $OUT = ''; $GLOBALS['ranking_content'] = array();
121
122         // Clear rankings by default
123         $GLOBALS['ranking_content']['rankings'] = '';
124
125         // How many ranks do we have?
126         $ranks = count(explode(';', getTurboRates())) + 1;
127
128         // Load current user's data
129         $result = SQL_QUERY_ESC("SELECT `level`,`points`,`timemark` FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE `%s`=%s AND `userid`=%s LIMIT 1",
130                 array(
131                         $type,
132                         $data,
133                         bigintval($userid)
134                 ), __FUNCTION__, __LINE__);
135
136         // Entry found?
137         if (SQL_NUMROWS($result) == 1) {
138                 // Load data
139                 $GLOBALS['ranking_content'] = merge_array($GLOBALS['ranking_content'], SQL_FETCHARRAY($result));
140
141                 // Remember all values for later use
142                 $self  = true;
143
144                 // Transfer data to template
145                 $GLOBALS['ranking_content']['timemark'] = generateDateTime($GLOBALS['ranking_content']['timemark'], 1);
146
147                 // Load template
148                 $GLOBALS['ranking_content']['own'] = loadTemplate('show_bonus_yr', true, $GLOBALS['ranking_content']);
149         } // END - if
150
151         // Load rankings
152         $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE `%s`=%s ORDER BY `level` ASC LIMIT {?bonus_lines?}",
153                 array($type, $data), __FUNCTION__, __LINE__);
154         if (!SQL_HASZERONUMS($result)) {
155                 // Output all ranks (levels)
156                 for ($rank = 1; $rank <= SQL_NUMROWS($result); $rank++) {
157                         // Load data
158                         $result_users = SQL_QUERY_ESC("SELECT
159         `userid`,`points`
160 FROM
161         `{?_MYSQL_PREFIX?}_bonus_turbo`
162 WHERE
163         `%s`=%s AND
164         `level`=%s
165 LIMIT 1",
166                                 array($type, $data, $rank), __FUNCTION__, __LINE__);
167
168                         // Nothing found by default
169                         $rows['userid'] = '---';
170                         $rows['points'] = '---';
171
172                         // Are you one of them?
173                         if (SQL_NUMROWS($result_users) == 1) {
174                                 // Load data
175                                 $rows = merge_array($rows, SQL_FETCHARRAY($result_users));
176                         } // END - if
177
178                         // Free result
179                         SQL_FREERESULT($result_users);
180
181                         // Add more
182                         $rows['rank'] = $rank;
183
184                         // Load row template
185                         $OUT .= loadTemplate('member_bonus_turbo_row', true, $rows);
186                 } // END - for
187
188                 if ($self === false) {
189                         // If current user was not found set constant
190                         $GLOBALS['ranking_content']['rankings'] = '{--MEMBER_BONUS_RANK_YOU_ARE_404--}';
191                 } // END - if
192         } else {
193                 // No entries found
194                 // @TODO Move this HTML to a template
195                 $OUT = '<tr>
196   <td colspan="3" align="center" height="30" class="bottom">
197     <div class="notice">{%message,MEMBER_BONUS_NO_RANKS=' . $data . '%}</div>
198   </td>
199 </tr>';
200         }
201
202         // Retutn content
203         return $OUT;
204 }
205
206 // Hanle any bonus points the given user shall become
207 function handleBonusPoints ($mode, $userid) {
208         // Shall we add bonus points?
209         if (!isBonusRallyeActive()) {
210                 return;
211         } // END - if
212
213         // Default is not working
214         $return = false;
215
216         // Switch to jackpot-mode when no UID is supplied but userid-mode is selected
217         if ((getBonusMode() == 'UID') && ($userid == '0') && (isExtensionActive('jackpot'))) {
218                 // Update database & config
219                 updateConfiguration('bonus_mode', 'JACKPOT');
220         } // END - if
221
222         if ($mode == 'login_bonus') {
223                 // Login bonus detected
224                 $points = getConfig('login_bonus');
225         } else {
226                 // Direct points supplied
227                 $points = $mode;
228         }
229
230         // Check his amount first
231         $total = getTotalPoints($userid);
232
233         // Subtract points from...
234         switch (getBonusMode()) {
235                 case 'ADD': // Only add them (no subtraction)
236                         // Ignored
237                         break;
238
239                 case 'JACKPOT': // ... jackpot
240                         if ((isExtensionActive('jackpot')) && (subtractPointsFromJackpot($points) === false) && (isValidUserId($userid))) {
241                                 if ($total >= $points) {
242                                         // Subtract points from userid's account
243                                         $return = subtractPointsFromJackpot('bonus_payout_jackpot', $userid, $points);
244                                 } // END - if
245                         } // END - if
246                         break;
247
248                 case 'UID': // ... userid's account
249                         if ($total >= $points) {
250                                 // Subtract points from userid's account
251                                 $return = subtractPoints('bonus_payout_userid', $userid, $points);
252                         } elseif (isExtensionActive('jackpot')) {
253                                 // Try to subtract from jackpot
254                                 $return = subtractPointsFromJackpot($points);
255                         }
256                         break;
257
258                 default: // This should not happen
259                         debug_report_bug(__FUNCTION__, __LINE__, 'Invalid bonus-mode ' . getBonusMode() . ' detected.');
260                         break;
261         } // END - switch
262
263         // Return status
264         return $return;
265 }
266
267 // Purges expired fast-click bonus entries
268 function purgeExpiredTurboBonus() {
269         // Remove entries
270         $result = SQL_QUERY('DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE (UNIX_TIMESTAMP() - `timemark`) >= {?bonus_timeout?}', __FUNCTION__, __LINE__);
271
272         if (!SQL_HASZEROAFFECTED()) {
273                 // Send out email to admin
274                 sendAdminNotification('{--ADMIN_AUTOPURGE_TURBO_SUBJECT--}', 'admin_autopurge_turbo', SQL_AFFECTEDROWS());
275         } // END - if
276 }
277
278 //-----------------------------------------------------------------------------
279 //                             Wrapper Functions
280 //-----------------------------------------------------------------------------
281
282 // Determines wether the "bonus rallye" is active
283 function isBonusRallyeActive () {
284         // Do we have cache?
285         if (!isset($GLOBALS['bonus_rallye_active'])) {
286                 // Just determine it
287                 $GLOBALS['bonus_rallye_active'] = (getConfig('bonus_active') == 'Y');
288         } // END - if
289
290         // Return cache
291         return $GLOBALS['bonus_rallye_active'];
292 }
293
294 // Determines wether the "bonus new_member_notify" is active
295 function isBonusNewMemberNotifyEnabled () {
296         // Do we have cache?
297         if (!isset($GLOBALS['bonus_new_member_notify_active'])) {
298                 // Just determine it
299                 $GLOBALS['bonus_new_member_notify_active'] = (getConfig('bonus_new_member_notify') == 'Y');
300         } // END - if
301
302         // Return cache
303         return $GLOBALS['bonus_new_member_notify_active'];
304 }
305
306 // Getter for bonus_timeout
307 function getBonusTimeout () {
308         // Do we have cache?
309         if (!isset($GLOBALS[__FUNCTION__])) {
310                 // Determine it
311                 $GLOBALS[__FUNCTION__] = getConfig('bonus_timeout');
312         } // END - if
313
314         // Return cache
315         return $GLOBALS[__FUNCTION__];
316 }
317
318 // Getter for bonus_mode
319 function getBonusMode () {
320         // Do we have cache?
321         if (!isset($GLOBALS[__FUNCTION__])) {
322                 // Determine it
323                 $GLOBALS[__FUNCTION__] = getConfig('bonus_mode');
324         } // END - if
325
326         // Return cache
327         return $GLOBALS[__FUNCTION__];
328 }
329
330 // Getter for bonus_ranks
331 function getBonusRanks () {
332         // Do we have cache?
333         if (!isset($GLOBALS[__FUNCTION__])) {
334                 // Determine it
335                 $GLOBALS[__FUNCTION__] = getConfig('bonus_ranks');
336         } // END - if
337
338         // Return cache
339         return $GLOBALS[__FUNCTION__];
340 }
341
342 // Getter for turbo_rates
343 function getTurboRates () {
344         // Do we have cache?
345         if (!isset($GLOBALS[__FUNCTION__])) {
346                 // Determine it
347                 $GLOBALS[__FUNCTION__] = getConfig('turbo_rates');
348         } // END - if
349
350         // Return cache
351         return $GLOBALS[__FUNCTION__];
352 }
353
354 // [EOF]
355 ?>