Even more rewritten
[mailer.git] / inc / modules / admin / what-unlock_sponsor.php
1 <?php
2 /************************************************************************
3  * M-XChange v0.2.1                                   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 - 2008 by Roland Haeder                           *
21  * For more information visit: http://www.mxchange.org                  *
22  *                                                                      *
23  * This program is free software; you can redistribute it and/or modify *
24  * it under the terms of the GNU General Public License as published by *
25  * the Free Software Foundation; either version 2 of the License, or    *
26  * (at your option) any later version.                                  *
27  *                                                                      *
28  * This program is distributed in the hope that it will be useful,      *
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
31  * GNU General Public License for more details.                         *
32  *                                                                      *
33  * You should have received a copy of the GNU General Public License    *
34  * along with this program; if not, write to the Free Software          *
35  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
36  * MA  02110-1301  USA                                                  *
37  ************************************************************************/
38
39 // Some security stuff...
40 if ((!defined('__SECURITY')) || (!IS_ADMIN())) {
41         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4)."/security.php";
42         require($INC);
43 }
44
45 // Add description as navigation point
46 ADD_DESCR('admin', __FILE__);
47
48 // Check if admin has submitted form
49 if (IS_FORM_SENT()) {
50         // Does he have selected at least one sponsor?
51         if (SELECTION_COUNT(REQUEST_POST('id')) > 0) {
52                 // At least one entry selected
53                 foreach (REQUEST_POST('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 gender, surname, family, email, remote_addr, sponsor_created, points_amount, refid
62 FROM `{!_MYSQL_PREFIX!}_sponsor_data`
63 WHERE `status`='PENDING' AND id='%s' LIMIT 1",
64                                 array($content['id']), __FILE__, __LINE__);
65
66                         if (SQL_NUMROWS($result_main) == 1) {
67                                 // Load data
68                                 $content = SQL_FETCHARRAY($result_main);
69
70                                 // Check for open payments and close them
71                                 $result = SQL_QUERY_ESC("SELECT DISTINCT so.aid, so.pay_count, so.pay_ordered, so.pay_status,
72 sp.pay_name, sp.pay_rate, sp.pay_currency
73 FROM `{!_MYSQL_PREFIX!}_sponsor_orders` AS so
74 LEFT JOIN `{!_MYSQL_PREFIX!}_sponsor_paytypes` AS sp
75 ON sp.id=so.payid
76 WHERE so.sponsorid='%s'
77 ORDER BY sp.pay_name",
78                                         array($content['id']), __FILE__, __LINE__);
79                                 if (SQL_NUMROWS($result) > 0) {
80                                         // Payment does exist
81                                         while ($content2 = SQL_FETCHARRAY($result)) {
82                                                 // Merge both arrays
83                                                 $content = merge_array($content, $content2);
84
85                                                 // Set default email
86                                                 $content['email'] = getMessage('SPONSOR_NO_ADMIN');
87                                                 if ($content['aid'] > "0") {
88                                                         // Load admin's email address for contact
89                                                         $content['email'] = GET_ADMIN_EMAIL($content['aid']);
90                                                 } // END - if
91
92                                                 // Transfer data to array
93                                                 $data = array(
94                                                         'aid'   => $content['email'],
95                                                         'order' => ($content['pay_count'] * $content['pay_rate'])." ".$content['pay_currency'],
96                                                         'stamp' => MAKE_DATETIME($content['pay_ordered'], "2"),
97                                                         'pname' => $content['pay_name'],
98                                                 );
99         
100                                                 // Load email template
101                                                 $content['msg'] = LOAD_EMAIL_TEMPLATE("sponsor_unlock_sponsor_pay", $data, $content['id']);
102                                         }
103                                 } else {
104                                         // No payments found
105                                         $content['msg'] = getMessage('SPONSOR_NO_PAYMENTS_FOUND');
106                                 }
107
108                                 // Free memory
109                                 SQL_FREERESULT($result);
110                         }
111
112                         // Free memory
113                         SQL_FREERESULT($result_main);
114
115                         // Unlock sponsor account
116                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_sponsor_data` SET `status`='CONFIRMED'
117 WHERE id='%s' AND `status`='PENDING' LIMIT 1",
118                                 array($content['id']), __FILE__, __LINE__);
119
120                         // Update, if applyable, referal count and points
121                         if (($content['refid'] > 0) && ($content['refid'] != $content['id'])) {
122                                 // Update referal account
123                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_sponsor_data`
124 SET points_amount=points_amount+%s, ref_count=ref_count+1
125 WHERE id='%s' LIMIT 1",
126                                         array(getConfig('sponsor_ref_points'), bigintval($content['refid'])), __FILE__, __LINE__);
127
128                                 // Whas that update fine?
129                                 if (SQL_AFFECTEDROWS() == 1) {
130                                         // Load referal's data
131                                         $result = SQL_QUERY_ESC("SELECT id, gender, surname, family, email,
132 (points_amount - points_used) AS points, receive_warnings, ref_count AS refs
133 FROM `{!_MYSQL_PREFIX!}_sponsor_data`
134 WHERE id='%s' LIMIT 1",
135                                                 array(bigintval($content['refid'])), __FILE__, __LINE__);
136                                         $REFERRAL = SQL_FETCHARRAY($result);
137
138                                         // Send warnings out?
139                                         if ($REFERRAL['receive_warnings'] == 'Y') {
140                                                 // Translate some data
141                                                 $REFERRAL['points']     = TRANSLATE_COMMA($REFERRAL['points']);
142                                                 $REFERRAL['ref_points'] = TRANSLATE_COMMA(getConfig('sponsor_ref_points'));
143                                                 $REFERRAL['gender']     = TRANSLATE_GENDER($REFERRAL['gender']);
144
145                                                 // Send notification to referal
146                                                 $REF_MSG = LOAD_EMAIL_TEMPLATE("sponsor_ref_notify", $REFERRAL);
147                                                 SEND_EMAIL($REFERRAL['email'], getMessage('SPONSOR_REF_NOTIFY_SUBJ'), $REF_MSG);
148                                         }
149
150                                         // Free memory
151                                         SQL_FREERESULT($result);
152                                 }
153                         }
154
155                         // Transfer data to array
156                         $content['gender']   = TRANSLATE_GENDER($content['gender']);
157                         $content['sponsor']  = $content['id'];
158                         $content['points']   = TRANSLATE_COMMA($content['points_amount']);
159
160                         // So let's send the email away
161                         $msg = LOAD_EMAIL_TEMPLATE("sponsor_unlocked", $content);
162                         SEND_EMAIL($content['email'], getMessage('SPONSOR_UNLOCKED_SUBJ'), $msg);
163                 }
164         } else {
165                 // Nothing selected
166                 LOAD_TEMPLATE('admin_settings_saved', false, getMessage('ADMIN_SPONSOR_NONE_SELECTED_UNLOCK'));
167         }
168
169         // Add seperator
170         OUTPUT_HTML("<br />");
171 }
172
173 // Begin listing of all pending sponsor accounts
174 $result = SQL_QUERY("SELECT
175         id, gender, surname, family, email, remote_addr, sponsor_created
176 FROM
177         `{!_MYSQL_PREFIX!}_sponsor_data`
178 WHERE
179         `status`='PENDING'
180 ORDER BY
181         `id`", __FILE__, __LINE__);
182
183 if (SQL_NUMROWS($result) > 0) {
184         // Entries found so let's list them!
185         $OUT = ''; $SW = 2;
186         while ($content = SQL_FETCHARRAY($result)) {
187                 // Transfer data to array
188                 // @TODO Rewritings: remote->remote_addr, created->sponsor_created in template
189                 $content = array(
190                         'sw'      => $SW,
191                         'id'      => $content['id'],
192                         'gender'   => TRANSLATE_GENDER($content['gender']),
193                         'surname' => $content['surname'],
194                         'family'  => $content['family'],
195                         'email'   => "mailto:".$content['email'],
196                         'remote'  => $content['remote_addr'],
197                         'created' => MAKE_DATETIME($content['sponsor_created'], "2"),
198                 );
199
200                 // Load row template and switch colors
201                 $OUT .= LOAD_TEMPLATE("admin_unlock_sponsor_row", true, $content);
202                 $SW = 3 - $SW;
203         }
204         define('__SPONSOR_ROWS', $OUT);
205
206         // Load template
207         LOAD_TEMPLATE("admin_unlock_sponsor");
208 } else {
209         // No pending accounts found
210         LOAD_TEMPLATE('admin_settings_saved', false, getMessage('ADMIN_SPONSOR_NONE_PENDING'));
211 }
212
213 // Free memory
214 SQL_FREERESULT($result);
215
216 //
217 ?>