class UcAddressesUcZoneFieldHandler in Ubercart Addresses 6.2
Same name and namespace in other branches
- 7 handlers/ubercart.handlers.inc \UcAddressesUcZoneFieldHandler
Class for the Ubercart zone field.
Hierarchy
- class \UcAddressesFieldHandler
- class \UcAddressesUcFieldHandler
Expanded class hierarchy of UcAddressesUcZoneFieldHandler
2 string references to 'UcAddressesUcZoneFieldHandler'
- 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 67 - Field handlers for Ubercart core address fields: first_name, last_name, company, etc.
View source
class UcAddressesUcZoneFieldHandler extends UcAddressesUcFieldHandler {
/**
* Implements UcAddressesFieldHandler::getFormField().
*/
public function getFormField($form, $form_values) {
$address = $this
->getAddress();
$fieldName = $this
->getFieldName();
$fieldValue = $address
->getField($fieldName);
$default = isset($form_values[$fieldName]) ? $form_values[$fieldName] : $fieldValue;
$country_id = isset($form_values['country']) ? $form_values['country'] : $this
->getAddress()
->getField('country');
if (!empty($country_id)) {
// Check if country exists.
$result = db_result(db_query("SELECT country_id FROM {uc_countries} WHERE country_id = %d", $country_id));
if (!$result) {
$country_id = uc_store_default_country();
}
}
if (empty($country_id)) {
$country_id = uc_store_default_country();
}
$result = db_query("SELECT * FROM {uc_zones} WHERE zone_country_id = %d ORDER BY zone_name", $country_id);
$options[''] = t('Please select');
while ($zone = db_fetch_object($result)) {
$options[$zone->zone_id] = $zone->zone_name;
}
if (empty($form['#key_prefix'])) {
if ($address instanceof UcAddressesAddress) {
// When no key prefix is set, use the address ID as part of the zone wrapper ID.
$zone_wrapper_id = 'uc-address' . $address
->getId() . '-zone-wrapper';
}
else {
// When no instance of UcAddressesAddress is given, we have no unique
// value to create a wrapper for.
$zone_wrapper_id = 'uc-address-zone-wrapper';
}
}
else {
// When a key prefix is set, make this part of the zone wrapper ID.
$zone_wrapper_id = 'uc-store-address-' . str_replace('_', '-', $form['#key_prefix']) . '-zone-wrapper';
}
if (count($options) == 1) {
$options = array(
-1 => t('Not applicable'),
);
}
return array(
$fieldName => array(
'#type' => 'select',
'#title' => $this
->getFieldTitle(),
'#required' => $this
->isFieldRequired(),
'#options' => $options,
'#default_value' => $default,
'#prefix' => '<div id="' . $zone_wrapper_id . '">',
'#suffix' => '<span class="zone-throbber"></span></div>',
),
);
}
/**
* Overrides UcAddressesFieldHandler::getDefaultValue().
*
* The zone ID should always be an integer.
*/
public function getDefaultValue() {
return 0;
}
/**
* Implements UcAddressesFieldHandler::getOutputFormats().
*/
public function getOutputFormats() {
return array(
'zone_code' => t('Abbreviation of the zone'),
'zone_name' => t('Full name of the zone'),
);
}
/**
* Overrides UcAddressesFieldHandler::outputValue().
*
* The zone field can be outputted in different formats.
*/
public function outputValue($value = '', $format = '') {
if ($value === '') {
$value = $this
->getAddress()
->getField($this
->getFieldName());
}
// Get zone data.
$result = db_query("SELECT * FROM {uc_zones} WHERE zone_id = %d", $value);
if (!($zone_data = db_fetch_array($result))) {
$zone_data = array(
'zone_code' => t('N/A'),
'zone_name' => t('Unknown'),
);
}
if (isset($zone_data[$format])) {
return $zone_data[$format];
}
// If no format is specified, return zone name.
return $zone_data['zone_name'];
}
}