X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=httemplate%2Fmisc%2Femail-customers.html;h=d086c676d4a0afb679b82f0a7f6a2fc00192b0e4;hb=dbf521cf2f1f1a05f4722006f339b2595c8ee70f;hp=8ac44afc1b0c5446015d6fa8f80fbe7dd0fb09b5;hpb=866994096d21b914815cb06353397dc4d00438f3;p=freeside.git diff --git a/httemplate/misc/email-customers.html b/httemplate/misc/email-customers.html index 8ac44afc1..d086c676d 100644 --- a/httemplate/misc/email-customers.html +++ b/httemplate/misc/email-customers.html @@ -1,13 +1,14 @@ <%doc> -Allows emailing one or more customers, based on a search for customers. Search can -be specified either through cust_main fields as cgi params, or through a base64 encoded -frozen hash in the 'search' cgi param. Form allows selecting an existing msg_template, -or creating a custom message, and shows a preview of the message before sending. -If linked to as a popup, include the cgi parameter 'popup' for proper header handling. - -This may also be used as an element in other pages, enabling you to provide an -alternate initial form while using this for search freezing/thawing and +Allows emailing one or more customers, based on a search for customers. +Search can be specified either through cust_main fields as cgi params, or +through a base64 encoded frozen hash in the 'search' cgi param. Form allows +selecting an existing msg_template, or creating a custom message, and shows a +preview of the message before sending. If linked to as a popup, include the +cgi parameter 'popup' for proper header handling. + +This may also be used as an element in other pages, enabling you to provide +an alternate initial form while using this for search freezing/thawing and preview/send actions, with the following options: acl - the access right to use (defaults to 'Bulk send customer notices') @@ -21,12 +22,13 @@ title - the title of the page no_search_fields - arrayref of additional fields that are not search parameters alternate_form - subroutine that returns alternate html for the initial form, -replaces msgnum/from/subject/html_body/action inputs and submit button, -not used if an action is specified +replaces msgnum/from/subject/body/action inputs and submit button, not +used if an action is specified -post_search_hook - sub hook for additional processing after search has been processed from cgi, -gets passed options 'conf' and 'search' (a reference to the unfrozen %search hash), -should be used to set msgnum or from/subject/html_body cgi params +post_search_hook - sub hook for additional processing after search has been +processed from cgi, gets passed options 'conf' and 'search' (a reference to +the unfrozen %search hash), should be used to set msgnum or +from/subject/body cgi params %doc> % if ($popup) { @@ -35,6 +37,7 @@ should be used to set msgnum or from/subject/html_body cgi params <& /elements/header.html, $title &> % } +<& /elements/error.html &>
-% if ( $cgi->param('action') eq 'send' ) { - -% } - <& /elements/footer.html &> <%init> @@ -215,7 +231,7 @@ die "access denied" unless $FS::CurrentUser::CurrentUser->access_right($opt{'acl'}); my $conf = FS::Conf->new; -my @no_search_fields = qw( action table from subject html_body text_body popup url ); +my @no_search_fields = qw( table from subject html_body text_body popup url ); my $form_action = $opt{'form_action'} || 'email-customers.html'; my $process_url = $opt{'process_url'} || 'process/email-customers.html'; @@ -259,12 +275,29 @@ if ( $cgi->param('from') ) { $from = $cgi->param('from_addr'); } -my $subject = $cgi->param('subject') || ''; -my $html_body = $cgi->param('html_body') || ''; - my $msg_template = ''; +if ( $cgi->param('msgnum') =~ /^(\d+)$/ ) { + $msg_template = FS::msg_template->by_key($1) + or die "template not found: ".$cgi->param('msgnum'); +} + +my @contact_classnum; +my @contact_classname; + +my $subject = $cgi->param('subject'); +my $body = $cgi->param('body'); +my ($html_body, $text_body); + +if ( !$cgi->param('preview') ) { -if ( $cgi->param('action') eq 'preview' ) { + # edit mode: initialize the fields from the saved draft, if there is one + if ( $msg_template and $msg_template->disabled eq 'D' ) { + my $content = $msg_template->content(''); # no localization on these yet + $subject ||= $content->subject; + $body ||= $content->body; + } + +} else { my $sql_query = "FS::$table"->search(\%search); my $count_query = delete($sql_query->{'count_query'}); @@ -275,10 +308,40 @@ if ( $cgi->param('action') eq 'preview' ) { my $count_arrayref = $count_sth->fetchrow_arrayref; $num_cust = $count_arrayref->[0]; - if ( $cgi->param('msgnum') ) { - $msg_template = qsearchs('msg_template', - { msgnum => scalar($cgi->param('msgnum')) } ) - or die "template not found: ".$cgi->param('msgnum'); + if ( !$msg_template or $msg_template->disabled eq 'D' ) { + # then this is a one-off template; edit it in place + my $subject = $cgi->param('subject') || ''; + my $body = $cgi->param('body') || ''; + + # create a draft template + $msg_template ||= FS::msg_template->new({ + msgclass => 'email', + disabled => 'D', + }); + # anyone have a better idea for msgname? + $msg_template->set('msgname' => "Notice " . DateTime->now->iso8601); + $msg_template->set('from_addr' => $from); + my %content = ( + subject => $subject, + body => $body, + ); + my $error; + if ( $msg_template->msgnum ) { + $error = $msg_template->replace(%content); + } else { + $error = $msg_template->insert(%content); + } + + if ( $error ) { + $cgi->param('error', $error); + $cgi->delete('preview'); # don't go on to preview stage yet + undef $msg_template; + } + } + # unless creating the msg_template failed, we now have one, so construct a + # preview message from the first customer/whatever in the search results + + if ( $msg_template ) { $sql_query->{'extra_sql'} .= ' LIMIT 1'; $sql_query->{'select'} = "$table.*"; $sql_query->{'order_by'} = ''; @@ -288,9 +351,36 @@ if ( $cgi->param('action') eq 'preview' ) { 'cust_main' => $cust, 'object' => $object, ); - my %message = $msg_template->prepare(%msgopts); - ($from, $subject, $html_body) = @message{'from', 'subject', 'html_body'}; + + my $cust_msg = $msg_template->prepare(%msgopts); + $from = $cust_msg->env_from; + $html_body = $cust_msg->preview; + if ( $cust_msg->header =~ /^subject: (.*)/mi ) { + $subject = $1; + } + } + + # contact_class_X params + foreach my $param ( $cgi->multi_param ) { + if ( $param =~ /^contact_class_(\w+)$/ ) { + push @contact_classnum, $1; + if ( $1 eq 'invoice' ) { + push @contact_classname, 'Invoice recipients'; + } else { + my $contact_class = FS::contact_class->by_key($1); + push @contact_classname, encode_entities($contact_class->classname); + } + } } } +my @contact_checkboxes = ( + [ 'invoice' => { label => 'Invoice recipients' } ] +); +foreach my $class (qsearch('contact_class', { disabled => '' })) { + push @contact_checkboxes, [ + $class->classnum, + { label => $class->classname } + ]; +} %init>