Code cleanup, more usage of API functions
[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`,
63         (`points_amount` + `points_used`) AS `points`,
64         `refid`
65 FROM
66         `{?_MYSQL_PREFIX?}_sponsor_data`
67 WHERE
68         `status` IN('PENDING','UNCONFIRMED') AND
69         `id`=%s
70 LIMIT 1",
71                         array($content['id']), __FILE__, __LINE__);
72
73                         /// Is there an entry?
74                         if (SQL_NUMROWS($result_main) == 1) {
75                                 // Load data
76                                 $content = SQL_FETCHARRAY($result_main);
77
78                                 // Check for open payments and close them
79                                 $result = SQL_QUERY_ESC("SELECT
80         so.admin_id, so.pay_count,
81         UNIX_TIMESTAMP(so.pay_ordered) AS `pay_ordered`,
82         so.pay_status, sp.pay_name, sp.pay_rate, sp.pay_currency
83 FROM
84         `{?_MYSQL_PREFIX?}_sponsor_orders` AS so
85 LEFT JOIN
86         `{?_MYSQL_PREFIX?}_sponsor_paytypes` AS sp
87 ON
88         sp.id=so.payment_id
89 WHERE
90         so.sponsor_id=%s
91 ORDER BY
92         sp.pay_name ASC",
93                                         array($content['id']), __FILE__, __LINE__);
94                                 if (!SQL_HASZERONUMS($result)) {
95                                         // Payment does exist
96                                         while ($content2 = SQL_FETCHARRAY($result)) {
97                                                 // Merge both arrays
98                                                 $content = merge_array($content, $content2);
99
100                                                 // Set default email
101                                                 $content['email'] = '{--SPONSOR_NO_ADMIN--}';
102                                                 if ($content['admin_id'] > 0) {
103                                                         // Load admin's email address for contact
104                                                         $content['email'] = getAdminEmail($content['admin_id']);
105                                                 } // END - if
106
107                                                 // Transfer data to array
108                                                 $data = array(
109                                                         'admin_id' => $content['email'],
110                                                         'order'    => ($content['pay_count'] * $content['pay_rate']) . ' ' . $content['pay_currency'],
111                                                         'stamp'    => generateDateTime($content['pay_ordered'], 2),
112                                                         'pname'    => $content['pay_name'],
113                                                 );
114
115                                                 // Load email template
116                                                 $content['message'] = loadEmailTemplate('sponsor_unlock_sponsor_pay', $data, $content['id']);
117                                         }
118                                 } else {
119                                         // No payments found
120                                         $content['message'] = '{--SPONSOR_NO_PAYMENTS_FOUND--}';
121                                 }
122
123                                 // Free memory
124                                 SQL_FREERESULT($result);
125                         } else {
126                                 // Not found
127                                 loadTemplate('admin_settings_saved', false, getMaskedMessage('ADMIN_SPONSOR_ACCOUNT_404_ALREADY_CONFIRMED', $content['id']));
128                                 return;
129                         }
130
131                         // Free memory
132                         SQL_FREERESULT($result_main);
133
134                         // Unlock sponsor account
135                         SQL_QUERY_ESC("UPDATE
136         `{?_MYSQL_PREFIX?}_sponsor_data`
137 SET
138         `status`='CONFIRMED'
139 WHERE
140         `id`=%s AND
141         `status` IN('PENDING','UNCONFIRMED')
142 LIMIT 1",
143                         array($content['id']), __FILE__, __LINE__);
144
145                         // Update, if applyable, referal count and points
146                         if ((isValidUserId($content['refid'])) && ($content['refid'] != $content['id'])) {
147                                 // Update referal account
148                                 SQL_QUERY_ESC("UPDATE
149         `{?_MYSQL_PREFIX?}_sponsor_data`
150 SET
151         `points_amount`=`points_amount`+{?sponsor_ref_points?}, `ref_count`=`ref_count`+1
152 WHERE
153         `id`=%s
154 LIMIT 1",
155                                         array(bigintval($content['refid'])), __FILE__, __LINE__);
156
157                                 // Whas that update fine?
158                                 if (SQL_AFFECTEDROWS() == 1) {
159                                         // Load referal's data
160                                         $result = SQL_QUERY_ESC("SELECT
161         `id`, `gender`, `surname`, `family`, `email`,
162         (`points_amount` - `points_used`) AS points,
163         `receive_warnings`, `ref_count` AS refs
164 FROM
165         `{?_MYSQL_PREFIX?}_sponsor_data`
166 WHERE
167         `id`=%s
168 LIMIT 1",
169                                         array(bigintval($content['refid'])), __FILE__, __LINE__);
170                                         $REFERAL = SQL_FETCHARRAY($result);
171
172                                         // Send warnings out?
173                                         if ($REFERAL['receive_warnings'] == 'Y') {
174                                                 // Send notification to referal
175                                                 $REFERAL_MSG = loadEmailTemplate('sponsor_ref_notify', $REFERAL);
176                                                 sendEmail($REFERAL['email'], '{--SPONSOR_REFERAL_NOTIFY_SUBJECT--}', $REFERAL_MSG);
177                                         } // END - if
178
179                                         // Free memory
180                                         SQL_FREERESULT($result);
181                                 } // END - if
182                         } // END - if
183
184                         // So let's send the email away
185                         $message = loadEmailTemplate('sponsor_unlocked', $content);
186                         sendEmail($content['email'], '{--SPONSOR_UNLOCKED_SUBJECT--}', $message);
187                 } // END - foreach
188         } else {
189                 // Nothing selected
190                 loadTemplate('admin_settings_saved', false, '{--ADMIN_SPONSOR_NONE_SELECTED_UNLOCK--}');
191         }
192 } // END - if
193
194 // Begin listing of all pending sponsor accounts
195 $result = SQL_QUERY("SELECT
196         `id`, `gender`, `surname`, `family`, `email`, `remote_addr`,
197         UNIX_TIMESTAMP(`sponsor_created`) AS `sponsor_created`
198 FROM
199         `{?_MYSQL_PREFIX?}_sponsor_data`
200 WHERE
201         `status` IN('PENDING','UNCONFIRMED')
202 ORDER BY
203         `id`", __FILE__, __LINE__);
204
205 if (!SQL_HASZERONUMS($result)) {
206         // Entries found so let's list them!
207         $OUT = '';
208         while ($content = SQL_FETCHARRAY($result)) {
209                 // Translate some data
210                 $content['sponsor_created'] = generateDateTime($content['sponsor_created'], 2);
211
212                 // Load row template and switch colors
213                 $OUT .= loadTemplate('admin_unlock_sponsor_row', true, $content);
214         } // END - while
215
216         // Load template
217         loadTemplate('admin_unlock_sponsor', false, $OUT);
218 } else {
219         // No pending accounts found
220         loadTemplate('admin_settings_saved', false, '{--ADMIN_SPONSOR_NONE_PENDING--}');
221 }
222
223 // Free memory
224 SQL_FREERESULT($result);
225
226 // [EOF]
227 ?>