| 1 |
<?php
|
| 2 |
// $Id: addresses.settings.inc,v 1.24 2009/01/20 20:39:49 brmassa Exp $
|
| 3 |
/**
|
| 4 |
* @author Bruno Massa
|
| 5 |
* @file
|
| 6 |
* All settings functions for Addresses module.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* It splits the _addresses_addressesfieldapi_form into more pieces,
|
| 11 |
* reducing the memory consumption on non related pages.
|
| 12 |
*
|
| 13 |
* @ingroup form
|
| 14 |
*/
|
| 15 |
function _addresses_addressesfieldapi_form($fields = array(), $values = array()) {
|
| 16 |
$field_weights = variable_get('addresses_field_weight', array());
|
| 17 |
|
| 18 |
// Let users select if the address is the primary
|
| 19 |
if (!empty($fields['is_primary'])) {
|
| 20 |
$form['is_primary'] = array(
|
| 21 |
'#type' => 'checkbox',
|
| 22 |
'#title' => t('Default address'),
|
| 23 |
'#default_value' => isset($values['is_primary']) ? $values['is_primary'] : '',
|
| 24 |
'#weight' => empty($field_weights['is_primary']['weight']) ? 0 :
|
| 25 |
$field_weights['is_primary']['weight'],
|
| 26 |
);
|
| 27 |
}
|
| 28 |
|
| 29 |
// Adds the Address Name (Home, Office, Rio de Janeiro...)
|
| 30 |
if (!empty($fields['aname'])) {
|
| 31 |
$form['aname'] = array(
|
| 32 |
'#type' => 'textfield',
|
| 33 |
'#title' => t('Address name'),
|
| 34 |
'#default_value' => isset($values['aname']) ? $values['aname'] : '',
|
| 35 |
'#size' => 50,
|
| 36 |
'#maxlength' => 75,
|
| 37 |
'#description' => t('e.g. a place of business, venue, meeting point'),
|
| 38 |
'#attributes' => NULL,
|
| 39 |
'#required' => ($fields['aname'] == ADDRESSES_FIELD_REQUIRED),
|
| 40 |
'#weight' => empty($field_weights['aname']['weight']) ? 0 :
|
| 41 |
$field_weights['aname']['weight'],
|
| 42 |
);
|
| 43 |
}
|
| 44 |
|
| 45 |
// Adds the Country
|
| 46 |
if (!empty($fields['country'])) {
|
| 47 |
// Get a list of enabled countries
|
| 48 |
$countries = _addresses_country_get(variable_get('addresses_country_list', array()));
|
| 49 |
if (count($countries) > 1) {
|
| 50 |
$countries = array('' => '') + $countries;
|
| 51 |
}
|
| 52 |
|
| 53 |
$form['country'] = array(
|
| 54 |
'#type' => 'select',
|
| 55 |
'#title' => t('Country'),
|
| 56 |
'#default_value' => isset($values['country']) ? $values['country'] : '',
|
| 57 |
'#options' => $countries,
|
| 58 |
'#description' => NULL,
|
| 59 |
'#extra' => 0,
|
| 60 |
'#multiple' => FALSE,
|
| 61 |
'#required' => ($fields['country'] == ADDRESSES_FIELD_REQUIRED),
|
| 62 |
'#weight' => empty($field_weights['country']['weight']) ? 0 :
|
| 63 |
$field_weights['country']['weight'],
|
| 64 |
);
|
| 65 |
}
|
| 66 |
|
| 67 |
// Adds the Province field
|
| 68 |
if (!empty($fields['province'])) {
|
| 69 |
$form['province'] = array(
|
| 70 |
'#default_value' => isset($values['province']) ? $values['province'] : '',
|
| 71 |
'#maxlength' => 16,
|
| 72 |
'#required' => ($fields['province'] == ADDRESSES_FIELD_REQUIRED),
|
| 73 |
'#size' => 16,
|
| 74 |
'#title' => t('State / Province'),
|
| 75 |
'#type' => 'textfield',
|
| 76 |
'#weight' => empty($field_weights['province']['weight']) ? 0 :
|
| 77 |
$field_weights['province']['weight'],
|
| 78 |
);
|
| 79 |
$form['#element_validate'][] = '_addresses_province_validate';
|
| 80 |
}
|
| 81 |
|
| 82 |
// Adds the City
|
| 83 |
if (!empty($fields['city'])) {
|
| 84 |
$form['city'] = array(
|
| 85 |
'#type' => 'textfield',
|
| 86 |
'#title' => t('City'),
|
| 87 |
'#default_value' => isset($values['city']) ? $values['city'] : '',
|
| 88 |
'#size' => 50,
|
| 89 |
'#maxlength' => 255,
|
| 90 |
'#required' => ($fields['city'] == ADDRESSES_FIELD_REQUIRED),
|
| 91 |
'#weight' => empty($field_weights['city']['weight']) ? 0 :
|
| 92 |
$field_weights['city']['weight'],
|
| 93 |
);
|
| 94 |
}
|
| 95 |
|
| 96 |
// Adds the Streets and the Additional fields
|
| 97 |
if (!empty($fields['street'])) {
|
| 98 |
$form['street'] = array(
|
| 99 |
'#type' => 'textfield',
|
| 100 |
'#title' => t('Street'),
|
| 101 |
'#default_value' => isset($values['street']) ? $values['street'] : '',
|
| 102 |
'#size' => 50,
|
| 103 |
'#maxlength' => 255,
|
| 104 |
'#required' => ($fields['street'] == ADDRESSES_FIELD_REQUIRED),
|
| 105 |
'#weight' => empty($field_weights['street']['weight']) ? 0 :
|
| 106 |
$field_weights['street']['weight'],
|
| 107 |
);
|
| 108 |
if (!empty($fields['additional'])) {
|
| 109 |
$form['additional'] = array(
|
| 110 |
'#type' => 'textfield',
|
| 111 |
'#title' => t('Additional'),
|
| 112 |
'#default_value' => isset($values['additional']) ? $values['additional'] : '',
|
| 113 |
'#size' => 50,
|
| 114 |
'#maxlength' => 255,
|
| 115 |
'#weight' => empty($field_weights['additional']['weight']) ? 0 :
|
| 116 |
$field_weights['additional']['weight'],
|
| 117 |
);
|
| 118 |
}
|
| 119 |
}
|
| 120 |
|
| 121 |
// Adds the Postal Code
|
| 122 |
if (!empty($fields['postal_code'])) {
|
| 123 |
$form['postal_code'] = array(
|
| 124 |
'#type' => 'textfield',
|
| 125 |
'#title' => t('Postal code'),
|
| 126 |
'#default_value' => isset($values['postal_code']) ? $values['postal_code'] : '',
|
| 127 |
'#size' => 16,
|
| 128 |
'#maxlength' => 16,
|
| 129 |
'#required' => ($fields['postal_code'] == ADDRESSES_FIELD_REQUIRED),
|
| 130 |
'#weight' => empty($field_weights['postal_code']['weight']) ? 0 :
|
| 131 |
$field_weights['postal_code']['weight'],
|
| 132 |
);
|
| 133 |
}
|
| 134 |
|
| 135 |
foreach (array_keys($fields) as $ftype) {
|
| 136 |
if ($fields[$ftype] == ADDRESSES_FIELD_HIDDEN) {
|
| 137 |
$form[$ftype] = array(
|
| 138 |
'#type' => 'hidden',
|
| 139 |
'#value' => isset($values[$ftype]) ? $values[$ftype] : '',
|
| 140 |
);
|
| 141 |
}
|
| 142 |
}
|
| 143 |
|
| 144 |
return $form;
|
| 145 |
}
|
| 146 |
|
| 147 |
/**
|
| 148 |
* Create a list of states from a given country.
|
| 149 |
*
|
| 150 |
* @param $country
|
| 151 |
* String. The country code
|
| 152 |
* @param $string
|
| 153 |
* String (optional). The state name typed by user
|
| 154 |
* @return
|
| 155 |
* Javascript array. List of states
|
| 156 |
*/
|
| 157 |
function _addresses_autocomplete($country, $string = '') {
|
| 158 |
$matches = array();
|
| 159 |
|
| 160 |
// Check if the country code is there
|
| 161 |
if ($country) {
|
| 162 |
|
| 163 |
$string = drupal_strtolower($string);
|
| 164 |
$string = '/^'. $string .'/';
|
| 165 |
|
| 166 |
$provinces = _addresses_province_get($country);
|
| 167 |
|
| 168 |
// Get only the first 5 provinces that matches
|
| 169 |
// partially with the given piece of text
|
| 170 |
if (!empty($provinces)) {
|
| 171 |
while (list($code, $name) = each($provinces)) {
|
| 172 |
if ($counter < 5) {
|
| 173 |
if (preg_match($string, drupal_strtolower($name))) {
|
| 174 |
$matches[$code] = drupal_strtolower($name);
|
| 175 |
++$counter;
|
| 176 |
}
|
| 177 |
}
|
| 178 |
}
|
| 179 |
}
|
| 180 |
}
|
| 181 |
// Print the results as a JS array.
|
| 182 |
echo drupal_to_js($matches);
|
| 183 |
|
| 184 |
// Finish the page. Its necessary to not continue
|
| 185 |
// to build a regular page
|
| 186 |
exit();
|
| 187 |
}
|
| 188 |
|
| 189 |
/**
|
| 190 |
* Validate the province field
|
| 191 |
*/
|
| 192 |
function _addresses_province_validate($form, &$form_state) {
|
| 193 |
// Check if its a valid province.
|
| 194 |
// It should be the Province code listed on the
|
| 195 |
// given COUNTRY.inc file
|
| 196 |
if ($province = $form['province']['#value']
|
| 197 |
and $country = $form['country']['#value']
|
| 198 |
and $provinces = _addresses_province_get($country)
|
| 199 |
and empty($provinces[drupal_strtoupper($province)])) {
|
| 200 |
|
| 201 |
// Get the province fieldname
|
| 202 |
$field_name = $form['province']['#name'];
|
| 203 |
$field_name = drupal_substr($field_name, 0, -1);
|
| 204 |
$field_name = preg_replace('/([^]])\[/', '\1][', $field_name);
|
| 205 |
|
| 206 |
$countries = _addresses_country_get();
|
| 207 |
form_set_error($field_name, t('Could not find %province as a province from %country. Try to use the province abbreviation or number.',
|
| 208 |
array('%province' => $form['province']['#value'],
|
| 209 |
'%country' => $countries[$country])));
|
| 210 |
|
| 211 |
}
|
| 212 |
}
|
| 213 |
|
| 214 |
/**
|
| 215 |
* Modules settings page
|
| 216 |
*
|
| 217 |
* @ingroup form
|
| 218 |
*/
|
| 219 |
function _addresses_settings(&$form_state) {
|
| 220 |
module_load_include('inc', 'addresses');
|
| 221 |
$countries = _addresses_country_get();
|
| 222 |
$country_list = variable_get('addresses_country_list', array_keys(_addresses_country_get()));
|
| 223 |
|
| 224 |
// List of countries that the site allow
|
| 225 |
$form['addresses_country_list'] = array(
|
| 226 |
'#default_value' => $country_list,
|
| 227 |
'#description' => t('You might want to limit the country lists. Select the countries you want.'),
|
| 228 |
'#multiple' => TRUE,
|
| 229 |
'#options' => $countries,
|
| 230 |
'#type' => 'select',
|
| 231 |
'#title' => t('Possible country'),
|
| 232 |
);
|
| 233 |
|
| 234 |
// Let users to change the address formats
|
| 235 |
$countries = array_intersect_key($countries, $country_list);
|
| 236 |
array_unshift($countries, t('Default'));
|
| 237 |
$countries_max = ceil(count($countries) / 4);
|
| 238 |
$country_num = 0;
|
| 239 |
foreach ($countries as $country_code => $country) {
|
| 240 |
$columns[$country_num++ / $countries_max][] = l($country, 'admin/settings/address/format/'. $country_code);
|
| 241 |
}
|
| 242 |
foreach (array_keys($columns[0]) as $row) {
|
| 243 |
$rows[] = array($columns[0][$row], $columns[1][$row], $columns[2][$row], $columns[3][$row]);
|
| 244 |
}
|
| 245 |
$form['addresses_format'] = array(
|
| 246 |
'#children' => theme('table', array(), $rows),
|
| 247 |
'#collapsed' => TRUE,
|
| 248 |
'#collapsible' => TRUE,
|
| 249 |
'#description' => t('Change how the addresses are displayed, for each country. The Default is used if the country doesnt have a preset address format.'),
|
| 250 |
'#type' => 'fieldset',
|
| 251 |
'#title' => t('Address formats'),
|
| 252 |
);
|
| 253 |
|
| 254 |
// Field weight
|
| 255 |
$fields = module_invoke_all('addressesfieldapi', 'fields');
|
| 256 |
$field_weights = variable_get('addresses_field_weight', array());
|
| 257 |
$form['addresses_field_weight'] = array(
|
| 258 |
'#collapsed' => $field_weights,
|
| 259 |
'#collapsible' => TRUE,
|
| 260 |
'#description' => t('Choose what fields should appear first.'),
|
| 261 |
'#theme' => 'addresses_field_weight',
|
| 262 |
'#title' => t('Field Order'),
|
| 263 |
'#type' => 'fieldset',
|
| 264 |
'#tree' => TRUE,
|
| 265 |
);
|
| 266 |
foreach ($fields as $field_code => $field) {
|
| 267 |
$form['addresses_field_weight'][$field_code] = array(
|
| 268 |
'#tree' => TRUE,
|
| 269 |
);
|
| 270 |
$form['addresses_field_weight'][$field_code]['field'] = array(
|
| 271 |
'#attributes' => array('class' => 'field'),
|
| 272 |
'#type' => 'markup',
|
| 273 |
'#value' => $field['title'],
|
| 274 |
);
|
| 275 |
$form['addresses_field_weight'][$field_code]['weight'] = array(
|
| 276 |
'#attributes' => array('class' => 'field'),
|
| 277 |
'#default_value' => $field_weights[$field_code]['weight'],
|
| 278 |
'#type' => 'weight',
|
| 279 |
);
|
| 280 |
}
|
| 281 |
|
| 282 |
// We will use the this function that automatically
|
| 283 |
// save all form fields into global variables
|
| 284 |
$form = system_settings_form($form);
|
| 285 |
|
| 286 |
$form['buttons']['#weight'] = 10;
|
| 287 |
return $form;
|
| 288 |
}
|
| 289 |
|
| 290 |
/**
|
| 291 |
* Generate a settings form that will allow admins to choose
|
| 292 |
* which addresses fields shuold be used.
|
| 293 |
*
|
| 294 |
* @param field_values
|
| 295 |
* Array (optional). The default values for each field, if any
|
| 296 |
* @return
|
| 297 |
* Array. A form with settings
|
| 298 |
* @ingroup form
|
| 299 |
*/
|
| 300 |
function _addresses_settings_fields($field_values = array()) {
|
| 301 |
$ftypes = module_invoke_all('addressesfieldapi', 'fields');
|
| 302 |
foreach ($ftypes as $ftype => $field) {
|
| 303 |
// List of address fields
|
| 304 |
$form['addresses'][$ftype] = array(
|
| 305 |
'#default_value' => empty($field_values[$ftype]) ? $field['display'] : $field_values[$ftype],
|
| 306 |
'#options' => array(
|
| 307 |
ADDRESSES_FIELD_NONE => '',
|
| 308 |
ADDRESSES_FIELD_SHOW => '',
|
| 309 |
ADDRESSES_FIELD_REQUIRED => '',
|
| 310 |
ADDRESSES_FIELD_HIDDEN => '',
|
| 311 |
),
|
| 312 |
'#title' => $field['title'],
|
| 313 |
'#type' => 'radios',
|
| 314 |
);
|
| 315 |
}
|
| 316 |
|
| 317 |
$form['addresses']['#theme'] = 'addresses_settings_fields';
|
| 318 |
|
| 319 |
return $form;
|
| 320 |
}
|
| 321 |
|
| 322 |
/**
|
| 323 |
* Modules settings page
|
| 324 |
*
|
| 325 |
* @ingroup form
|
| 326 |
*/
|
| 327 |
function _addresses_settings_format(&$form_state, $country) {
|
| 328 |
module_load_include('inc', 'addresses');
|
| 329 |
$countries = _addresses_country_get();
|
| 330 |
|
| 331 |
if (!$cname = $countries[$country]) {
|
| 332 |
$cname = t('Default');
|
| 333 |
$country = 'default';
|
| 334 |
}
|
| 335 |
|
| 336 |
if (!$format_address = variable_get('addresses_format_'. $country, '')) {
|
| 337 |
include_once drupal_get_path('module', 'addresses') .'/countries/us.inc';
|
| 338 |
$format_address = addresses_address_format_us();
|
| 339 |
variable_set('addresses_format_default', $format_address);
|
| 340 |
}
|
| 341 |
|
| 342 |
$form['addresses_format_'. $country] = array(
|
| 343 |
'#default_value' => $format_address,
|
| 344 |
'#title' => t('Address Format: %country', array('%country' => $cname)),
|
| 345 |
'#type' => 'textarea',
|
| 346 |
);
|
| 347 |
|
| 348 |
// Simulate theme('token_help'), but include two types and
|
| 349 |
// erase the global types
|
| 350 |
$full_list = token_get_list('addresses_general') + token_get_list('addresses_adr');
|
| 351 |
unset($full_list['global']);
|
| 352 |
$headers = array(t('Token'), t('Replacement value'));
|
| 353 |
$rows = array();
|
| 354 |
foreach ($full_list as $key => $category) {
|
| 355 |
$rows[] = array(array('data' => drupal_ucfirst($key) .' '. t('tokens'), 'class' => 'region', 'colspan' => 2));
|
| 356 |
foreach ($category as $token => $description) {
|
| 357 |
$row = array();
|
| 358 |
$row[] = '!'. $token .'';
|
| 359 |
$row[] = $description;
|
| 360 |
$rows[] = $row;
|
| 361 |
}
|
| 362 |
}
|
| 363 |
|
| 364 |
$output = theme('table', $headers, $rows, array('class' => 'description'));
|
| 365 |
|
| 366 |
$form['token_help'] = array(
|
| 367 |
'#value' => $output,
|
| 368 |
);
|
| 369 |
|
| 370 |
return system_settings_form($form);
|
| 371 |
}
|
| 372 |
|
| 373 |
/**
|
| 374 |
* Format a date selection element.
|
| 375 |
*
|
| 376 |
* @param $element
|
| 377 |
* An associative array containing the properties of the element.
|
| 378 |
* Properties used: title, value, options, description, required and attributes.
|
| 379 |
* @return
|
| 380 |
* A themed HTML string representing the date selection boxes.
|
| 381 |
*
|
| 382 |
* @ingroup themeable
|
| 383 |
*/
|
| 384 |
function theme_addresses_elements(&$element) {
|
| 385 |
return theme('form_element', $element, '<div class="addresses-form">'. $element['#children'] .'</div>');
|
| 386 |
}
|
| 387 |
|
| 388 |
/**
|
| 389 |
* Print a nice settings table for selecting which address fields
|
| 390 |
* should be used.
|
| 391 |
*
|
| 392 |
* @param $form
|
| 393 |
* Array. The address field settings.
|
| 394 |
* @return
|
| 395 |
* String. The single line address
|
| 396 |
* @ingroup themeable
|
| 397 |
*/
|
| 398 |
function theme_addresses_settings_fields(&$form) {
|
| 399 |
foreach (element_children($form) as $ftype) {
|
| 400 |
$field = &$form[$ftype];
|
| 401 |
$rows[] = array(
|
| 402 |
$field['#title'],
|
| 403 |
drupal_render($field[0]),
|
| 404 |
drupal_render($field[1]),
|
| 405 |
drupal_render($field[2]),
|
| 406 |
drupal_render($field[3]),
|
| 407 |
);
|
| 408 |
unset($form[$ftype]);
|
| 409 |
}
|
| 410 |
|
| 411 |
$header = array(t('Field'), t('None'), t('Normal'), t('Required'), t('Hidden'));
|
| 412 |
$output = theme('table', $header, $rows);
|
| 413 |
|
| 414 |
$output .= drupal_render($form);
|
| 415 |
|
| 416 |
return $output;
|
| 417 |
}
|
| 418 |
|
| 419 |
/**
|
| 420 |
* Add a draggable item effect on fields.
|
| 421 |
*
|
| 422 |
* @param $form
|
| 423 |
* Array. The address field weight values.
|
| 424 |
* @return
|
| 425 |
* String. The table with draggable items
|
| 426 |
* @ingroup themeable
|
| 427 |
*/
|
| 428 |
function theme_addresses_field_weight(&$form) {
|
| 429 |
drupal_add_tabledrag('addresses-field-weight', 'order', 'sibling', 'field');
|
| 430 |
foreach (element_children($form) as $field) {
|
| 431 |
$rows[] = array(
|
| 432 |
'class' => 'draggable',
|
| 433 |
'data' => array(
|
| 434 |
drupal_render($form[$field]['field']),
|
| 435 |
drupal_render($form[$field]['weight'])
|
| 436 |
),
|
| 437 |
);
|
| 438 |
}
|
| 439 |
return theme('table', array(), $rows, array('id' => 'addresses-field-weight')) . drupal_render($form);
|
| 440 |
}
|