Forum OpenACS Development: Re: ad_form for new and edit requests
Posted by
Joel Aufrecht
on 02/10/04 12:19 PM
Okay, here's an example of a view/edit page built on ad_form. A functional description would be:
- Record: word
- Fields
- word:text
- locale:lookup to ad_locale.
- default to system locale for new records.
- ipa_phonetic:text
- local_phonetic:text
- only show this field if word's locale has a "local phonetic alphabet" defined.
- If creating a new word, determine based on system locale
- Field title is dynamic, based on locale
And the code (actual running code; I've just omitted some irrelevant bits)
ad_page_contract { Form for editing individual words } { id:integer,optional {return_url ""} } set page_title "Edit" set context [list $page_title] set package_id [ad_conn package_id] # catch missing local_phonetic when submitting from a form # where that field was hidden if {![exists_and_not_null local_phonetic] } { set local_phonetic "" } set working_locale [lang::user::locale] # pre-query for the object's locale, which can override working_locale # yes, we're querying the database twice for the same thing # that's still faster than hacking ad_form if { [exists_and_not_null id]} { set working_locale [db_string get_locale " select locale from vocab_word where id = :id " -default ""] } set phonetic_alphabet [vocab::phonetic_alphabet_for_locale -locale $working_locale] set locale_options [vocab::locale_list -all all] set form_mode [ad_decode [ad_form_new_p -key id] 1 "edit" "display"] ad_form \ -name word \ -mode $form_mode \ -form { {id:key} {word:text {label Word} } {locale:text(select) {label Locale} {options $locale_options} } {ipa_phonetic:text,optional {label "IPA"} } } if { ![string equal $phonetic_alphabet none]} { ad_form -extend -name word -form { {local_phonetic:text,optional {label "[set phonetic_alphabet]"} } } } ad_form -extend -name word \ -new_request { set page_title "Add a Word" set context [list $page_title] # this pre-sets the form locale set locale $working_locale } -edit_request { db_1row word_select { select word, locale, ipa_phonetic, local_phonetic from vocab_word where id = :id } set page_title "$word" set context [list $page_title] # this may be redundant with the prefetch set working_locale $locale set phonetic_alphabet [vocab::phonetic_alphabet_for_locale -locale $working_locale] } -new_data { set user_id [auth::require_login] set new_id [db_string word_insert " select vocab_word__new(:package_id, :word, :locale, :ipa_phonetic, :local_phonetic, :user_id,null) "] if {![exists_and_not_null return_url]} { #TODO: new_id is the wrong thing to redirect to, so this breaks # set return_url [export_vars -base word-edit {{id $new_id}}] set return_url "." } ad_returnredirect $return_url } -edit_data { auth::require_login db_dml word_edit { update vocab_word set word = :word, locale = :locale, ipa_phonetic = :ipa_phonetic, local_phonetic = :local_phonetic where id = :id } } -after_submit { ad_returnredirect $return_url ad_script_abort }