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