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