You are here

class UcAddressesUcCountryFieldHandler in Ubercart Addresses 6.2

Same name and namespace in other branches
  1. 7 handlers/ubercart.handlers.inc \UcAddressesUcCountryFieldHandler

Class for the Ubercart country field.

Hierarchy

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;
  }

}

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.
UcAddressesUcCountryFieldHandler::$countries private static property An array of all loaded countries.
UcAddressesUcCountryFieldHandler::getCountry private function Returns country data.
UcAddressesUcCountryFieldHandler::getCountryDescriptionMessage private function Used in getOutputFormats() to output a description for the country format.
UcAddressesUcCountryFieldHandler::getDefaultValue public function Overrides UcAddressesFieldHandler::getDefaultValue(). Overrides UcAddressesFieldHandler::getDefaultValue
UcAddressesUcCountryFieldHandler::getFormField public function Implements UcAddressesFieldHandler::getFormField(). Overrides UcAddressesFieldHandler::getFormField
UcAddressesUcCountryFieldHandler::getOutputFormats public function Implements UcAddressesFieldHandler::getOutputFormats(). Overrides UcAddressesFieldHandler::getOutputFormats
UcAddressesUcCountryFieldHandler::outputValue public function Overrides UcAddressesFieldHandler::outputValue(). Overrides UcAddressesFieldHandler::outputValue
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