You are here

public function UcAddressesUcZoneFieldHandler::getFormField in Ubercart Addresses 6.2

Same name and namespace in other branches
  1. 7 handlers/ubercart.handlers.inc \UcAddressesUcZoneFieldHandler::getFormField()

Implements UcAddressesFieldHandler::getFormField().

Overrides UcAddressesFieldHandler::getFormField

File

handlers/ubercart.handlers.inc, line 71
Field handlers for Ubercart core address fields: first_name, last_name, company, etc.

Class

UcAddressesUcZoneFieldHandler
Class for the Ubercart zone field.

Code

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>',
    ),
  );
}