You are here

function uc_zone_select in Ubercart 7.3

Same name and namespace in other branches
  1. 5 uc_store/uc_store.module \uc_zone_select()
  2. 6.2 uc_store/uc_store.module \uc_zone_select()

Creates a zone select box for a form.

Parameters

$title: The label for the field.

$default: The default zone ID.

$country_id: The country ID

array $options: An associative array of additional options, with the following elements:

  • 'description': The description for the field (defaults to none).
  • 'display': The values to display, either 'name' (default) or 'code'.
  • 'required': TRUE if the field is required (defaults to FALSE).

Return value

A Form API select element.

File

uc_store/uc_store.module, line 1314
Contains global Ubercart functions and store administration functionality.

Code

function uc_zone_select($title = '', $default = NULL, $country_id = NULL, $options = array()) {
  $options += array(
    'description' => NULL,
    'display' => 'name',
    'required' => FALSE,
  );
  if (empty($country_id)) {
    $country_id = uc_store_default_country();
  }
  $order_by = $options['display'] == 'code' ? 'zone_code' : 'zone_name';
  $result = db_query('SELECT * FROM {uc_zones} WHERE zone_country_id = :id ORDER BY :field', array(
    ':id' => $country_id,
    ':field' => $order_by,
  ));
  $zones = array(
    '' => t('Please select'),
  );
  foreach ($result as $zone) {
    $zones[$zone->zone_id] = $zone->{$order_by};
  }
  if (count($zones) == 1) {
    $zones = array(
      -1 => t('Not applicable'),
    );
  }
  $select = array(
    '#type' => 'select',
    '#title' => $title,
    '#description' => $options['description'],
    '#options' => $zones,
    '#default_value' => $default,
    '#required' => $options['required'],
    '#disabled' => isset($zones[-1]),
  );
  return $select;
}