Complete rewrite of and , wrapper functions added, see bug #101
[mailer.git] / inc / modules / admin / what-list_sponsor_pay.php
1 <?php
2 /************************************************************************
3  * M-XChange v0.2.1                                   Start: 06/10/2005 *
4  * ================                             Last change: 05/19/2008 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-list_sponsor_pay.php                        *
8  * -------------------------------------------------------------------- *
9  * Short description : List/edit/delete all payments and add new        *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Auflisten/Aendern/Loeschen aller Buchungspakete  *
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 $MSG = "";
44
45 if (REQUEST_ISSET_POST(('add'))) {
46         // Check input variables
47         if (!REQUEST_ISSET_POST(('pay_name'))) REQUEST_UNSET_POST(('add'));
48         if ((round(REQUEST_POST('pay_rate')) == 0) || (!REQUEST_ISSET_POST(('pay_rate')))) REQUEST_UNSET_POST(('add'));
49
50         REQUEST_SET_POST('pay_min_count', bigintval(REQUEST_POST('pay_min_count')));
51
52         if ((REQUEST_POST('pay_min_count') == 0) || (!REQUEST_ISSET_POST(('pay_min_count')))) REQUEST_UNSET_POST(('add'));
53         if (!REQUEST_ISSET_POST(('pay_currency'))) REQUEST_UNSET_POST(('add'));
54
55 } elseif ((REQUEST_ISSET_POST(('edit'))) || (REQUEST_ISSET_POST(('del'))) || (REQUEST_ISSET_POST(('change'))) || (REQUEST_ISSET_POST(('remove')))) {
56         // Check if at least one entry was selected
57         if (!REQUEST_ISSET_POST(('id'))) {
58                 // Nothing selected for editing / deleting???
59                 REQUEST_UNSET_POST(('edit'));
60                 REQUEST_UNSET_POST(('del'));
61                 REQUEST_UNSET_POST(('change'));
62                 REQUEST_UNSET_POST(('remove'));
63         } elseif (REQUEST_ISSET_POST(('change'))) {
64                 // Change entries here...
65                 foreach (REQUEST_POST('id') as $id => $sel) {
66                         // Secure ID
67                         $id = bigintval($id);
68
69                         // Save entry
70                         SQL_QUERY_ESC("UPDATE `{!_MYSQL_PREFIX!}_sponsor_paytypes`
71 SET pay_name='%s', pay_rate='%s', pay_min_count='%s', pay_currency='%s' WHERE id='%s' LIMIT 1",
72                                 array(
73                                         REQUEST_POST('name', $id),
74                                         REQUEST_POST('rate', $id),
75                                         bigintval(REQUEST_POST('min', $id)),
76                                         REQUEST_POST('curr', $id),
77                                         $id
78                                 ), __FILE__, __LINE__);
79                 }
80
81                 // Generate message
82                 $MSG = getMessage('SPONSOR_PAY_ENTRIES_CHANGED');
83         } elseif (REQUEST_ISSET_POST(('remove'))) {
84                 // Remove entries here...
85                 foreach (REQUEST_POST('id') as $id => $sel) {
86                         // Remove entry
87                         SQL_QUERY_ESC("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_sponsor_paytypes` WHERE id='%s' LIMIT 1",
88                                 array(bigintval($id)), __FILE__, __LINE__);
89                 }
90
91                 // Generate message
92                 $MSG = getMessage('SPONSOR_PAY_ENTRIES_REMOVED');
93         }
94
95         if (!empty($MSG)) {
96                 // Output message
97                 LOAD_TEMPLATE("admin_settings_saved", false, $MSG);
98         }
99 }
100
101 if (REQUEST_ISSET_POST(('add'))) {
102         // Check if entry with same name does exists
103         $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_sponsor_paytypes` WHERE pay_name='%s' LIMIT 1",
104                 array(REQUEST_POST('pay_name')), __FILE__, __LINE__);
105         if (SQL_NUMROWS($result) == 0) {
106                 // No entry found so add this line
107                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_sponsor_paytypes` (pay_name, pay_rate, pay_min_count, pay_currency) VALUES ('%s','%s','%s','%s')",
108                         array(
109                                 htmlspecialchars(REQUEST_POST('pay_name')),
110                                 REVERT_COMMA(REQUEST_POST('pay_rate')),
111                                 bigintval(REQUEST_POST('pay_min_count')),
112                                 htmlspecialchars(REQUEST_POST('pay_currency'))
113                         ), __FILE__, __LINE__);
114
115                 // Payment type added!
116                 $MSG = ADMIN_SPONSOR_PAYTYPE_ADDED_1.REQUEST_POST('pay_name').ADMIN_SPONSOR_PAYTYPE_ADDED_2;
117         } else {
118                 // Free memory
119                 SQL_FREERESULT($result);
120
121                 // Entry does already exists
122                 $MSG = ADMIN_SPONSOR_PAYTYPE_ALREADY_1.REQUEST_POST('pay_name').ADMIN_SPONSOR_PAYTYPE_ALREADY_2;
123         }
124
125         // Output message
126         LOAD_TEMPLATE("admin_settings_saved", false, $MSG);
127 } elseif ((REQUEST_ISSET_POST(('edit'))) || (REQUEST_ISSET_POST(('del')))) {
128         // Load all data
129         $OUT = ""; $SW = 2;
130         foreach (REQUEST_POST('id') as $id => $sel) {
131                 // Load entry
132                 $result = SQL_QUERY_ESC("SELECT pay_name, pay_rate, pay_min_count, pay_currency FROM `{!_MYSQL_PREFIX!}_sponsor_paytypes` WHERE id='%s' LIMIT 1",
133                  array(bigintval($id)), __FILE__, __LINE__);
134                 if (SQL_NUMROWS($result) == 1) {
135                         // Load data
136                         list($name, $rate, $min, $curr) = SQL_FETCHROW($result);
137                         SQL_FREERESULT($result);
138
139                         // Transfer data to array
140                         $content = array(
141                                 'id'   => bigintval($id),
142                                 'sw'   => bigintval($SW),
143                                 'name' => htmlspecialchars($name),
144                                 'rate' => TRANSLATE_COMMA($rate),
145                                 'min'  => bigintval($min),
146                                 'curr' => htmlspecialchars($curr)
147                         );
148
149                         if (REQUEST_ISSET_POST(('edit'))) {
150                                 // Edit entry
151                                 $OUT .= LOAD_TEMPLATE("admin_list_sponsor_pay_edit_row", true, $content);
152                         } else {
153                                 // Delete entry
154                                 $OUT .= LOAD_TEMPLATE("admin_list_sponsor_pay_del_row", true, $content);
155                         }
156                 } else {
157                         // Entry invalid
158                         $OUT .= LOAD_TEMPLATE("admin_list_sponsor_pay_404", true, $id);
159                 }
160
161                 // Switch colors
162                 $SW = 3 - $SW;
163         }
164
165         // Remember content in constant
166         define('__SPONSOR_ROWS', $OUT);
167
168         // Load main template depending on mode (edit/delete)
169         if (REQUEST_ISSET_POST(('edit'))) {
170                 // Load main edit template
171                 LOAD_TEMPLATE("admin_list_sponsor_pay_edit");
172         } else {
173                 // Load main delete template
174                 LOAD_TEMPLATE("admin_list_sponsor_pay_del");
175         }
176 } else {
177         // Load all payment types
178         $result = SQL_QUERY("SELECT id, pay_name, pay_rate, pay_min_count, pay_currency FROM `{!_MYSQL_PREFIX!}_sponsor_paytypes` ORDER BY pay_name",
179          __FILE__, __LINE__);
180
181         // Do we have some paytypes setup?
182         if (SQL_NUMROWS($result) > 0) {
183                 // Prepare variables for listing
184                 $SW = 2; $OUT = "";
185
186                 // List alle found payment types
187                 while (list($id, $name, $rate, $min, $currency) = SQL_FETCHROW($result)) {
188                         // Remember data in array
189                         $content = array(
190                                 'sw'       => $SW,
191                                 'id'       => $id,
192                                 'name'     => $name,
193                                 'rate'     => TRANSLATE_COMMA($rate),
194                                 'min'      => $min,
195                                 'currency' => $currency
196                         );
197
198                         // Add row
199                         $OUT .= LOAD_TEMPLATE("admin_list_sponsor_pay_row", true, $content);
200
201                         // Switch colors
202                         $SW = 3 - $SW;
203                 }
204
205                 // Free memory
206                 SQL_FREERESULT($result);
207
208                 // Remember rows in constant for the template
209                 define('__LIST_ROWS', $OUT);
210
211                 // Load list template
212                 define('__LIST_CONTENT', LOAD_TEMPLATE("admin_list_sponsor_pay", true));
213         } else {
214                 // Noting setup so far!
215                 define('__LIST_CONTENT', LOAD_TEMPLATE("admin_settings_saved", true, getMessage('ADMIN_SPONSOR_NO_PAYTYPES')));
216         }
217
218         // Add new payment types here
219         define('__ADD_CONTENT', LOAD_TEMPLATE("admin_add_sponsor_paytype", true));
220
221         // Load final template
222         LOAD_TEMPLATE("admin_sponsor_paytypes");
223 }
224
225 //
226 ?>