A lot fixes to templates and missing functions added, more rewrites
[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  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // This function must be run *BEFORE* a link is removed from table 'mxchange_user_links' !
41 function BONUS_ADD_TURBO_POINTS ($mid, $uid, $type) {
42         // Shall we add bonus points?
43         if (getConfig('bonus_active') != "Y") return false;
44
45         // Init variables
46         $SQL = ""; $bonys = 0; $mail = 0; $column = "";
47
48         // Select SQL command
49         switch ($type)
50         {
51         case "bonusid":
52                 $column = "bonus_id";
53                 $bonus = $mid;
54                 break;
55
56         case "mailid" :
57                 $column = "mail_id";
58                 $mail = $mid;
59                 break;
60
61         default:
62                 DEBUG_LOG(__FUNCTION__, __LINE__, sprintf("Invalid type %s detected.", $type));
63                 break;
64         }
65
66         // Is a column name set?
67         if (empty($column)) {
68                 // No, then abort here
69                 return false;
70         } // END - if
71
72         // Check for entry
73         $result = SQL_QUERY_ESC("SELECT COUNT(id) AS cnt FROM  `{!_MYSQL_PREFIX!}_bonus_turbo` WHERE userid=%s AND %s=%s LIMIT 1",
74                 array($uid, $column, $mid), __FUNCTION__, __LINE__);
75
76         // Load clicks from table as current rank
77         list($rank) = SQL_FETCHROW($result);
78
79         // Add one line
80         $rank++;
81
82         // Free result
83         SQL_FREERESULT($result);
84
85         // Which rank?
86         if ($rank == 1) {
87                 // First rank!
88                 $points = getConfig('turbo_bonus');
89         } else {
90                 // Anything else so let's explode all entered rank points
91                 $test = explode(";", getConfig('turbo_rates'));
92                 if (!empty($test[$rank - 2])) {
93                         // Level found
94                         $points = $test[$rank - 2];
95                 } else {
96                         // Level not found!
97                         $points = "0.00000";
98                 }
99         }
100
101         // Add points to his account directly
102         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET turbo_bonus=turbo_bonus+".$points." WHERE userid=%s LIMIT 1",
103                 array(bigintval($uid)), __FUNCTION__, __LINE__);
104
105         // Rember this whole data for displaying ranking list
106         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())",
107                 array(bigintval($uid), bigintval($mail), bigintval($bonus), $rank, $points), __FUNCTION__, __LINE__);
108
109         if ((GET_EXT_VERSION("bonus") >= "0.3.5") && (getConfig('bonus_mode') != "ADD") && ($points > 0)) BONUS_POINTS_HANDLER($points);
110 }
111 //
112 function BONUS_MAKE_RANK_ROWS ($data, $type, $uid) {
113         $self = false; $OUT = "";
114
115         // How many ranks do we have?
116         $ranks = sizeof(explode(";", getConfig('turbo_rates'))) + 1;
117
118         // Load current user's data
119         $result = SQL_QUERY_ESC("SELECT level, points, timemark FROM `{!_MYSQL_PREFIX!}_bonus_turbo` WHERE %s=%s AND userid=%s LIMIT 1",
120                 array($type, $data, $uid), __FUNCTION__, __LINE__);
121         if (SQL_NUMROWS($result) == 1) {
122                 // Load data
123                 list($rank, $points, $mark) = SQL_FETCHROW($result);
124
125                 // Remember all values for later use
126                 $self  = true; $points = TRANSLATE_COMMA($points);
127
128                 // Transfer data to template
129                 define('__YR_LEVEL' , $rank);
130                 define('__YR_POINTS', $points);
131                 define('__YR_TMARK' , MAKE_DATETIME($mark, "1"));
132
133                 // Load template
134                 define('__YOUR_RANKING_LINE', LOAD_TEMPLATE("show_bonus_yr", true));
135         } // END - if
136
137         // Load rankings
138         $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_bonus_turbo` WHERE %s=%s ORDER BY level LIMIT %s",
139                 array($type, $data, getConfig('bonus_lines')), __FUNCTION__, __LINE__);
140         if (SQL_NUMROWS($result) > 0) {
141                 // Start generating the ranking list
142                 $max = SQL_NUMROWS($result);
143
144                 $SW = 2;
145                 for ($rank = 1; $rank <= $max; $rank++) {
146                         // Load data
147                         $result_users = SQL_QUERY_ESC("SELECT userid, points, timemark FROM `{!_MYSQL_PREFIX!}_bonus_turbo` WHERE %s=%s AND level=%s LIMIT 1",
148                                 array($type, $data, $rank), __FUNCTION__, __LINE__);
149
150                         // Nothing found by default
151                         $userid = "---"; $points = "---";
152
153                         // Are you one of them?
154                         if (SQL_NUMROWS($result_users) == 1) {
155                                 // Load data
156                                 list($userid, $points, $mark) = SQL_FETCHROW($result_users);
157
158                                 // Translate comma
159                                 $points = TRANSLATE_COMMA($points);
160                         } // END - if
161
162                         // Output row
163                         $OUT .= "<tr>
164   <td class=\"bonus_rank_".$rank." bottom2 switch_sw".$SW."\">&nbsp;".$rank.".</td>
165   <td class=\"bonus_rank_".$rank." bottom2 switch_sw".$SW."\" align=\"center\">".$userid."</td>
166   <td class=\"bonus_rank_".$rank." bottom2 switch_sw".$SW."\" align=\"center\">".$points."</td>
167 </tr>\n";
168                         $SW = 3 - $SW;
169                 } // END - for
170
171                 if (!$self) {
172                         // If current user was not found set constant
173                         define('__YOUR_RANKING_LINE', BONUS_RANK_YOU_ARE_NOT_FOUND);
174                 } // END - if
175         } else {
176                 // No entries found!
177                 $OUT = "<tr>
178   <td colspan=\"3\" align=\"center\" height=\"30\" class=\"bottom2\">
179     <div class=\"guest_failed\">".BONUS_NO_RANKS_1.$data.BONUS_NO_RANKS_2."</div>
180   </td>
181 </tr>\n";
182                 define('__YOUR_RANKING_LINE', "");
183         }
184         return $OUT;
185 }
186 //
187 function BONUS_POINTS_HANDLER ($MODE) {
188         // Shall we add bonus points?
189         if (getConfig('bonus_active') != "Y") return;
190
191         // Switch to jackpot-mode when no UID is supplied but userid-mode is selected
192         if ((getConfig('bonus_mode') == "UID") && (getConfig('bonus_uid') == "0")) {
193                 // Update database & config
194                 UPDATE_CONFIG('bonus_mode', "JACKPOT");
195         } // END - if
196
197         if ($MODE == "login_bonus") {
198                 // Login bonus detected
199                 $points = getConfig('login_bonus');
200         } else {
201                 // Direct points supplied
202                 $points = $MODE;
203         }
204
205         // Subtract points from...
206         switch (getConfig('bonus_mode'))
207         {
208         case "JACKPOT": // ... jackpot
209                 if ((SUB_JACKPOT($points) == -1) && (getConfig('bonus_uid') > 0)) {
210                         // Check points amount first...
211                         $TOTAL = GET_TOTAL_DATA(getConfig('bonus_uid'), "user_points", "points") - GET_TOTAL_DATA(getConfig('bonus_uid'), "user_data", "used_points");
212                         if ($TOTAL >= $points) {
213                                 // Subtract points from userid's account
214                                 SUB_POINTS("bonus_payout_jackpot", getConfig('bonus_uid'), $points);
215                         } // END - if
216                 } // END - if
217                 break;
218
219         case "UID": // ... userid's account
220                 // Check his amount first
221                 $TOTAL = GET_TOTAL_DATA(getConfig('bonus_uid'), "user_points", "points") - GET_TOTAL_DATA(getConfig('bonus_uid'), "user_data", "used_points");
222                 if ($TOTAL >= $points) {
223                         // Subtract points from userid's account
224                         SUB_POINTS("bonus_payout_uid", getConfig('bonus_uid'), $points);
225                 } else {
226                         // Try to subtract from jackpot
227                         $dummy = SUB_JACKPOT($points);
228                 }
229                 break;
230         }
231 }
232
233 //
234 function BONUS_PURGE_EXPIRED_TURBO_BONUS() {
235         // Remove entries
236         $result = SQL_QUERY("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_bonus_turbo` WHERE timemark < (UNIX_TIMESTAMP() - ".getConfig('bonus_timeout').")", __FUNCTION__, __LINE__);
237         if (SQL_AFFECTEDROWS() > 0) {
238                 // Send out email to admin
239                 SEND_ADMIN_NOTIFICATION(AUTOPURGE_ADMIN_TURBO_SUBJECT, "admin_autopurge_turbo", SQL_AFFECTEDROWS(), "");
240         }
241 }
242 //
243 ?>