You are here

class UcAddressesUcZoneFieldHandler in Ubercart Addresses 7

Same name and namespace in other branches
  1. 6.2 handlers/ubercart.handlers.inc \UcAddressesUcZoneFieldHandler

Class for the Ubercart zone field.

Hierarchy

Expanded class hierarchy of UcAddressesUcZoneFieldHandler

2 string references to 'UcAddressesUcZoneFieldHandler'
uc_addresses_uc_addresses_fields in ./uc_addresses.uc_addresses_fields.inc
Implements hook_uc_addresses_fields().
uc_addresses_uc_addresses_field_handlers in ./uc_addresses.uc_addresses_fields.inc
Implements 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_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'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UcAddressesFieldHandler::$address private property Address object.
UcAddressesFieldHandler::$context private property The context in which this field is used.
UcAddressesFieldHandler::$definition private property The declared field definition.
UcAddressesFieldHandler::$name private property Name of this field.
UcAddressesFieldHandler::checkContext public function Checks if the field passes the context. 1
UcAddressesFieldHandler::getAddress final public function Returns the address attached to this field.
UcAddressesFieldHandler::getContext final public function Returns the context in which this field is used.
UcAddressesFieldHandler::getFieldName final public function Returns the field name.
UcAddressesFieldHandler::getProperty final public function Returns a property from the field definition.
UcAddressesFieldHandler::getTokenInfo public function Returns supported tokens. 1
UcAddressesFieldHandler::init protected function Can be used by subclasses to do some initialization upon construction of the object. 2
UcAddressesFieldHandler::setValue public function Sets value in the address object.
UcAddressesFieldHandler::validateValue public function Check a fields' value. 2
UcAddressesFieldHandler::__construct final public function UcAddressesFormField object constructor.
UcAddressesUcFieldHandler::getFieldTitle public function Overrides UcAddressesFieldHandler::getFieldTitle(). Overrides UcAddressesFieldHandler::getFieldTitle
UcAddressesUcFieldHandler::isFieldEnabled public function Implements UcAddressesFieldHandler::isFieldEnabled(). Overrides UcAddressesFieldHandler::isFieldEnabled
UcAddressesUcFieldHandler::isFieldRequired public function Implements UcAddressesFieldHandler::isFieldRequired(). Overrides UcAddressesFieldHandler::isFieldRequired
UcAddressesUcZoneFieldHandler::getDefaultValue public function Overrides UcAddressesFieldHandler::getDefaultValue(). Overrides UcAddressesFieldHandler::getDefaultValue
UcAddressesUcZoneFieldHandler::getFormField public function Implements UcAddressesFieldHandler::getFormField(). Overrides UcAddressesFieldHandler::getFormField
UcAddressesUcZoneFieldHandler::getMappingTargets public function Overrides UcAddressesFieldHandler::getMappingTargets(). Overrides UcAddressesFieldHandler::getMappingTargets
UcAddressesUcZoneFieldHandler::getOutputFormats public function Implements UcAddressesFieldHandler::getOutputFormats(). Overrides UcAddressesFieldHandler::getOutputFormats
UcAddressesUcZoneFieldHandler::mapValue public function Overrides UcAddressesFieldHandler::mapValue(). Overrides UcAddressesFieldHandler::mapValue
UcAddressesUcZoneFieldHandler::outputValue public function Overrides UcAddressesFieldHandler::outputValue(). Overrides UcAddressesFieldHandler::outputValue