]> git.mxchange.org Git - mailer.git/blob - inc/libs/bonus_functions.php
22c6f07d4ae698ae87e8994ff88d3ed86891ab1c
[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 - 2012 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')) {
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`,
160         `points`
161 FROM
162         `{?_MYSQL_PREFIX?}_bonus_turbo`
163 WHERE
164         `%s`=%s AND
165         `level`=%s
166 LIMIT 1",
167                                 array($type, $data, $rank), __FUNCTION__, __LINE__);
168
169                         // Nothing found by default
170                         $rows['userid'] = '---';
171                         $rows['points'] = '---';
172
173                         // Are you one of them?
174                         if (SQL_NUMROWS($result_users) == 1) {
175                                 // Load data
176                                 $rows = merge_array($rows, SQL_FETCHARRAY($result_users));
177                         } // END - if
178
179                         // Free result
180                         SQL_FREERESULT($result_users);
181
182                         // Add more
183                         $rows['rank'] = $rank;
184
185                         // Load row template
186                         $OUT .= loadTemplate('member_bonus_turbo_row', true, $rows);
187                 } // END - for
188
189                 if ($self === false) {
190                         // If current user was not found set constant
191                         $GLOBALS['ranking_content']['rankings'] = '{--MEMBER_BONUS_RANK_YOU_ARE_404--}';
192                 } // END - if
193         } else {
194                 // No entries found
195                 // @TODO Move this HTML to a template
196                 $OUT = '<tr>
197   <td colspan="3" align="center" height="30" class="bottom">
198     <div class="bad">{%message,MEMBER_BONUS_NO_RANKS=' . $data . '%}</div>
199   </td>
200 </tr>';
201         }
202
203         // Retutn content
204         return $OUT;
205 }
206
207 // Hanle any bonus points the given user shall become
208 function handleBonusPoints ($mode, $userid) {
209         // Shall we add bonus points?
210         if (!isBonusRallyeActive()) {
211                 return;
212         } // END - if
213
214         // Default is not working
215         $return = false;
216
217         // Switch to jackpot-mode when no UID is supplied but userid-mode is selected
218         if ((getBonusMode() == 'UID') && ($userid == '0') && (isExtensionActive('jackpot'))) {
219                 // Update database & config
220                 updateConfiguration('bonus_mode', 'JACKPOT');
221         } // END - if
222
223         if ($mode == 'login_bonus') {
224                 // Login bonus detected
225                 $points = getConfig('login_bonus');
226         } else {
227                 // Direct points supplied
228                 $points = $mode;
229         }
230
231         // Check his amount first
232         $total = getTotalPoints($userid);
233
234         // Subtract points from...
235         switch (getBonusMode()) {
236                 case 'ADD': // Only add them (no subtraction)
237                         // Ignored
238                         break;
239
240                 case 'JACKPOT': // ... jackpot
241                         if ((isExtensionActive('jackpot')) && (subtractPointsFromJackpot($points) === false) && (isValidUserId($userid))) {
242                                 if ($total >= $points) {
243                                         // Subtract points from userid's account
244                                         $return = subtractPointsFromJackpot('bonus_payout_jackpot', $userid, $points);
245                                 } // END - if
246                         } // END - if
247                         break;
248
249                 case 'UID': // ... userid's account
250                         if ($total >= $points) {
251                                 // Subtract points from userid's account
252                                 $return = subtractPoints('bonus_payout_userid', $userid, $points);
253                         } elseif (isExtensionActive('jackpot')) {
254                                 // Try to subtract from jackpot
255                                 $return = subtractPointsFromJackpot($points);
256                         }
257                         break;
258
259                 default: // This should not happen
260                         reportBug(__FUNCTION__, __LINE__, 'Invalid bonus-mode ' . getBonusMode() . ' detected.');
261                         break;
262         } // END - switch
263
264         // Return status
265         return $return;
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 whether the "bonus rallye" is active
284 function isBonusRallyeActive () {
285         // Is there 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 whether the "bonus new_member_notify" is active
296 function isBonusNewMemberNotifyEnabled () {
297         // Is there 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         // Is there 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         // Is there 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         // Is there 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 // Getter for turbo_rates
344 function getTurboRates () {
345         // Is there cache?
346         if (!isset($GLOBALS[__FUNCTION__])) {
347                 // Determine it
348                 $GLOBALS[__FUNCTION__] = getConfig('turbo_rates');
349         } // END - if
350
351         // Return cache
352         return $GLOBALS[__FUNCTION__];
353 }
354
355 // Getter for login_timeout
356 function getLoginTimeout () {
357         // Is there cache?
358         if (!isset($GLOBALS[__FUNCTION__])) {
359                 // Determine it
360                 $GLOBALS[__FUNCTION__] = getConfig('login_timeout');
361         } // END - if
362
363         // Return cache
364         return $GLOBALS[__FUNCTION__];
365 }
366
367 // Getter for bonus_ref
368 function getBonusRef () {
369         // Is there cache?
370         if (!isset($GLOBALS[__FUNCTION__])) {
371                 // Determine it
372                 $GLOBALS[__FUNCTION__] = getConfig('bonus_ref');
373         } // END - if
374
375         // Return cache
376         return $GLOBALS[__FUNCTION__];
377 }
378
379 // [EOF]
380 ?>