Index: openacs-4/packages/ams/tcl/address-widget-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/ams/tcl/address-widget-procs.tcl,v diff -u -r1.3 -r1.4 --- openacs-4/packages/ams/tcl/address-widget-procs.tcl 18 May 2005 17:11:48 -0000 1.3 +++ openacs-4/packages/ams/tcl/address-widget-procs.tcl 21 May 2005 17:49:23 -0000 1.4 @@ -84,7 +84,7 @@ ad_proc -public template::util::address::formats {} { Returns a list of valid address formats } { -# MGEDDERT NOTE: there needs to be a way to implement a way to portray addresses differently by country + # MGEDDERT NOTE: there needs to be a way to implement a way to portray addresses differently by country return { US CA DE } } @@ -108,6 +108,8 @@ } { set country_list [db_list_of_lists get_countries {}] set return_country_list [list] + set reserved_country_codes [parameter::get_from_package_key -parameter "DefaultISOCountryCode" -package_key "ams" -default ""] + foreach country $country_list { set this_locale $locale set country_name_db [lindex $country 0] @@ -123,18 +125,24 @@ } } # mgeddert customization for mbbs - if { [lsearch [list US CA] $country_code_db] < 0 } { + if { [lsearch $reserved_country_codes $country_code_db] < 0 } { # the reason not to use the "list" command here is because a curly bracket # needs to be used in the list for countries with a single word name # so that alphabetizing (via lsort) works later on in this proc lappend return_country_list "{[lang::message::lookup $this_locale $key]} {$country_code_db}" } } set country_code [list] - lappend country_code [list "United States" US] - lappend country_code [list "Canada" CA] - lappend country_code [list "--" ""] - append country_code " [lsort $return_country_list]" + if { [exists_and_not_null reserved_country_codes] } { + foreach code $reserved_country_codes { + set code [string toupper $code] + lappend country_code [list [lang::message::lookup $this_locale "ams.country_${code}"] $code] + } + lappend country_code [list "--" ""] + append country_code " [lsort $return_country_list]" + } else { + set country_code [lsort $return_country_list] + } return $country_code } @@ -150,27 +158,62 @@ set additional_text [template::util::address::get_property additional_text $address_list] set postal_type [template::util::address::get_property postal_type $address_list] - set message "" + set message [list] # this is used to make sure there are no invalid characters in the address set address_temp "$delivery_address $municipality $region $postal_code $country_code $additional_text $postal_type" if { [::string match "\{" $address_temp] || [::string match "\}" $address_temp] } { # for built in display purposes these characters are not allowed, if you need it # to be allowed make SURE that retrieval procs in AMS are also updated # to deal with this change - if { [exists_and_not_null message_temp] } { append message " " } - append message "[_ ams.Your_entry_must_not_contain_the_following_characters]: \{ \}." + lappend message "[_ ams.Your_entry_must_not_contain_the_following_characters]: \{ \}." } - if { $country_code == "US" } { - # this should check a cached list - # this proc cannot for some reason go in the postgresql file... - if { ![db_0or1row validate_state { - select 1 from us_states where abbrev = upper(:region) or state_name = upper(:region) -} ] } { - if { [exists_and_not_null message_temp] } { append message " " } - append message "\"$region\" [_ ams.is_not_a_valid_US_state]." + + # Country Specific Validation + switch $country_code { + CA { + # Canada + if { ![exists_and_not_null region] } { + lappend message "\"[_ ams.region]\" is required." + } + if { [exists_and_not_null postal_code] } { + if { ![regexp {^([A-Z][0-9][A-Z] [0-9][A-Z][0-9])$} $postal_code] } { + lappend message "\"$postal_code\" [_ ams.is_not_a_valid_Canadian_postal_code]." + } + } else { + lappend message "\"[_ ams.postal_code]\" is required." + } + if { ![exists_and_not_null municipality] } { + lappend message "\"[_ ams.municipality]\" is required." + } } + US { + # United States + if { [exists_and_not_null region] } { + # this should check a cached list + # this query for some reason cannot go in the address-widget-procs.xql file... + if { ![db_0or1row validate_state { + select 1 from us_states where abbrev = upper(:region) or state_name = upper(:region) + } ] } { + lappend message "\"$region\" [_ ams.is_not_a_valid_US_state]." + } + } else { + lappend message "\"[_ ams.region]\" is required." + } + if { [exists_and_not_null postal_code] } { + if { ![regexp {^([0-9]{5})(-([0-9]{4}))??$} $postal_code] } { + lappend message "\"$postal_code\" [_ ams.is_not_a_valid_US_zip_code]." + } + } else { + lappend message "\"[_ ams.postal_code]\" is required." + } + if { ![exists_and_not_null municipality] } { + lappend message "\"[_ ams.municipality]\" is required." + } + } } - if { [exists_and_not_null message_temp] } { + set message [join $message " "] + ns_log notice "MESSAGE: $message" + if { [exists_and_not_null message] } { return 0 } else { return 1 @@ -201,6 +244,53 @@ # as a non-value in case of a required element. return [list] } else { + if { $country_code == "US" } { + # since we have reference data installed we can automatically fill in these values for + # US States and Cities + if { [regexp {^([0-9]{5})(-([0-9]{4}))??$} $postal_code] } { + regexp {^([0-9]{5})(-([0-9]{4}))??$} $postal_code match zipcode + if { ![exists_and_not_null region] } { + set region [db_string get_region { + select abbrev + from us_states + where us_states.fips_state_code = ( select us_zipcodes.fips_state_code + from us_zipcodes + where zipcode = :zipcode ) + } -default {}] + } + if { ![exists_and_not_null municipality] } { + set municipality [db_string get_municipality { + select name + from us_zipcodes + where zipcode = :zipcode} -default {}] + } + } + } + if { $country_code == "CA" && [string length $postal_code] == "6" } { + set postal_list [split $postal_code {}] + set postal_code_temp "[lrange $postal_list 0 2] [lrange $postal_list 3 5]" + if { [regexp {^([A-Z][0-9][A-Z] [0-9][A-Z][0-9])$} $postal_code_temp] } { + set postal_code $postal_code_temp + } + } + if { $country_code == "US" || $country_code == "CA" } { + # make the city pretty + set municipality_temp [list] + foreach word $municipality { + # I am sure there are more then "Mc" words when they come up add them here + if { [regexp {^MC([a-zA-Z]+?)} [string toupper $word]] } { + lappend municipality_temp [join "Mc[string toupper [lindex [split $word {}] 2]][string tolower [lrange [split $word {}] 3 [llength [split $word {}]]]]" {}] + } else { + lappend municipality_temp [string totitle $word] + } + } + set municipality [join $municipality_temp " "] + + } + + + set postal_code [string toupper $postal_code] + return [list [list $delivery_address $municipality $region $postal_code $country_code $additional_text $postal_type]] } } @@ -346,7 +436,7 @@ set postal_code {} set municipality {} set region {} - set country_code [parameter::get -parameter "DefaultISOCountryCode" -default "US"] + set country_code [lindex [parameter::get_from_package_key -parameter "DefaultISOCountryCode" -package_key "ams" -default ""] 0] set additional_text {} set postal_type {} }