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