<?php
// $Id: india_356_2.cif,v 1.2 2011-02-15 05:08:12 tr Exp $

/**
 * Implements hook_install() using the name of the country as the base of
 * the function name.
 */
function india_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' => 356,
      'country_name' => 'India',
      'country_iso_code_2' => 'IN',
      'country_iso_code_3' => 'IND',
      'version' => 2,
    ))
    ->execute();

  // Make the entries in the zones table.
  $zones = array(
    array(356, 'AN', 'Andaman and Nicobar'),
    array(356, 'AP', 'Andhra Pradesh'),
    array(356, 'AR', 'Arunachal Pradesh'),
    array(356, 'AS', 'Assam'),
    array(356, 'BR', 'Bihar'),
    array(356, 'CH', 'Chandigarh'),
    array(356, 'CT', 'Chhattisgarh'),
    array(356, 'DN', 'Dadra and Nagar Haveli'),
    array(356, 'DD', 'Daman and Diu'),
    array(356, 'DL', 'Delhi'),
    array(356, 'GA', 'Goa'),
    array(356, 'GJ', 'Gujarat'),
    array(356, 'HR', 'Haryana'),
    array(356, 'HP', 'Himachal Pradesh'),
    array(356, 'JK', 'Jammu and Kashmir'),
    array(356, 'JH', 'Jharkhand'),
    array(356, 'KA', 'Karnataka'),
    array(356, 'KL', 'Kerala'),
    array(356, 'LD', 'Lakshadweep'),
    array(356, 'MP', 'Madhya Pradesh'),
    array(356, 'MH', 'Maharashtra'),
    array(356, 'MN', 'Manipur'),
    array(356, 'ML', 'Meghalaya'),
    array(356, 'MZ', 'Mizoram'),
    array(356, 'NL', 'Nagaland'),
    array(356, 'OR', 'Orissa'),
    array(356, 'PY', 'Puducherry'),
    array(356, 'PB', 'Punjab'),
    array(356, 'RJ', 'Rajasthan'),
    array(356, 'SK', 'Sikkim'),
    array(356, 'TN', 'Tamil Nadu'),
    array(356, 'TR', 'Tripura'),
    array(356, 'UL', 'Uttarakhand'),
    array(356, 'UP', 'Uttar Pradesh'),
    array(356, 'WB', 'West Bengal'),
  );

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

function india_update($version) {
  switch ($version) {
    case 2:
      // Add some missing zones
      $zones = array(
        array(356, 'LD', 'Lakshadweep'),
        array(356, 'PY', 'Puducherry'),
      );

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

      // Correct zone name misspelling and ISO-3166-2 code
      db_update('uc_zones')
        ->fields(array('zone_name' => 'Chhattisgarh', 'zone_code' => 'CT'))
        ->condition('zone_country_id', 356)
        ->condition('zone_code', 'CG')
        ->execute();

      // Replace ampersand with 'and' for consistency within .cif
      // and for agreement with ISO-3166-2
      db_update('uc_zones')
        ->fields(array('zone_name' => 'Jammu and Kashmir', 'zone_code' => 'JK'))
        ->condition('zone_country_id', 356)
        ->condition('zone_code', 'JK')
        ->execute();
      db_update('uc_zones')
        ->fields(array('zone_name' => 'Andaman and Nicobar', 'zone_code' => 'AN'))
        ->condition('zone_country_id', 356)
        ->condition('zone_code', 'AN')
        ->execute();
      db_update('uc_zones')
        ->fields(array('zone_name' => 'Daman and Diu', 'zone_code' => 'DD'))
        ->condition('zone_country_id', 356)
        ->condition('zone_code', 'DD')
        ->execute();

      // Correct ISO-3166-2 code
      db_update('uc_zones')
        ->fields(array('zone_name' => 'Uttarakhand', 'zone_code' => 'UL'))
        ->condition('zone_country_id', 356)
        ->condition('zone_code', 'UA')
        ->execute();

      break;
  }
}
