ad_library { routines for creating, managing input via html forms @creation-date 21 Nov 2010 @cs-id $Id: } # use _ to clear a new default # use upvar to grab previous defaults and re-use (with qf_input only) # main namespace vars: # __form_input_arr = array that contains existing form input and defaults, only one form can be posted at a time # __form_ids_list = list that contains existing form ids # __form_ids_open_list = list that contains forms that are not closed # __form_ids_fieldset_open_list = list that contains form ids where a fieldset tag is open # __form_arr contains forms built as strings by appending tags to strings, indexed by form id, for example __form_arr($id) # __qf_arr contains last attribute values of tag, indexed by {tag}_attribute, __form_last_id is in __qf_arr(form_id) # a blank id passed in anything other than qf_form assumes the current (most recent used form_id) # to fix: id for not form tag should not be same as form id. add an attribute -form_id for assigning tags to specific forms. ad_proc -public qf_get_inputs { {-form_array_name __form_input_arr} } { get inputs from form submission } { upvar $form_array_name __form_input_arr # get form variables passed with connection set __form [ns_getform] if { $__form eq "" } { set __form_size 0 } else { set __form_size [ns_set size $__form] } for { set __form_counter_i 0 } { $__form_counter_i < $__form_size } { incr __form_counter_i } { # The name of the argument passed in the form set __form_key [ns_set key $__form $__form_counter_i] # This is the value set __form_input [ns_set value $__form $__form_counter_i] if { [info exists --form_input_arr($__form_key) ] } { if { $__form_input ne $__form_input_arr($__form_key) } { # which one is correct? log error ns_log Error "qf_get_form_input: form input error. duplcate key provided for ${__form_key}" ad_script_abort } else { ns_log Warning "qf_get_form_input: notice, form has two keys with same info.." } } else { set __form_input_arr($__form_key) [ns_set value $__form $__form_counter_i] } } } ad_proc -public qf_remember_attributes { } { changes qf_* form building procs to use the previous attribute values used with the last tag of same type (input,select,button etc). } { upvar __qf_remember_attributes __qf_remember_attributes set __qf_remember_attributes 1 } ad_proc -public qf_open { {-action ""} {-class ""} {-id ""} {-method "post"} {-name ""} {-style ""} {-target ""} {-title ""} } { initiates a form with form tag and supplied attributes. Returns an id. A clumsy url based id is provided if not passed (not recommended). } { set attributes_list [list action class id method name style target title] array set attributes_arr [list action $action class $class id $id method $method name $name style $style target $target title $title] # use upvar to set form content, set/change defaults # __qf_arr contains last attribute values of tag, indexed by {tag}_attribute, __form_last_id is in __qf_arr(form_id) upvar __form_ids_list __form_ids_list, __form_arr __form_arr upvar __qf_remember_attributes __qf_remember_attributes, __qf_arr __qf_arr if { ![info exists __qf_remember_attributes] } { set __qf_remember_attributes 0 } if { ![info exists __form_ids_list] } { set __form_ids_list [list] } # use previous tag attribute values? if { $__qf_remember_attributes } { foreach attribute $attributes_list { if { $attribute ne "id" && $attributes_arr($attribute) eq "" && [info exists __qf_arr(form_$attribute)] } { set attriubtes_arr($attribute) $__qf_arr(form_$attribute) } } } # every form gets an id, if only to help identify it in debugging if { $attributes_arr(id) eq "" } { set attributes_arr(id) "[ad_conn url]-[llength $__form_ids_list]" } # prepare attributes to process set tag_attributes_list [list] foreach attribute $attributes_list { set __qf_arr(form_$attribute) $attributes_arr($attribute) # if a form tag requires an attribute, the following test needs to be forced true if { $attributes_arr($attribute) ne "" } { lappend tag_attributes_list $attribute $attributes_arr($attribute) } } set tag_html "