<?php

function new_zealand_install() {
  // Make the entry in the country table.
  // VALUES = Country ID, Country Name, 2-digit Code, 3-digit Code, File Version
  db_insert('uc_countries')
    ->fields(array(
      'country_id' => 554,
      'country_name' => 'New Zealand',
      'country_iso_code_2' => 'NZ',
      'country_iso_code_3' => 'NZL',
      'version' => 3,
    ))
    ->execute();

  // Make the entries in the zones table.
  $zones = array(
    array(554, 'AUK', 'Auckland'),
    array(554, 'BOP', 'Bay of Plenty'),
    array(554, 'CAN', 'Canterbury'),
    array(554, 'COR', 'Coromandel'),
    array(554, 'GIS', 'Gisborne'),
    array(554, 'FIO', 'Fiordland'),
    array(554, 'HKB', "Hawke's Bay"),
    array(554, 'MBH', 'Marlborough'),
    array(554, 'MWT', 'Manawatu-Wanganui'),
    array(554, 'MCM', 'Mt Cook-Mackenzie'),
    array(554, 'NSN', 'Nelson'),
    array(554, 'NTL', 'Northland'),
    array(554, 'OTA', 'Otago'),
    array(554, 'STL', 'Southland'),
    array(554, 'TKI', 'Taranaki'),
    array(554, 'WGN', 'Wellington'),
    array(554, 'WKO', 'Waikato'),
    array(554, 'WAI', 'Wairarapa'),
    array(554, 'WTC', 'West Coast'),
  );

  $query = db_insert('uc_zones')->fields(array('zone_country_id', 'zone_code', 'zone_name'));
  foreach ($zones as $zone) {
    $query->values($zone);
  }
  $query->execute();

  // Set address format
  uc_set_address_format(
    554,
    "!first_name !last_name\r\n".
    "!company\r\n".
    "!street1\r\n".
    "!street2\r\n".
    "!city !postal_code\r\n".
    "!country_name_if"
  );
}

function new_zealand_update($version) {
  switch ($version) {
    case 2:
      $zones = array(
        array(554, 'AUK', 'Auckland'),
        array(554, 'BOP', 'Bay of Plenty'),
        array(554, 'CAN', 'Canterbury'),
        array(554, 'COR', 'Coromandel'),
        array(554, 'GIS', 'Gisborne'),
        array(554, 'FIO', 'Fiordland'),
        array(554, 'HKB', "Hawke's Bay"),
        array(554, 'MBH', 'Marlborough'),
        array(554, 'MWT', 'Manawatu-Wanganui'),
        array(554, 'MCM', 'Mt Cook-Mackenzie'),
        array(554, 'NSN', 'Nelson'),
        array(554, 'NTL', 'Northland'),
        array(554, 'OTA', 'Otago'),
        array(554, 'STL', 'Southland'),
        array(554, 'TKI', 'Taranaki'),
        array(554, 'WGN', 'Wellington'),
        array(554, 'WKO', 'Waikato'),
        array(554, 'WAI', 'Wairarapa'),
        array(554, 'WTC', 'West Coast'),
      );

      $query = db_insert('uc_zones')->fields(array('zone_country_id', 'zone_code', 'zone_name'));
      foreach ($zones as $zone) {
        $query->values($zone);
      }
      $query->execute();
      break;

    case 3:
      db_update('uc_zones')
        ->fields(array('zone_name' => 'Wairarapa'))
        ->condition('zone_name', 'Wairprarapa')
        ->execute();
      break;
  }
}
