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