class UcAddressesUcCountryFieldHandler in Ubercart Addresses 6.2
Same name and namespace in other branches
- 7 handlers/ubercart.handlers.inc \UcAddressesUcCountryFieldHandler
Class for the Ubercart country field.
Hierarchy
- class \UcAddressesFieldHandler
Expanded class hierarchy of UcAddressesUcCountryFieldHandler
2 string references to 'UcAddressesUcCountryFieldHandler'
- uc_addresses_uc_addresses_fields in ./
uc_addresses.uc_addresses_fields.inc - Implementation of hook_uc_addresses_fields().
- uc_addresses_uc_addresses_field_handlers in ./
uc_addresses.uc_addresses_fields.inc - Implementation of hook_uc_addresses_field_handlers().
File
- handlers/
ubercart.handlers.inc, line 177 - Field handlers for Ubercart core address fields: first_name, last_name, company, etc.
View source
class UcAddressesUcCountryFieldHandler extends UcAddressesUcFieldHandler {
/**
* An array of all loaded countries.
*
* @var array $countries
* @access private
* @static
*/
private static $countries;
/**
* Implements UcAddressesFieldHandler::getFormField().
*/
public function getFormField($form, $form_values) {
$fieldName = $this
->getFieldName();
$fieldValue = $this
->getAddress()
->getField($fieldName);
$default = isset($form_values[$fieldName]) ? $form_values[$fieldName] : $fieldValue;
$result = db_query("SELECT * FROM {uc_countries} WHERE version > 0 ORDER BY country_name");
$countries = array();
while ($country = db_fetch_object($result)) {
// Create option.
$countries[$country->country_id] = t($country->country_name);
// Save for later use.
self::$countries[$country->country_id] = $country;
}
if (count($countries) == 0) {
$countries[] = t('No countries found.');
}
else {
// Sort countries list in natural order.
natcasesort($countries);
}
drupal_add_js(drupal_get_path('module', 'uc_store') . '/uc_country_select.js');
// Ensure the path setting only gets added once.
static $added = FALSE;
if (!$added) {
drupal_add_js(array(
'ucURL' => array(
'zoneSelect' => url('uc_js_util/zone_select'),
),
), 'setting');
$added = TRUE;
}
return array(
$fieldName => array(
'#type' => 'select',
'#title' => $this
->getFieldTitle(),
'#options' => $countries,
'#required' => $this
->isFieldRequired(),
'#default_value' => empty($default) ? uc_store_default_country() : $default,
),
);
}
/**
* Overrides UcAddressesFieldHandler::getDefaultValue().
*/
public function getDefaultValue() {
return uc_store_default_country();
}
/**
* Implements UcAddressesFieldHandler::getOutputFormats().
*/
public function getOutputFormats() {
return array(
'country_name' => t('Name of the country'),
'country_code2' => t('2 digit country abbreviation'),
'country_code3' => t('3 digit country abbreviation'),
'country_name_if' => $this
->getCountryDescriptionMessage(t('Name of the country')),
'country_code2_if' => $this
->getCountryDescriptionMessage(t('2 digit country abbreviation')),
'country_code3_if' => $this
->getCountryDescriptionMessage(t('3 digit country abbreviation')),
);
}
/**
* Used in getOutputFormats() to output a description for the country format.
*
* @param string $country_description
* First part of the country description.
*
* @access private
* @return string
* Description message for getOutputFormats().
*
* @see getOutputFormats()
*/
private function getCountryDescriptionMessage($country_description) {
return t('!country_description, display only for addresses whose country is different than the default store country.', array(
'!country_description' => $country_description,
));
}
/**
* Overrides UcAddressesFieldHandler::outputValue().
*
* The country field can be outputted in different formats.
*/
public function outputValue($value = '', $format = '') {
if ($value === '') {
$value = $this
->getAddress()
->getField($this
->getFieldName());
}
$country = $this
->getCountry($value);
if (!$country) {
return t('Unknown');
}
// Return country only if the country is not equal to the store's default country.
if (preg_match('/\\_if$/', $format)) {
if (uc_store_default_country() == $country->country_id) {
return '';
}
}
switch ($format) {
case 'country_name':
case 'country_name_if':
return t($country->country_name);
case 'country_code2':
case 'country_code2_if':
return $country->country_iso_code_2;
case 'country_code3':
case 'country_code3_if':
return $country->country_iso_code_3;
}
// If no format is specified, return country name.
return t($country->country_name);
}
/**
* Returns country data.
*
* @param int $country_id
* The country to get.
*
* @return object
* Data of country if the country is found.
* NULL otherwise.
*/
private function getCountry($country_id) {
if (isset(self::$countries[$country_id])) {
return self::$countries[$country_id];
}
$result = db_query("SELECT * FROM {uc_countries} WHERE country_id = %d", $country_id);
if ($country = db_fetch_object($result)) {
self::$countries[$country->country_id] = $country;
return $country;
}
return NULL;
}
}