<?php

/**
 * Implements hook_install() using the name of the country as the base of
 * the function name.
 */
function united_kingdom_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' => 826,
      'country_name' => 'United Kingdom',
      'country_iso_code_2' => 'GB',
      'country_iso_code_3' => 'GBR',
      'version' => 2,
    ))
    ->execute();

  // Make the entries in the zones table.
  $zones = array(
    array(826, 'ABN', 'Aberdeen'),
    array(826, 'ABNS', 'Aberdeenshire'),
    array(826, 'ANG', 'Anglesey'),
    array(826, 'AGS', 'Angus'),
    array(826, 'ARY', 'Argyll and Bute'),
    array(826, 'AVON', 'Avon'),
    array(826, 'BEDS', 'Bedfordshire'),
    array(826, 'BERKS', 'Berkshire'),
    array(826, 'BLA', 'Blaenau Gwent'),
    array(826, 'BRI', 'Bridgend'),
    array(826, 'BSTL', 'Bristol'),
    array(826, 'BUCKS', 'Buckinghamshire'),
    array(826, 'CAE', 'Caerphilly'),
    array(826, 'CAMBS', 'Cambridgeshire'),
    array(826, 'CDF', 'Cardiff'),
    array(826, 'CARM', 'Carmarthenshire'),
    array(826, 'CDGN', 'Ceredigion'),
    array(826, 'CHES', 'Cheshire'),
    array(826, 'CLACK', 'Clackmannanshire'),
    array(826, 'CLVD', 'Cleveland'),
    array(826, 'CON', 'Conwy'),
    array(826, 'CORN', 'Cornwall'),
    array(826, 'COAN', 'County Antrim'),
    array(826, 'COAR', 'County Armagh'),
    array(826, 'CODN', 'County Down'),
    array(826, 'COFE', 'County Fermanagh'),
    array(826, 'COLO', 'County Londonderry'),
    array(826, 'COTY', 'County Tyrone'),
    array(826, 'CMA', 'Cumbria'),
    array(826, 'DNBG', 'Denbighshire'),
    array(826, 'DERBY', 'Derbyshire'),
    array(826, 'DVN', 'Devon'),
    array(826, 'DOR', 'Dorset'),
    array(826, 'DGL', 'Dumfries and Galloway'),
    array(826, 'DUND', 'Dundee'),
    array(826, 'DHM', 'Durham'),
    array(826, 'ARYE', 'East Ayrshire'),
    array(826, 'DUNBE', 'East Dunbartonshire'),
    array(826, 'LOTE', 'East Lothian'),
    array(826, 'RENE', 'East Renfrewshire'),
    array(826, 'ERYS', 'East Riding of Yorkshire'),
    array(826, 'SXE', 'East Sussex'),
    array(826, 'EDIN', 'Edinburgh'),
    array(826, 'ESX', 'Essex'),
    array(826, 'FALK', 'Falkirk'),
    array(826, 'FFE', 'Fife'),
    array(826, 'FLINT', 'Flintshire'),
    array(826, 'GLAS', 'Glasgow'),
    array(826, 'GLOS', 'Gloucestershire'),
    array(826, 'MAN', 'Greater Manchester'),
    array(826, 'GDD', 'Gwynedd'),
    array(826, 'HANTS', 'Hampshire'),
    array(826, 'HWR', 'Herefordshire'),
    array(826, 'HERTS', 'Hertfordshire'),
    array(826, 'HLD', 'Highlands'),
    array(826, 'IVER', 'Inverclyde'),
    array(826, 'IOW', 'Isle of Wight'),
    array(826, 'IOS', 'Isles of Scilly'),
    array(826, 'KNT', 'Kent'),
    array(826, 'LANCS', 'Lancashire'),
    array(826, 'LEICS', 'Leicestershire'),
    array(826, 'LINCS', 'Lincolnshire'),
    array(826, 'LDN', 'London'),
    array(826, 'MSY', 'Merseyside'),
    array(826, 'MERT', 'Merthyr Tydfil'),
    array(826, 'MIDDX', 'Middlesex'),
    array(826, 'MLOT', 'Midlothian'),
    array(826, 'MMOUTH', 'Monmouthshire'),
    array(826, 'MORAY', 'Moray'),
    array(826, 'NPRTAL', 'Neath Port Talbot'),
    array(826, 'NEWPT', 'Newport'),
    array(826, 'NOR', 'Norfolk'),
    array(826, 'ARYN', 'North Ayrshire'),
    array(826, 'LANN', 'North Lanarkshire'),
    array(826, 'YSN', 'North Yorkshire'),
    array(826, 'NHM', 'Northamptonshire'),
    array(826, 'NLD', 'Northumberland'),
    array(826, 'NOT', 'Nottinghamshire'),
    array(826, 'ORK', 'Orkney Islands'),
    array(826, 'OFE', 'Oxfordshire'),
    array(826, 'PEM', 'Pembrokeshire'),
    array(826, 'PERTH', 'Perth and Kinross'),
    array(826, 'PWS', 'Powys'),
    array(826, 'REN', 'Renfrewshire'),
    array(826, 'RHON', 'Rhondda Cynon Taff'),
    array(826, 'RUT', 'Rutland'),
    array(826, 'BOR', 'Scottish Borders'),
    array(826, 'SHET', 'Shetland Islands'),
    array(826, 'SPE', 'Shropshire'),
    array(826, 'SOM', 'Somerset'),
    array(826, 'ARYS', 'South Ayrshire'),
    array(826, 'LANS', 'South Lanarkshire'),
    array(826, 'YSS', 'South Yorkshire'),
    array(826, 'SFD', 'Staffordshire'),
    array(826, 'STIR', 'Stirling'),
    array(826, 'SFK', 'Suffolk'),
    array(826, 'SRY', 'Surrey'),
    array(826, 'SWAN', 'Swansea'),
    array(826, 'TORF', 'Torfaen'),
    array(826, 'TWR', 'Tyne and Wear'),
    array(826, 'VGLAM', 'Vale of Glamorgan'),
    array(826, 'WARKS', 'Warwickshire'),
    array(826, 'WDUN', 'West Dunbartonshire'),
    array(826, 'WLOT', 'West Lothian'),
    array(826, 'WMD', 'West Midlands'),
    array(826, 'SXW', 'West Sussex'),
    array(826, 'YSW', 'West Yorkshire'),
    array(826, 'WIL', 'Western Isles'),
    array(826, 'WLT', 'Wiltshire'),
    array(826, 'WORCS', 'Worcestershire'),
    array(826, 'WRX', 'Wrexham')
  );

  $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(
    826,
    "!first_name !last_name\r\n".
    "!company\r\n".
    "!street1\r\n".
    "!street2\r\n".
    "!city\r\n".
    "!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 united_kingdom_update($version) {
  switch ($version) {
    case 2:
      $zones = array(
        array(826, 'AVON', 'Avon'),
        array(826, 'COAN', 'County Antrim'),
        array(826, 'COAR', 'County Armagh'),
        array(826, 'CODN', 'County Down'),
        array(826, 'COFE', 'County Fermanagh'),
        array(826, 'COLO', 'County Londonderry'),
        array(826, 'COTY', 'County Tyrone'),
        array(826, 'CLVD', 'Cleveland'),
        array(826, 'IOS', 'Isles of Scilly'),
        array(826, 'MIDDX', 'Middlesex'),
      );

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

      db_update('uc_zones')
        ->fields(array('zone_name' => 'Greater Manchester'))
        ->condition('zone_country_id', 826)
        ->condition('zone_name', 'Manchester')
        ->execute();

      uc_set_address_format(
        826,
        "!first_name !last_name\r\n".
        "!company\r\n".
        "!street1\r\n".
        "!street2\r\n".
        "!city\r\n".
        "!postal_code\r\n".
        "!country_name_if"
      );
      break;
  }
}
