You are here

class UcAddressesUcZoneFieldHandler in Ubercart Addresses 6.2

Same name and namespace in other branches
  1. 7 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
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'];
  }

}

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::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::getOutputFormats public function Implements UcAddressesFieldHandler::getOutputFormats(). Overrides UcAddressesFieldHandler::getOutputFormats
UcAddressesUcZoneFieldHandler::outputValue public function Overrides UcAddressesFieldHandler::outputValue(). Overrides UcAddressesFieldHandler::outputValue