<?php

/**
 * Implements hook_install() using the name of the country as the base of
 * the function name.
 */
function tanzania_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' => 834,
      'country_name' => 'Tanzania, United Republic of',
      'country_iso_code_2' => 'TZ',
      'country_iso_code_3' => 'TZA',
      'version' => 2,
    ))
    ->execute();

  // Make the entries in the zones table.
  $zones = array(
    array(834, '01', 'Arusha'),
    array(834, '19', 'Coast'),
    array(834, '02', 'Dar es Salaam'),
    array(834, '03', 'Dodoma'),
    array(834, '04', 'Iringa'),
    array(834, '05', 'Kagera'),
    array(834, '06', 'Kaskazini Pemba'),
    array(834, '07', 'Kaskazini Unguja'),
    array(834, '08', 'Kigoma'),
    array(834, '09', 'Kilimanjaro'),
    array(834, '10', 'Kusini Pemba'),
    array(834, '11', 'Kusini Unguja'),
    array(834, '12', 'Lindi'),
    array(834, '26', 'Manyara'),
    array(834, '13', 'Mara'),
    array(834, '14', 'Mbeya'),
    array(834, '15', 'Mjini Mgharibi'),
    array(834, '16', 'Morogoro'),
    array(834, '17', 'Mtwara'),
    array(834, '18', 'Mwanza'),
    array(834, '06', 'Pemba North'),
    array(834, '10', 'Pemba South'),
    array(834, '19', 'Pwani'),
    array(834, '20', 'Rukwa'),
    array(834, '21', 'Ruvuma'),
    array(834, '22', 'Shinyanga'),
    array(834, '23', 'Singida'),
    array(834, '24', 'Tabora'),
    array(834, '25', 'Tanga'),
    array(834, '07', 'Zanzibar North'),
    array(834, '11', 'Zanzibar South'),
    array(834, '15', 'Zanzibar West'),
  );

  $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(
    834,
    "!company\r\n" .
    "!first_name !last_name\r\n" .
    "!street1\r\n" .
    "!street2\r\n" .
    "!city !zone_code !postal_code\r\n" .
    "!country_name_if"
  );
}

/**
 * Implements hook_update() with $version being the only argument.
 * Add a new case for each version update, and be sure to always include the
 * latest changes in the install function.
 */
function tanzania_update($version) {
  switch ($version) {
    case 2:
      // Correct ISO-3166-1 country name
      db_update('uc_countries')
        ->fields(array('country_name' => 'Tanzania, United Republic of'))
        ->condition('country_id', 834)
        ->execute();

      break;
  }
}
