Fix for the fix... :(
[mailer.git] / inc / modules / admin / what-config_points.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 10/12/2003 *
4  * ===============                              Last change: 12/12/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-config_points.php                           *
8  * -------------------------------------------------------------------- *
9  * Short description : Setup welcome-points for registration etc.       *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Einstellen der Willkommensgutschrift usw.        *
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 // Init variables
49 $message = '';
50
51 // Is the 'sub' parameter set?
52 if (REQUEST_ISSET_GET('sub')) {
53         // Yes, then do some sanity-checks
54         switch (REQUEST_GET('sub'))
55         {
56                 case "points":
57                         if ((!REQUEST_ISSET_POST(('points_register'))) || (!REQUEST_ISSET_POST(('points_ref')))) {
58                                 REQUEST_UNSET_POST('ok');
59                         }
60                         break;
61
62                 case "ref":
63                         if (REQUEST_ISSET_GET('do')) {
64                                 if (((!REQUEST_ISSET_POST(('lvl'))) || (!REQUEST_ISSET_POST(('perc')))) && (REQUEST_GET('do') == 'add')) {
65                                         REQUEST_UNSET_POST('ok');
66                                 }
67                         }
68                         break;
69         }
70 } else {
71         // Display overview
72         REQUEST_SET_GET('sub', "overview");
73 }
74
75 if (isFormSent()) {
76         INIT_SQLS();
77         switch (REQUEST_GET('sub'))
78         {
79                 case "points":
80                         ADD_SQL("UPDATE `{!_MYSQL_PREFIX!}_config` SET points_register='".REQUEST_POST('points_register')."', points_ref='".REQUEST_POST('points_ref')."' WHERE config=0 LIMIT 1");
81                         break;
82
83                 case "ref":
84                         switch (REQUEST_GET('do'))
85                         {
86                                 case 'add':
87                                         ADD_SQL("INSERT INTO `{!_MYSQL_PREFIX!}_refdepths` (level, percents) VALUES ('".REQUEST_POST('lvl')."','".REQUEST_POST('perc')."')");
88                                         break;
89
90                                 case 'edit': // Change entries
91                                         foreach (REQUEST_POST('lvl') as $id => $value) {
92                                                 // Secure ID
93                                                 $id = bigintval($id);
94
95                                                 // Revert german commata
96                                                 REQUEST_SET_POST('perc', array($id => convertCommaToDot(REQUEST_POST('perc', $id))));
97
98                                                 // Update entry
99                                                 SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_refdepths` SET level='%s', percents='%s' WHERE `id`=%s LIMIT 1",
100                                                 array(bigintval($value), REQUEST_POST('perc', $id), $id), __FILE__, __LINE__);
101                                         }
102                                         $message = getMessage('REF_DEPTHS_SAVED');
103                                         break;
104
105                                 case 'del':
106                                         foreach (REQUEST_POST('id') as $id => $value) {
107                                                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_refdepths` WHERE `id`=%s LIMIT 1",
108                                                 array(bigintval($id)), __FILE__, __LINE__);
109                                         }
110                                         $message = getMessage('REF_DEPTHS_DELETED');
111                                         break;
112                         }
113
114                         // Update cache file
115                         if (GET_EXT_VERSION('cache') >= '0.1.2') {
116                                 if ($GLOBALS['cache_instance']->loadCacheFile("refdepths")) $GLOBALS['cache_instance']->destroyCacheFile();
117                         }
118                         break;
119
120                                 case "settings":
121                                         $REF = bigintval(REQUEST_POST('ref_payout'));
122                                         ADD_SQL(sprintf("UPDATE `{!_MYSQL_PREFIX!}_config` SET
123 allow_direct_pay='%s',
124 reg_points_mode='%s',
125 ref_payout='%s'
126 WHERE config=0 LIMIT 1",
127                                         REQUEST_POST('allow_direct_pay'),
128                                         REQUEST_POST('reg_points_mode'),
129                                         $REF
130                                         ));
131                                         if ((getConfig('ref_payout') == 0) && (REQUEST_POST('ref_payout') > 0)) {
132                                                 // Update account's ref_payout for "must-confirm"
133                                                 ADD_SQL(sprintf("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET ref_payout=(%s - mails_confirmed)
134 WHERE mails_confirmed < %s", $REF, $REF));
135                                         } elseif ((getConfig('ref_payout') > 0) && (REQUEST_POST('ref_payout') == 0)) {
136                                                 // Update account's ref_payout for "not-must-confirm"
137                                                 ADD_SQL("UPDATE `{!_MYSQL_PREFIX!}_user_data` SET ref_payout=0 WHERE ref_payout > 0");
138                                                 ADD_SQL("UPDATE `{!_MYSQL_PREFIX!}_user_points` SET points=points+locked_points WHERE locked_points>0");
139                                                 ADD_SQL("UPDATE `{!_MYSQL_PREFIX!}_user_points` SET locked_points=0 WHERE locked_points>0");
140                                         }
141                                         break;
142         }
143
144         if ((IS_SQLS_VALID()) && (IS_SQLS_VALID())) {
145                 if (strpos($GLOBALS['sqls'][0], "INSERT") > -1) {
146                         $result = SQL_QUERY_ESC("SELECT `id` FROM `{!_MYSQL_PREFIX!}_refdepths` WHERE level='%s' LIMIT 1",
147                         array(bigintval(REQUEST_POST('lvl'))), __FILE__, __LINE__);
148                         SQL_FREERESULT($result);
149                 } // END - if
150
151                 if (COUNT_SQLS() > 0) {
152                         // Run all SQL commands
153                         runFilterChain('run_sqls');
154
155                         // Entry saved...
156                         $message = getMessage('SETTINGS_SAVED');
157
158                         // Destroy config cache file here...
159                         rebuildCacheFiles("config", "config");
160                 } else {
161                         // Prepare failed-message
162                         $message = "<span class=\"admin_failed\">{--SETTINGS_NOT_SAVED--}</span>";
163                 }
164
165                 // Remove SQL queries
166                 UNSET_SQLS();
167         }
168
169         // Shall we display a message?
170         if (!empty($message)) {
171                 // When do so...
172                 LOAD_TEMPLATE('admin_settings_saved', false, $message);
173         }
174 } elseif (REQUEST_GET('sub') == "settings") {
175         // Setup some settings like direct pay and so on
176         // Including new add-mode for one-time referal bonus
177         switch (getConfig('allow_direct_pay')) {
178                 case 'Y':
179                         define('__DIRECT_Y', ' checked="checked"');
180                         define('__DIRECT_N', '');
181                         break;
182
183                 case 'N':
184                         define('__DIRECT_Y', '');
185                         define('__DIRECT_N', ' checked="checked"');
186                         break;
187         }
188
189         // One-time referal bonus add-mode
190         switch (getConfig('reg_points_mode')) {
191                 case 'ref'   :
192                         define('__MODE_REF', ' checked="checked"');
193                         define('__MODE_DIRECT', '');
194                         break;
195
196                 case 'direct':
197                         define('__MODE_REF', '');
198                         define('__MODE_DIRECT', ' checked="checked"');
199                         break;
200         }
201
202         // Referal payout value
203         define('__REF_PAYOUT', round(getConfig('ref_payout')));
204
205         // Load template
206         LOAD_TEMPLATE("admin_config_point_settings");
207 } elseif (REQUEST_GET('sub') == "ref") {
208         // 12                  3     32    2                  3     32    2               3            4     43    21
209         if ((REQUEST_ISSET_POST('del')) && (REQUEST_ISSET_POST('sel')) && (countPostSelection() > 0)) {
210                 // Delete entries
211                 $OUT = ''; $SW = 2;
212                 foreach (REQUEST_POST('sel') as $id => $value) {
213                         $result = SQL_QUERY_ESC("SELECT level, percents FROM `{!_MYSQL_PREFIX!}_refdepths` WHERE `id`=%s LIMIT 1",
214                         array(bigintval($id)), __FILE__, __LINE__);
215                         list($lvl, $perc) = SQL_FETCHROW($result);
216                         SQL_FREERESULT($result);
217
218                         // Prepare data for the row template
219                         $content = array(
220                                 'sw'  => $SW,
221                                 'id'  => $id,
222                                 'lvl' => $lvl,
223                                 'per' => translateComma($perc),
224                         );
225
226                         // Load row template and switch color
227                         $OUT .= LOAD_TEMPLATE("admin_points_del_row", true, $content);
228                         $SW = 3 - $SW;
229                 }
230                 define('__LEVEL_ROWS', $OUT);
231
232                 // Load main template
233                 LOAD_TEMPLATE("admin_points_del");
234         } elseif ((REQUEST_ISSET_POST('edit')) && (REQUEST_ISSET_POST('sel')) && (countPostSelection() > 0)) {
235                 // Edit entries
236                 $OUT = ''; $SW = 2;
237                 foreach (REQUEST_POST('sel') as $id => $value) {
238                         $result = SQL_QUERY_ESC("SELECT level, percents FROM `{!_MYSQL_PREFIX!}_refdepths` WHERE `id`=%s LIMIT 1",
239                         array(bigintval($id)), __FILE__, __LINE__);
240                         list($lvl, $perc) = SQL_FETCHROW($result);
241                         SQL_FREERESULT($result);
242
243                         // Prepare data for the row template
244                         $content = array(
245                                 'sw'  => $SW,
246                                 'id'  => $id,
247                                 'lvl' => $lvl,
248                                 'per' => translateComma($perc),
249                         );
250
251                         // Load row template and switch color
252                         $OUT .= LOAD_TEMPLATE("admin_points_edit_row", true, $content);
253                         $SW = 3 - $SW;
254                 }
255                 define('__LEVEL_ROWS', $OUT);
256
257                 // Load main template
258                 LOAD_TEMPLATE("admin_points_edit");
259         } else {
260                 // Referal levels
261                 $result = SQL_QUERY("SELECT id, level, percents FROM `{!_MYSQL_PREFIX!}_refdepths` ORDER BY level", __FILE__, __LINE__);
262                 if (SQL_NUMROWS($result) > 0) {
263                         // Make referal levels editable and deletable
264                         $OUT = ''; $SW = 2;
265
266                         // List already existing categories for editing
267                         while ($content = SQL_FETCHARRAY($result)) {
268                                 // Prepare data for the row template
269                                 // @TODO Rewritings: lvl->level, per->percents in template
270                                 $content = array(
271                                         'sw'  => $SW,
272                                         'id'  => $content['id'],
273                                         'lvl' => $content['level'],
274                                         'per' => translateComma($content['percents']),
275                                 );
276
277                                 // Load row template and switch color
278                                 $OUT .= LOAD_TEMPLATE("admin_points_row", true, $content);
279                                 $SW = 3 - $SW;
280                         }
281
282                         // Free memory
283                         SQL_FREERESULT($result);
284                         define('__LEVEL_ROWS', $OUT);
285
286                         // Load main template
287                         LOAD_TEMPLATE("admin_points");
288                 }
289
290                 // Form for adding new referal levels
291                 LOAD_TEMPLATE("admin_add_reflvl");
292         }
293 } elseif (REQUEST_GET('sub') == "points") {
294         // First points for registration and other fixed points including new add-mode for one-time referal bonus...
295         define('P_REG_VALUE', getConfig('points_register'));
296         define('P_REF_VALUE', getConfig('points_ref'));
297
298         // Load templates
299         LOAD_TEMPLATE("admin_config_sub_points");
300 } else {
301         // Display selection box
302         LOAD_TEMPLATE("admin_config_points");
303 }
304
305 //
306 ?>