Forum OpenACS Development: Re: Trying to get it right with ad_form

Collapse
Posted by Janine Ohmer on
Ok, here we go:

element.tcl:

ad_page_contract {
    edit a static element

    @author arjun (arjun@openforce)
    @cvs_id $Id: element.tcl,v 1.2 2003/05/30 16:15:56 janine Exp $
} -query {
    content_id:optional
    referer:notnull
    portal_id:integer,notnull
    {package_id:integer ""}
}  -properties {
    title:onevalue
}

set element_pretty_name [ad_parameter static_admin_portlet_element_pretty_name static-portlet]
if { [ad_form_new_p -key content_id] } {
  set title "New $element_pretty_name"
  set new_p 1
} else {
  set title "Edit $element_pretty_name"
  set new_p 0
}

#these are set for display and instructions.
set community_id $package_id

set portal_name [portal::get_name $portal_id]

ad_form -name static_element -form {
    content_id:key
    {pretty_name:text(text)     {label "Name"} {html {size 60}}}     {content:text(textarea)     {label "Content"} {html {rows 15 cols 80 wrap soft}}} 
    {portal_id:text(hidden)     {value $portal_id}}
    {package_id:text(hidden)    {value $package_id}}
    {referer:text(hidden)       {value $referer}}
} -select_query_name get_content_element -validate {
} -new_data {
    db_transaction {
        set item_id [static_portal_content::new \ 
                         -package_id $package_id  \
                         -content $content \
                         -pretty_name $pretty_name
        ]
        
        static_portal_content::add_to_portal \
            -portal_id $portal_id \ 
            -package_id $package_id \
            -content_id $item_id
    }
        
    # redirect and abort
    ad_returnredirect $referer
    ad_script_abort
} -edit_data {
    db_transaction {
        static_portal_content::update \
                -portal_id $portal_id \ 
                -content_id $content_id \ 
                -pretty_name $pretty_name \
                -content $content
    }
    
    # redirect and abort
    ad_returnredirect $referer
    ad_script_abort
}


ad_form -name static_file -html {enctype multipart/form-data} -form {
    content_id:key
    {pretty_name:text(text)     {label "Name"} {html {size 60}}}
    {upload_file:file           {label "File"}}
    {portal_id:text(hidden)     {value $portal_id}}
    {package_id:text(hidden)    {value $package_id}}
    {referer:text(hidden)       {value $referer}}
} -select_query_name get_content_element -validate {
} -new_data {
    set filename [template::util::file::get_property filename $upload_file]    set tmp_filename [template::util::file::get_property tmp_filename $upload_file]   
    set mime_type [template::util::file::get_property mime_type $upload_file]    if { [string equal -length 4 "text" $mime_type] || [string length $mime_type] == 0 } {
      # it's a text file, we can do something with this
      set fd [open $tmp_filename "r"]
      set content [read $fd]
      close $fd
    } else { 
      # they probably wanted to attach this file, but we can't do that.      set content "Binary file $filename was uploaded but we only support text files here"
    }
    
    db_transaction {
        set item_id [static_portal_content::new \ 
                         -package_id $package_id  \
                         -content $content \
                         -pretty_name $pretty_name
        ]
        static_portal_content::add_to_portal \
            -portal_id $portal_id \ 
            -package_id $package_id \
            -content_id $item_id
    }
        
    # redirect and abort
    ad_returnredirect $referer
    ad_script_abort
} -edit_data {
    set filename [template::util::file::get_property filename $upload_file]
    set tmp_filename [template::util::file::get_property tmp_filename $upload_file]       set mime_type [template::util::file::get_property mime_type $upload_file]
    if { [string equal -length 4 "text" $mime_type] || [string length $mime_type] == 0 } {
      # it's a text file, we can do something with this
      set fd [open $tmp_filename "r"]
      set content [read $fd]
      close $fd
    } else {      # they probably wanted to attach this file, but we can't do that.
      set content "Binary file $filename was uploaded but we only support text files here"
    }
        
    db_transaction {
        static_portal_content::update \ 
                -portal_id $portal_id \
                -content_id $content_id \
                -pretty_name $pretty_name \
                -content $content
    } 
    
    # redirect and abort
    ad_returnredirect $referer
    ad_script_abort
}
I was going to include element.adp also but the HTML filter goes haywire on all the things it thinks are invalid tags, like master. Let me know if you need to see it and I'll send it via e-mail.

If I've done anything wrong here please let me know, even if it's unrelated to my question - this is my first time using ad_form and I may well have gotten something wrong.

Now that I've posted all this I might as well ask if there's any objection to my committing this change to the toolkit? Seems like it would be generally useful.

Thanks!