Huge script change, see http://forum.mxchange.org/topic-458.html for details:
[mailer.git] / inc / libs / bonus_functions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    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  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if (!defined('__SECURITY')) {
41         die();
42 } // END - if
43
44 // This function must be run *BEFORE* a link is removed from table 'mxchange_user_links' !
45 function addTurboBonus ($mid, $userid, $type) {
46         // Shall we add bonus points?
47         if (getConfig('bonus_active') != 'Y') return false;
48
49         // Init variables
50         $sql = ''; $bonys = 0; $mail = 0; $column = '';
51
52         // Select SQL command
53         switch ($type)
54         {
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         }
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($userid, 'bonus_turbo', 'id', 'userid', true, sprintf(" AND `%s`=%s", $column, $mid)) + 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(bigintval($userid), $points), __FUNCTION__, __LINE__);
98
99         // Rember this whole data for displaying ranking list
100         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())",
101                 array(
102                         bigintval($userid),
103                         bigintval($mail),
104                         bigintval($bonus),
105                         $rank,
106                         $points
107                 ), __FUNCTION__, __LINE__);
108
109         if ((getExtensionVersion('bonus') >= '0.3.5') && (getConfig('bonus_mode') != "ADD") && ($points > 0)) handleBonusPoints($points);
110 }
111
112 //
113 function addBonusRanks ($data, $type, $userid) {
114         // Init variables
115         $self = false; $OUT = ''; $content = array();
116
117         // Clear rankings by default
118         setConfigEntry('__rankings', '');
119
120         // How many ranks do we have?
121         $ranks = count(explode(';', getConfig('turbo_rates'))) + 1;
122
123         // Load current user's data
124         $result = SQL_QUERY_ESC("SELECT `level`, `points`, `timemark` FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE `%s`=%s AND `userid`=%s LIMIT 1",
125                 array($type, $data, $userid), __FUNCTION__, __LINE__);
126         if (SQL_NUMROWS($result) == 1) {
127                 // Load data
128                 $content = merge_array($content, SQL_FETCHARRAY($result));
129
130                 // Remember all values for later use
131                 $self  = true;
132
133                 // Transfer data to template
134                 $content['yr_level']  = $content['level'];
135                 $content['yr_points'] = translateComma($content['points']);
136                 $content['yr_tmark']  = generateDateTime($content['timemark'], '1');
137
138                 // Load template
139                 setConfigEntry('__rankings', loadTemplate('show_bonus_yr', true, $content));
140         } // END - if
141
142         // Load rankings
143         $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE %s=%s ORDER BY `level` ASC LIMIT %s",
144                 array($type, $data, getConfig('bonus_lines')), __FUNCTION__, __LINE__);
145         if (SQL_NUMROWS($result) > 0) {
146                 // Start generating the ranking list
147                 $max = SQL_NUMROWS($result);
148
149                 // Init variables
150                 $SW = 2; $content = array();
151
152                 // Output all ranks (levels)
153                 for ($rank = 1; $rank <= $max; $rank++) {
154                         // Load data
155                         $result_users = SQL_QUERY_ESC("SELECT `userid`, `points`, `timemark` FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE `%s`=%s AND level=%s LIMIT 1",
156                                 array($type, $data, $rank), __FUNCTION__, __LINE__);
157
158                         // Nothing found by default
159                         $content['userid'] = '---'; $content['points'] = '---';
160
161                         // Are you one of them?
162                         if (SQL_NUMROWS($result_users) == 1) {
163                                 // Load data
164                                 $content = merge_array($content, SQL_FETCHARRAY($result_users));
165
166                                 // Translate comma
167                                 $content['points'] = translateComma($content['points']);
168                         } // END - if
169
170                         // Add more
171                         $content['rank'] = $rank;
172                         $content['sw']   = $SW;
173
174                         // Output row
175                         $OUT .= "<tr>
176   <td class=\"bonus_rank_".$content['level']." bottom2 switch_sw".$content['sw']."\">&nbsp;".$content['level'].".</td>
177   <td class=\"bonus_rank_".$content['level']." bottom2 switch_sw".$content['sw']."\" align=\"center\">".$content['userid']."</td>
178   <td class=\"bonus_rank_".$content['level']." bottom2 switch_sw".$content['sw']."\" align=\"center\">".$content['points']."</td>
179 </tr>\n";
180
181                         // Switch color
182                         $SW = 3 - $SW;
183                 } // END - for
184
185                 if ($self === false) {
186                         // If current user was not found set constant
187                         // @TODO Try to find a way for rewriting this constant
188                         setConfigEntry('__rankings', getMessage('BONUS_RANK_YOU_ARE_404'));
189                 } // END - if
190         } else {
191                 // No entries found!
192                 $OUT = "<tr>
193   <td colspan=\"3\" align=\"center\" height=\"30\" class=\"bottom2\">
194     <div class=\"guest_failed\">".sprintf(getMessage('BONUS_NO_RANKS'), $data)."</div>
195   </td>
196 </tr>\n";
197         }
198
199         // Retutn content
200         return $OUT;
201 }
202
203 //
204 function handleBonusPoints ($mode) {
205         // Shall we add bonus points?
206         if (getConfig('bonus_active') != 'Y') return;
207
208         // Switch to jackpot-mode when no UID is supplied but userid-mode is selected
209         if ((getConfig('bonus_mode') == 'UID') && (getConfig('bonus_userid') == '0') && (isExtensionActive('jackpot'))) {
210                 // Update database & config
211                 updateConfiguration('bonus_mode', 'JACKPOT');
212         } // END - if
213
214         if ($mode == 'login_bonus') {
215                 // Login bonus detected
216                 $points = getConfig('login_bonus');
217         } else {
218                 // Direct points supplied
219                 $points = $mode;
220         }
221
222         // Subtract points from...
223         switch (getConfig('bonus_mode')) {
224                 case 'JACKPOT': // ... jackpot
225                         if ((isExtensionActive('jackpot')) && (subtractPointsFromJackpot($points) == -1) && (getConfig('bonus_userid') > 0)) {
226                                 // Check points amount first...
227                                 $total = countSumTotalData(getConfig('bonus_userid'), 'user_points', 'points') - countSumTotalData(getConfig('bonus_userid'), 'user_data', 'used_points');
228                                 if ($total >= $points) {
229                                         // Subtract points from userid's account
230                                         subtractPoints('bonus_payout_jackpot', getConfig('bonus_userid'), $points);
231                                 } // END - if
232                         } // END - if
233                         break;
234
235                 case 'UID': // ... userid's account
236                         // Check his amount first
237                         $total = countSumTotalData(getConfig('bonus_userid'), 'user_points', 'points') - countSumTotalData(getConfig('bonus_userid'), 'user_data', 'used_points');
238                         if ($total >= $points) {
239                                 // Subtract points from userid's account
240                                 subtractPoints('bonus_payout_userid', getConfig('bonus_userid'), $points);
241                         } elseif (isExtensionActive('jackpot')) {
242                                 // Try to subtract from jackpot
243                                 $dummy = subtractPointsFromJackpot($points);
244                         }
245                         break;
246         }
247 }
248
249 // Purges expired fast-click bonus entries
250 function purgeExpiredTurboBonus() {
251         // Remove entries
252         $result = SQL_QUERY("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_bonus_turbo` WHERE `timemark` < (UNIX_TIMESTAMP() - {?bonus_timeout?})", __FUNCTION__, __LINE__);
253
254         if (SQL_AFFECTEDROWS() > 0) {
255                 // Send out email to admin
256                 sendAdminNotification(getMessage('AUTOPURGE_ADMIN_TURBO_SUBJECT'), 'admin_autopurge_turbo', SQL_AFFECTEDROWS(), '');
257         } // END - if
258 }
259
260 //
261 ?>