Index: openacs-4/packages/acs-templating/tcl/data-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-templating/tcl/data-procs.tcl,v diff -u -r1.15 -r1.16 --- openacs-4/packages/acs-templating/tcl/data-procs.tcl 29 Aug 2007 14:01:33 -0000 1.15 +++ openacs-4/packages/acs-templating/tcl/data-procs.tcl 13 Nov 2010 23:55:58 -0000 1.16 @@ -20,14 +20,19 @@ @see template::data::validate::boolean @see template::data::validate::date @see template::data::validate::email + @see template::data::validate::enumeration @see template::data::validate::filename @see template::data::validate::float @see template::data::validate::integer @see template::data::validate::keyword @see template::data::validate::naturalnum + @see template::data::validate::number @see template::data::validate::search @see template::data::validate::string @see template::data::validate::text + @see template::data::validate::textdate + @see template::data::validate::timestamp + @see template::data::validate::time_of_day @see template::data::validate::url } { @@ -326,7 +331,78 @@ return [template::util::date::validate $value message] } +ad_proc -public template::data::validate::timestamp { + value_ref + message_ref +} { + Validate that a submitted date conforms to the template system's notion + of what a date should be. + @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 value + + return [template::util::date::validate $value message] +} + +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::data::validate::search { value_ref message_ref @@ -366,3 +442,76 @@ } } +ad_proc -public template::data::validate::number { + value_ref + message_ref +} { + Validate number - any float - should be any rational number? + + @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 value + + # Not allowing for scientific notation. Would the databases swallow it? + set result [regexp {^([+-]?)(?=\d|\.\d)\d*(\.\d*)?$} $value] + + if { ! $result } { + set message "Invalid number \"$value\"" + } + + return $result +} + +ad_proc -public template::data::validate::enumeration { + value_ref + message_ref +} { + Validate enumeration as a unique csv alphanum list. + + @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 value + + # alphanumeric csv + set result [regexp {^([A-z0-9]+,?)+$} $value] + + if { ! $result } { + set message "Invalid enumeration \"$value\"" + return $result + } + + # unique list + set list [split $value ,] + set result [expr [llength $list] == [llength [lsort -unique $list]]] + + if { ! $result } { + set message "Invalid enumeration. \"$value\" does not contain unique values." + } + + return $result +} + +ad_proc -public template::data::validate::time_of_day { + value_ref + message_ref +} { + Validate time of day. + + @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 value + + return [template::util::date::validate $value message] +} 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] +} + Index: openacs-4/packages/acs-templating/tcl/richtext-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/acs-templating/tcl/richtext-procs.tcl,v diff -u -r1.49 -r1.50 --- openacs-4/packages/acs-templating/tcl/richtext-procs.tcl 13 Sep 2009 23:54:42 -0000 1.49 +++ openacs-4/packages/acs-templating/tcl/richtext-procs.tcl 13 Nov 2010 23:55:58 -0000 1.50 @@ -13,6 +13,8 @@ namespace eval template::util {} namespace eval template::util::richtext {} namespace eval template::widget {} +namespace eval template::data::to_sql {} +namespace eval template::data::from_sql {} ad_proc -public template::util::richtext { command args } { Dispatch procedure for the richtext object @@ -613,3 +615,13 @@ return $output } + +# handle richtext transformations using a standardized naming convention. + +namespace eval template::util::richtext {} + +ad_proc template::data::to_sql::richtext { value } { + return "'[DoubleApos [list [template::util::richtext::get_property content $value] \ + [template::util::richtext::get_property format $value]]]'" +} +