Next wave of lesser getMessage()
[mailer.git] / inc / modules / member / what-payout.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                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  * $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  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if (!defined('__SECURITY')) {
42         // Don't call this directly!
43         die();
44 } elseif (!isMember()) {
45         // Not logged in
46         redirectToIndexMemberOnlyModule();
47 }
48
49 // Add description as navigation point
50 addMenuDescription('member', __FILE__);
51
52 if ((!isExtensionActive('payout')) && (!isAdmin())) {
53         loadTemplate('admin_settings_saved', false, generateExtensionInactiveNotInstalledMessage('payout'));
54         return;
55 } // END - if
56
57 // Get total points
58 $totalPoints = countSumTotalData(getMemberId(), 'user_points', 'points');
59
60 // Get used points
61 $usedPoints = countSumTotalData(getMemberId(), 'user_data', 'used_points');
62
63 // Translate point into comma
64 $totalPoints = ($totalPoints - $usedPoints);
65
66 // Sanity check...
67 if (empty($totalPoints)) $totalPoints = '0.00000';
68
69 if (!isGetRequestParameterSet('payout')) {
70         // Load payout types
71         $result = SQL_QUERY_ESC("SELECT
72         `id`, `type`, `rate`, `min_points`, `allow_url`
73 FROM
74         `{?_MYSQL_PREFIX?}_payout_types`
75 WHERE
76         %s >= `min_points`
77 ORDER BY
78         `type` ASC",
79                 array($totalPoints), __FILE__, __LINE__);
80         if (SQL_NUMROWS($result) > 0) {
81                 // Free memory
82                 SQL_FREERESULT($result);
83
84                 // Check for his payouts
85                 $result_payouts = SQL_QUERY_ESC("SELECT
86         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
87 FROM
88         `{?_MYSQL_PREFIX?}_user_payouts` AS p
89 LEFT JOIN
90         `{?_MYSQL_PREFIX?}_payout_types` AS t
91 ON
92         p.payout_id = t.id
93 WHERE
94         p.userid = %s
95 ORDER BY
96         p.payout_timestamp DESC",
97                         array(getMemberId()), __FILE__, __LINE__);
98                 if (SQL_NUMROWS($result_payouts) > 0) {
99                         // List all his requests
100                         $OUT = ''; $SW = 2;
101                         while ($content = SQL_FETCHARRAY($result_payouts)) {
102                                 // Translate status
103                                 $content['status'] = translatePayoutStatus();
104                                 $content['status'] = '<div class="member_failed">' . $content['status'] . '</div>';
105
106                                 // Nothing entered must be secured in member/what-payputs.php !
107                                 if ($content['allow'] == 'Y') {
108                                         // Banner/Textlink views/clicks request
109                                         if (!empty($content['banner'])) {
110                                                 // Banner
111                                                 $content['target_account'] = '<img src="' . $content['banner'] . '" alt="' . $content['alt'] . '" title="' . $content['alt'] . '" border="0" />';
112                                         } else {
113                                                 // Textlink
114                                                 $content['target_account'] = $content['alt'];
115                                         }
116                                         $content['target_bank'] = '<a href="' . generateDerefererUrl($content['url']) . '" target="_blank">{--CLICK_HERE--}</a>';
117                                 } else {
118                                         // e-currency payout request
119                                         if (empty($content['target_account'])) $content['target_account'] = '---';
120                                         if (empty($content['target_bank']))    $content['target_bank']    = '---';
121                                 }
122
123                                 // Prepare data for the template
124                                 $content = array(
125                                         'sw'               => $SW,
126                                         'target_account'   => $content['target_account'],
127                                         'payout_total'     => $content['payout_total'],
128                                         'target_bank'      => $content['target_bank'],
129                                         'payout_timestamp' => generateDateTime($content['payout_timestamp'], 2),
130                                         'status'           => $content['status'],
131                                         'type'             => $content['type'],
132                                 );
133
134                                 // Load row template and switch colors
135                                 $OUT .= loadTemplate('member_payout_row', true, $content);
136                                 $SW = 3 - $SW;
137                         }
138
139                         // Load template
140                         loadTemplate('member_payout', false, $OUT);
141                 }
142
143                 // Free memory
144                 SQL_FREERESULT($result_payouts);
145
146                 // Output payout list
147                 outputPayoutList($totalPoints);
148         } else {
149                 // No payout types setup
150                 loadTemplate('admin_settings_saved', false, '{--MEMBER_PAYOUT_SETUP_INCOMPLETE--}');
151         }
152 } else {
153         // Chedk if he can get paid by selected type
154         $result = SQL_QUERY_ESC("SELECT type, rate, min_points, allow_url AS allow FROM `{?_MYSQL_PREFIX?}_payout_types` WHERE `id`=%s LIMIT 1",
155                 array(bigintval(getRequestParameter('payout'))), __FILE__, __LINE__);
156
157         if (SQL_NUMROWS($result) == 1) {
158                 // id is valid so load the data
159                 $content = SQL_FETCHARRAY($result);
160
161                 // Calculate maximum value
162                 $max     = round($totalPoints * $content['rate'] - 0.5);
163                 $PAY_MAX = '0';
164
165                 // Calulcate points from submitted amount
166                 $points = '0';
167                 if (isPostRequestParameterSet('payout')) {
168                         $points  = bigintval(postRequestParameter('payout')) / $content['rate'];
169                         $PAY_MAX = $max / $content['rate'];
170                 }
171
172                 // Has enougth points to payout?
173                 if ($totalPoints >= $content['min_points']) {
174                         // Ok, he can get be paid
175                         if ((isFormSent()) && ($points <= $PAY_MAX) && ($points >= $content['min_points'])) {
176                                 // Remember points in array
177                                 setPostRequestParameter('payout_points', translateComma($points));
178                                 setPostRequestParameter('type'         , $content['type']);
179
180                                 // Subtract points from member's account
181                                 subtractPoints('payout', getMemberId(), $points);
182
183                                 // Add entry to his tranfer history
184                                 if ($content['allow'] == 'Y') {
185                                         // Banner/textlink ordered
186                                         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_payouts` (`userid`, `payout_total`, `payout_id`, `payout_timestamp`, `status`, `target_url`, `link_text`, `banner_url`)
187 VALUES (%s,%s,%s, UNIX_TIMESTAMP(), 'NEW','%s','%s','%s')",
188                                         array(
189                                                 getMemberId(),
190                                                 bigintval(postRequestParameter('payout')),
191                                                 bigintval(getRequestParameter('payout')),
192                                                 postRequestParameter('turl'),
193                                                 postRequestParameter('alt'),
194                                                 postRequestParameter('banner')
195                                         ), __FILE__, __LINE__);
196
197                                         // Load templates
198                                         $message_mem = loadEmailTemplate('member_payout_request_banner', postRequestArray(), getMemberId());
199                                         if (isExtensionInstalledAndNewer('admins', '0.4.1')) {
200                                                 $adm_tpl = 'admin_payout_request_banner';
201                                         } else {
202                                                 $message_adm = loadEmailTemplate('admin_payout_request_banner', postRequestArray(), getMemberId());
203                                         }
204                                 } else {
205                                         // e-currency payout requested
206                                         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_payouts` (`userid`, `payout_total`, `target_account`, `target_bank`, `payout_id`, `payout_timestamp`, `status`, `password`)
207 VALUES (%s,%s,%s,'%s',%s, UNIX_TIMESTAMP(), 'NEW','%s')",
208                                         array(
209                                                 getMemberId(),
210                                                 bigintval(postRequestParameter('payout')),
211                                                 bigintval(postRequestParameter('account')),
212                                                 postRequestParameter('bank'),
213                                                 bigintval(getRequestParameter('payout')),
214                                                 postRequestParameter('pass')
215                                         ), __FILE__, __LINE__);
216
217                                         // Load templates
218                                         $message_mem = loadEmailTemplate('member_payout_request', postRequestArray(), getMemberId());
219                                         $message_adm = loadEmailTemplate('admin_payout_request', postRequestArray(), getMemberId());
220                                         $admin_tpl = '';
221
222                                         // @TODO Rewrite this to a filter
223                                         if (isExtensionInstalledAndNewer('admins', '0.4.1')) {
224                                                 $admin_tpl = 'admin_payout_request';
225                                         } // END - if
226                                 }
227
228                                 // Generate task
229                                 createNewTask('[payout:] {--ADMIN_PAYOUY_REQUEST_SUBJ--}', $message_adm, 'PAYOUT_REQUEST', getMemberId());
230
231                                 // Send out mails
232                                 sendEmail(getMemberId(), '{--MEMBER_PAYOUT_REQUEST_SUBJ--}', $message_mem);
233
234                                 // To admin(s)
235                                 sendAdminNotification(getMessage('ADMIN_PAYOUY_REQUEST_SUBJ'), $admin_tpl, postRequestArray(), getMemberId());
236
237                                 // Load template and output it
238                                 loadTemplate('admin_settings_saved', false, '{--MEMBER_PAYOUT_REQUEST_SENT--}');
239                         } elseif ($content['allow'] == 'Y') {
240                                 // Prepare content
241                                 $content = array(
242                                         'max'    => $max,
243                                         'type'   => $content['type'],
244                                         'payout' => bigintval(getRequestParameter('payout'))
245                                 );
246
247                                 // Generate banner order form
248                                 loadTemplate('member_payout_form_banner', false, $content);
249                         } else {
250                                 // Prepare content
251                                 $content = array(
252                                         'max'    => $max,
253                                         'type'   => $content['type'],
254                                         'payout' => bigintval(getRequestParameter('payout'))
255                                 );
256
257                                 // Generate normal form
258                                 loadTemplate('member_payout_form', false, $content);
259                         }
260                 } else {
261                         // Not enougth points
262                         loadTemplate('admin_settings_saved', false, '{--MEMBER_PAYOUT_POINTS_NOT_ENOUGTH--}');
263                 }
264         } else {
265                 // id is invalid
266                 loadTemplate('admin_settings_saved', false, '{--MEMBER_PAYOUT_ID_INVALID--}');
267         }
268
269         // Free result
270         SQL_FREERESULT($result);
271 }
272
273 // [EOF]
274 ?>