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