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