class UcAddressesUcZoneFieldHandler in Ubercart Addresses 7
Same name and namespace in other branches
- 6.2 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'
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_select('uc_countries')
->fields('uc_countries', array(
'country_id',
))
->condition('country_id', $country_id)
->execute()
->fetchField();
if (!$result) {
$country_id = uc_store_default_country();
}
}
if (empty($country_id)) {
$country_id = uc_store_default_country();
}
$result = db_select('uc_zones')
->condition('zone_country_id', $country_id)
->fields('uc_zones')
->orderBy('zone_name')
->execute();
$options = array();
foreach ($result as $zone) {
$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) == 0) {
return array(
$fieldName => array(
'#title' => $this
->getFieldTitle(),
'#type' => 'hidden',
'#value' => 0,
'#required' => FALSE,
'#prefix' => '<div id="' . $zone_wrapper_id . '">',
'#suffix' => '</div>',
),
);
}
$return = array(
$fieldName => array(
'#type' => 'select',
'#title' => $this
->getFieldTitle(),
'#required' => $this
->isFieldRequired(),
'#options' => $options,
'#default_value' => $default,
'#prefix' => '<div id="' . $zone_wrapper_id . '">',
'#suffix' => '</div>',
'#empty_value' => 0,
),
);
// If the country value was updated, the selected zone does not exists.
// In that case, just empty the value.
if (!empty($form_values['zone']) && !isset($options[$form_values['zone']])) {
$return[$fieldName]['#value'] = 0;
}
return $return;
}
/**
* Overrides UcAddressesFieldHandler::getDefaultValue().
*
* The zone ID should always be an integer.
*/
public function getDefaultValue() {
return 0;
}
/**
* Overrides UcAddressesFieldHandler::getMappingTargets().
*
* The zone field has some extra mapping targets.
*/
public function getMappingTargets() {
$targets = parent::getMappingTargets();
// Specify clearer names and descriptions.
$targets['zone:zone_name']['name'] = t('Zone name');
$targets['zone:zone_code']['name'] = t('Zone code');
$targets['zone']['description'] = t('Zone ID as known to Ubercart');
return $targets;
}
/**
* Overrides UcAddressesFieldHandler::mapValue().
*
* The zone field has some extra mapping targets.
*/
public function mapValue($value, $format = '') {
switch ($format) {
case 'zone_code':
case 'zone_name':
// Lookup zone data.
$zone_id = db_select('uc_zones', 'uc_zones')
->condition($format, $value)
->fields('uc_zones', array(
'zone_id',
))
->execute()
->fetchField();
if ($zone_id) {
$value = $zone_id;
}
break;
}
parent::mapValue($value, $format);
}
/**
* 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_select('uc_zones')
->condition('zone_id', $value)
->fields('uc_zones')
->execute();
$row = $result
->fetch();
if ($row) {
$zone_data = array(
'zone_code' => $row->zone_code,
'zone_name' => $row->zone_name,
);
}
else {
$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'];
}
}