]> git.mxchange.org Git - mailer.git/blob - inc/modules/member/what-payout.php
A lot while() conditions rewritten to SQL_FETCHARRAY(), see bug #107, @TODO tags...
[mailer.git] / inc / modules / member / what-payout.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 04/12/2004 *
4  * ================                             Last change: 12/01/2008 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-payout.php                                  *
8  * -------------------------------------------------------------------- *
9  * Short description : Payout requests                                  *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Auszahlungsanfragen                              *
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         // Don't call this directly!
37         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
38         require($INC);
39 } elseif (!IS_MEMBER()) {
40         // Not logged in
41         LOAD_URL("modules.php?module=index");
42 } elseif ((!EXT_IS_ACTIVE("payout")) && (!IS_ADMIN())) {
43         addFatalMessage(__FILE__, __LINE__, getMessage('EXTENSION_PROBLEM_EXT_INACTIVE'), "payout");
44         return;
45 }
46
47 // Add description as navigation point
48 ADD_DESCR("member", __FILE__);
49
50 $result_depths = SQL_QUERY("SELECT level, percents FROM `{!_MYSQL_PREFIX!}_refdepths` ORDER BY level", __FILE__, __LINE__);
51 $TPTS = 0;
52 while ($content = SQL_FETCHARRAY($result_depths)) {
53         // Load referal points
54         $result_points = SQL_QUERY_ESC("SELECT points FROM `{!_MYSQL_PREFIX_user_points!}` WHERE userid=%s AND ref_depth=%d LIMIT 1",
55                 array($GLOBALS['userid'], bigintval($content['level'])), __FILE__, __LINE__);
56
57         // Entry found?
58         if (SQL_NUMROWS($result_points) == 1) {
59                 // Load points
60                 list($points) = SQL_FETCHROW($result_points);
61
62                 // Add them to total
63                 $TPTS += $points;
64         }
65
66         // Free result
67         SQL_FREERESULT($result_points);
68 }
69
70 // Free memory
71 SQL_FREERESULT($result_depths);
72
73 // Get used points
74 $USED = GET_TOTAL_DATA($GLOBALS['userid'], "user_data", "used_points");
75
76 // Translate point into comma
77 $TPTS = TRANSLATE_COMMA($TPTS - $USED);
78
79 // Sanity check...
80 if (empty($TPTS)) $TPTS = "0.00000";
81
82 if (!REQUEST_ISSET_GET(('payout'))) {
83         // Load payout types
84         $result = SQL_QUERY_ESC("SELECT id, type, rate, min_points, allow_url
85 FROM `{!_MYSQL_PREFIX!}_payout_types`
86 WHERE %s >= min_points
87 ORDER BY type", array(REVERT_COMMA($TPTS)), __FILE__, __LINE__);
88         if (SQL_NUMROWS($result) > 0) {
89                 // Free memory
90                 SQL_FREERESULT($result);
91
92                 // Check for his payouts
93                 $result_payouts = SQL_QUERY_ESC("SELECT DISTINCT p.id, p.payout_total, p.target_account, p.target_bank, t.type, p.payout_timestamp, p.status, t.allow_url AS allow, p.target_url AS url, p.link_text AS alt, p.banner_url AS banner
94 FROM `{!_MYSQL_PREFIX!}_user_payouts` AS p
95 LEFT JOIN `{!_MYSQL_PREFIX!}_payout_types` AS t
96 ON p.payout_id = t.id
97 WHERE p.userid = %s
98 ORDER BY p.payout_timestamp DESC",
99                  array($GLOBALS['userid']), __FILE__, __LINE__);
100                 if (SQL_NUMROWS($result_payouts) > 0) {
101                         // List all his requests
102                         $OUT = ""; $SW = 2;
103                         while ($content = SQL_FETCHARRAY($result_payouts)) {
104                                 // Translate status
105                                 $content['status'] = getMessage('PAYOUT_MEMBER_STATUS_'.strtoupper($content['status']).'');
106                                 $content['status'] = "<div class=\"member_failed\">".$content['status']."</div>";
107
108                                 // Nothing entered must be secured in member/what-payputs.php !
109                                 if ($content['allow'] == "Y") {
110                                         // Banner/Textlink views/clicks request
111                                         if (!empty($content['banner'])) {
112                                                 // Banner
113                                                 $content['target_account'] = "<img src=\"".$content['banner']."\" alt=\"".$content['alt']."\" title=\"".$content['alt']."\" border=\"0\" />";
114                                         } else {
115                                                 // Textlink
116                                                 $content['target_account'] = $content['alt'];
117                                         }
118                                         $content['target_bank'] = "<a href=\"".DEREFERER($content['url'])."\" target=\"_blank\">{--CLICK_HERE--}</a>";
119                                 } else {
120                                         // e-currency payout request
121                                         if (empty($content['target_account'])) $content['target_account'] = "---";
122                                         if (empty($content['target_bank']))    $content['target_bank']    = "---";
123                                 }
124
125                                 // Prepare data for the template
126                                 // @TODO Rewritings: acc->target_account,bank->target_bank in templates
127                                 $content = array(
128                                         'sw'     => $SW,
129                                         'acc'    => $content['target_account'],
130                                         'points' => TRANSLATE_COMMA($content['payout_total'])." ".COMPILE_CODE($content['type']),
131                                         'bank'   => $content['target_bank'],
132                                         'stamp'  => MAKE_DATETIME($content['payout_timestamp'], "2"),
133                                         'status' => $content['status']
134                                 );
135
136                                 // Load row template and switch colors
137                                 $OUT .= LOAD_TEMPLATE("member_payout_row", true, $content);
138                                 $SW = 3 - $SW;
139                         }
140
141                         // Remember rows in constant
142                         // @TODO Rewrite this constant
143                         define('__PAYOUT_ROWS', $OUT);
144
145                         // Load template
146                         LOAD_TEMPLATE("member_payout");
147                 }
148
149                 // Free memory
150                 SQL_FREERESULT($result_payouts);
151
152                 // Output payout list
153                 PAYOUT_OUTPUT_PAYOUT_LIST($TPTS);
154         }
155 } else {
156         // Chedk if he can get paid by selected type
157         $result = SQL_QUERY_ESC("SELECT type, rate, min_points, allow_url AS allow FROM `{!_MYSQL_PREFIX!}_payout_types` WHERE id=%s LIMIT 1",
158                 array(bigintval(REQUEST_GET('payout'))), __FILE__, __LINE__);
159
160         if (SQL_NUMROWS($result) == 1) {
161                 // ID is valid so load the data
162                 $content = SQL_FETCHARRAY($result);
163                 SQL_FREERESULT($result);
164
165                 // Calculate maximum value
166                 $max = round($TPTS * $content['rate'] - 0.5);
167
168                 // Calulcate points from submitted amount
169                 $PAYOUT = 0;
170                 if (REQUEST_ISSET_POST(('payout'))) {
171                         $PAYOUT  = bigintval(REQUEST_POST('payout')) / $content['rate'];
172                         $PAY_MAX = $max / $content['rate'];
173                 }
174
175                 // Move variables into constants for templates
176                 // @TODO Rewrite these constants
177                 define('PAYOUT_MAX_VALUE' , $max);
178                 define('PAYOUT_TYPE_VALUE', COMPILE_CODE($content['type']));
179
180                 if (REVERT_COMMA($TPTS) >= $content['min_points']) {
181                         // Ok, he can get be paid
182                         if ((IS_FORM_SENT()) && ($PAYOUT <= $PAY_MAX) && ($PAYOUT >= $content['min_points'])) {
183                                 // Calculate exact value
184                                 // @TODO Rewrite this constant
185                                 define('PAYOUT_POINTS_VALUE', $PAYOUT);
186
187                                 // Subtract points from member's account
188                                 SUB_POINTS("payout", $GLOBALS['userid'], $PAYOUT);
189
190                                 // Add entry to his tranfer history
191                                 if ($content['allow'] == "Y") {
192                                         // Banner/textlink ordered
193                                         SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_user_payouts` (userid, payout_total, payout_id, payout_timestamp, status, target_url, link_text, banner_url)
194 VALUES (%s,%s,%s, UNIX_TIMESTAMP(), 'NEW','%s','%s','%s')",
195  array(
196         $GLOBALS['userid'],
197         bigintval(REQUEST_POST('payout')),
198         bigintval(REQUEST_GET('payout')),
199         REQUEST_POST('turl'),
200         REQUEST_POST('alt'),
201         REQUEST_POST('banner')
202 ), __FILE__, __LINE__);
203
204                                         // Load templates
205                                         $msg_mem = LOAD_EMAIL_TEMPLATE("member_payout_request_banner", array(), $GLOBALS['userid']);
206                                         if (GET_EXT_VERSION("admins") >= "0.4.1") {
207                                                 $adm_tpl = "admin_payout_request_banner";
208                                         } else {
209                                                 $msg_adm = LOAD_EMAIL_TEMPLATE("admin_payout_request_banner", array(), $GLOBALS['userid']);
210                                         }
211                                 } else {
212                                         // e-currency payout requested
213                                         SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_user_payouts` (userid, payout_total, target_account, target_bank, payout_id, payout_timestamp, status, password)
214 VALUES (%s,%s,%s,'%s',%s, UNIX_TIMESTAMP(), 'NEW','%s')",
215  array(
216         $GLOBALS['userid'],
217         bigintval(REQUEST_POST('payout')),
218         bigintval(REQUEST_POST('account')),
219         REQUEST_POST('bank'),
220         bigintval(REQUEST_GET('payout')),
221         REQUEST_POST('pass')
222 ), __FILE__, __LINE__);
223
224                                         // Load templates
225                                         $msg_mem = LOAD_EMAIL_TEMPLATE("member_payout_request", array(), $GLOBALS['userid']);
226                                         $msg_adm = LOAD_EMAIL_TEMPLATE("admin_payout_request", array(), $GLOBALS['userid']);
227                                         $admin_tpl = "";
228                                         if (GET_EXT_VERSION("admins") >= "0.4.1")
229                                         {
230                                                 $admin_tpl = "admin_payout_request";
231                                         }
232                                 }
233
234                                 // Generate task
235                                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_task_system` (assigned_admin, status, task_type, subject, text, task_created, userid)
236 VALUES (0, 'NEW','PAYOUT_REQUEST','[payout:] {--PAYOUT_REQUEST_ADMIN--}','%s', UNIX_TIMESTAMP(), %s)",
237  array(
238         $msg_adm,
239         $GLOBALS['userid']
240 ), __FILE__, __LINE__);
241
242                                 // Send out mails
243                                 SEND_EMAIL($GLOBALS['userid'], getMessage('PAYOUT_REQUEST_MEMBER'), $msg_mem);
244
245                                 // To admin(s)
246                                 SEND_ADMIN_NOTIFICATION(getMessage('PAYOUT_REQUEST_ADMIN'), $admin_tpl, array(), $GLOBALS['userid']);
247
248                                 // Load template and output it
249                                 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('PAYOUT_REQUEST_SENT'));
250                         } elseif ($content['allow'] == "Y") {
251                                 // Generate banner order form
252                                 LOAD_TEMPLATE("member_payout_form_banner");
253                         } else {
254                                 // Generate normal form
255                                 LOAD_TEMPLATE("member_payout_form");
256                         }
257                 } else {
258                         // Not enougth points
259                         LOAD_TEMPLATE("admin_settings_saved", false, getMessage('PAYOUT_POINTS_NOT_ENOUGTH'));
260                 }
261         } else {
262                 // ID is invalid
263                 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('PAYOUT_ID_INVALID'));
264         }
265 }
266
267 //
268 ?>