Two tickets resolved (57/89)
[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 ((isset($_POST['add'])) && (!empty($_POST['code'])) && (!empty($_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($_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(strtoupper(substr($_POST['code'], 0, 2)), $_POST['descr'], $_POST['is_active']), __FILE__, __LINE__);
52
53                 // Country added
54                 $MSG = ADMIN_COUNTRY_ADDED_1.strtoupper($_POST['descr']).ADMIN_COUNTRY_ADDED_2;
55         } else {
56                 // Free memory
57                 SQL_FREERESULT($result);
58
59                 // Does already exist
60                 $MSG = ADMIN_COUNTRY_ALREADY_1.strtoupper($_POST['code']).ADMIN_COUNTRY_ALREADY_2;
61         }
62
63         // Display message
64         LOAD_TEMPLATE("admin_settings_saved", false, $MSG);
65 } elseif ((isset($_POST['change'])) && (!empty($_POST['id']))) {
66         // Change all status
67         ADMIN_CHANGE_ACTIVATION_STATUS($_POST['id'], "countries", "is_active");
68 } elseif (((isset($_POST['edit'])) || (isset($_POST['delete']))) && (!empty($_POST['id']))) {
69         if (count($_POST['id']) > 0) {
70                 if (isset($_POST['edit'])) {
71                         // Edit template
72                         $row    = "admin_list_country_edit_row";
73                         $post   = "modify";
74                         $class  = "admin_submit";
75                         $submit = ADMIN_COUNTRY_EDIT_NOW;
76                         $title  = ADMIN_COUNTRY_EDIT_TITLE;
77                         $reset  = "<input type=\"reset\" class=\"admin_reset\" value=\"{!UNDO_SELECTIONS!}\" />&nbsp;*\n";
78                 } else {
79                         // Delete template
80                         $row    = "admin_list_country_del_row";
81                         $post   = "remove";
82                         $class  = "admin_delete";
83                         $submit = ADMIN_COUNTRY_DELETE_NOW;
84                         $title  = ADMIN_COUNTRY_DELETE_TITLE;
85                         $reset  = "";
86                 }
87
88                 // Edit all selected country codes
89                 $OUT = ""; $SW = 2;
90                 foreach ($_POST['id'] as $id => $status) {
91                         // Load data from DB
92                         $result = SQL_QUERY_ESC("SELECT code, descr FROM `{!_MYSQL_PREFIX!}_countries` WHERE id=%s LIMIT 1",
93                                 array(bigintval($id)), __FILE__, __LINE__);
94                         if (SQL_NUMROWS($result) == 1) {
95                                 // Load data
96                                 list($code, $descr) = SQL_FETCHROW($result);
97                                 SQL_FREERESULT($result);
98
99                                 // Prepare data, load row template and switch colors
100                                 $content = array(
101                                         'id'    => $id,
102                                         'code'  => $code,
103                                         'descr' => $descr,
104                                         'sw'    => $SW,
105                                 );
106
107                                 if ($post == "modify") {
108                                         // Generate default selection in edit-mode
109                                         $content['status'] = ADD_OPTION_LINES("/ARRAY/", array("Y","N"), array(YES, NO ), $status);
110                                 } else {
111                                         // Only display status when in delete-mode
112                                         $content['status'] = TRANSLATE_YESNO($status);
113                                 }
114
115                                 // Insert row template and switch color
116                                 $OUT .= LOAD_TEMPLATE($row, true, $content);
117                                 $SW = 3 - $SW;
118                         }
119                 }
120                 define('__COUNTRY_ROWS'  , $OUT);
121                 define('__COUNTRY_MODE'  , $post);
122                 define('__COUNTRY_CLASS' , $class);
123                 define('__COUNTRY_SUBMIT', $submit);
124                 define('__COUNTRY_TITLE' , $title);
125                 define('__COUNTRY_RESET' , $reset);
126
127                 // Load main template
128                 LOAD_TEMPLATE("admin_list_country_form");
129         }
130 } else {
131         // Shall we modify / remove entries now?
132         $MSG = ""; $SQLs = array();
133         if ((isset($_POST['modify'])) && (!empty($_POST['id']))) {
134                 // Modify
135                 foreach ($_POST['id'] as $id => $sel) {
136                         $SQLs[] = "UPDATE `{!_MYSQL_PREFIX!}_countries` SET code='".$_POST['code'][$id]."', descr='".$_POST['descr'][$id]."', is_active='".$_POST['is_active'][$id]."' WHERE id='".$id."' LIMIT 1";
137                 }
138
139                 // Create message
140                 $MSG = ADMIN_COUNTRIES_MODIFIED;
141         } elseif ((isset($_POST['remove'])) && (!empty($_POST['id']))) {
142                 // Remove
143                 $IDs = implode(",", array_keys($_POST['id']));
144                 $SQLs[] = "DELETE LOW_PRIORITY FROM `{!_MYSQL_PREFIX!}_countries` WHERE id IN (".$IDs.") LIMIT ".count($_POST['id'])."";
145
146                 // Create message
147                 $MSG = ADMIN_COUNTRIES_REMOVED;
148         }
149
150         if ((!empty($MSG)) && (count($SQLs) > 0)) {
151                 // Run SQL commands
152                 foreach ($SQLs as $sql) {
153                         $result = SQL_QUERY($sql, __FILE__, __LINE__);
154                 }
155
156                 // Display message
157                 LOAD_TEMPLATE("admin_settings_saved", false, $MSG);
158         }
159
160         // Load currenty setup country codes to list
161         $result = SQL_QUERY("SELECT id, code, descr, is_active FROM `{!_MYSQL_PREFIX!}_countries` ORDER BY code", __FILE__, __LINE__);
162         if (SQL_NUMROWS($result) > 0) {
163                 // List all countries
164                 $OUT = ""; $SW = 2;
165                 while (list($id, $code, $descr, $active) = SQL_FETCHROW($result)) {
166                         // Prepare array for the template
167                         $content = array(
168                                 'id'     => $id,
169                                 'code'   => $code,
170                                 'name'   => $descr,
171                                 'active' => TRANSLATE_YESNO($active),
172                                 'sw'     => $SW,
173                                 'status' => $active,
174                         );
175
176                         // Load row template and switch colors
177                         $OUT .= LOAD_TEMPLATE("admin_list_country_row", true, $content);
178                         $SW = 3 - $SW;
179                 }
180
181                 // Free memory
182                 SQL_FREERESULT($result);
183         } else {
184                 // No code setup so far (not possible by this software! 'DE' for 'Deutschland' is default
185                 $OUT = LOAD_TEMPLATE("admin_list_country_no_row", true);
186         }
187
188         // Add list to constant for the template
189         define('__COUNTRY_ROWS', $OUT);
190
191         // Include add template
192         define('__ADD_COUNTRY_FORM', LOAD_TEMPLATE("admin_add_country", true));
193
194         // Load main template
195         LOAD_TEMPLATE("admin_list_country");
196 }
197
198 //
199 ?>