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