A lot while() conditions rewritten to SQL_FETCHARRAY(), see bug #107, @TODO tags...
[mailer.git] / inc / modules / admin / what-config_rallye_prices.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 07/03/2004 *
4  * ================                             Last change: 08/22/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-config_rallye_prices.php                    *
8  * -------------------------------------------------------------------- *
9  * Short description : Setup rallye prices                              *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Rallye-Preise einrichten                         *
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 if (REQUEST_ISSET_GET(('rallye'))) {
44         // Price submitted?
45         if (REQUEST_ISSET_POST(('add'))) {
46                 if ((REQUEST_ISSET_POST(('level'))) && ((REQUEST_ISSET_POST(('points'))) || (REQUEST_ISSET_POST(('info'))))) {
47                         // Submitted data is valid, but maybe we already have this price level?
48                         $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_rallye_prices` WHERE rallye_id=%s AND price_level='%s' LIMIT 1",
49                                 array(bigintval(REQUEST_GET('rallye')), bigintval(REQUEST_POST('level'))), __FILE__, __LINE__);
50
51                         if (SQL_NUMROWS($result) == 0) {
52                                 // Ok, new price level entered!
53                                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_rallye_prices` (rallye_id, price_level, points, info)
54 VALUES ('%s','%s','%s','%s')",
55  array(
56         bigintval(REQUEST_GET('rallye')),
57         bigintval(REQUEST_POST('level')),
58         REQUEST_POST('points'),
59         REQUEST_POST('info')
60 ), __FILE__, __LINE__);
61                                 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('RALLYE_PRICE_LEVEL_SAVED'));
62                         } else {
63                                 // Free memory
64                                 SQL_FREERESULT($result);
65
66                                 // Price level found!
67                                 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('RALLYE_PRICE_ALREADY_FOUND'));
68                         }
69                 }
70         } elseif (REQUEST_ISSET_POST(('remove'))) {
71                 // Check if at last one line is selected
72                 $SEL = SELECTION_COUNT(REQUEST_POST('sel'));
73                 if ($SEL > 0) {
74                         // Delete selected entries
75                         foreach (REQUEST_POST('sel') as $id => $sel) {
76                                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_rallye_prices` WHERE id=%s LIMIT 1",
77                                         array(bigintval($id)), __FILE__, __LINE__);
78                         }
79
80                         // Output message
81                         LOAD_TEMPLATE("admin_settings_saved", false, getMessage('RALLYE_ENTRIES_DELETED'));
82                 } else {
83                         LOAD_TEMPLATE("admin_settings_saved", false, getMessage('RALLYE_ENTRIES_NOT_DELETED'));
84                 }
85         } elseif (REQUEST_ISSET_POST(('change'))) {
86                 // Change entries
87                 foreach (REQUEST_POST('level') as $id => $level) {
88                         // Secure ID
89                         $id = bigintval($id);
90
91                         // Update entry
92                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_rallye_prices` SET rallye_id=%s, price_level='%s', points='%s', info='%s' WHERE id=%s LIMIT 1",
93                                 array(
94                                         REQUEST_POST('rallye_id', $id),
95                                         bigintval($level),
96                                         REQUEST_POST('points', $id]),
97                                         REQUEST_POST('infos', $id),
98                                         $id
99                                 ), __FILE__, __LINE__);
100                 }
101
102                 // Output message
103                 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('RALLYE_ENTRIES_CHANGED'));
104         }
105
106         if (REQUEST_ISSET_POST('edit')) {
107                 // Check if at last one line is selected
108                 $SEL = SELECTION_COUNT(REQUEST_POST('sel'));
109                 if ($SEL > 0) {
110                         // Make selected editable
111                         $OUT = ""; $SW = 2;
112                         foreach (REQUEST_POST('sel') as $id => $sel) {
113                                 // Load data to selected rallye
114                                 $result = SQL_QUERY_ESC("SELECT rallye_id, price_level, points, info FROM `{!_MYSQL_PREFIX!}_rallye_prices` WHERE id=%s LIMIT 1",
115                                         array(bigintval($id)), __FILE__, __LINE__);
116                                 list($rallye, $level, $points, $infos) = SQL_FETCHROW($result);
117                                 SQL_FREERESULT($result);
118
119                                 // Prepare data for the row template
120                                 $content = array(
121                                         'sw'      => $SW,
122                                         'id'      => $id,
123                                         'rallyes' => ADD_OPTION_LINES("rallye_data", "id", "title", $rallye),
124                                         'level'   => $level,
125                                         'points'  => $points,
126                                         'infos'   => $infos,
127                                 );
128
129                                 // Load row template and switch color
130                                 $OUT .= LOAD_TEMPLATE("admin_config_rallye_edit_row", true, $content);
131                                 $SW = 3 - $SW;
132                         }
133                         define('__PRICE_ROWS', $OUT);
134
135                         // Prepare data for the main template
136                         define('__RALLYE_ID', REQUEST_GET('rallye'));
137
138                         // Load main template
139                         LOAD_TEMPLATE("admin_config_rallye_edit");
140                 } else {
141                         // Nothing selected
142                         $content = sprintf(getMessage('RALLYE_NO_PRICES_SELECTED'), "<a href=\"{!URL!}/modules.php?module=admin&amp;what=config_rallye_prices&amp;rallye=".REQUEST_GET('rallye')."\">", "</a>");
143                         LOAD_TEMPLATE("admin_settings_saved", false, $content);
144                 }
145         } elseif (REQUEST_ISSET_POST('del')) {
146                 // Check if at last one line is selected
147                 $SEL = SELECTION_COUNT(REQUEST_POST('sel'));
148                 if ($SEL > 0) {
149                         // List all prices
150                         $OUT = ""; $SW = 2;
151                         foreach (REQUEST_POST('sel') as $id => $sel) {
152                                 // Load data to selected rallye
153                                 $result = SQL_QUERY_ESC("SELECT rallye_id, price_level, points, info FROM `{!_MYSQL_PREFIX!}_rallye_prices` WHERE id=%s LIMIT 1",
154                                         array(bigintval($id)), __FILE__, __LINE__);
155                                 list($rallye, $level, $points, $infos) = SQL_FETCHROW($result);
156                                 SQL_FREERESULT($result);
157
158                                 if (empty($infos)) $infos = "---";
159
160                                 // Prepare data for the row template
161                                 $content = array(
162                                         'sw'      => $SW,
163                                         'id'      => $id,
164                                         'level'   => $level,
165                                         'points'  => $points,
166                                         'infos'   => $infos,
167                                 );
168
169                                 // Load row template and switch color
170                                 $OUT .= LOAD_TEMPLATE("admin_config_rallye_del_row", true, $content);
171                                 $SW = 3 - $SW;
172                         }
173                         define('__PRICE_ROWS', $OUT);
174
175                         // Prepare data for the main template
176                         define('__RALLYE_ID', REQUEST_GET('rallye'));
177
178                         // Load main template
179                         LOAD_TEMPLATE("admin_config_rallye_del");
180                 } else {
181                         // Nothing selected
182                         $content = RALLYE_NO_PRICES_SELECTED_1."<a href=\"{!URL!}/modules.php?module=admin&amp;what=config_rallye_prices&amp;rallye=".REQUEST_GET('rallye')."\">".RALLYE_NO_PRICES_SELECTED_2."</a>".RALLYE_NO_PRICES_SELECTED_3;
183                         LOAD_TEMPLATE("admin_settings_saved", false, $content);
184                 }
185         } else {
186                 // a rallye was selected, so check if there are already prices assigned...
187                 $result = SQL_QUERY_ESC("SELECT id, price_level, points, info FROM `{!_MYSQL_PREFIX!}_rallye_prices` WHERE rallye_id=%s ORDER BY price_level",
188                         array(bigintval(REQUEST_GET('rallye'))), __FILE__, __LINE__);
189
190                 if (SQL_NUMROWS($result) > 0) {
191                         // Load all prices for the selected rallye
192                         $OUT = ""; $SW = 2;
193                         while ($content = SQL_FETCHARRAY($result)) {
194                                 // Fix empty info
195                                 if (empty($content['info'])) $content['info'] = "---";
196
197                                 // Prepare data for the row template
198                                 // @TODO Rewritings: level->price_level, infos->info in template
199                                 $content = array(
200                                         'sw'      => $SW,
201                                         'id'      => $content['id'],
202                                         'level'   => $content['price_level'],
203                                         'points'  => $content['points'],
204                                         'infos'   => $content['info'],
205                                 );
206
207                                 // Load row template and switch color
208                                 $OUT .= LOAD_TEMPLATE("admin_config_rallye_prices_row", true, $content);
209                                 $SW = 3 - $SW;
210                         } // END - while
211
212                         // Free memory
213                         SQL_FREERESULT($result);
214
215                         // @TODO Rewrite these two constants
216                         define('__PRICE_ROWS', $OUT);
217
218                         // Prepare data for the main template
219                         define('__RALLYE_ID', REQUEST_GET('rallye'));
220
221                         // Load main template
222                         LOAD_TEMPLATE("admin_config_rallye_prices");
223                 }
224         }
225
226         // Add form for adding new price level
227         if (!REQUEST_ISSET_POST('edit')) {
228                 LOAD_TEMPLATE("admin_add_rallye_prices", false, REQUEST_GET('rallye'));
229         }
230 } else {
231         // No rallye selected so display all available without prices
232         $result = SQL_QUERY("SELECT d.id, d.admin_id, d.start_time, d.end_time, d.title, a.login, d.is_active
233 FROM `{!_MYSQL_PREFIX!}_rallye_data` AS d, `{!_MYSQL_PREFIX!}_admins` AS a
234 WHERE d.admin_id=a.id ORDER BY start_time DESC", __FILE__, __LINE__);
235         if (SQL_NUMROWS($result) > 0) {
236                 // List found rallyes
237                 $OUT = ""; $SW = 2;
238                 while ($content = SQL_FETCHARRAY($result)) {
239                         $select = "<input type=\"checkbox\" name=\"sel[".$content['id']."]\" class=\"admin_normal\" value=\"1\">";
240                         if ($content['is_active'] == "Y") $select = "<div class=\"big\">".$content['id']."</div>";
241
242                         // Prepare data for the row template
243                         // @TODO Rewritings: aid->admin_id, alogin->login in template
244                         $content = array(
245                                 'sw'         => $SW,
246                                 'id'         => $content['id'],
247                                 'select'     => $select,
248                                 'title'      => $content['title'],
249                                 'aid'        => $content['admin_id'],
250                                 'email_link' => CREATE_EMAIL_LINK($content['admin_id']),
251                                 'alogin'     => $content['login'],
252                                 'start'      => MAKE_DATETIME($content['start_time'], "3"),
253                                 'end'        => MAKE_DATETIME($content['end_time'], "3"),
254                         );
255
256                         // Load row template and switch color
257                         $OUT .= LOAD_TEMPLATE("admin_list_rallye_prices_row", true, $content);
258                         $SW = 3 - $SW;
259                 }
260
261                 // Free memory
262                 SQL_FREERESULT($result);
263                 define('__RALLYE_ROWS', $OUT);
264
265                 // Load main template
266                 LOAD_TEMPLATE("admin_list_rallye_prices");
267         } else {
268                 // No rallyes setup so far
269                 LOAD_TEMPLATE("admin_settings_saved", false, getMessage('RALLYE_NO_RALLYES_SETUP'));
270         }
271 }
272 //
273 ?>