Index: openacs-4/packages/notes/www/add-edit.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/notes/www/add-edit.tcl,v diff -u -r1.7 -r1.8 --- openacs-4/packages/notes/www/add-edit.tcl 21 Sep 2003 22:16:52 -0000 1.7 +++ openacs-4/packages/notes/www/add-edit.tcl 10 Feb 2004 19:39:49 -0000 1.8 @@ -1,91 +1,123 @@ # packages/notes/www/add-edit.tcl ad_page_contract { - @author rhs@mit.edu + @author Don Baccus (dhogaza@pacifier.com) @creation-date 2000-10-23 @cvs-id $Id$ -} { + + Example script that allows for the creation or editing of a simple note + object type, using ad_form and package Tcl API tools. + +} -query { note_id:integer,notnull,optional } -properties { context:onevalue + page_title:onevalue } +# When using ad_form to generate or edit acs_objects, the object type's +# key column must be specified in ad_page_contract as is done above, +# and defined with the type "key" in ad_form. This enables the use of +# the various request and submission code blocks. + set package_id [ad_conn package_id] -if {[info exists note_id]} { - ad_require_permission $note_id write +ad_form -form { - set context [list "Edit Note"] -} else { - ad_require_permission $package_id create - - set context [list "New Note"] -} + # The "note" object type's key -template::form create new_note + {note_id:key} -if {[template::form is_request new_note] && [info exists note_id]} { + # "title" is of type text and will use a "text" widget. - template::element create new_note note_id \ - -widget hidden \ - -datatype number \ - -value $note_id + {title:text \ + {label Title} + {html {size 20}} + } - db_1row note_select { - select title, body - from notes - where note_id = :note_id - } -} else { - set title {} - set body {} -} + # "body" is of type text and will use a "textarea" widget. -template::element create new_note title \ - -datatype text \ - -label "Title" \ - -html { size 20 } \ - -value $title + {body:text(textarea) \ + {label Body} + {html {rows 10 cols 40 wrap soft}} + } -template::element create new_note body \ - -widget textarea \ - -datatype text \ - -label "Body" \ - -html { rows 10 cols 40 wrap soft } \ - -value $body +} -new_request { + # By convention packages only allow a user to create new objects if the user has + # the "create" privilege on the package instance itself. -if [template::form is_valid new_note] { - form get_values new_note title body + permission::require_permission -object_id $package_id -privilege create - set user_id [ad_conn user_id] - set peeraddr [ad_conn peeraddr] + # Customize the page title to reflect the fact that this form is used to + # create a new note. - if [info exists note_id] { - db_dml note_update { - update notes - set title = :title, - body = :body - where note_id = :note_id + set page_title "New Note" + +} -edit_request { + + permission::require_permission -object_id $note_id -privilege write + + # Customize the page title to reflect the fact that this form is used to + # edit an existing note. + + set page_title "Edit Note" + + # Fill the form with the values from the note we're editing. + + db_1row note_select {} + +} -on_validation_error { + + # There was an error in the form, let the page title reflect this. + + set page_title "Error in submission" + +} -new_data { + + # Create a new note. + + # Generate the new object automatically from the data set in the form. Standard + # acs_object attributes like creation_user are set automatically. + + package_instantiate_object -var_list [list [list context_id $package_id]] \ + -form_id add-edit \ + note + +} -edit_data { + + # Currently we need to update our object manually ... + + set modifying_user [ad_conn user_id] + set modifying_ip [ad_conn peeraddr] + + db_transaction { + db_dml object_update {} + db_dml note_update {} } - } else { - db_exec_plsql new_note { - declare - id integer; - begin - id := note.new( - owner_id => :user_id, - title => :title, - body => :body, - creation_user => :user_id, - creation_ip => :peeraddr, - context_id => :package_id - ); - end; - } - } - ad_returnredirect "./" +} -after_submit { + + # We've successfully processed the submission, send the user back to the index page. + + ad_returnredirect "./" + + # ad_returnredirect returns after redirecting the user, so abort the script rather + # than fall through to the display code. Failure to abort the script will burden + # your server with needless template processing, though the user won't notice due to + # having been redirected. + + ad_script_abort + } +# The following is only executed if we did not process a valid submission, in other +# words on the initial "new" or "edit" form request or after a submission which +# contained errors. Add the page title to the breadcrumb context bar. + +set context [list $page_title] + +# Display the form, blank if we're processing a "new" request, filled with data if we're +# processing an "edit" request or a submitted form that contains errors. + ad_return_template