Functions imported, some dev-scripts added
[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 (!empty($_GET['sub'])) {
48         // Yes, then do some sanity-checks
49         switch ($_GET['sub'])
50         {
51         case "points":
52                 if ((empty($_POST['points_register'])) || (empty($_POST['points_ref']))) {
53                         unset($_POST['ok']);
54                 }
55                 break;
56
57         case "ref":
58                 if (isset($_GET['do'])) {
59                         if (((empty($_POST['lvl'])) || (empty($_POST['perc']))) && ($_GET['do'] == "add")) {
60                                 unset($_POST['ok']);
61                         }
62                 }
63                 break;
64         }
65 } else {
66         // Display overview
67         $_GET['sub'] = "overview";
68 }
69
70 if (isset($_POST['ok'])) {
71         $SQLs = array();
72         switch ($_GET['sub'])
73         {
74         case "points":
75                 $SQLs[] = "UPDATE `{!_MYSQL_PREFIX!}_config` SET points_register='".$_POST['points_register']."', points_ref='".$_POST['points_ref']."' WHERE config=0 LIMIT 1";
76                 break;
77
78         case "ref":
79                 switch ($_GET['do'])
80                 {
81                 case "add":
82                         $SQLs[] = "INSERT INTO `{!_MYSQL_PREFIX!}_refdepths` (level, percents) VALUES ('".$_POST['lvl']."','".$_POST['perc']."')";
83                         break;
84
85                 case "edit": // Change entries
86                         foreach ($_POST['lvl'] as $id => $value) {
87                                 // Secure ID
88                                 $id = bigintval($id);
89
90                                 // Revert german commata
91                                 $_POST['perc'][$id] = REVERT_COMMA($_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), $_POST['perc'][$id], $id), __FILE__, __LINE__);
96                         }
97                         $message = REF_DEPTHS_SAVED;
98                         break;
99
100                 case "del":
101                         foreach ($_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 = REF_DEPTHS_DELETED;
106                         break;
107                 }
108
109                 // Update cache file
110                 if (GET_EXT_VERSION("cache") >= "0.1.2") {
111                         if ($cacheInstance->loadCacheFile("refdepths")) $cacheInstance->destroyCacheFile();
112                 }
113                 break;
114
115         case "settings":
116                 $REF = bigintval($_POST['ref_payout']);
117                 $SQLs[] = 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                         $_POST['allow_direct_pay'],
123                         $_POST['reg_points_mode'],
124                         $REF
125                 );
126                 if ((getConfig('ref_payout') == 0) && ($_POST['ref_payout'] > 0)) {
127                         // Update account's ref_payout for "must-confirm"
128                         $SQLs[] = 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) && ($_POST['ref_payout'] == 0)) {
131                         // Update account's ref_payout for "not-must-confirm"
132                         $SQLs[] = "UPDATE `{!_MYSQL_PREFIX!}_user_data` SET ref_payout=0 WHERE ref_payout > 0";
133                         $SQLs[] = "UPDATE `{!_MYSQL_PREFIX!}_user_points` SET points=points+locked_points WHERE locked_points>0";
134                         $SQLs[] = "UPDATE `{!_MYSQL_PREFIX!}_user_points` SET locked_points=0 WHERE locked_points>0";
135                 }
136                 break;
137         }
138
139         if ((isset($SQLs)) && (is_array($SQLs)) && (!empty($SQLs[0]))) {
140                 if (strpos($SQLs[0], "INSERT") > -1) {
141                         $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_refdepths` WHERE level='%s' LIMIT 1",
142                                 array(bigintval($_POST['lvl'])), __FILE__, __LINE__);
143                         SQL_FREERESULT($result);
144                 } // END - if
145
146                 if (count($SQLs) > 0) {
147                         // Run all SQL commands
148                         RUN_FILTER('run_sqls', array('dry_run' => false, 'sqls' => $SQLs));
149
150                         // Entry saved...
151                         $message = SETTINGS_SAVED;
152
153                         // Destroy config cache file here...
154                         REBUILD_CACHE("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 ($_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 ($_GET['sub'] == "ref") {
196         if ((isset($_POST['del'])) && (isset($_POST['sel'])) && ((SELECTION_COUNT($_POST['sel']) > 0) || (isset($_POST['sel'][0])))) {
197                 // Delete entries
198                 $SW = 2; $OUT = "";
199                 foreach ($_POST['sel'] as $id => $value) {
200                         $result = SQL_QUERY_ESC("SELECT level, percents FROM `{!_MYSQL_PREFIX!}_refdepths` WHERE id=%s LIMIT 1",
201                                 array(bigintval($id)), __FILE__, __LINE__);
202                         list($lvl, $perc) = SQL_FETCHROW($result);
203                         SQL_FREERESULT($result);
204
205                         // Prepare data for the row template
206                         $content = array(
207                                 'sw'  => $SW,
208                                 'id'  => $id,
209                                 'lvl' => $lvl,
210                                 'per' => TRANSLATE_COMMA($perc),
211                         );
212
213                         // Load row template and switch color
214                         $OUT .= LOAD_TEMPLATE("admin_points_del_row", true, $content);
215                         $SW = 3 - $SW;
216                 }
217                 define('__LEVEL_ROWS', $OUT);
218
219                 // Load main template
220                 LOAD_TEMPLATE("admin_points_del");
221         } elseif ((isset($_POST['edit'])) && (isset($_POST['sel'])) && ((SELECTION_COUNT($_POST['sel']) > 0) || (isset($_POST['sel'][0])))) {
222                 // Edit entries
223                 $SW = 2; $OUT = "";
224                 foreach ($_POST['sel'] as $id => $value) {
225                         $result = SQL_QUERY_ESC("SELECT level, percents FROM `{!_MYSQL_PREFIX!}_refdepths` WHERE id=%s LIMIT 1",
226                          array(bigintval($id)), __FILE__, __LINE__);
227                         list($lvl, $perc) = SQL_FETCHROW($result);
228                         SQL_FREERESULT($result);
229
230                         // Prepare data for the row template
231                         $content = array(
232                                 'sw'  => $SW,
233                                 'id'  => $id,
234                                 'lvl' => $lvl,
235                                 'per' => TRANSLATE_COMMA($perc),
236                         );
237
238                         // Load row template and switch color
239                         $OUT .= LOAD_TEMPLATE("admin_points_edit_row", true, $content);
240                         $SW = 3 - $SW;
241                 }
242                 define('__LEVEL_ROWS', $OUT);
243
244                 // Load main template
245                 LOAD_TEMPLATE("admin_points_edit");
246         } else {
247                 // Referal levels
248                 $result = SQL_QUERY("SELECT id, level, percents FROM `{!_MYSQL_PREFIX!}_refdepths` ORDER BY level", __FILE__, __LINE__);
249                 if (SQL_NUMROWS($result) > 0) {
250                         // Make referal levels editable and deletable
251                         $SW = 2; $OUT = "";
252
253                         // List already existing categories for editing
254                         while (list($id, $lvl, $perc) = SQL_FETCHROW($result)) {
255                                 // Prepare data for the row template
256                                 $content = array(
257                                         'sw'  => $SW,
258                                         'id'  => $id,
259                                         'lvl' => $lvl,
260                                         'per' => TRANSLATE_COMMA($perc),
261                                 );
262
263                                 // Load row template and switch color
264                                 $OUT .= LOAD_TEMPLATE("admin_points_row", true, $content);
265                                 $SW = 3 - $SW;
266                         }
267
268                         // Free memory
269                         SQL_FREERESULT($result);
270                         define('__LEVEL_ROWS', $OUT);
271
272                         // Load main template
273                         LOAD_TEMPLATE("admin_points");
274                 }
275
276                 // Form for adding new referal levels
277                 LOAD_TEMPLATE("admin_add_reflvl");
278         }
279 } elseif ($_GET['sub'] == "points") {
280         // First points for registration and other fixed points including new add-mode for one-time referal bonus...
281         define('P_REG_VALUE', getConfig('points_register'));
282         define('P_REF_VALUE', getConfig('points_ref'));
283
284         // Load templates
285         LOAD_TEMPLATE("admin_config_sub_points");
286 } else {
287         // Display selection box
288         LOAD_TEMPLATE("admin_config_points");
289 }
290
291 //
292 ?>