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