A lot while() conditions rewritten to SQL_FETCHARRAY(), see bug #107, @TODO tags...
[mailer.git] / inc / modules / admin / what-list_country.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 04/30/2005 *
4  * ================                             Last change: 04/30/2005 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : what-list_country.php                            *
8  * -------------------------------------------------------------------- *
9  * Short description : Manage country codes                             *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Country codes verwalten                          *
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 // Add new code?
44 if ((REQUEST_ISSET_POST(('add'))) && (REQUEST_ISSET_POST(('code'))) && (REQUEST_ISSET_POST(('descr')))) {
45         // Check if country code does already exist
46         $result = SQL_QUERY_ESC("SELECT id FROM `{!_MYSQL_PREFIX!}_countries` WHERE code='%s' LIMIT 1",
47          array(strtoupper(REQUEST_POST('code'))), __FILE__, __LINE__);
48         if (SQL_NUMROWS($result) == 0) {
49                 // Save entry
50                 SQL_QUERY_ESC("INSERT INTO `{!_MYSQL_PREFIX!}_countries` (code, descr, is_active) VALUES ('%s','%s','%s')",
51                         array(
52                                 strtoupper(substr(REQUEST_POST('code'), 0, 2)),
53                                 REQUEST_POST('descr'),
54                                 REQUEST_POST('is_active')
55                         ), __FILE__, __LINE__);
56
57                 // Country added
58                 $MSG = ADMIN_COUNTRY_ADDED_1.strtoupper(REQUEST_POST('descr')).ADMIN_COUNTRY_ADDED_2;
59         } else {
60                 // Free memory
61                 SQL_FREERESULT($result);
62
63                 // Does already exist
64                 $MSG = ADMIN_COUNTRY_ALREADY_1.strtoupper(REQUEST_POST('code')).ADMIN_COUNTRY_ALREADY_2;
65         }
66
67         // Display message
68         LOAD_TEMPLATE("admin_settings_saved", false, $MSG);
69 } elseif ((REQUEST_ISSET_POST(('change'))) && (REQUEST_ISSET_POST(('id')))) {
70         // Change all status
71         ADMIN_CHANGE_ACTIVATION_STATUS(REQUEST_POST('id'), "countries", "is_active");
72
73         // Show next link
74         LOAD_TEMPLATE("admin_next_link", false, array(
75                 'url'   => "modules.php?module=admin&amp;what=list_country",
76                 'title' => getMessage('ADMIN_COUNTRY_ACTIVATION_NEXT_LINK')
77         ));
78 } elseif (((REQUEST_ISSET_POST('edit')) || (REQUEST_ISSET_POST(('delete')))) && (REQUEST_ISSET_POST(('id')))) {
79         if (count(REQUEST_POST('id')) > 0) {
80                 if (REQUEST_ISSET_POST('edit')) {
81                         // Edit template
82                         $row    = "admin_list_country_edit_row";
83                         $post   = "modify";
84                         $class  = "admin_submit";
85                         $submit = ADMIN_COUNTRY_EDIT_NOW;
86                         $title  = ADMIN_COUNTRY_EDIT_TITLE;
87                         $reset  = "<input type=\"reset\" class=\"admin_reset\" value=\"{!UNDO_SELECTIONS!}\" />&nbsp;*\n";
88                 } else {
89                         // Delete template
90                         $row    = "admin_list_country_del_row";
91                         $post   = "remove";
92                         $class  = "admin_delete";
93                         $submit = ADMIN_COUNTRY_DELETE_NOW;
94                         $title  = ADMIN_COUNTRY_DELETE_TITLE;
95                         $reset  = "";
96                 }
97
98                 // Edit all selected country codes
99                 $OUT = ""; $SW = 2;
100                 foreach (REQUEST_POST('id') as $id => $status) {
101                         // Load data from DB
102                         $result = SQL_QUERY_ESC("SELECT code, descr FROM `{!_MYSQL_PREFIX!}_countries` WHERE id=%s LIMIT 1",
103                                 array(bigintval($id)), __FILE__, __LINE__);
104                         if (SQL_NUMROWS($result) == 1) {
105                                 // Load data
106                                 list($code, $descr) = SQL_FETCHROW($result);
107                                 SQL_FREERESULT($result);
108
109                                 // Prepare data, load row template and switch colors
110                                 $content = array(
111                                         'id'    => $id,
112                                         'code'  => $code,
113                                         'descr' => $descr,
114                                         'sw'    => $SW,
115                                 );
116
117                                 if ($post == "modify") {
118                                         // Generate default selection in edit-mode
119                                         $content['status'] = ADD_OPTION_LINES("/ARRAY/", array("Y","N"), array(YES, NO ), $status);
120                                 } else {
121                                         // Only display status when in delete-mode
122                                         $content['status'] = TRANSLATE_YESNO($status);
123                                 }
124
125                                 // Insert row template and switch color
126                                 $OUT .= LOAD_TEMPLATE($row, true, $content);
127                                 $SW = 3 - $SW;
128                         }
129                 }
130                 define('__COUNTRY_ROWS'  , $OUT);
131                 define('__COUNTRY_MODE'  , $post);
132                 define('__COUNTRY_CLASS' , $class);
133                 define('__COUNTRY_SUBMIT', $submit);
134                 define('__COUNTRY_TITLE' , $title);
135                 define('__COUNTRY_RESET' , $reset);
136
137                 // Load main template
138                 LOAD_TEMPLATE("admin_list_country_form");
139         }
140 } else {
141         // Shall we modify / remove entries now?
142         $MSG = ""; INIT_SQLS();
143         if ((REQUEST_ISSET_POST(('modify'))) && (REQUEST_ISSET_POST(('id')))) {
144                 // Modify
145                 foreach (REQUEST_POST('id') as $id => $sel) {
146                         ADD_SQL("UPDATE `{!_MYSQL_PREFIX!}_countries` SET code='".REQUEST_POST('code', $id)."', descr='".REQUEST_POST('descr', $id)."', is_active='".REQUEST_POST('is_active', $id)."' WHERE id='".$id."' LIMIT 1");
147                 }
148
149                 // Create message
150                 $MSG = getMessage('ADMIN_COUNTRIES_MODIFIED');
151         } elseif ((REQUEST_ISSET_POST(('remove'))) && (REQUEST_ISSET_POST(('id')))) {
152                 // Remove
153                 $IDs = implode(",", array_keys(REQUEST_POST('id')));
154                 ADD_SQL("DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_countries` WHERE id IN (".$IDs.") LIMIT ".count(REQUEST_POST('id'))."");
155
156                 // Create message
157                 $MSG = getMessage('ADMIN_COUNTRIES_REMOVED');
158         }
159
160         if ((!empty($MSG)) && (COUNT_SQLS() > 0)) {
161                 // Run SQL commands
162                 runFilterChain('run_sqls');
163
164                 // Display message
165                 LOAD_TEMPLATE("admin_settings_saved", false, $MSG);
166         }
167
168         // Load currenty setup country codes to list
169         $result = SQL_QUERY("SELECT id, code, descr, is_active FROM `{!_MYSQL_PREFIX!}_countries` ORDER BY code", __FILE__, __LINE__);
170         if (SQL_NUMROWS($result) > 0) {
171                 // List all countries
172                 $OUT = ""; $SW = 2;
173                 while ($content = SQL_FETCHARRAY($result)) {
174                         // Prepare array for the template
175                         // @TODO Rewritings: name->descr,status->is_active in template
176                         $content = array(
177                                 'id'     => $content['id'],
178                                 'code'   => $content['code'],
179                                 'name'   => $content['descr'],
180                                 'active' => TRANSLATE_YESNO($content['is_active']),
181                                 'sw'     => $SW,
182                                 'status' => $content['is_active'],
183                         );
184
185                         // Load row template and switch colors
186                         $OUT .= LOAD_TEMPLATE("admin_list_country_row", true, $content);
187                         $SW = 3 - $SW;
188                 }
189
190                 // Free memory
191                 SQL_FREERESULT($result);
192         } else {
193                 // No code setup so far (not possible by this software! 'DE' for 'Deutschland' is default
194                 $OUT = LOAD_TEMPLATE("admin_list_country_no_row", true);
195         }
196
197         // Add list to constant for the template
198         define('__COUNTRY_ROWS', $OUT);
199
200         // Include add template
201         define('__ADD_COUNTRY_FORM', LOAD_TEMPLATE("admin_add_country", true));
202
203         // Load main template
204         LOAD_TEMPLATE("admin_list_country");
205 }
206
207 //
208 ?>