/[drupal]/contributions/modules/country_code/country_code.admin.inc
ViewVC logotype

Contents of /contributions/modules/country_code/country_code.admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.37 - (show annotations) (download) (as text)
Wed Nov 12 14:41:59 2008 UTC (12 months, 1 week ago) by snpower
Branch: MAIN
CVS Tags: HEAD
Changes since 1.36: +20 -10 lines
File MIME type: text/x-php
Removed country_code_country_list() function - now using
site_country_country_list() function provided by site_country module.
Added in a hook_form_alter() on the system_site_settings form so we can update
the country_code_country table if a new site default country is set on
admin/settings/site-information.
Now preventing users from the deleting the last (and hence default) country -
both by removing the 'delete' link and by preventing the operation.
1 <?php
2 // $Id: country_code.admin.inc,v 1.36 2008/10/30 17:49:12 nedjo Exp $
3
4 /**
5 * @file
6 * Admin page callbacks for the country_code module.
7 */
8
9 /**
10 * User interface for the country overview screen.
11 */
12 function country_code_admin_overview(&$form_state) {
13 $countries = country_code_countries(FALSE);
14
15 $options = array();
16 $enabled = array();
17 $form = array();
18 foreach ($countries as $code => $country) {
19 $options[$code] = '';
20 if ($country->enabled) {
21 $enabled[] = $code;
22 }
23 $form['name'][$code] = array(
24 '#value' => check_plain($country->name),
25 );
26 $languages = array();
27 foreach (country_code_languages($code, FALSE) as $language) {
28 $languages[] = $language->name;
29 }
30 $form['language'][$code] = array(
31 '#value' => implode(', ', $languages),
32 );
33 }
34 $form['enabled'] = array(
35 '#type' => 'checkboxes',
36 '#options' => $options,
37 '#default_value' => $enabled,
38 );
39 $form['site_default'] = array(
40 '#type' => 'radios',
41 '#options' => $options,
42 '#default_value' => variable_get('site_country_default_country', ''),
43 );
44 $form['submit'] = array(
45 '#type' => 'submit',
46 '#value' => t('Save configuration'),
47 );
48 return $form;
49 }
50
51 /**
52 * Theme the country overview form.
53 *
54 * @ingroup themeable
55 */
56 function theme_country_code_admin_overview($form) {
57 if (isset($form['name']) && is_array($form['name'])) {
58 foreach ($form['name'] as $key => $element) {
59 if ($key == variable_get('site_country_default_country', '')) {
60 $form['enabled'][$key]['#attributes']['disabled'] = 'disabled';
61 }
62 // Do not take form control structures.
63 if (is_array($element) && element_child($key)) {
64 $delete = l(t('delete'), 'admin/settings/country-code/delete/'. $key);
65 // Remove delete button if it's the last (and hence default) country.
66 if (count(country_code_countries(FALSE)) == 1) {
67 $delete = '';
68 }
69 $rows[] = array(
70 drupal_render($form['enabled'][$key]),
71 drupal_render($form['name'][$key]),
72 drupal_render($form['site_default'][$key]),
73 drupal_render($form['language'][$key]),
74 l(t('edit'), 'admin/settings/country-code-country/'. $key) . ' ' . $delete,
75 );
76 }
77 }
78 }
79 else {
80 $rows[] = array(array('data' => t('No countries available.'), 'colspan' => '5'));
81 }
82
83 $header = array(
84 array('data' => t('Enabled')),
85 array('data' => t('Country')),
86 array('data' => t('Default')),
87 array('data' => t('Languages')),
88 array('data' => t('Operations')),
89 );
90
91 $output = theme('table', $header, $rows);
92 $output .= drupal_render($form);
93
94 return $output;
95 }
96
97 /**
98 * Process country overview form submissions, updating existing countries.
99 */
100 function country_code_admin_overview_submit($form, &$form_state) {
101 $countries = country_code_countries(FALSE);
102 $enabled_count = 0;
103 foreach ($countries as $code => $country) {
104 if ($form_state['values']['site_default'] == $code || $code == variable_get('site_country_default_country', '')) {
105 // Automatically enable the default country and the country
106 // which was default previously (because we will not get the
107 // value from that disabled checkox).
108 $form_state['values']['enabled'][$code] = 1;
109 }
110 if ($form_state['values']['enabled'][$code]) {
111 $enabled_count++;
112 $country->enabled = 1;
113 }
114 else {
115 $country->enabled = 0;
116 // If we're disabling the active country, reset cached data so we won't end up
117 // at a now-inaccessible path.
118 if ($code == country_code()) {
119 _country_code_reset();
120 }
121 }
122 db_query("UPDATE {country_code_country} SET enabled = %d WHERE country = '%s'", $country->enabled, $code);
123 $countries[$code] = $country;
124 }
125 variable_set('site_country_default_country', $form_state['values']['site_default']);
126 variable_set('country_code_count', $enabled_count);
127 drupal_set_message(t('Configuration saved.'));
128 $form_state['redirect'] = 'admin/settings/country-code';
129 return;
130 }
131
132 /**
133 * Add country form.
134 */
135 function country_code_admin_country_form(&$form_state, $country = NULL) {
136 $form = array();
137
138 if (isset($country)) {
139 $form['country'] = array(
140 '#type' => 'value',
141 '#value' => $country['country'],
142 );
143 drupal_set_title(t('Editing country %country', array('%country' => country_code_country_name($country['country']))));
144 }
145 else {
146 // Filter out countries already added.
147 $options = array_diff_key(site_country_country_list(), country_code_countries(FALSE));
148
149 $form['country'] = array(
150 '#type' => 'select',
151 '#title' => t('Country'),
152 '#options' => $options,
153 '#description' => t('Select the desired country from the list.'),
154 );
155 }
156
157 $default_value = isset($country['enabled']) ? $country['enabled'] : TRUE;
158 $disabled = (variable_get('site_country_default_country', '') == $country['country']) ? TRUE : FALSE;
159 $form['enabled'] = array(
160 '#type' => 'checkbox',
161 '#title' => t('Enabled'),
162 '#default_value' => $default_value,
163 '#disabled' => $disabled,
164 );
165
166 $form['submit'] = array(
167 '#type' => 'submit',
168 '#value' => isset($country['country']) ? t('Save') : t('Add country'),
169 );
170
171 return $form;
172 }
173
174 /**
175 * Add country form submit.
176 */
177 function country_code_admin_country_form_submit($form, &$form_state) {
178 $flag = country_code_country_save($form_state['values']);
179 $variables = array('%country' => country_code_country_name($form_state['values']['country']));
180 $message = ($flag == SAVED_NEW ? t('Country %country successfully added.', $variables) : t('Country %country successfully updated.', $variables));
181 $form_state['redirect'] = 'admin/settings/country-code';
182 return;
183 }
184
185 /**
186 * Confirm country deletion.
187 */
188 function country_code_admin_country_delete(&$form_state, $country) {
189 $form['country'] = array(
190 '#type' => 'value',
191 '#value' => $country,
192 );
193
194 return confirm_form($form, t('Are you sure you want to delete the country %name?', array('%name' => $country['name'])), 'admin/settings/country-code', t('Deleting a country will remove all settings associated with it. This action cannot be undone.'), t('Delete'), t('Cancel'));
195 }
196
197 /**
198 * Process country deletion submissions.
199 */
200 function country_code_admin_country_delete_submit($form, &$form_state) {
201 if ($form_state['values']['confirm']) {
202 $status = country_code_country_delete($form_state['values']['country']);
203 if ($status == 1) {
204 drupal_set_message(t('The country %country has been removed.', array('%country' => $form_state['values']['country']['name'])));
205 }
206 elseif ($status == 0) {
207 drupal_set_message(t('The country %country could not be removed as it is the default and last country.', array('%country' => $form_state['values']['country']['name'])));
208 }
209 }
210 $form_state['redirect'] = 'admin/settings/country-code';
211 return;
212 }
213
214 function country_code_admin_country_languages(&$form_state, $country) {
215 $languages = country_code_languages($country['country'], FALSE);
216
217 $form['country'] = array(
218 '#type' => 'value',
219 '#value' => $country['country'],
220 );
221
222 $form['languages']['#tree'] = TRUE;
223
224 foreach ($languages as $lang_code => $language) {
225 $form['languages'][$lang_code] = _country_code_country_languages_language($country, $language);
226 }
227
228 $form['buttons']['submit'] = array(
229 '#type' => 'submit',
230 '#value' => t('Save changes'),
231 '#disabled' => empty($languages),
232 );
233
234 return $form;
235 }
236
237 function _country_code_country_languages_language($country, $language) {
238 $form['language'] = array(
239 '#type' => 'hidden',
240 '#value' => $language->language,
241 );
242 $form['name'] = array(
243 '#type' => 'markup',
244 '#value' => $language->name,
245 );
246 $form['weight'] = array(
247 '#type' => 'weight',
248 '#default_value' => $language->weight,
249 );
250 $form['operations'] = array(
251 '#type' => 'markup',
252 '#value' => l(t('delete'), 'admin/settings/language/delete/' . $language->language, array('query' => drupal_get_destination())),
253 );
254
255 return $form;
256 }
257
258 function country_code_admin_country_languages_submit($form, &$form_state) {
259 foreach ($form_state['values']['languages'] as $language) {
260 db_query("UPDATE {languages} SET weight = %d WHERE language = '%s'", $language['weight'], $language['language']);
261 }
262 $form_state['redirect'] = 'admin/settings/country-code-country/' . $form_state['values']['country'] . '/languages';
263 return;
264 }
265
266 function theme_country_code_admin_country_languages($form) {
267 $rows = array();
268 foreach (element_children($form['languages']) as $key) {
269 $row = array();
270
271 $row[] = drupal_render($form['languages'][$key]['language']) . drupal_render($form['languages'][$key]['name']);
272
273 $form['languages'][$key]['weight']['#attributes']['class'] = 'country-code-language-weight';
274 $row[] = drupal_render($form['languages'][$key]['weight']);
275
276 $row[] = drupal_render($form['languages'][$key]['operations']);
277
278 $rows[] = array(
279 'data' => $row,
280 'class' => 'draggable',
281 );
282 }
283
284 if (count($rows) == 0) {
285 $rows[] = array(t('No languages have been added.'), '<span class="country-code-language-weight"></span>', '');
286 }
287
288 $header = array(t('Language'), t('Weight'), t('Operations'));
289 $output = theme('table', $header, $rows, array('id' => 'country-code-languages'));
290 $output .= drupal_render($form);
291
292 drupal_add_tabledrag('country-code-languages', 'order', 'self', 'country-code-language-weight');
293
294 return $output;
295 }
296
297 /**
298 * Add language form.
299 */
300 function country_code_admin_language_form(&$form_state, $country = NULL) {
301 $form = array();
302
303 $language_codes = country_code_languages($country['country'], FALSE, COUNTRY_CODE_LANGUAGE_SHORT);
304
305 // Filter to show only the languages not already added.
306 $options = array_diff_key(country_code_languages_short(), $language_codes);
307 if (empty($options)) {
308 drupal_set_message(t('No languages were found that have not already been added to the country. You may add a new language on the <a href="@language-add">add language screen</a>.', array('@language-add' => url('admin/settings/language/add'))), 'warning');
309 return $form;
310 }
311 $form['country'] = array(
312 '#type' => 'value',
313 '#value' => $country['country'],
314 );
315
316 $default_language = language_default();
317
318 $form['language'] = array(
319 '#type' => 'select',
320 '#title' => t('Language'),
321 '#default_value' => isset($country) ? $language['language'] : NULL,
322 '#options' => $options,
323 '#required' => TRUE,
324 '#description' => t("Select the desired language from the list. When you add a language, a new language will be created. For example, if you add %language_name, you will get a language %country_language_code (%country_language_name).", array('%language_name' => $default_language->name, '%country_language_code' => country_code_get_country_language($default_language->language, $country['country']), '%country_language_name' => t('@language_name (@country_name)', array('@language_name' => $default_language->name, '@country_name' => $country['name'])))),
325 );
326
327 $form['submit'] = array(
328 '#type' => 'submit',
329 '#value' => t('Add language'),
330 );
331
332 return $form;
333 }
334
335 /**
336 * Add language form submit.
337 */
338 function country_code_admin_language_form_submit($form, &$form_state) {
339 // Custom language creation.
340
341 $langcode = country_code_get_country_language($form_state['values']['language'], $form_state['values']['country'], FALSE);
342 // Only create if the language doesn't exist.
343 if (!country_code_language_exists($langcode)) {
344 $languages = language_list();
345 $language = $languages[$form_state['values']['language']];
346 // Load the language this language is based on.
347 $custom_language = array(
348 'langcode' => $langcode,
349 'name' => $language->name . ' (' . country_code_country_name($form_state['values']['country']) . ')',
350 'native' => $language->native . ' (' . $form_state['values']['country'] . ')',
351 'direction' => $language->direction,
352 'domain' => '',
353 'prefix' => strtolower($langcode),
354 );
355 locale_inc_callback('locale_add_language', $custom_language['langcode'], $custom_language['name'], $custom_language['native'], $custom_language['direction'], $custom_language['domain'], $custom_language['prefix']);
356 drupal_set_message(t('The language %language has been created and can now be used. You may customize this language on the <a href="@language-edit">language edit screen</a>.', array('%language' => t($custom_language['name']), '@language-edit' => url('admin/settings/language/edit/' . $langcode))));
357 }
358
359 $form_state['redirect'] = 'admin/settings/country-code-country/' . $form_state['values']['country'] . '/languages';
360 return;
361 }
362
363 /**
364 * Menu callback; presents the country_code settings page.
365 */
366 function country_code_admin_settings() {
367 $form = array();
368
369 $form['country'] = array(
370 '#type' => 'fieldset',
371 '#title' => t('Country settings'),
372 '#collapsible' => TRUE,
373 );
374 $form['country']['country_code_fallback'] = array(
375 '#type' => 'radios',
376 '#title' => t('Country fallback option'),
377 '#default_value' => variable_get('country_code_fallback', COUNTRY_CODE_GLOBAL),
378 '#options' => array(COUNTRY_CODE_GLOBAL => t('Global'), COUNTRY_CODE_DEFAULT => t('Default')),
379 '#description' => t('Default setting to use when no user country is detected, or where the user country is not among the enabled countries. Select "Global" to show a global version of the site (with no country-specific paths, filtering, or display). Select "Default" to use the default country.'),
380 );
381
382 $form['country']['country_code_user_country'] = array(
383 '#type' => 'radios',
384 '#title' => t('User default country'),
385 '#default_value' => variable_get('country_code_user_country', 0),
386 '#options' => array(t('Disabled'), t('Enabled')),
387 '#description' => t('Enable this option to allow users to select a default country on their user profile edit form.'),
388 );
389
390 $form['country']['country_code_external'] = array(
391 '#type' => 'radios',
392 '#title' => t('Set country by path for initial or external links'),
393 '#default_value' => variable_get('country_code_external', 0),
394 '#options' => array(t('Disabled'), t('Enabled')),
395 '#description' => t('Enable this option to have the country determined through the path prefix when the site is first visited or is visited from other sites. Disable this option to have the default country selection used (based on user preferred country or IP detection).'),
396 );
397
398 $handlers = module_invoke_all('country_code_handler');
399 // Only show element for selecting a handler if there is more than one
400 // to select from.
401 if (count($handlers) > 1) {
402 $options = array();
403 foreach ($handlers as $code => $handler) {
404 $options[$code] = $handler['title'];
405 }
406
407 $form['country']['country_code_handler'] = array(
408 '#type' => 'radios',
409 '#title' => t('Set country by path for initial or external links'),
410 '#default_value' => variable_get('country_code_handler', 'country_code_hostip'),
411 '#options' => $options,
412 '#description' => t('Select a handler for country code determination based on IP.'),
413 );
414 }
415
416 $form['language'] = array(
417 '#type' => 'fieldset',
418 '#title' => t('Language settings'),
419 '#collapsible' => TRUE,
420 );
421
422 $form['language']['country_code_user_language'] = array(
423 '#type' => 'radios',
424 '#title' => t('User default language'),
425 '#default_value' => variable_get('country_code_user_language', 1),
426 '#options' => array(t('Disabled'), t('Enabled')),
427 '#description' => t('Enable this option to allow users to select a default language on their user profile edit form. If both this option and <em>User default country</em> are enabled, users will be offered the languages for the country they have selected.'),
428 );
429
430 $form['language']['country_code_browser_language'] = array(
431 '#type' => 'radios',
432 '#title' => t('Consider user browser language'),
433 '#default_value' => variable_get('country_code_browser_language', 1),
434 '#options' => array(t('Disabled'), t('Enabled')),
435 '#description' => t("Select this option to have the user's browser language set as the language, if that language is available for the user's country."),
436 );
437
438 $form['links'] = array(
439 '#type' => 'fieldset',
440 '#title' => t('Links'),
441 '#collapsible' => TRUE,
442 );
443 $form['links']['country_code_node_links'] = array(
444 '#type' => 'radios',
445 '#title' => t('Show translations in node links'),
446 '#default_value' => variable_get('country_code_node_links', 1),
447 '#options' => array(t('Disabled'), t('Enabled')),
448 '#description' => t('Choose whether to show links to translations in node links.'),
449 );
450
451 return system_settings_form($form);
452 }

  ViewVC Help
Powered by ViewVC 1.1.2