From fe37f4a2897a8f99416257e0efe873e66f3f15ba Mon Sep 17 00:00:00 2001
From: Marco Villegas <marvil07@gmail.com>
Date: Fri, 22 Mar 2013 13:06:56 -0500
Subject: [PATCH] Initial patch

---
 plugins/format/address.inc |   51 +++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 48 insertions(+), 3 deletions(-)

diff --git a/plugins/format/address.inc b/plugins/format/address.inc
index 6fe37df..1a63b8d 100644
--- a/plugins/format/address.inc
+++ b/plugins/format/address.inc
@@ -70,7 +70,7 @@ function addressfield_format_address_generate(&$format, $address, $context = arr
   );
 
   // Those countries do not seem to have a relevant postal code.
-  if (in_array($address['country'], array('AF', 'AG', 'AL', 'AO', 'BB', 'BI', 'BJ', 'BO', 'BS', 'BW', 'BZ', 'CF', 'CG', 'CM', 'CO', 'DJ', 'DM', 'EG', 'ER', 'FJ', 'GD', 'GH', 'GM', 'GQ', 'GY', 'IE', 'KI', 'KM', 'KP', 'KY', 'LC', 'LY', 'ML', 'MR', 'NA', 'NR', 'RW', 'SB', 'SC', 'SL', 'SR', 'ST', 'TD', 'TG', 'TL', 'TO', 'TT', 'TV', 'TZ', 'UG', 'VC', 'VU', 'WS', 'ZW'))) {
+  if (in_array($address['country'], array('AF', 'AG', 'AL', 'AO', 'BB', 'BI', 'BJ', 'BO', 'BS', 'BW', 'BZ', 'CF', 'CG', 'CM', 'CO', 'DJ', 'DM', 'EG', 'ER', 'FJ', 'GD', 'GH', 'GM', 'GQ', 'GY', 'IE', 'KI', 'KM', 'KP', 'KY', 'LC', 'LY', 'ML', 'MR', 'NA', 'NR', 'PE', 'RW', 'SB', 'SC', 'SL', 'SR', 'ST', 'TD', 'TG', 'TL', 'TO', 'TT', 'TV', 'TZ', 'UG', 'VC', 'VU', 'WS', 'ZW'))) {
     unset($format['locality_block']['postal_code']);
 
     // Remove the prefix from the first widget of the block.
@@ -80,7 +80,7 @@ function addressfield_format_address_generate(&$format, $address, $context = arr
   }
 
   // Those countries generally use their administrative division in postal addresses.
-  if (in_array($address['country'], array('AR', 'AU', 'BR', 'BS', 'BY', 'BZ', 'CA', 'CN', 'DO', 'EG', 'ES', 'FJ', 'FM', 'GB', 'HN', 'ID', 'IE', 'IN', 'IT', 'JO', 'JP', 'KI', 'KN', 'KR', 'KW', 'KY', 'KZ', 'MX', 'MY', 'MZ', 'NG', 'NI', 'NR', 'NZ', 'OM', 'PA', 'PF', 'PG', 'PH', 'PR', 'PW', 'RU', 'SM', 'SO', 'SR', 'SV', 'TH', 'TW', 'UA', 'US', 'UY', 'VE', 'VI', 'VN', 'YU', 'ZA'))) {
+  if (in_array($address['country'], array('AR', 'AU', 'BR', 'BS', 'BY', 'BZ', 'CA', 'CN', 'DO', 'EG', 'ES', 'FJ', 'FM', 'GB', 'HN', 'ID', 'IE', 'IN', 'IT', 'JO', 'JP', 'KI', 'KN', 'KR', 'KW', 'KY', 'KZ', 'MX', 'MY', 'MZ', 'NG', 'NI', 'NR', 'NZ', 'OM', 'PA', 'PE', 'PF', 'PG', 'PH', 'PR', 'PW', 'RU', 'SM', 'SO', 'SR', 'SV', 'TH', 'TW', 'UA', 'US', 'UY', 'VE', 'VI', 'VN', 'YU', 'ZA'))) {
     $format['locality_block']['administrative_area'] = array(
       '#title' => t('State'),
       '#size' => 10,
@@ -337,9 +337,54 @@ function addressfield_format_address_generate(&$format, $address, $context = arr
       'WA' => t('Western Australia'),
     );
   }
+  elseif ($address['country'] == 'PE') {
+    // Peru has official national postal codes since 2011[1], but it is not
+    // really common yet and it does not respect department/province/district,
+    // so some districts are now split in diferent postal codes or join with
+    // others. Let's use ISO_3166-2[2] for deparments as current code in this
+    // module, and INEI's ubigeo[3] for province and districts.
+    // For now, let's define only:
+    // - Lima deparment Lima province districts.
+    // - Callao department Callao province districts.
+    //
+    // [1] http://www.mtc.gob.pe/portal/CPOSTAL/inicio_codigo_postal2.html
+    // [2] http://en.wikipedia.org/wiki/ISO_3166-2:PE
+    // [3] http://iinei.inei.gob.pe/iinei/siscodes/UbigeoMarco.htm
+    $format['locality_block']['administrative_area']['#title'] = t('Department');
+    $format['locality_block']['administrative_area']['#options'] = array(
+      ''   => t('--'),
+      'AMA' => t('Amazonas'),
+      'ANC' => t('Ancash'),
+      'APU' => t('Apurimac'),
+      'ARE' => t('Arequipa'),
+      'AYA' => t('Ayacucho'),
+      'CAJ' => t('Cajamarca'),
+      'CAL' => t('Callao'),
+      'CUS' => t('Cusco'),
+      'HUV' => t('Huancavelica'),
+      'HUC' => t('Huanuco'),
+      'ICA' => t('Ica'),
+      'JUN' => t('Junin'),
+      'LAL' => t('La Libertad'),
+      'LAM' => t('Lambayeque'),
+      'LIM' => t('Lima'),
+      'LOR' => t('Loreto'),
+      'MDD' => t('Madre de Dios'),
+      'MOQ' => t('Moquegua'),
+      'PAS' => t('Pasco'),
+      'PIU' => t('Piura'),
+      'PUN' => t('Puno'),
+      'SAM' => t('San Martin'),
+      'TAC' => t('Tacna'),
+      'TUM' => t('Tumbes'),
+      'UCA' => t('Ucayali'),
+    );
+    $format['locality_block']['sub_administrative_area']['#title'] = t('Province');
+    $format['locality_block']['locality']['#title'] = t('District');
+  }
 
   // Those countries tend to put the postal code after the locality.
-  if (in_array($address['country'], array('AU', 'BD', 'BF', 'BH', 'BM', 'BN', 'BT', 'CA', 'FM', 'GB', 'ID', 'IN', 'JM', 'JO', 'KH', 'LB', 'LS', 'LV', 'MM', 'MN', 'MV', 'MW', 'NG', 'NP', 'NZ', 'PE', 'PK', 'PR', 'PW', 'SA', 'SG', 'SO', 'TH', 'US', 'VI', 'VG', 'VN'))) {
+  if (in_array($address['country'], array('AU', 'BD', 'BF', 'BH', 'BM', 'BN', 'BT', 'CA', 'FM', 'GB', 'ID', 'IN', 'JM', 'JO', 'KH', 'LB', 'LS', 'LV', 'MM', 'MN', 'MV', 'MW', 'NG', 'NP', 'NZ', 'PK', 'PR', 'PW', 'SA', 'SG', 'SO', 'TH', 'US', 'VI', 'VG', 'VN'))) {
     // Take the widget out of the array.
     $postal_code_widget = $format['locality_block']['postal_code'];
     $postal_code_widget['#prefix'] = ' ';
-- 
1.7.10.4

