9033627032e8fa88e1a83ae2ef46af2fa7ccaf7e
[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(';', getConfig('turbo_rates'));
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                         bigintval($userid),
99                         $points
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);
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(';', getConfig('turbo_rates'))) + 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
177                                 // Is ext-nickname active?
178                                 if (isExtensionActive('nickname')) {
179                                         // Then get the nickname
180                                         $nick = getNickname($rows['userid']);
181
182                                         // Is it not empty? Then use it
183                                         if (!empty($nick)) $rows['userid'] = $nick;
184                                 } // END - if
185                         } // END - if
186
187                         // Free result
188                         SQL_FREERESULT($result_users);
189
190                         // Add more
191                         $rows['rank'] = $rank;
192
193                         // Load row template
194                         $OUT .= loadTemplate('member_bonus_turbo_row', true, $rows);
195                 } // END - for
196
197                 if ($self === false) {
198                         // If current user was not found set constant
199                         $GLOBALS['ranking_content']['rankings'] = '{--MEMBER_BONUS_RANK_YOU_ARE_404--}';
200                 } // END - if
201         } else {
202                 // No entries found
203                 // @TODO Move this HTML to a template
204                 $OUT = '<tr>
205   <td colspan="3" align="center" height="30" class="bottom">
206     <div class="notice">' . getMaskedMessage('MEMBER_BONUS_NO_RANKS', $data) . '</div>
207   </td>
208 </tr>';
209         }
210
211         // Retutn content
212         return $OUT;
213 }
214
215 //
216 function handleBonusPoints ($mode) {
217         // Shall we add bonus points?
218         if (!isBonusRallyeActive()) return;
219
220         // Switch to jackpot-mode when no UID is supplied but userid-mode is selected
221         if ((getBonusMode() == 'UID') && (getBonusUserid() == '0') && (isExtensionActive('jackpot'))) {
222                 // Update database & config
223                 updateConfiguration('bonus_mode', 'JACKPOT');
224         } // END - if
225
226         if ($mode == 'login_bonus') {
227                 // Login bonus detected
228                 $points = getConfig('login_bonus');
229         } else {
230                 // Direct points supplied
231                 $points = $mode;
232         }
233
234         // Check his amount first
235         $total = getTotalPoints(getBonusUserid());
236
237         // Subtract points from...
238         switch (getBonusMode()) {
239                 case 'ADD': // Only add them (no subtraction)
240                         // Ignored
241                         break;
242
243                 case 'JACKPOT': // ... jackpot
244                         if ((isExtensionActive('jackpot')) && (subtractPointsFromJackpot($points) == -1) && (isValidUserId(getBonusUserid()))) {
245                                 if ($total >= $points) {
246                                         // Subtract points from userid's account
247                                         subtractPointsFromJackpot('bonus_payout_jackpot', getBonusUserid(), $points);
248                                 } // END - if
249                         } // END - if
250                         break;
251
252                 case 'UID': // ... userid's account
253                         if ($total >= $points) {
254                                 // Subtract points from userid's account
255                                 subtractPoints('bonus_payout_userid', getBonusUserid(), $points);
256                         } elseif (isExtensionActive('jackpot')) {
257                                 // Try to subtract from jackpot
258                                 $dummy = subtractPointsFromJackpot($points);
259                         }
260                         break;
261
262                 default: // This should not happen
263                         debug_report_bug(__FUNCTION__, __LINE__, 'Invalid bonus-mode ' . getBonusMode() . ' detected.');
264                         break;
265         } // END - switch
266 }
267
268 // Purges expired fast-click bonus entries
269 function purgeExpiredTurboBonus() {
270         // Remove entries
271         $result = SQL_QUERY('DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE (UNIX_TIMESTAMP() - `timemark`) >= {?bonus_timeout?}', __FUNCTION__, __LINE__);
272
273         if (!SQL_HASZEROAFFECTED()) {
274                 // Send out email to admin
275                 sendAdminNotification('{--ADMIN_AUTOPURGE_TURBO_SUBJECT--}', 'admin_autopurge_turbo', SQL_AFFECTEDROWS());
276         } // END - if
277 }
278
279 //-----------------------------------------------------------------------------
280 //                             Wrapper Functions
281 //-----------------------------------------------------------------------------
282
283 // Determines wether the "bonus rallye" is active
284 function isBonusRallyeActive () {
285         // Do we have cache?
286         if (!isset($GLOBALS['bonus_rallye_active'])) {
287                 // Just determine it
288                 $GLOBALS['bonus_rallye_active'] = (getConfig('bonus_active') == 'Y');
289         } // END - if
290
291         // Return cache
292         return $GLOBALS['bonus_rallye_active'];
293 }
294
295 // Determines wether the "bonus new_member_notify" is active
296 function isBonusNewMemberNotifyEnabled () {
297         // Do we have cache?
298         if (!isset($GLOBALS['bonus_new_member_notify_active'])) {
299                 // Just determine it
300                 $GLOBALS['bonus_new_member_notify_active'] = (getConfig('bonus_new_member_notify') == 'Y');
301         } // END - if
302
303         // Return cache
304         return $GLOBALS['bonus_new_member_notify_active'];
305 }
306
307 // Getter for bonus_timeout
308 function getBonusTimeout () {
309         // Do we have cache?
310         if (!isset($GLOBALS[__FUNCTION__])) {
311                 // Determine it
312                 $GLOBALS[__FUNCTION__] = getConfig('bonus_timeout');
313         } // END - if
314
315         // Return cache
316         return $GLOBALS[__FUNCTION__];
317 }
318
319 // Getter for bonus_mode
320 function getBonusMode () {
321         // Do we have cache?
322         if (!isset($GLOBALS[__FUNCTION__])) {
323                 // Determine it
324                 $GLOBALS[__FUNCTION__] = getConfig('bonus_mode');
325         } // END - if
326
327         // Return cache
328         return $GLOBALS[__FUNCTION__];
329 }
330
331 // Getter for bonus_ranks
332 function getBonusRanks () {
333         // Do we have cache?
334         if (!isset($GLOBALS[__FUNCTION__])) {
335                 // Determine it
336                 $GLOBALS[__FUNCTION__] = getConfig('bonus_ranks');
337         } // END - if
338
339         // Return cache
340         return $GLOBALS[__FUNCTION__];
341 }
342
343 // [EOF]
344 ?>