Rewrites/fixes for handling config entries in SQLs
[mailer.git] / inc / modules / admin / what-config_rallye_prices.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                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  * $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 - 2009 by Roland Haeder                           *
21  * Copyright (c) 2009, 2010 by Mailer Developer Team                    *
22  * For more information visit: http://www.mxchange.org                  *
23  *                                                                      *
24  * This program is free software; you can redistribute it and/or modify *
25  * it under the terms of the GNU General Public License as published by *
26  * the Free Software Foundation; either version 2 of the License, or    *
27  * (at your option) any later version.                                  *
28  *                                                                      *
29  * This program is distributed in the hope that it will be useful,      *
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
32  * GNU General Public License for more details.                         *
33  *                                                                      *
34  * You should have received a copy of the GNU General Public License    *
35  * along with this program; if not, write to the Free Software          *
36  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
37  * MA  02110-1301  USA                                                  *
38  ************************************************************************/
39
40 // Some security stuff...
41 if ((!defined('__SECURITY')) || (!isAdmin())) {
42         die();
43 } // END - if
44
45 // Add description as navigation point
46 addMenuDescription('admin', __FILE__);
47
48 if (isGetRequestParameterSet('rallye')) {
49         // Price submitted?
50         if (isFormSent('add')) {
51                 if ((isPostRequestParameterSet(('level'))) && ((isPostRequestParameterSet('points')) || (isPostRequestParameterSet(('info'))))) {
52                         // Submitted data is valid, but maybe we already have this price level?
53                         $result = SQL_QUERY_ESC("SELECT `id` FROM `{?_MYSQL_PREFIX?}_rallye_prices` WHERE `rallye_id`=%s AND `price_level`='%s' LIMIT 1",
54                         array(bigintval(getRequestParameter('rallye')), bigintval(postRequestParameter('level'))), __FILE__, __LINE__);
55
56                         if (SQL_HASZERONUMS($result)) {
57                                 // Ok, new price level entered!
58                                 SQL_QUERY_ESC("INSERT INTO `{?_MYSQL_PREFIX?}_rallye_prices` (rallye_id, price_level, points, info)
59 VALUES ('%s','%s','%s','%s')",
60                                 array(
61                                         bigintval(getRequestParameter('rallye')),
62                                         bigintval(postRequestParameter('level')),
63                                         postRequestParameter('points'),
64                                         postRequestParameter('info')
65                                 ), __FILE__, __LINE__);
66                                 loadTemplate('admin_settings_saved', false, getMessage('RALLYE_PRICE_LEVEL_SAVED'));
67                         } else {
68                                 // Free memory
69                                 SQL_FREERESULT($result);
70
71                                 // Price level found!
72                                 loadTemplate('admin_settings_saved', false, getMessage('RALLYE_PRICE_ALREADY_FOUND'));
73                         }
74                 }
75         } elseif (isFormSent('remove')) {
76                 // Check if at last one line is selected
77                 if (countPostSelection() > 0) {
78                         // Delete selected entries
79                         foreach (postRequestParameter('sel') as $id => $sel) {
80                                 SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{?_MYSQL_PREFIX?}_rallye_prices` WHERE `id`=%s LIMIT 1",
81                                         array(bigintval($id)), __FILE__, __LINE__);
82                         } // END - foreach
83
84                         // Output message
85                         loadTemplate('admin_settings_saved', false, getMessage('RALLYE_ENTRIES_DELETED'));
86                 } else {
87                         loadTemplate('admin_settings_saved', false, getMessage('RALLYE_ENTRIES_NOT_DELETED'));
88                 }
89         } elseif (isFormSent('change')) {
90                 // Change entries
91                 foreach (postRequestParameter('level') as $id => $level) {
92                         // Secure id
93                         $id = bigintval($id);
94
95                         // Update entry
96                         SQL_QUERY_ESC("UPDATE
97         `{?_MYSQL_PREFIX?}_rallye_prices`
98 SET
99         `rallye_id`=%s,
100         `price_level`=%d,
101         `points`=%s,
102         `info`='%s'
103 WHERE
104         `id`=%s
105 LIMIT 1",
106                                 array(
107                                         postRequestParameter('rallye_id', $id),
108                                         bigintval($level),
109                                         postRequestParameter('points', $id),
110                                         postRequestParameter('infos', $id),
111                                         $id
112                                 ), __FILE__, __LINE__);
113                 }
114
115                 // Output message
116                 loadTemplate('admin_settings_saved', false, getMessage('RALLYE_ENTRIES_CHANGED'));
117         }
118
119         if (isFormSent('edit')) {
120                 // Check if at last one line is selected
121                 if (countPostSelection() > 0) {
122                         // Make selected editable
123                         $OUT = ''; $SW = 2;
124                         foreach (postRequestParameter('sel') as $id => $sel) {
125                                 // Load data to selected rallye
126                                 $result = SQL_QUERY_ESC("SELECT rallye_id, price_level, points, info FROM `{?_MYSQL_PREFIX?}_rallye_prices` WHERE `id`=%s LIMIT 1",
127                                         array(bigintval($id)), __FILE__, __LINE__);
128                                 list($rallye, $level, $points, $infos) = SQL_FETCHROW($result);
129                                 SQL_FREERESULT($result);
130
131                                 // Prepare data for the row template
132                                 $content = array(
133                                         'sw'      => $SW,
134                                         'id'      => $id,
135                                         'rallyes' => generateOptionList('rallye_data', 'id', 'title', $rallye),
136                                         'level'   => $level,
137                                         'points'  => $points,
138                                         'infos'   => $infos,
139                                 );
140
141                                 // Load row template and switch color
142                                 $OUT .= loadTemplate('admin_config_rallye_edit_row', true, $content);
143                                 $SW = 3 - $SW;
144                         } // END - foreach
145                         $content['rows'] = $OUT;
146
147                         // Prepare data for the main template
148                         $content['rallye'] = getRequestParameter('rallye');
149
150                         // Load main template
151                         loadTemplate('admin_config_rallye_edit', false, $content);
152                 } else {
153                         // Nothing selected
154                         $content = '{--RALLYE_NO_PRICES_SELECTED_1--}<a href="{%url=modules.php?module=admin&amp;what=config_rallye_prices&amp;rallye=' . getRequestParameter('rallye') . '%}">{--RALLYE_NO_PRICES_SELECTED_2--}</a>{--RALLYE_NO_PRICES_SELECTED_3--}';
155                         loadTemplate('admin_settings_saved', false, $content);
156                 }
157         } elseif (isFormSent('del')) {
158                 // Check if at last one line is selected
159                 if (countPostSelection() > 0) {
160                         // List all prices
161                         $OUT = ''; $SW = 2;
162                         foreach (postRequestParameter('sel') as $id => $sel) {
163                                 // Load data to selected rallye
164                                 $result = SQL_QUERY_ESC("SELECT `rallye_id`, `price_level`, `points`, `info` FROM `{?_MYSQL_PREFIX?}_rallye_prices` WHERE `id`=%s LIMIT 1",
165                                         array(bigintval($id)), __FILE__, __LINE__);
166                                 list($rallye, $level, $points, $infos) = SQL_FETCHROW($result);
167                                 SQL_FREERESULT($result);
168
169                                 if (empty($infos)) $infos = '---';
170
171                                 // Prepare data for the row template
172                                 $content = array(
173                                         'sw'      => $SW,
174                                         'id'      => $id,
175                                         'level'   => $level,
176                                         'points'  => $points,
177                                         'infos'   => $infos,
178                                 );
179
180                                 // Load row template and switch color
181                                 $OUT .= loadTemplate('admin_config_rallye_del_row', true, $content);
182                                 $SW = 3 - $SW;
183                         } // END - foreach
184                         $content['rows'] = $OUT;
185
186                         // Prepare data for the main template
187                         $content['rallye'] = getRequestParameter('rallye');
188
189                         // Load main template
190                         loadTemplate('admin_config_rallye_del', false, $content);
191                 } else {
192                         // Nothing selected
193                         $content = '{--RALLYE_NO_PRICES_SELECTED_1--}<a href="{%url=modules.php?module=admin&amp;what=config_rallye_prices&amp;rallye=' . getRequestParameter('rallye') . '%}">{--RALLYE_NO_PRICES_SELECTED_2--}</a>{--RALLYE_NO_PRICES_SELECTED_3--}';
194                         loadTemplate('admin_settings_saved', false, $content);
195                 }
196         } else {
197                 // a rallye was selected, so check if there are already prices assigned...
198                 $result = SQL_QUERY_ESC("SELECT `id`, `price_level`, `points`, `info` FROM `{?_MYSQL_PREFIX?}_rallye_prices` WHERE `rallye_id`=%s ORDER BY `price_level` ASC",
199                         array(bigintval(getRequestParameter('rallye'))), __FILE__, __LINE__);
200
201                 if (SQL_NUMROWS($result) > 0) {
202                         // Load all prices for the selected rallye
203                         $OUT = ''; $SW = 2;
204                         while ($content = SQL_FETCHARRAY($result)) {
205                                 // Fix empty info
206                                 if (empty($content['info'])) $content['info'] = '---';
207
208                                 // Prepare data for the row template
209                                 // @TODO Rewritings: level->price_level, infos->info in template
210                                 $content = array(
211                                         'sw'      => $SW,
212                                         'id'      => $content['id'],
213                                         'level'   => $content['price_level'],
214                                         'points'  => $content['points'],
215                                         'infos'   => $content['info'],
216                                 );
217
218                                 // Load row template and switch color
219                                 $OUT .= loadTemplate('admin_config_rallye_prices_row', true, $content);
220                                 $SW = 3 - $SW;
221                         } // END - while
222
223                         // Free memory
224                         SQL_FREERESULT($result);
225
226                         // @TODO Rewrite these two constants
227                         $content['rows'] = $OUT;
228
229                         // Prepare data for the main template
230                         $content['rallye'] = getRequestParameter('rallye');
231
232                         // Load main template
233                         loadTemplate('admin_config_rallye_prices', false, $content);
234                 } // END - if
235         }
236
237         // Add form for adding new price level
238         if (!isFormSent('edit')) {
239                 loadTemplate('admin_add_rallye_prices', false, getRequestParameter('rallye'));
240         } // END - if
241 } else {
242         // No rallye selected so display all available without prices
243         $result = SQL_QUERY("SELECT
244         d.id, d.admin_id, d.start_time, d.end_time, d.title, a.login, d.is_active
245 FROM
246         `{?_MYSQL_PREFIX?}_rallye_data` AS d
247 LEFT JOIN
248         `{?_MYSQL_PREFIX?}_admins` AS a
249 ON
250         d.admin_id=a.id
251 ORDER BY
252         d.start_time DESC", __FILE__, __LINE__);
253         if (SQL_NUMROWS($result) > 0) {
254                 // List found rallyes
255                 $OUT = ''; $SW = 2;
256                 while ($content = SQL_FETCHARRAY($result)) {
257                         // Prepare data for the row template
258                         $content = array(
259                                 'sw'         => $SW,
260                                 'id'         => $content['id'],
261                                 'title'      => $content['title'],
262                                 'admin_id'   => $content['admin_id'],
263                                 'admin_link' => generateAdminLink($content['admin_id']),
264                                 'login'      => $content['login'],
265                                 'start'      => generateDateTime($content['start_time'], 3),
266                                 'end'        => generateDateTime($content['end_time'], 3),
267                         );
268
269                         // Load row template and switch color
270                         $OUT .= loadTemplate('admin_list_rallye_prices_row', true, $content);
271                         $SW = 3 - $SW;
272                 } // END - while
273
274                 // Free memory
275                 SQL_FREERESULT($result);
276
277                 // Load main template
278                 loadTemplate('admin_list_rallye_prices', false, $OUT);
279         } else {
280                 // No rallyes setup so far
281                 loadTemplate('admin_settings_saved', false, getMessage('RALLYE_NO_RALLYES_SETUP'));
282         }
283 }
284
285 // [EOF]
286 ?>