Index: openacs-4/packages/contacts/lib/email.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/contacts/lib/email.tcl,v diff -u -r1.12 -r1.13 --- openacs-4/packages/contacts/lib/email.tcl 28 Sep 2005 22:13:26 -0000 1.12 +++ openacs-4/packages/contacts/lib/email.tcl 24 Oct 2005 18:11:03 -0000 1.13 @@ -5,203 +5,415 @@ # @arch-tag: 48fe00a8-a527-4848-b5de-0f76dfb60291 # @cvs-id $Id$ -foreach required_param {party_ids recipients} { +foreach required_param {party_ids} { if {![info exists $required_param]} { return -code error "$required_param is a required parameter." } } -foreach optional_param {return_url file_ids} { + +foreach optional_param {return_url content export_vars file_ids object_id cc item_id} { if {![info exists $optional_param]} { set $optional_param {} } } +if {![info exists mime_type]} { + set mime_type "text/plain" +} +if {![info exists cancel_url]} { + set cancel_url $return_url +} -set form_elements { - message_id:key - party_ids:text(hidden) - return_url:text(hidden) - title:text(hidden),optional +if {![info exists no_callback_p]} { + set no_callback_p f } +# Somehow when the form is submited the party_ids values became +# only one element of a list, this avoid that problem + +set recipients [list] +foreach party_id $party_ids { + if {![empty_string_p $party_id]} { + lappend recipients [list "[contact::name -party_id $party_id] ([cc_email_from_party $party_id])" $party_id] + } +} + +# The element check_uncheck only calls a javascript function +# to check or uncheck all recipients +set recipients_num [llength $recipients] +if { $recipients_num <= 1 } { + set form_elements { + message_id:key + return_url:text(hidden) + no_callback_p:text(hidden) + title:text(hidden),optional + {message_type:text(hidden) {value "email"}} + {to:text(checkbox),multiple + {label "[_ contacts.Recipients]"} + {options $recipients } + {html {checked 1}} + {section "[_ contacts.Recipients]"} + } + {cc:text(text),optional + {label "[_ contacts.CC]:"} + {html {size 56}} + {help_text "[_ contacts.cc_help]"} + } + } +} else { + set form_elements { + message_id:key + return_url:text(hidden) + no_callback_p:text(hidden) + title:text(hidden),optional + {message_type:text(hidden) {value "email"}} + {check_uncheck:text(checkbox),multiple,optional + {label "[_ contacts.check_uncheck]"} + {options {{"" 1}}} + {section "[_ contacts.Recipients]"} + {html {onclick check_uncheck_boxes(this.checked)}} + } + {to:text(checkbox),multiple + {label "[_ contacts.Recipients]"} + {options $recipients } + {html {checked 1}} + } + } +} + + if { [exists_and_not_null file_ids] } { + set files [list] + foreach file $file_ids { + set file_title [lang::util::localize [db_string get_file_title { } -default "[_ contacts.Untitled]"]] + lappend files "$file_title " + } + set files [join $files ", "] + append form_elements { - file_ids:text(inform) + {files_display:text(inform),optional {label "[_ contacts.Associated_files]"} {value "$files" }} + {file_ids:text(hidden),optional {value $file_ids}} } } -append form_elements { - {message_type:text(hidden) {value "email"}} - {to:text(inform),optional {label "[_ contacts.Recipients]"} {value $recipients}} +foreach var $export_vars { + upvar $var var_value + + # We need to split to construct the element with two lappends + # becasue if we put something like this {value $value} the value + # of the variable is not interpreted + + set element [list] + lappend element "${var}:text(hidden)" + lappend element "value $var_value" + + # Adding the element to the form + lappend form_elements $element } +set content_list [list $content $mime_type] append form_elements { - {subject:text(text) + {subject:text(text),optional {label "[_ contacts.Subject]"} {html {size 55}} + {section "[_ contacts.Message]"} } - {content:text(textarea) + {content_body:text(richtext),optional {label "[_ contacts.Message]"} {html {cols 55 rows 18}} - {help_text {[_ contacts.lt_remember_that_you_can]}} + {value $content_list} + {help_text "[_ contacts.lt_remember_that_you_can]"} } - + {upload_file:file(file),optional + {label "[_ contacts.Upload_file]"} + } } -if { [parameter::get -boolean -parameter "EmailAttachmentsAllowed" -default "1"] } { +if { [exists_and_not_null item_id] } { append form_elements { - {upload_file:file(file),optional - {label "[_ contacts.Upload_File]"} + {item_id:text(hidden),optional + {value $item_id} } } } -ad_form -action message \ +if { ![exists_and_not_null action] } { + set action [ad_conn url] +} + +set edit_buttons [list [list [_ acs-mail-lite.Send] send]] + +ad_form -action $action \ -html {enctype multipart/form-data} \ -name email \ - -cancel_label "[_ contacts.Cancel]" \ - -cancel_url $return_url \ - -edit_buttons [list [list [_ contacts.Send] send]] \ + -cancel_label "[_ acs-kernel.common_Cancel]" \ + -cancel_url $cancel_url \ + -edit_buttons $edit_buttons \ -form $form_elements \ -on_request { } -new_request { - if {[exists_and_not_null folder_id]} { + if {[exists_and_not_null folder_id] } { callback contacts::email_subject -folder_id $folder_id } - if {[exists_and_not_null item_id]} { + if {[exists_and_not_null item_id] } { contact::message::get -item_id $item_id -array message_info set subject $message_info(description) - set content [ad_html_text_convert \ - -to "text/plain" \ - -from $message_info(content_format) \ - -- $message_info(content) \ - ] + set content_body [list $message_info(content) $message_info(content_format)] set title $message_info(title) } - if {[exists_and_not_null signature_id]} { + if {[exists_and_not_null signature_id] } { set signature [contact::signature::get -signature_id $signature_id] -# set signature [ad_convert_to_html -- "$signature"] if { [exists_and_not_null signature] } { - append content "\n\n" - append content $signature + append content_body "{

$signature } text/html" } } } -edit_request { } -on_submit { - set user_id [ad_conn user_id] - set from [contact::name -party_id $user_id] - set from_addr [contact::email -party_id $user_id] - template::multirow create messages message_type to_addr subject content party_id title to + + # We get the attribute_id of the salutation attribute + set attribute_id [db_string get_attribute_id { }] + + # List to store know wich emails recieved the message + set recipients_addr [list] + set from [ad_conn user_id] + set from_addr [cc_email_from_party $from] + + # Remove all spaces in cc + regsub -all " " $cc "" cc + + set cc_list [split $cc ";"] + + template::multirow create messages message_type to_addr to_party_id subject content_body + # Insert the uploaded file linked under the package_id - if { [parameter::get -boolean -parameter "EmailAttachmentsAllowed" -default "1"] } { - set filename [template::util::file::get_property filename $upload_file] - } else { - set filename "" - } set package_id [ad_conn package_id] - if {$filename != "" } { - set tmp_filename [template::util::file::get_property tmp_filename $upload_file] - set mime_type [template::util::file::get_property mime_type $upload_file] - set tmp_size [file size $tmp_filename] - set extension [contact::util::get_file_extension \ - -filename $filename] - if {![exists_and_not_null title]} { - regsub -all ".${extension}\$" $filename "" title - } -# set filename [contact::util::generate_filename \ - -title $title \ - -extension $extension \ - -party_id $party_id \ - ] - - set revision_id [cr_import_content \ - -storage_type "file" \ - -title $title \ - $package_id \ - $tmp_filename \ - $tmp_size \ - $mime_type \ - $filename \ - ] + + if {![empty_string_p $upload_file] } { + set revision_id [content::item::upload_file \ + -package_id $package_id \ + -upload_file $upload_file \ + -parent_id $party_id] - if {[exists_and_not_null file_ids]} { - append file_ids ",$revision_id" - } else { - set file_ids $revision_id - } - - content::item::set_live_revision -revision_id $revision_id + lappend file_ids $revision_id } - foreach party_id $party_ids { + # Send the mail to all parties. + foreach party_id $to { set name [contact::name -party_id $party_id] set first_names [lindex $name 0] set last_name [lindex $name 1] - set date [dt_sysdate] + set date [lc_time_fmt [dt_sysdate] "%q"] set to $name - set to_addr [contact::email -party_id $party_id] + set to_addr [cc_email_from_party $party_id] + set party_revision_id [contact::live_revision -party_id $party_id] + if { ![exists_and_not_null locale]} { + set locale [lang::user::site_wide_locale -user_id $party_id] + } + set salutation [ams::value \ + -attribute_id $attribute_id \ + -attribute_name "salutation" \ + -object_id $party_revision_id \ + -locale $locale] + lappend recipients_addr $to_addr + if {[empty_string_p $to_addr]} { - # We are going to check if this party_id has an employer and if this - # employer has an email - set employer_id [relation::get_object_two -object_id_one $party_id \ - -rel_type "contact_rels_employment"] - if { ![empty_string_p $employer_id] } { - # Get the employer email adress - set to_addr [contact::email -party_id $employer_id] - if {[empty_string_p $to_addr]} { - ad_return_error [_ contacts.Error] [_ contacts.lt_there_was_an_error_processing_this_request] + # We are going to check if this party_id has an employer and if this + # employer has an email + set employer_id [relation::get_object_two -object_id_one $party_id \ + -rel_type "contact_rels_employment"] + if { ![empty_string_p $employer_id] } { + # Get the employer email adress + set to_addr [cc_email_from_party -party_id $employer_id] + if {[empty_string_p $to_addr]} { + ad_return_error [_ acs-kernel.common_Error] [_ acs-mail-lite.lt_there_was_an_error_processing] break - } - } else { - ad_return_error [_ contacts.Error] [_ contacts.lt_there_was_an_error_processing_this_request] - break - } - } + } + } else { + ad_return_error [_ acs-mail-lite.Error] [_ acs-mail-lite.lt_there_was_an_error_processing] + break + } + } set values [list] - set locale [lang::user::site_wide_locale -user_id $party_id] - if {[empty_string_p $locale]} { - set locale [lang::user::site_wide_locale -user_id $user_id] - } - set date [lc_time_fmt [join [template::util::date::get_property linear_date_no_time $date] "-"] "%q" "$locale"] - set party_revision_id [contact::live_revision -party_id $party_id] - set salutation [ams::value -attribute_name "salutation" -object_id $party_revision_id -locale $locale] foreach element [list first_names last_name name date salutation] { lappend values [list "{$element}" [set $element]] } - template::multirow append messages $message_type $to_addr [contact::message::interpolate -text $subject -values $values] [contact::message::interpolate -text $content -values $values] $party_id $title $to - - # Link the file to all parties + template::multirow append messages $message_type $to_addr $party_id [acs_mail_lite::message_interpolate -text $subject -values $values] [acs_mail_lite::message_interpolate -text $content_body -values $values] + + # Link the files to all parties if {[exists_and_not_null revision_id]} { application_data_link::new -this_object_id $revision_id -target_object_id $party_id } } + + # Send the email to all CC in cc_list + foreach email_addr $cc_list { + set name $email_addr + set first_names [split $email_addr "@"] + set last_name $first_names + set date [lc_time_fmt [dt_sysdate] "%q"] + set to $name + set to_addr $email_addr + lappend recipients_addr $to_addr + set values [list] + foreach element [list first_names last_name name date] { + lappend values [list "{$element}" [set $element]] + } + set party_revision_id [contact::live_revision -party_id $party_id] + set salutation [ams::value \ + -attribute_id $attribute_id \ + -attribute_name "salutation" \ + -object_id $party_revision_id \ + -locale $locale] + + if {![empty_string_p $salutation]} { + lappend values [list "{salutation}" $salutation] + } + template::multirow append messages $message_type $to_addr "" [acs_mail_lite::message_interpolate -text $subject -values $values] [acs_mail_lite::message_interpolate -text $content_body -values $values] + + } - set recipients [list] + + set to_list [list] template::multirow foreach messages { + + lappend to_list [list $to_addr] + if {[exists_and_not_null file_ids]} { - acs_mail_lite::complex_send -to_addr $to_addr -from_addr "$from_addr" -subject "$subject" -body "$content" -package_id $package_id -file_ids $file_ids + # If the no_callback_p is set to "t" then no callback will be executed + if { $no_callback_p } { + + acs_mail_lite::complex_send \ + -to_addr $to_addr \ + -from_addr "$from_addr" \ + -subject "$subject" \ + -body "$content_body" \ + -package_id $package_id \ + -file_ids $file_ids \ + -mime_type $mime_type \ + -object_id $object_id \ + -no_callback_p + + } else { + + acs_mail_lite::complex_send \ + -to_addr $to_addr \ + -from_addr "$from_addr" \ + -subject "$subject" \ + -body "$content_body" \ + -package_id $package_id \ + -file_ids $file_ids \ + -mime_type $mime_type \ + -object_id $object_id + + } + } else { - acs_mail_lite::send -to_addr $to_addr -from_addr "$from_addr" -subject "$subject" -body "$content" -package_id $package_id + + # acs_mail_lite does not know about sending the + # correct mime types.... + if {$mime_type == "text/html"} { + + + if { $no_callback_p } { + # If the no_callback_p is set to "t" then no callback will be executed + acs_mail_lite::complex_send \ + -to_addr $to_addr \ + -from_addr "$from_addr" \ + -subject "$subject" \ + -body "$content_body" \ + -package_id $package_id \ + -mime_type $mime_type \ + -object_id $object_id \ + -no_callback_p + + } else { + + acs_mail_lite::complex_send \ + -to_addr $to_addr \ + -from_addr "$from_addr" \ + -subject "$subject" \ + -body "$content_body" \ + -package_id $package_id \ + -mime_type $mime_type \ + -object_id $object_id + + } + + } else { + + if { [exists_and_not_null object_id] } { + # If the no_callback_p is set to "t" then no callback will be executed + if { $no_callback_p } { + acs_mail_lite::complex_send \ + -to_addr $to_addr \ + -from_addr "$from_addr" \ + -subject "$subject" \ + -body "$content_body" \ + -package_id $package_id \ + -mime_type "text/html" \ + -object_id $object_id \ + -no_callback_p + } else { + + acs_mail_lite::complex_send \ + -to_addr $to_addr \ + -from_addr "$from_addr" \ + -subject "$subject" \ + -body "$content_body" \ + -package_id $package_id \ + -mime_type "text/html" \ + -object_id $object_id + } + } else { + + if { $no_callback_p } { + # If the no_callback_p is set to "t" then no callback will be executed + acs_mail_lite::send \ + -to_addr $to_addr \ + -from_addr "$from_addr" \ + -subject "$subject" \ + -body "$content_body" \ + -package_id $package_id \ + -no_callback_p + + } else { + acs_mail_lite::send \ + -to_addr $to_addr \ + -from_addr "$from_addr" \ + -subject "$subject" \ + -body "$content_body" \ + -package_id $package_id + } + + } + } } - - contact::message::log \ - -message_type "email" \ - -sender_id $user_id \ - -recipient_id $party_id \ - -title $title \ - -description $subject \ - -content $content \ - -content_format "text/plain" - lappend recipients "$to" - } - set recipients [join $recipients ", "] - util_user_message -html -message [_ contacts.Your_message_was_sent_to_-recipients-] + if { ![empty_string_p $to_party_id] && ![empty_string_p $item_id]} { + contact::message::log \ + -message_type "email" \ + -sender_id $from \ + -recipient_id $to_party_id \ + -title $title \ + -description $subject \ + -content $content_body \ + -content_format "text/plain" \ + -item_id "$item_id" + + lappend recipients "$to" + + } else { + lappend recipients "$to" + } + } + + set recipients [join $recipients_addr ", "] + util_user_message -html -message "[_ acs-mail-lite.Your_message_was_sent_to]" + } -after_submit { ad_returnredirect $return_url - ad_script_abort }