e8676e512ac9274e2e097c1e0105d5afc06f7ad8
[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  * Needs to be in all Files and every File needs "svn propset           *
18  * svn:keywords Date Revision" (autoprobset!) at least!!!!!!            *
19  * -------------------------------------------------------------------- *
20  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
21  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if ((!defined('__SECURITY')) || (!isAdmin())) {
42         die();
43 } // END - if
44
45 // Add description as navigation point
46 addMenuDescription('admin', __FILE__);
47
48 // Check if admin has submitted form
49 if (isFormSent()) {
50         // Does he have selected at least one sponsor?
51         if (countSelection(postRequestParameter('id')) > 0) {
52                 // At least one entry selected
53                 foreach (postRequestParameter('id') as $id => $selected) {
54                         // Secure id number and init $content
55                         $content = array(
56                                 'id'    => bigintval($id),
57                                 'refid' => 0
58                         );
59
60                         // Load his personal data
61                         $result_main = SQL_QUERY_ESC("SELECT
62         `gender`, `surname`, `family`, `email`, `remote_addr`, `sponsor_created`, `points_amount`, `refid`
63 FROM
64         `{?_MYSQL_PREFIX?}_sponsor_data`
65 WHERE
66         `status`='PENDING' AND
67         `id`=%s
68 LIMIT 1",
69                         array($content['id']), __FILE__, __LINE__);
70
71                         if (SQL_NUMROWS($result_main) == 1) {
72                                 // Load data
73                                 $content = SQL_FETCHARRAY($result_main);
74
75                                 // Check for open payments and close them
76                                 $result = SQL_QUERY_ESC("SELECT
77         so.admin_id, so.pay_count, so.pay_ordered, so.pay_status,
78         sp.pay_name, sp.pay_rate, sp.pay_currency
79 FROM
80         `{?_MYSQL_PREFIX?}_sponsor_orders` AS so
81 LEFT JOIN
82         `{?_MYSQL_PREFIX?}_sponsor_paytypes` AS sp
83 ON
84         sp.id=so.payid
85 WHERE
86         so.sponsorid='%s'
87 ORDER BY
88         sp.pay_name ASC",
89                                         array($content['id']), __FILE__, __LINE__);
90                                 if (SQL_NUMROWS($result) > 0) {
91                                         // Payment does exist
92                                         while ($content2 = SQL_FETCHARRAY($result)) {
93                                                 // Merge both arrays
94                                                 $content = merge_array($content, $content2);
95
96                                                 // Set default email
97                                                 $content['email'] = getMessage('SPONSOR_NO_ADMIN');
98                                                 if ($content['admin_id'] > 0) {
99                                                         // Load admin's email address for contact
100                                                         $content['email'] = getAdminEmail($content['admin_id']);
101                                                 } // END - if
102
103                                                 // Transfer data to array
104                                                 $data = array(
105                                                         'admin_id' => $content['email'],
106                                                         'order'    => ($content['pay_count'] * $content['pay_rate']) . ' ' . $content['pay_currency'],
107                                                         'stamp'    => generateDateTime($content['pay_ordered'], 2),
108                                                         'pname'    => $content['pay_name'],
109                                                 );
110
111                                                 // Load email template
112                                                 $content['message'] = loadEmailTemplate('sponsor_unlock_sponsor_pay', $data, $content['id']);
113                                         }
114                                 } else {
115                                         // No payments found
116                                         $content['message'] = getMessage('SPONSOR_NO_PAYMENTS_FOUND');
117                                 }
118
119                                 // Free memory
120                                 SQL_FREERESULT($result);
121                         }
122
123                         // Free memory
124                         SQL_FREERESULT($result_main);
125
126                         // Unlock sponsor account
127                         SQL_QUERY_ESC("UPDATE
128         `{?_MYSQL_PREFIX?}_sponsor_data`
129 SET
130         `status`='CONFIRMED'
131 WHERE
132         `id`=%s AND
133         `status`='PENDING'
134 LIMIT 1",
135                         array($content['id']), __FILE__, __LINE__);
136
137                         // Update, if applyable, referal count and points
138                         if ((isValidUserId($content['refid'])) && ($content['refid'] != $content['id'])) {
139                                 // Update referal account
140                                 SQL_QUERY_ESC("UPDATE
141         `{?_MYSQL_PREFIX?}_sponsor_data`
142 SET
143         `points_amount`=`points_amount`+{?sponsor_ref_points?}, `ref_count`=`ref_count`+1
144 WHERE
145         `id`=%s
146 LIMIT 1",
147                                         array(bigintval($content['refid'])), __FILE__, __LINE__);
148
149                                 // Whas that update fine?
150                                 if (SQL_AFFECTEDROWS() == 1) {
151                                         // Load referal's data
152                                         $result = SQL_QUERY_ESC("SELECT
153         `id`, `gender`, `surname`, `family`, `email`,
154         (`points_amount` - `points_used`) AS points,
155         `receive_warnings`, `ref_count` AS refs
156 FROM
157         `{?_MYSQL_PREFIX?}_sponsor_data`
158 WHERE
159         `id`=%s
160 LIMIT 1",
161                                         array(bigintval($content['refid'])), __FILE__, __LINE__);
162                                         $REFERAL = SQL_FETCHARRAY($result);
163
164                                         // Send warnings out?
165                                         if ($REFERAL['receive_warnings'] == 'Y') {
166                                                 // Send notification to referal
167                                                 $REF_MSG = loadEmailTemplate('sponsor_ref_notify', $REFERAL);
168                                                 sendEmail($REFERAL['email'], getMessage('SPONSOR_REF_NOTIFY_SUBJ'), $REF_MSG);
169                                         } // END - if
170
171                                         // Free memory
172                                         SQL_FREERESULT($result);
173                                 } // END - if
174                         } // END - if
175
176                         // Transfer data to array
177                         $content['points']   = translateComma($content['points_amount']);
178
179                         // So let's send the email away
180                         $message = loadEmailTemplate('sponsor_unlocked', $content);
181                         sendEmail($content['email'], getMessage('SPONSOR_UNLOCKED_SUBJ'), $message);
182                 } // END - foreach
183         } else {
184                 // Nothing selected
185                 loadTemplate('admin_settings_saved', false, '{--ADMIN_SPONSOR_NONE_SELECTED_UNLOCK--}');
186         }
187 } // END - if
188
189 // Begin listing of all pending sponsor accounts
190 $result = SQL_QUERY("SELECT
191         `id`, `gender`, `surname`, `family`, `email`, `remote_addr`, `sponsor_created`
192 FROM
193         `{?_MYSQL_PREFIX?}_sponsor_data`
194 WHERE
195         `status`='PENDING'
196 ORDER BY
197         `id`", __FILE__, __LINE__);
198
199 if (SQL_NUMROWS($result) > 0) {
200         // Entries found so let's list them!
201         $OUT = ''; $SW = 2;
202         while ($content = SQL_FETCHARRAY($result)) {
203                 // Transfer data to array
204                 $content = array(
205                         'sw'              => $SW,
206                         'id'              => $content['id'],
207                         'gender'          => $content['gender'],
208                         'surname'         => $content['surname'],
209                         'family'          => $content['family'],
210                         'email'           => generateEmailLink($content['email'], 'sponsor_data'),
211                         'remote_addr'     => $content['remote_addr'],
212                         'sponsor_created' => generateDateTime($content['sponsor_created'], 2),
213                 );
214
215                 // Load row template and switch colors
216                 $OUT .= loadTemplate('admin_unlock_sponsor_row', true, $content);
217                 $SW = 3 - $SW;
218         } // END - while
219
220         // Load template
221         loadTemplate('admin_unlock_sponsor', false, $OUT);
222 } else {
223         // No pending accounts found
224         loadTemplate('admin_settings_saved', false, '{--ADMIN_SPONSOR_NONE_PENDING--}');
225 }
226
227 // Free memory
228 SQL_FREERESULT($result);
229
230 // [EOF]
231 ?>