d8018db2d332e2acf86cf9dd0906daf4e42dd045
[mailer.git] / inc / modules / admin / what-unlock_sponsor.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 04/23/2005 *
4  * ===============                              Last change: 05/18/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.       *
21  *                                                                      *
22  * This program is distributed in the hope that it will be useful,      *
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
25  * GNU General Public License for more details.                         *
26  *                                                                      *
27  * You should have received a copy of the GNU General Public License    *
28  * along with this program; if not, write to the Free Software          *
29  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
30  * MA  02110-1301  USA                                                  *
31  ************************************************************************/
32
33 // Some security stuff...
34 if ((ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) || (!is_admin()))
35 {
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", basename(__FILE__));
42
43 // Check if admin has submitted form
44 if (isset($HTTP_POST_VARS['ok']))
45 {
46         // Does he have selected at least one sponsor?
47         if (SELECTION_COUNT($HTTP_POST_VARS['id']) > 0)
48         {
49                 // At least one entry selected
50                 foreach ($HTTP_POST_VARS['id'] as $id=>$sel)
51                 {
52                         // Load his personal data
53                         $result_main = SQL_QUERY_ESC("SELECT salut, 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(bigintval($id)), __FILE__, __LINE__);
57                         $refid = 0;
58                         if (SQL_NUMROWS($result_main) == 1)
59                         {
60                                 // Load data and free memory
61                                 list($salut, $sname, $fname, $email, $ip, $created, $points, $refid) = SQL_FETCHROW($result_main);
62
63                                 // Check for open payments and close them
64                                 $result = SQL_QUERY_ESC("SELECT DISTINCT so.aid, so.pay_count, so.pay_ordered, so.pay_status,
65 sp.pay_name, sp.pay_rate, sp.pay_currency
66 FROM "._MYSQL_PREFIX."_sponsor_orders AS so
67 LEFT JOIN "._MYSQL_PREFIX."_sponsor_paytypes AS sp
68 ON sp.id=so.payid
69 WHERE so.sponsorid='%s'
70 ORDER BY sp.pay_name",
71  array(bigintval($id)), __FILE__, __LINE__);
72                                 if (SQL_NUMROWS($result) > 0)
73                                 {
74                                         // Payment does exist
75                                         while(list($aid, $count, $ordered, $status, $pname, $prate, $pcurr) = SQL_FETCHROW($result))
76                                         {
77                                                 if ($aid == "0")
78                                                 {
79                                                         // No admin assigned!
80                                                         $aid = SPONSOR_NO_ADMIN;
81                                                 }
82                                                  else
83                                                 {
84                                                         // Load admin's email address for contact
85                                                         $result_aid = SQL_QUERY_ESC("SELECT email FROM "._MYSQL_PREFIX."_admins WHERE id='%s' LIMIT 1",
86                                                          array(bigintval($aid)), __FILE__, __LINE__);
87                                                         list($aid) = SQL_FETCHROW($result_aid);
88                                                         SQL_FREERESULT($result_aid);
89
90                                                         // Check if admin login is still in db
91                                                         if (empty($aid)) $aid = ADMIN_NOT_FOUND;
92                                                 }
93
94                                                 // Transfer data to array
95                                                 $content = array(
96                                                         'aid'   => $aid,
97                                                         'order' => ($count * $prate)." ".$pcurr,
98                                                         'stamp' => MAKE_DATETIME($ordered, "2"),
99                                                         'pname' => $pname,
100                                                 );
101         
102                                                 // Load email template
103                                                 $content['msg'] = LOAD_EMAIL_TEMPLATE("sponsor_unlock_sponsor_pay", $content, $id);
104                                         }
105                                 }
106                                          else
107                                 {
108                                         // No payments found
109                                         $content['msg'] = SPONSOR_NO_PAYMENTS_FOUND;
110                                 }
111
112                                 // Free memory
113                                 SQL_FREERESULT($result);
114                         }
115
116                         // Free memory
117                         SQL_FREERESULT($result_main);
118
119                         // Unlock sponsor account
120                         $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_sponsor_data SET status='CONFIRMED'
121 WHERE id='%s' AND status='PENDING' LIMIT 1",
122  array(bigintval($id)), __FILE__, __LINE__);
123
124                         // Update, if applyable, referral count and points
125                         if (($refid > 0) && ($refid != $id))
126                         {
127                                 // Update referral account
128                                 $result = SQL_QUERY_ESC("UPDATE "._MYSQL_PREFIX."_sponsor_data
129 SET points_amount=points_amount+%s, ref_count=ref_count+1
130 WHERE id='%s' LIMIT 1",
131  array($CONFIG['sponsor_ref_points'], bigintval($refid)), __FILE__, __LINE__);
132
133                                 // Load referral's data
134                                 if (SQL_AFFECTED_ROWS($link) == 1)
135                                 {
136                                         $result = SQL_QUERY_ESC("SELECT id, salut, surname, family, email,
137 (points_amount - points_used) AS points, receive_warnings, ref_count AS refs
138 FROM "._MYSQL_PREFIX."_sponsor_data
139 WHERE id='%s' LIMIT 1",
140  array(bigintval($refid)), __FILE__, __LINE__);
141                                         $REFERRAL = SQL_FETCHARRAY($result);
142                                         if ($REFERRAL['receive_warnings'] == "Y")
143                                         {
144                                                 // Translate some data
145                                                 $REFERRAL['points']     = TRANSLATE_COMMA($REFERRAL['points']);
146                                                 $REFERRAL['ref_points'] = TRANSLATE_COMMA($CONFIG['sponsor_ref_points']);
147                                                 $REFERRAL['salut']      = TRANSLATE_SEX($REFERRAL['salut']);
148
149                                                 // Send notification to referral
150                                                 $REF_MSG = LOAD_EMAIL_TEMPLATE("sponsor_ref_notify", $REFERRAL);
151                                                 SEND_EMAIL($REFERRAL['email'], SPONSOR_REF_NOTIFY_SUBJ, $REF_MSG);
152                                         }
153
154                                         // Free memory
155                                         SQL_FREERESULT($result);
156                                 }
157                         }
158
159                         // Transfer data to array
160                         $content['salut']   = TRANSLATE_SEX($salut);
161                         $content['surname'] = $sname;
162                         $content['family']  = $fname;
163                         $content['sponsor'] = $id;
164                         $content['points']  = TRANSLATE_COMMA($points);
165
166                         // So let's send the email away
167                         $msg = LOAD_EMAIL_TEMPLATE("sponsor_unlocked", $content);
168                         SEND_EMAIL($email, SPONSOR_UNLOCKED_SUBJ, $msg);
169                 }
170         }
171          else
172         {
173                 // Nothing selected
174                 LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_SPONSOR_NONE_SELECTED_UNLOCK);
175         }
176
177         // Add seperator
178         OUTPUT_HTML("<BR>");
179 }
180
181 // Begin listing of all pending sponsor accounts
182 $result = SQL_QUERY("SELECT id, salut, surname, family, email, remote_addr, sponsor_created
183 FROM "._MYSQL_PREFIX."_sponsor_data
184 WHERE status='PENDING' ORDER BY id", __FILE__, __LINE__);
185
186 if (SQL_NUMROWS($result) > 0)
187 {
188         // Entries found so let's list them!
189         $OUT = ""; $SW = 2;
190         while(list($id, $salut, $sname, $fname, $email, $ip, $created) = SQL_FETCHROW($result))
191         {
192                 // Transfer data to array
193                 $content = array(
194                         'sw'      => $SW,
195                         'id'      => $id,
196                         'salut'   => TRANSLATE_SEX($salut),
197                         'surname' => $sname,
198                         'family'  => $fname,
199                         'email'   => "mailto:".$email,
200                         'remote'  => $ip,
201                         'created' => MAKE_DATETIME($created, "2"),
202                 );
203
204                 // Load row template and switch colors
205                 $OUT .= LOAD_TEMPLATE("admin_unlock_sponsor_row", true, $content);
206                 $SW = 3 - $SW;
207         }
208         define('__SPONSOR_ROWS', $OUT);
209
210         // Load template
211         LOAD_TEMPLATE("admin_unlock_sponsor");
212 }
213  else
214 {
215         // No pending accounts found
216         LOAD_TEMPLATE("admin_settings_saved", false, ADMIN_SPONSOR_NONE_PENDING);
217 }
218
219 // Free memory
220 SQL_FREERESULT($result);
221
222 //
223 ?>