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.46 -r1.47 --- openacs-4/packages/acs-templating/tcl/date-procs.tcl 17 Oct 2010 21:06:09 -0000 1.46 +++ openacs-4/packages/acs-templating/tcl/date-procs.tcl 13 Nov 2010 23:55:58 -0000 1.47 @@ -15,10 +15,13 @@ namespace eval template::data {} namespace eval template::data::validate {} namespace eval template::util {} +namespace eval template::util::timestamp {} namespace eval template::util::date {} namespace eval template::util::textdate {} namespace eval template::widget {} namespace eval template::data::transform {} +namespace eval template::data::to_sql {} +namespace eval template::data::from_sql {} ad_proc -public template::util::date { command args } { Dispatch procedure for the date object @@ -1252,59 +1255,6 @@ return $value } } - -ad_proc -public template::data::validate::textdate { - value_ref - message_ref -} { - Validate that a submitted textdate if properly formatted. - - @param value_ref Reference variable to the submitted value. - @param message_ref Reference variable for returning an error message. - - @return True (1) if valid, false (0) if not. -} { - - 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 [_ acs-templating.Month_must_be_between_1_and_12] - } else { - set maxdays [template::util::date::get_property days_in_month $datelist] - if { $day < 1 || $day > $maxdays } { - set month_pretty [template::util::date::get_property long_month_name $datelist] - if { $month == "2" } { - # February has a different number of days depending on the year - append month_pretty " ${year}" - } - lappend error_msg [_ acs-templating.lt_day_between_for_month_pretty] - } - } - } - } else { - # the textdate is not formatted properly - set format [::string toupper [template::util::textdate_localized_format]] - lappend error_msg [_ acs-templating.lt_Dates_must_be_formatted_] - } - } - 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. @@ -1350,3 +1300,70 @@ return $output } + +# handle date transformations using a standardized naming convention. + +ad_proc template::data::to_sql::date { value } { +} { + return [template::util::date::get_property sql_date $value] +} + +ad_proc template::data::from_sql::date { value } { +} { + return [template::util::date::acquire ansi $value] +} + +# The abstract type system includes a timestamp type, so we need to implement one +# in the template "data type" system (even though in reality it should really just +# be a widget working on the abstract type "date", or "timestamp" should replace "date") + +ad_proc template::data::to_sql::timestamp { value } { +} { + return [template::data::to_sql::date $value] +} + +ad_proc template::data::from_sql::timestamp { value } { +} { + return [template::data::from_sql::date $value] +} + +ad_proc -public template::data::transform::timestamp { element_ref } { + Collect a timestamp object from the form +} { + upvar $element_ref element + return [template::data::transform::date element] +} + +ad_proc -public template::util::timestamp::set_property { what date value } { + + get a property in a list created by a timestamp widget. It's the same + as the date one. + + This is needed by the form builder to support explicit from_sql element modifiers. + +} { + return [template::util::date::set_property $what $date $value] +} + +ad_proc -public template::util::timestamp::get_property { what date } { + + Replace a property in a list created by a timestamp widget. It's the same + as the date one. + + This is needed by the form builder to support explicit to_sql element modifiers. +} { + return [template::util::date::get_property $what $date] +} + +ad_proc -public template::widget::timestamp { element_reference tag_attributes } { + Render a timestamp widget. Default is the localized version. +} { + + upvar $element_reference element + + if { ! [info exists element(format)] } { + set element(format) "[_ acs-lang.localization-formbuilder_date_format] [_ acs-lang.localization-formbuilder_time_format]" + } + return [template::widget::date element $tag_attributes] +} +