<?php

/**
* Implements hook_install() using the name of the country as the base of
* the function name.
*/
function italy_install() {
  // Make the entry in the country table.
  db_insert('uc_countries')
    ->fields(array(
      'country_id' => 380,
      'country_name' => 'Italy',
      'country_iso_code_2' => 'IT',
      'country_iso_code_3' => 'ITA',
      'version' => 3,
    ))
    ->execute();

  // Make the entries in the zones table.
  $zones = array(
    array(380, 'AG', 'Agrigento'),
    array(380, 'AL', 'Alessandria'),
    array(380, 'AN', 'Ancona'),
    array(380, 'AO', 'Aosta'),
    array(380, 'AP', 'Ascoli Piceno'),
    array(380, 'AQ', "L'Aquila"),
    array(380, 'AR', 'Arezzo'),
    array(380, 'AT', 'Asti'),
    array(380, 'AV', 'Avellino'),
    array(380, 'BA', 'Bari'),
    array(380, 'BG', 'Bergamo'),
    array(380, 'BI', 'Biella'),
    array(380, 'BL', 'Belluno'),
    array(380, 'BN', 'Benevento'),
    array(380, 'BO', 'Bologna'),
    array(380, 'BR', 'Brindisi'),
    array(380, 'BS', 'Brescia'),
    array(380, 'BT', 'Barletta-Andria-Trani'),
    array(380, 'BZ', 'Bolzano'),
    array(380, 'CA', 'Cagliari'),
    array(380, 'CB', 'Campobasso'),
    array(380, 'CE', 'Caserta'),
    array(380, 'CH', 'Chieti'),
    array(380, 'CI', 'Carbonia-Iglesias'),
    array(380, 'CL', 'Caltanissetta'),
    array(380, 'CN', 'Cuneo'),
    array(380, 'CO', 'Como'),
    array(380, 'CR', 'Cremona'),
    array(380, 'CS', 'Cosenza'),
    array(380, 'CT', 'Catania'),
    array(380, 'CZ', 'Catanzaro'),
    array(380, 'EN', 'Enna'),
    array(380, 'FC', 'Forlì-Cesena'),
    array(380, 'FE', 'Ferrara'),
    array(380, 'FG', 'Foggia'),
    array(380, 'FI', 'Firenze'),
    array(380, 'FM', 'Fermo'),
    array(380, 'FR', 'Frosinone'),
    array(380, 'GE', 'Genova'),
    array(380, 'GO', 'Gorizia'),
    array(380, 'GR', 'Grosseto'),
    array(380, 'IM', 'Imperia'),
    array(380, 'IS', 'Isernia'),
    array(380, 'KR', 'Crotone'),
    array(380, 'LC', 'Lecco'),
    array(380, 'LE', 'Lecce'),
    array(380, 'LI', 'Livorno'),
    array(380, 'LO', 'Lodi'),
    array(380, 'LT', 'Latina'),
    array(380, 'LU', 'Lucca'),
    array(380, 'MB', 'Monza e Brianza'),
    array(380, 'MC', 'Macerata'),
    array(380, 'ME', 'Messina'),
    array(380, 'MI', 'Milano'),
    array(380, 'MN', 'Mantova'),
    array(380, 'MO', 'Modena'),
    array(380, 'MS', 'Massa e Carrara'),
    array(380, 'MT', 'Matera'),
    array(380, 'NA', 'Napoli'),
    array(380, 'NO', 'Novara'),
    array(380, 'NU', 'Nuoro'),
    array(380, 'OG', 'Ogliastra'),
    array(380, 'OR', 'Oristano'),
    array(380, 'OT', 'Olbia-Tempio'),
    array(380, 'PA', 'Palermo'),
    array(380, 'PC', 'Piacenza'),
    array(380, 'PD', 'Padova'),
    array(380, 'PE', 'Pescara'),
    array(380, 'PG', 'Perugia'),
    array(380, 'PI', 'Pisa'),
    array(380, 'PN', 'Pordenone'),
    array(380, 'PO', 'Prato'),
    array(380, 'PR', 'Parma'),
    array(380, 'PU', 'Pesaro e Urbino'),
    array(380, 'PT', 'Pistoia'),
    array(380, 'PV', 'Pavia'),
    array(380, 'PZ', 'Potenza'),
    array(380, 'RA', 'Ravenna'),
    array(380, 'RC', 'Reggio Calabria'),
    array(380, 'RE', 'Reggio Emilia'),
    array(380, 'RG', 'Ragusa'),
    array(380, 'RI', 'Rieti'),
    array(380, 'RM', 'Roma'),
    array(380, 'RN', 'Rimini'),
    array(380, 'RO', 'Rovigo'),
    array(380, 'SA', 'Salerno'),
    array(380, 'SI', 'Siena'),
    array(380, 'SO', 'Sondrio'),
    array(380, 'SP', 'La Spezia'),
    array(380, 'SR', 'Siracusa'),
    array(380, 'SS', 'Sassari'),
    array(380, 'SV', 'Savona'),
    array(380, 'TA', 'Taranto'),
    array(380, 'TE', 'Teramo'),
    array(380, 'TN', 'Trento'),
    array(380, 'TO', 'Torino'),
    array(380, 'TP', 'Trapani'),
    array(380, 'TR', 'Terni'),
    array(380, 'TS', 'Trieste'),
    array(380, 'TV', 'Treviso'),
    array(380, 'UD', 'Udine'),
    array(380, 'VA', 'Varese'),
    array(380, 'VB', 'Verbano-Cusio-Ossola'),
    array(380, 'VC', 'Vercelli'),
    array(380, 'VE', 'Venezia'),
    array(380, 'VI', 'Vicenza'),
    array(380, 'VR', 'Verona'),
    array(380, 'VS', 'Medio Campidano'),
    array(380, 'VT', 'Viterbo'),
    array(380, 'VV', 'Vibo Valentia'),
    array(380, 'RSM', 'Rep San Marino'),
    array(380, 'CTV', 'Città del Vaticano'),
  );

  $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(
    380,
    "!company\r\n".
    "!first_name !last_name\r\n".
    "!street1\r\n".
    "!street2\r\n".
    "!postal_code, !city\r\n".
    "!zone_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 italy_update($version) {
  switch ($version) {
    case 2:
      $zones = array(
        array(380, 'CI', 'Carbonia-Iglesias'),
        array(380, 'VS', 'Medio-Campidano'),
        array(380, 'OG', 'Ogliastra'),
        array(380, 'OT', 'Olbia-Tempio'),
      );

      $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:
      $zones = array(
        array(380, 'BT', 'Barletta-Andria-Trani'),
        array(380, 'FM', 'Fermo'),
        array(380, 'MB', 'Monza e Brianza'),
      );

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

      $zones = array(
        array(380, 'MS', 'Massa e Carrara'),
        array(380, 'PU', 'Pesaro e Urbino'),
        array(380, 'VB', 'Verbano-Cusio-Ossola'),
        array(380, 'VV', 'Vibo Valentia'),
      );

      foreach ($zones as $zone) {
        db_update('uc_zones')
          ->fields(array('zone_name' => $zone[2]))
          ->condition('zone_code', $zone[1])
          ->condition('zone_country_id', 380)
          ->execute();
      }

      break;
  }
}
