New wrapper function introduced, more EL rewrites:
[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 // Translate point into comma
58 $totalPoints = getTotalPoints(getMemberId());
59
60 if (!isGetRequestParameterSet('payout')) {
61         // Load payout types
62         $result = SQL_QUERY_ESC("SELECT
63         `id`, `type`, `rate`, `min_points`, `allow_url`
64 FROM
65         `{?_MYSQL_PREFIX?}_payout_types`
66 WHERE
67         %s >= `min_points`
68 ORDER BY
69         `type` ASC",
70                 array($totalPoints), __FILE__, __LINE__);
71         if (!SQL_HASZERONUMS($result)) {
72                 // Free memory
73                 SQL_FREERESULT($result);
74
75                 // Check for his payouts
76                 $result_payouts = SQL_QUERY_ESC("SELECT
77         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
78 FROM
79         `{?_MYSQL_PREFIX?}_user_payouts` AS p
80 LEFT JOIN
81         `{?_MYSQL_PREFIX?}_payout_types` AS t
82 ON
83         p.payout_id = t.id
84 WHERE
85         p.userid = %s
86 ORDER BY
87         p.payout_timestamp DESC",
88                         array(getMemberId()), __FILE__, __LINE__);
89                 if (!SQL_HASZERONUMS($result_payouts)) {
90                         // List all his requests
91                         $OUT = '';
92                         while ($content = SQL_FETCHARRAY($result_payouts)) {
93                                 // Translate status
94                                 $content['status'] = translatePayoutStatus();
95                                 $content['status'] = '<div class="member_failed">' . $content['status'] . '</div>';
96
97                                 // Nothing entered must be secured in member/what-payputs.php !
98                                 if ($content['allow'] == 'Y') {
99                                         // Banner/Textlink views/clicks request
100                                         if (!empty($content['banner'])) {
101                                                 // Banner
102                                                 $content['target_account'] = '<img src="' . $content['banner'] . '" alt="' . $content['alt'] . '" title="' . $content['alt'] . '" border="0" />';
103                                         } else {
104                                                 // Textlink
105                                                 $content['target_account'] = $content['alt'];
106                                         }
107                                         $content['target_bank'] = '<a href="' . generateDerefererUrl($content['url']) . '" target="_blank">{--CLICK_HERE--}</a>';
108                                 } // END - if
109
110                                 // Prepare data for the template
111                                 $content = array(
112                                         'target_account'   => $content['target_account'],
113                                         'payout_total'     => $content['payout_total'],
114                                         'target_bank'      => $content['target_bank'],
115                                         'payout_timestamp' => generateDateTime($content['payout_timestamp'], 2),
116                                         'status'           => $content['status'],
117                                         'type'             => $content['type'],
118                                 );
119
120                                 // Load row template and switch colors
121                                 $OUT .= loadTemplate('member_payout_row', true, $content);
122                         } // END - while
123
124                         // Load template
125                         loadTemplate('member_payout', false, $OUT);
126                 } // END - if
127
128                 // Free memory
129                 SQL_FREERESULT($result_payouts);
130
131                 // Output payout list
132                 outputPayoutList($totalPoints);
133         } else {
134                 // No payout types setup
135                 loadTemplate('admin_settings_saved', false, '{--MEMBER_PAYOUT_SETUP_INCOMPLETE--}');
136         }
137 } else {
138         // Chedk if he can get paid by selected type
139         $result = SQL_QUERY_ESC("SELECT `type`, `rate`, `min_points`, `allow_url` AS allow FROM `{?_MYSQL_PREFIX?}_payout_types` WHERE `id`=%s LIMIT 1",
140                 array(bigintval(getRequestParameter('payout'))), __FILE__, __LINE__);
141
142         if (SQL_NUMROWS($result) == 1) {
143                 // id is valid so load the data
144                 $content = SQL_FETCHARRAY($result);
145
146                 // Calculate maximum value
147                 $max     = round($totalPoints * $content['rate'] - 0.5);
148                 $PAY_MAX = '0';
149
150                 // Calulcate points from submitted amount
151                 $points = '0';
152                 if (isPostRequestParameterSet('payout')) {
153                         $points  = bigintval(postRequestParameter('payout')) / $content['rate'];
154                         $PAY_MAX = $max / $content['rate'];
155                 }
156
157                 // Has enougth points to payout?
158                 if ($totalPoints >= $content['min_points']) {
159                         // Ok, he can get be paid
160                         if ((isFormSent()) && ($points <= $PAY_MAX) && ($points >= $content['min_points'])) {
161                                 // Remember points in array
162                                 setPostRequestParameter('payout_points', translateComma($points));
163                                 setPostRequestParameter('type'         , $content['type']);
164
165                                 // Subtract points from member's account
166                                 subtractPoints('payout', getMemberId(), $points);
167
168                                 // Add entry to his tranfer history
169                                 if ($content['allow'] == 'Y') {
170                                         // Banner/textlink ordered
171                                         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_payouts` (`userid`, `payout_total`, `payout_id`, `payout_timestamp`, `status`, `target_url`, `link_text`, `banner_url`)
172 VALUES (%s,%s,%s, UNIX_TIMESTAMP(), 'NEW','%s','%s','%s')",
173                                         array(
174                                                 getMemberId(),
175                                                 bigintval(postRequestParameter('payout')),
176                                                 bigintval(getRequestParameter('payout')),
177                                                 postRequestParameter('turl'),
178                                                 postRequestParameter('alt'),
179                                                 postRequestParameter('banner')
180                                         ), __FILE__, __LINE__);
181
182                                         // Load templates
183                                         $message_mem = loadEmailTemplate('member_payout_request_banner', postRequestArray(), getMemberId());
184                                         if (isExtensionInstalledAndNewer('admins', '0.4.1')) {
185                                                 $adm_tpl = 'admin_payout_request_banner';
186                                         } else {
187                                                 $message_adm = loadEmailTemplate('admin_payout_request_banner', postRequestArray(), getMemberId());
188                                         }
189                                 } else {
190                                         // e-currency payout requested
191                                         SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_user_payouts` (`userid`, `payout_total`, `target_account`, `target_bank`, `payout_id`, `payout_timestamp`, `status`, `password`)
192 VALUES (%s,%s,%s,'%s',%s, UNIX_TIMESTAMP(), 'NEW','%s')",
193                                         array(
194                                                 getMemberId(),
195                                                 bigintval(postRequestParameter('payout')),
196                                                 bigintval(postRequestParameter('account')),
197                                                 postRequestParameter('bank'),
198                                                 bigintval(getRequestParameter('payout')),
199                                                 postRequestParameter('pass')
200                                         ), __FILE__, __LINE__);
201
202                                         // Load templates
203                                         $message_mem = loadEmailTemplate('member_payout_request', postRequestArray(), getMemberId());
204                                         $message_adm = loadEmailTemplate('admin_payout_request', postRequestArray(), getMemberId());
205                                         $admin_tpl = '';
206
207                                         // @TODO Rewrite this to a filter
208                                         if (isExtensionInstalledAndNewer('admins', '0.4.1')) {
209                                                 $admin_tpl = 'admin_payout_request';
210                                         } // END - if
211                                 }
212
213                                 // Generate task (we ignore the task id here)
214                                 createNewTask('[payout:] {--ADMIN_PAYOUY_REQUEST_SUBJECT--}', $message_adm, 'PAYOUT_REQUEST', getMemberId());
215
216                                 // Send out mails
217                                 sendEmail(getMemberId(), '{--MEMBER_PAYOUT_REQUEST_SUBJECT--}', $message_mem);
218
219                                 // To admin(s)
220                                 sendAdminNotification('{--ADMIN_PAYOUY_REQUEST_SUBJECT--}', $admin_tpl, postRequestArray(), getMemberId());
221
222                                 // Load template and output it
223                                 loadTemplate('admin_settings_saved', false, '{--MEMBER_PAYOUT_REQUEST_SENT--}');
224                         } elseif ($content['allow'] == 'Y') {
225                                 // Prepare content
226                                 $content = array(
227                                         'max'    => $max,
228                                         'type'   => $content['type'],
229                                         'payout' => bigintval(getRequestParameter('payout'))
230                                 );
231
232                                 // Generate banner order form
233                                 loadTemplate('member_payout_form_banner', false, $content);
234                         } else {
235                                 // Prepare content
236                                 $content = array(
237                                         'max'    => $max,
238                                         'type'   => $content['type'],
239                                         'payout' => bigintval(getRequestParameter('payout'))
240                                 );
241
242                                 // Generate normal form
243                                 loadTemplate('member_payout_form', false, $content);
244                         }
245                 } else {
246                         // Not enougth points
247                         loadTemplate('admin_settings_saved', false, '{--MEMBER_PAYOUT_POINTS_NOT_ENOUGTH--}');
248                 }
249         } else {
250                 // id is invalid
251                 loadTemplate('admin_settings_saved', false, '{--MEMBER_PAYOUT_ID_INVALID--}');
252         }
253
254         // Free result
255         SQL_FREERESULT($result);
256 }
257
258 // [EOF]
259 ?>