adb9b19b47719e6af6b9dd30c990ac42fcfcf4bd
[mailer.git] / inc / modules / admin / what-unlock_sponsor.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 04/23/2005 *
4  * ===================                          Last change: 05/19/2008 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-unlock_sponsor.php                          *
8  * -------------------------------------------------------------------- *
9  * Short description : Unlock sponsor accounts                          *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Sponsorenaccounts freigeben                      *
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, 2010 by Mailer Developer Team                    *
20  * For more information visit: http://www.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')) || (!isAdmin())) {
40         die();
41 } // END - if
42
43 // Add description as navigation point
44 addMenuDescription('admin', __FILE__);
45
46 // Check if admin has submitted form
47 if (isFormSent()) {
48         // Does he have selected at least one sponsor?
49         if (ifPostContainsSelections('id')) {
50                 // At least one entry selected
51                 foreach (postRequestParameter('id') as $id => $selected) {
52                         // Secure id number and init $content
53                         $content = array(
54                                 'id'    => bigintval($id),
55                                 'refid' => 0
56                         );
57
58                         // Load his personal data
59                         $result_main = SQL_QUERY_ESC("SELECT
60         `id`, `gender`, `surname`, `family`, `email`, `remote_addr`,
61         UNIX_TIMESTAMP(`sponsor_created`) AS `sponsor_created`,
62         `points_amount`, `points_used`, `refid`
63 FROM
64         `{?_MYSQL_PREFIX?}_sponsor_data`
65 WHERE
66         `status` IN('PENDING','UNCONFIRMED') AND
67         `id`=%s
68 LIMIT 1",
69                         array($content['id']), __FILE__, __LINE__);
70
71                         /// Is there an entry?
72                         if (SQL_NUMROWS($result_main) == 1) {
73                                 // Load data
74                                 $content = SQL_FETCHARRAY($result_main);
75
76                                 // Add total amount
77                                 $content['points'] = ($content['points_amount'] - $content['points_used']);
78
79                                 // Check for open payments and close them
80                                 $result = SQL_QUERY_ESC("SELECT
81         so.admin_id, so.pay_count,
82         UNIX_TIMESTAMP(so.pay_ordered) AS `pay_ordered`,
83         so.pay_status, sp.pay_name, sp.pay_rate, sp.pay_currency
84 FROM
85         `{?_MYSQL_PREFIX?}_sponsor_orders` AS so
86 LEFT JOIN
87         `{?_MYSQL_PREFIX?}_sponsor_paytypes` AS sp
88 ON
89         sp.id=so.payment_id
90 WHERE
91         so.sponsor_id=%s
92 ORDER BY
93         sp.pay_name ASC",
94                                         array($content['id']), __FILE__, __LINE__);
95                                 if (!SQL_HASZERONUMS($result)) {
96                                         // Payment does exist
97                                         while ($content2 = SQL_FETCHARRAY($result)) {
98                                                 // Merge both arrays
99                                                 $content = merge_array($content, $content2);
100
101                                                 // Set default email
102                                                 $content['email'] = '{--SPONSOR_NO_ADMIN--}';
103                                                 if ($content['admin_id'] > 0) {
104                                                         // Load admin's email address for contact
105                                                         $content['email'] = getAdminEmail($content['admin_id']);
106                                                 } // END - if
107
108                                                 // Transfer data to array
109                                                 $data = array(
110                                                         'admin_id' => $content['email'],
111                                                         'order'    => ($content['pay_count'] * $content['pay_rate']) . ' ' . $content['pay_currency'],
112                                                         'stamp'    => generateDateTime($content['pay_ordered'], 2),
113                                                         'pname'    => $content['pay_name'],
114                                                 );
115
116                                                 // Load email template
117                                                 $content['message'] = loadEmailTemplate('sponsor_unlock_sponsor_pay', $data, $content['id']);
118                                         }
119                                 } else {
120                                         // No payments found
121                                         $content['message'] = '{--SPONSOR_NO_PAYMENTS_FOUND--}';
122                                 }
123
124                                 // Free memory
125                                 SQL_FREERESULT($result);
126                         } else {
127                                 // Not found
128                                 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_SPONSOR_ACCOUNT_404_ALREADY_CONFIRMED', $content['id']));
129                                 return;
130                         }
131
132                         // Free memory
133                         SQL_FREERESULT($result_main);
134
135                         // Unlock sponsor account
136                         SQL_QUERY_ESC("UPDATE
137         `{?_MYSQL_PREFIX?}_sponsor_data`
138 SET
139         `status`='CONFIRMED'
140 WHERE
141         `id`=%s AND
142         `status` IN('PENDING','UNCONFIRMED')
143 LIMIT 1",
144                         array($content['id']), __FILE__, __LINE__);
145
146                         // Update, if applyable, referal count and points
147                         if ((isValidUserId($content['refid'])) && ($content['refid'] != $content['id'])) {
148                                 // Update referal account
149                                 SQL_QUERY_ESC("UPDATE
150         `{?_MYSQL_PREFIX?}_sponsor_data`
151 SET
152         `points_amount`=`points_amount`+{?sponsor_ref_points?}, `ref_count`=`ref_count`+1
153 WHERE
154         `id`=%s
155 LIMIT 1",
156                                         array(bigintval($content['refid'])), __FILE__, __LINE__);
157
158                                 // Whas that update fine?
159                                 if (SQL_AFFECTEDROWS() == 1) {
160                                         // Load referal's data
161                                         $result = SQL_QUERY_ESC("SELECT
162         `id`, `gender`, `surname`, `family`, `email`,
163         (`points_amount` - `points_used`) AS points,
164         `receive_warnings`, `ref_count` AS refs
165 FROM
166         `{?_MYSQL_PREFIX?}_sponsor_data`
167 WHERE
168         `id`=%s
169 LIMIT 1",
170                                         array(bigintval($content['refid'])), __FILE__, __LINE__);
171                                         $REFERAL = SQL_FETCHARRAY($result);
172
173                                         // Send warnings out?
174                                         if ($REFERAL['receive_warnings'] == 'Y') {
175                                                 // Send notification to referal
176                                                 $REFERAL_MSG = loadEmailTemplate('sponsor_ref_notify', $REFERAL);
177                                                 sendEmail($REFERAL['email'], '{--SPONSOR_REFERAL_NOTIFY_SUBJECT--}', $REFERAL_MSG);
178                                         } // END - if
179
180                                         // Free memory
181                                         SQL_FREERESULT($result);
182                                 } // END - if
183                         } // END - if
184
185                         // So let's send the email away
186                         $message = loadEmailTemplate('sponsor_unlocked', $content);
187                         sendEmail($content['email'], '{--SPONSOR_UNLOCKED_SUBJECT--}', $message);
188                 } // END - foreach
189         } else {
190                 // Nothing selected
191                 loadTemplate('admin_settings_saved', false, '{--ADMIN_SPONSOR_NONE_SELECTED_UNLOCK--}');
192         }
193 } // END - if
194
195 // Begin listing of all pending sponsor accounts
196 $result = SQL_QUERY("SELECT
197         `id`, `gender`, `surname`, `family`, `email`, `remote_addr`,
198         UNIX_TIMESTAMP(`sponsor_created`) AS `sponsor_created`
199 FROM
200         `{?_MYSQL_PREFIX?}_sponsor_data`
201 WHERE
202         `status` IN('PENDING','UNCONFIRMED')
203 ORDER BY
204         `id`", __FILE__, __LINE__);
205
206 if (!SQL_HASZERONUMS($result)) {
207         // Entries found so let's list them!
208         $OUT = '';
209         while ($content = SQL_FETCHARRAY($result)) {
210                 // Translate some data
211                 $content['sponsor_created'] = generateDateTime($content['sponsor_created'], 2);
212
213                 // Load row template and switch colors
214                 $OUT .= loadTemplate('admin_unlock_sponsor_row', true, $content);
215         } // END - while
216
217         // Load template
218         loadTemplate('admin_unlock_sponsor', false, $OUT);
219 } else {
220         // No pending accounts found
221         loadTemplate('admin_settings_saved', false, '{--ADMIN_SPONSOR_NONE_PENDING--}');
222 }
223
224 // Free memory
225 SQL_FREERESULT($result);
226
227 // [EOF]
228 ?>