<?php
// $Id: bahrain_48_3.cif,v 1.1.2.1 2011-02-11 05:17:33 tr Exp $

/**
 * First implement hook_install() using the name of the country as the base of
 * the function name.
 */
function bahrain_install() {
  // Make the entry in the country table.
  db_query("INSERT INTO {uc_countries} (country_id, country_name, country_iso_code_2, country_iso_code_3, version) VALUES ('48', 'Bahrain', 'BH', 'BHR', 3)");

  // Make the entries in the zones table.
  $zones = array(
    array(48, 'BH-13', 'Al Manamah'),
    array(48, 'BH-14', 'Al Janubiyah'),
    array(48, 'BH-15', 'Al Muharraq'),
    array(48, 'BH-16', 'Al Wusta'),
    array(48, 'BH-17', 'Ash Shamaliyah'),
  );

  foreach ($zones as $zone) {
    db_query("INSERT INTO {uc_zones} (zone_country_id, zone_code, zone_name) VALUES (%d, '%s', '%s')", $zone);
  }

  // Use uc_set_address_format() with the country ID as the first argument and
  // an address format string as the second.  Documentation on address formats
  // is available at:
  // http://www.ubercart.org/ubercart_users_guide/country_settings#address_format_docs
  uc_set_address_format(48, "!company\r\n!first_name !last_name\r\n!street1 !street2\r\n!zone_name !postal_code\r\n\r\n!country_name_if");
}

function bahrain_update($version) {
  switch ($version) {
    case 3:
      // Correct ISO-3166-2 and ISO-3166-3 codes
      db_query("UPDATE {uc_countries} SET country_iso_code_2 = 'BH', country_iso_code_3 = 'BHR' WHERE country_id = '48'");

      // Remove old obsolete zones
      $zones = array(
        array(48, 'AHD', 'Al Hidd'),
        array(48, 'AMG', 'Al Mintaqah al Gharbiyah'),
        array(48, 'JHA', 'Jidd Hafs'),
        array(48, 'STH', 'Sitrah'),
        array(48, 'MAT', 'Madinatisa'),
        array(48, 'ARR', 'Ar Rifae'),
        array(48, 'AMJ', 'Al Mintaqah Juzur Hawar'),
        array(48, 'MAH', 'Madinat Hamad'),
      );

      foreach ($zones as $zone) {
        db_query("DELETE FROM {uc_zones} WHERE zone_country_id = %d AND zone_code = '%s'", $zone[0], $zone[1]);
      }

      // Add some new zones
      $zones = array(
        array(48, 'BH-14', 'Al Janubiyah'),
      );

      foreach ($zones as $zone) {
        db_query("INSERT INTO {uc_zones} (zone_country_id, zone_code, zone_name) VALUES (%d, '%s', '%s')", $zone);
      }

      // Rename remaining zones
      $zones = array(
        array(48, 'AMH', 'BH-13', 'Al Manamah'),
        array(48, 'AMQ', 'BH-15', 'Al Muharraq'),
        array(48, 'AMW', 'BH-16', 'Al Wusta'),
        array(48, 'AMS', 'BH-17', 'Ash Shamaliyah'),
      );

      foreach ($zones as $zone) {
        db_query("UPDATE {uc_zones} SET zone_name = '%s', zone_code = '%s' WHERE zone_country_id = %d AND zone_code = '%s'", $zone[3], $zone[2], $zone[0], $zone[1]);
      }

      break;
  }
}
