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