Index: openacs-4/packages/acs-templating/tcl/date-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-templating/tcl/date-procs.tcl,v diff -u -r1.35.2.3 -r1.35.2.4 --- openacs-4/packages/acs-templating/tcl/date-procs.tcl 13 Oct 2005 17:42:39 -0000 1.35.2.3 +++ openacs-4/packages/acs-templating/tcl/date-procs.tcl 27 Apr 2006 00:17:41 -0000 1.35.2.4 @@ -13,8 +13,10 @@ namespace eval template {} namespace eval template::data {} +namespace eval template::data::validate {} namespace eval template::util {} namespace eval template::util::date {} +namespace eval template::util::textdate {} namespace eval template::widget {} namespace eval template::data::transform {} @@ -1135,3 +1137,208 @@ } } +ad_proc -public template::util::textdate { command args } { + Dispatch procedure for the textdate object +} { + eval template::util::textdate::$command $args +} + +ad_proc -public template::util::textdate_localized_format {} { + Gets the localized format for the textdate widget +} { + # we get the date format for the connected locale from acs-lang.localization-d_fmt + # as of the time of writing this proc the following were by default available that + # would work with this proc, and this should cover most installations, if this + # format isn't matched we will use the iso standard YYYY-MM-DD. + # + # %d-%m-%y %d.%m.%y %d/%m-%y %d/%m/%y %m/%d/%y %y-%m-%d %y.%m.%d "%d-%m-%y" + + set format [lc_get "d_fmt"] + regsub -all -nocase {\"} $format {} format + regsub -all -nocase {\%} $format {} format + set format [string tolower $format] + # this format key must now be at max five characters, and contain one y, one m and one d + # as well as two punction marks ( - . / ) + if { [regexp {^([y|m|d])([\-|\.|/])([y|m|d])([\-|\.|/])([y|m|d])} $format match first first_punct second second_punct third] && [string length $format] eq "5" } { + if { [lsort [list $first $second $third]] eq "d m y" } { + # we have a valid format from acs-lang.localization-d_fmt with all 3 necessary elements + # and only two valid punctuation marks + regsub {d} $format {dd} format + regsub {m} $format {mm} format + regsub {y} $format {yyyy} format + return $format + } + } + + # we use the iso standard + return "yyyy-mm-dd" +} + +ad_proc -public template::util::textdate::create { + {textdate {}} +} { + return $textdate +} + +ad_proc -public template::data::transform::textdate { element_ref } { + Collect a textdate from the form, it automatically + reformats it from the users locale to the iso standard + YYYY-MM-DD this is useful because it doesn't need + reformatting in tcl code +} { + + upvar $element_ref element + set element_id $element(id) + set value [ns_queryget "$element_id"] + + if { $value eq "" } { + # they didn't enter anything + return "" + } + + # we get the format they need to use + set format [template::util::textdate_localized_format] + set exp $format + regsub -all {(\-|\.|/)} $exp {(\1)} exp + regsub -all {dd|mm} $exp {([0-9]{1,2})} exp + regsub -all {yyyy} $exp {([0-9]{2,4})} exp + + # results is what comes out in a regexp + set results $format + regsub {\-|\.|/} $results { format_one} results + regsub {\-|\.|/} $results { format_two} results + regsub {mm} $results { month} results + regsub {dd} $results { day} results + regsub {yyyy} $results { year} results + set results [string trim $results] + + if { [regexp {([\-|\.|/])yyyy$} $format match year_punctuation] } { + # we might be willing to accept this date if it doesn't have a year + # at the end, since we can assume that the year is the current one + # this is useful for fast keyboard based date entry for formats that + # have years at the end (such as in en_US which is mm/dd/yyyy or + # de_DE which is dd.mm.yyyy) + + # we check if adding the year and punctuation makes it a valid date + set command "regexp {$exp} \"\${value}\${year_punctuation}\[dt_sysdate -format %Y\]\" match $results" + if { [eval $command] } { + if { ![catch { clock scan "${year}-${month}-${day}" }] } { + # we add the missing year and punctuation to the value + # we don't return it here because formatting is done + # later on (i.e. adding leading zeros if needed) + append value "${year_punctuation}[dt_sysdate -format %Y]" + } + } + } + + # now we verify that we have a valid date + # and adding leading/trailing zeros if needed + set command "regexp {$exp} \"\${value}\" match $results" + if { [eval $command] } { + # the regexp will have given us: year month day format_one format_two + if { [string length $month] eq "1" } { + set month "0$month" + } + if { [string length $day] eq "1" } { + set day "0$day" + } + if { [string length $year] eq "2" } { + # we'll copy microsoft excel's default assumptions + # about the year it is so if the year is 29 or + # lower its in this century otherwise its last century + if { $year < 30 } { + set year "20$year" + } else { + set year "19$year" + } + } + return "${year}-${month}-${day}" + } else { + # they did not provide a correctly formatted date so we send it back to them + return $value + } +} + +ad_proc -public template::data::validate::textdate { value_ref message_ref } { + + upvar 2 $message_ref message $value_ref textdate + set error_msg [list] + if { [exists_and_not_null textdate] } { + if { [regexp {^[0-9]{4}-[0-9]{2}-[0-9]{2}$} $textdate match] } { + if { [catch { clock scan "${textdate}" }] } { + # the textdate is formatted properly the template::data::transform::textdate proc + # will only return correctly formatted dates in iso format, but the date is not + # valid so they have entered some info incorrectly + set datelist [split $textdate "-"] + set year [lindex $datelist 0] + set month [::string trimleft [lindex $datelist 1] 0] + set day [::string trimleft [lindex $datelist 2] 0] + if { $month < 1 || $month > 12 } { + lappend error_msg "Month must be between 1 and 12" + } else { + + set maxdays [template::util::date::get_property days_in_month $datelist] + if { $day < 1 || $day > $maxdays } { + if { $month == "2" } { + lappend error_msg "The day must be between 1 and $maxdays for the month of [template::util::date::get_property long_month_name $datelist] ${year}" + } else { + lappend error_msg "The day must be between 1 and $maxdays for the month of [template::util::date::get_property long_month_name $datelist]" + } + } + } + # we do another check just in case the above stuff didn't catch a problem + if { [llength $error_msg] == "0" } { + lappend error_msg "The date specified is not valid" + } + } + } else { + # the textdate is not formatted properly + set format [::string toupper [template::util::textdate_localized_format]] + lappend error_msg "Dates must be formatted as: ${format}" + } + } + if { [llength $error_msg] > 0 } { + set message "[join $error_msg {
}]" + return 0 + } else { + return 1 + } +} + +ad_proc -public template::widget::textdate { element_reference tag_attributes } { + Implements the textdate widget. + +} { + + upvar $element_reference element + + if { [info exists element(value)] } { + set textdate $element(value) + if { [regexp {^([0-9]{4})-([0-9]{2})-([0-9]{2})$} $textdate match year month day] } { + # we have a correctly formatted iso date that we + # can reformat for display, we don't use lc_time_fmt + # because it could fail and cause a server error. + # The date may be formatted correctly but it may be + # an invalid date (which is caught by + # template::data::validate::textdate) so we need to + # re-format the input into the format the user specified + # by this means + set textdate [template::util::textdate_localized_format] + regsub {yyyy} $textdate $year textdate + regsub {mm} $textdate $month textdate + regsub {dd} $textdate $day textdate + } + } else { + set textdate "" + } + + if { [string equal $element(mode) "edit"] } { + append output "" + append output "" + } else { + append output $textdate + append output "" + } + + return $output +}