Code-style applied, surfbar URLs can now be edited
[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 ((ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) || (!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", basename(__FILE__));
42
43 if (!empty($_GET['sub']))
44 {
45         switch ($_GET['sub'])
46         {
47         case "points":
48                 if ((empty($_POST['points_register'])) || (empty($_POST['points_ref'])))
49                 {
50                         unset($_POST['ok']);
51                 }
52                 break;
53
54         case "ref":
55                 if (isset($_GET['do']))
56                 {
57                         if (((empty($_POST['lvl'])) || (empty($_POST['perc']))) && ($_GET['do'] == "add"))
58                         {
59                                 unset($_POST['ok']);
60                         }
61                 }
62                 break;
63         }
64 }
65  else
66 {
67         // Display overview
68         $_GET['sub'] = "overview";
69 }
70
71 if (isset($_POST['ok']))
72 {
73         $SQL = array();
74         switch ($_GET['sub'])
75         {
76         case "points":
77                 $SQL[] = "UPDATE "._MYSQL_PREFIX."_config SET points_register='".$_POST['points_register']."', points_ref='".$_POST['points_ref']."' WHERE config=0 LIMIT 1";
78                 break;
79
80         case "ref":
81                 switch ($_GET['do'])
82                 {
83                 case "add":
84                         $SQL[] = "INSERT INTO "._MYSQL_PREFIX."_refdepths (level, percents) VALUES ('".$_POST['lvl']."', '".$_POST['perc']."')";
85                         break;
86
87                 case "edit": // Change entries
88                         foreach ($_POST['lvl'] as $id => $value)
89                         {
90                                 // Secure ID
91                                 $id = bigintval($id);
92
93                                 // Update entry
94                                 $result = 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                         $TEXT = REF_DEPTHS_SAVED;
98                         break;
99
100                 case "del":
101                         foreach ($_POST['id'] as $id => $value)
102                         {
103                                 $result = SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM "._MYSQL_PREFIX."_refdepths WHERE id=%s LIMIT 1",
104                                  array(bigintval($id)), __FILE__, __LINE__);
105                         }
106                         $TEXT = REF_DEPTHS_DELETED;
107                         break;
108                 }
109
110                 // Update cache file
111                 if (GET_EXT_VERSION("cache") >= "0.1.2")
112                 {
113                         if ($cacheInstance->cache_file("refdepths", true)) $cacheInstance->cache_destroy();
114                 }
115                 break;
116
117         case "settings":
118                 $REF = bigintval($_POST['ref_payout']);
119                 $SQL[] = sprintf("UPDATE "._MYSQL_PREFIX."_config SET
120 allow_direct_pay='%s',
121 reg_points_mode='%s',
122 ref_payout='%s'
123 WHERE config=0 LIMIT 1",
124  $_POST['allow_direct_pay'],
125  $_POST['reg_points_mode'],
126  $REF
127                 );
128                 if (($_CONFIG['ref_payout'] == 0) && ($_POST['ref_payout'] > 0))
129                 {
130                         // Update account's ref_payout for "must-confirm"
131                         $SQL[] = sprintf("UPDATE "._MYSQL_PREFIX."_user_data SET ref_payout=(%s - mails_confirmed)
132 WHERE mails_confirmed < %s", $REF, $REF);
133                 }
134                  elseif (($_CONFIG['ref_payout'] > 0) && ($_POST['ref_payout'] == 0))
135                 {
136                         // Update account's ref_payout for "not-must-confirm"
137                         $SQL[] = "UPDATE "._MYSQL_PREFIX."_user_data SET ref_payout=0 WHERE ref_payout > 0";
138                         $SQL[] = "UPDATE "._MYSQL_PREFIX."_user_points SET points=points+locked_points WHERE locked_points>0";
139                         $SQL[] = "UPDATE "._MYSQL_PREFIX."_user_points SET locked_points=0 WHERE locked_points>0";
140                 }
141                 break;
142         }
143         if ((isset($SQL)) && (is_array($SQL)) && (!empty($SQL[0])))
144         {
145                 if (strpos($SQL[0], "INSERT") > -1)
146                 {
147                         $result = SQL_QUERY_ESC("SELECT id FROM "._MYSQL_PREFIX."_refdepths WHERE level='%s' LIMIT 1",
148                          array(bigintval($_POST['lvl'])), __FILE__, __LINE__);
149                         SQL_FREERESULT($result);
150                 }
151                 if (count($SQL) > 0)
152                 {
153                         // Run all SQL commands
154                         foreach ($SQL as $s)
155                         {
156                                 $result = SQL_QUERY($s, __FILE__, __LINE__);
157                         }
158
159                         $content = "<SPAN class=\"admin_done\">".SETTINGS_SAVED."</SPAN>";
160
161                         // Destroy config cache file here...
162                         if (GET_EXT_VERSION("cache") >= "0.1.2")
163                         {
164                                 if ($cacheInstance->cache_file("config", true)) $cacheInstance->cache_destroy();
165                                 unset($cacheArray);
166                         }
167                 }
168                  else
169                 {
170                         $content = "<SPAN class=\"admin_failed\">".SETTINGS_NOT_SAVED."</SPAN>";
171                 }
172                 unset($SQL);
173                 LOAD_TEMPLATE("admin_settings_saved", false, $content);
174         }
175          elseif (isset($TEXT))
176         {
177                 LOAD_TEMPLATE("admin_settings_saved", false, $TEXT);
178         }
179 }
180  elseif ($_GET['sub'] == "settings")
181 {
182         // Setup some settings like direct pay and so on
183         // Including new add-mode for one-time referral bonus
184         switch ($_CONFIG['allow_direct_pay'])
185         {
186         case 'Y':
187                 define('__DIRECT_Y', ' checked');
188                 define('__DIRECT_N', "");
189                 break;
190
191         case 'N':
192                 define('__DIRECT_Y', "");
193                 define('__DIRECT_N', ' checked');
194                 break;
195         }
196
197         // One-time referral bonus add-mode
198         switch ($_CONFIG['reg_points_mode'])
199         {
200                 case "ref"   : define('__MODE_REF', ' checked'); define('__MODE_DIRECT', "");         break;
201                 case "direct": define('__MODE_REF', "");         define('__MODE_DIRECT', ' checked'); break;
202         }
203
204         // Referral payout value
205         define('__REF_PAYOUT', round($_CONFIG['ref_payout']));
206
207         // Load template
208         LOAD_TEMPLATE("admin_config_point_settings");
209 }
210  elseif ($_GET['sub'] == "ref")
211 {
212          if ((isset($_POST['del'])) && ((SELECTION_COUNT($_POST['sel']) > 0) || (isset($_POST['sel'][0]))))
213         {
214                 // Delete entries
215                 $SW = 2; $OUT = "";
216                 foreach ($_POST['sel'] as $id => $value)
217                 {
218                         $result = SQL_QUERY_ESC("SELECT level, percents FROM "._MYSQL_PREFIX."_refdepths WHERE id=%s LIMIT 1",
219                          array(bigintval($id)), __FILE__, __LINE__);
220                         list($lvl, $perc) = SQL_FETCHROW($result);
221                         SQL_FREERESULT($result);
222
223                         // Prepare data for the row template
224                         $content = array(
225                                 'sw'  => $SW,
226                                 'id'  => $id,
227                                 'lvl' => $lvl,
228                                 'per' => $perc,
229                         );
230
231                         // Load row template and switch color
232                         $OUT .= LOAD_TEMPLATE("admin_points_del_row", true, $content);
233                         $SW = 3 - $SW;
234                 }
235                 define('__LEVEL_ROWS', $OUT);
236
237                 // Load main template
238                 LOAD_TEMPLATE("admin_points_del");
239         }
240          elseif ((isset($_POST['edit'])) && ((SELECTION_COUNT($_POST['sel']) > 0) || (isset($_POST['sel'][0]))))
241         {
242                 // Edit entries
243                 $SW = 2; $OUT = "";
244                 foreach ($_POST['sel'] as $id => $value)
245                 {
246                         $result = SQL_QUERY_ESC("SELECT level, percents FROM "._MYSQL_PREFIX."_refdepths WHERE id=%s LIMIT 1",
247                          array(bigintval($id)), __FILE__, __LINE__);
248                         list($lvl, $perc) = SQL_FETCHROW($result);
249                         SQL_FREERESULT($result);
250
251                         // Prepare data for the row template
252                         $content = array(
253                                 'sw'  => $SW,
254                                 'id'  => $id,
255                                 'lvl' => $lvl,
256                                 'per' => $perc,
257                         );
258
259                         // Load row template and switch color
260                         $OUT .= LOAD_TEMPLATE("admin_points_edit_row", true, $content);
261                         $SW = 3 - $SW;
262                 }
263                 define('__LEVEL_ROWS', $OUT);
264
265                 // Load main template
266                 LOAD_TEMPLATE("admin_points_edit");
267         }
268          else
269         {
270                 // Referral levels
271                 $result = SQL_QUERY("SELECT id, level, percents FROM "._MYSQL_PREFIX."_refdepths ORDER BY level", __FILE__, __LINE__);
272                 if (SQL_NUMROWS($result) > 0)
273                 {
274                         // Make referral levels editable and deletable
275                         $SW = 2; $OUT = "";
276
277                         // List already existing categories for editing
278                         while (list($id, $lvl, $perc) = SQL_FETCHROW($result))
279                         {
280                                 // Prepare data for the row template
281                                 $content = array(
282                                         'sw'  => $SW,
283                                         'id'  => $id,
284                                         'lvl' => $lvl,
285                                         'per' => $perc,
286                                 );
287
288                                 // Load row template and switch color
289                                 $OUT .= LOAD_TEMPLATE("admin_points_row", true, $content);
290                                 $SW = 3 - $SW;
291                         }
292
293                         // Free memory
294                         SQL_FREERESULT($result);
295                         define('__LEVEL_ROWS', $OUT);
296
297                         // Load main template
298                         LOAD_TEMPLATE("admin_points");
299                 }
300
301                 // Form for adding new referral levels
302                 LOAD_TEMPLATE("admin_add_reflvl");
303         }
304 }
305  elseif ($_GET['sub'] == "points")
306 {
307         // First points for registration and other fixed points including new add-mode for one-time referral bonus...
308         define('P_REG_VALUE', $_CONFIG['points_register']);
309         define('P_REF_VALUE', $_CONFIG['points_ref']);
310
311         // Load templates
312         LOAD_TEMPLATE("admin_config_sub_points");
313 }
314  else
315 {
316         // Display selection box
317         LOAD_TEMPLATE("admin_config_points");
318 }
319
320 //
321 ?>