1 package FS::msg_template;
4 use base qw( FS::Record );
6 use FS::Misc qw( generate_email send_email );
8 use FS::Record qw( qsearch qsearchs );
10 use Date::Format qw( time2str );
11 use HTML::Entities qw( decode_entities encode_entities ) ;
13 use HTML::TreeBuilder;
20 FS::msg_template - Object methods for msg_template records
26 $record = new FS::msg_template \%hash;
27 $record = new FS::msg_template { 'column' => 'value' };
29 $error = $record->insert;
31 $error = $new_record->replace($old_record);
33 $error = $record->delete;
35 $error = $record->check;
39 An FS::msg_template object represents a customer message template.
40 FS::msg_template inherits from FS::Record. The following fields are currently
55 Agent associated with this template. Can be NULL for a global template.
59 MIME type. Defaults to text/html.
67 The message subject line, in L<Text::Template> format.
71 The message body, as plain text or HTML, in L<Text::Template> format.
85 Creates a new template. To add the template to the database, see L<"insert">.
87 Note that this stores the hash reference, not a distinct copy of the hash it
88 points to. You can ask the object for a copy with the I<hash> method.
92 # the new method can be inherited from FS::Record, if a table method is defined
94 sub table { 'msg_template'; }
98 Adds this record to the database. If there is an error, returns the error,
99 otherwise returns false.
103 # the insert method can be inherited from FS::Record
107 Delete this record from the database.
111 # the delete method can be inherited from FS::Record
113 =item replace OLD_RECORD
115 Replaces the OLD_RECORD with this one in the database. If there is an error,
116 returns the error, otherwise returns false.
120 # the replace method can be inherited from FS::Record
124 Checks all fields to make sure this is a valid template. If there is
125 an error, returns the error, otherwise returns false. Called by the insert
130 # the check method should currently be supplied - FS::Record contains some
131 # data checking routines
137 $self->ut_numbern('msgnum')
138 || $self->ut_text('msgname')
139 || $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum')
140 || $self->ut_textn('mime_type')
141 || $self->ut_anything('subject')
142 || $self->ut_anything('body')
143 || $self->ut_enum('disabled', [ '', 'Y' ] )
144 || $self->ut_textn('from_addr')
146 return $error if $error;
148 $self->mime_type('text/html') unless $self->mime_type;
153 =item prepare OPTION => VALUE
155 Fills in the template and returns a hash of the 'from' address, 'to'
156 addresses, subject line, and body.
158 Options are passed as a list of name/value pairs:
164 Customer object (required).
168 Additional context object (currently, can be a cust_main, cust_pkg,
169 cust_bill, svc_acct, or cust_pay object).
176 my( $self, %opt ) = @_;
178 my $cust_main = $opt{'cust_main'};
179 my $object = $opt{'object'};
180 warn "preparing template '".$self->msgname."' to cust#".$cust_main->custnum."\n"
183 my $subs = $self->substitutions;
186 # create substitution table
189 foreach my $obj ($cust_main, $object || ()) {
190 foreach my $name (@{ $subs->{$obj->table} }) {
193 $hash{$name} = $obj->$name();
195 elsif( ref($name) eq 'ARRAY' ) {
196 # [ foo => sub { ... } ]
197 $hash{$name->[0]} = $name->[1]->($obj);
200 warn "bad msg_template substitution: '$name'\n";
205 $_ = encode_entities($_) foreach values(%hash);
211 my $subject_tmpl = new Text::Template (
213 SOURCE => $self->subject,
215 my $subject = $subject_tmpl->fill_in( HASH => \%hash );
217 my $body = $self->body;
218 my ($skin, $guts) = eviscerate($body);
220 $_ = decode_entities($_); # turn all punctuation back into itself
221 s/\r//gs; # remove \r's
222 s/<br[^>]*>/\n/gsi; # and <br /> tags
223 s/<p>/\n/gsi; # and <p>
224 s/<\/p>//gsi; # and </p>
225 s/\240/ /gs; # and
230 while(@$skin || @$guts) {
231 $body .= shift(@$skin) || '';
232 $body .= shift(@$guts) || '';
239 my $body_tmpl = new Text::Template (
244 $body = $body_tmpl->fill_in( HASH => \%hash );
250 my @to = $cust_main->invoicing_list_emailonly;
251 warn "prepared msg_template with no email destination (custnum ".
252 $cust_main->custnum.")\n"
255 my $conf = new FS::Conf;
258 'from' => $self->from ||
259 scalar( $conf->config('invoice_from', $cust_main->agentnum) ),
261 'subject' => $subject,
262 'html_body' => $body,
263 'text_body' => HTML::FormatText->new(leftmargin => 0, rightmargin => 70
264 )->format( HTML::TreeBuilder->new_from_content($body) ),
269 =item send OPTION => VALUE
271 Fills in the template and sends it to the customer. Options are as for
276 # broken out from prepare() in case we want to queue the sending,
280 send_email(generate_email($self->prepare(@_)));
283 # helper sub for package dates
284 my $ymd = sub { $_[0] ? time2str('%Y-%m-%d', $_[0]) : '' };
286 # needed for some things
287 my $conf = new FS::Conf;
289 #return contexts and fill-in values
290 # If you add anything, be sure to add a description in
291 # httemplate/edit/msg_template.html.
293 { 'cust_main' => [qw(
294 display_custnum agentnum agent_name
297 name name_short contact contact_firstlast
298 address1 address2 city county state zip
303 ship_last ship_first ship_company
304 ship_name ship_name_short ship_contact ship_contact_firstlast
305 ship_address1 ship_address2 ship_city ship_county ship_state ship_zip
307 ship_daytime ship_night ship_fax
309 paymask payname paytype payip
310 num_cancelled_pkgs num_ncancelled_pkgs num_pkgs
311 classname categoryname
313 invoicing_list_emailonly
314 cust_status ucfirst_cust_status cust_statuscolor
318 [ signupdate_ymd => sub { time2str('%Y-%m-%d', shift->signupdate) } ],
319 [ dundate_ymd => sub { time2str('%Y-%m-%d', shift->dundate) } ],
320 [ paydate_my => sub { sprintf('%02d/%04d', shift->paydate_monthyear) } ],
321 [ otaker_first => sub { shift->access_user->first } ],
322 [ otaker_last => sub { shift->access_user->last } ],
323 [ payby => sub { FS::payby->shortname(shift->payby) } ],
324 [ company_name => sub {
325 $conf->config('company_name', shift->agentnum)
330 pkgnum pkg_label pkg_label_long
334 start_date setup bill last_bill
338 [ cancel => sub { shift->getfield('cancel') } ], # grrr...
339 [ start_ymd => sub { $ymd->(shift->getfield('start_date')) } ],
340 [ setup_ymd => sub { $ymd->(shift->getfield('setup')) } ],
341 [ next_bill_ymd => sub { $ymd->(shift->getfield('bill')) } ],
342 [ last_bill_ymd => sub { $ymd->(shift->getfield('last_bill')) } ],
343 [ adjourn_ymd => sub { $ymd->(shift->getfield('adjourn')) } ],
344 [ susp_ymd => sub { $ymd->(shift->getfield('susp')) } ],
345 [ expire_ymd => sub { $ymd->(shift->getfield('expire')) } ],
346 [ cancel_ymd => sub { $ymd->(shift->getfield('cancel')) } ],
352 #XXX not really thinking about cust_bill substitutions quite yet
357 [ password => sub { shift->getfield('_password') } ],
358 ], # for welcome messages
363 [ paid => sub { sprintf("%.2f", shift->paid) } ],
364 # overrides the one in cust_main in cases where a cust_pay is passed
365 [ payby => sub { FS::payby->shortname(shift->payby) } ],
366 [ date => sub { time2str("%a %B %o, %Y", shift->_date) } ],
368 my $cust_pay = shift;
369 ($cust_pay->payby eq 'CARD' || $cust_pay->payby eq 'CHEK') ?
370 $cust_pay->paymask : $cust_pay->decrypt($cust_pay->payinfo)
377 my ($self, %opts) = @_;
380 [ 'alerter_msgnum', 'alerter_template', '', '' ],
381 [ 'cancel_msgnum', 'cancelmessage', 'cancelsubject', '' ],
382 [ 'decline_msgnum', 'declinetemplate', '', '' ],
383 [ 'impending_recur_msgnum', 'impending_recur_template', '', '' ],
384 [ 'payment_receipt_msgnum', 'payment_receipt_email', '', '' ],
385 [ 'welcome_msgnum', 'welcome_email', 'welcome_email-subject', 'welcome_email-from' ],
386 [ 'warning_msgnum', 'warning_email', 'warning_email-subject', 'warning_email-from' ],
389 my $conf = new FS::Conf;
390 my @agentnums = ('', map {$_->agentnum} qsearch('agent', {}));
391 foreach my $agentnum (@agentnums) {
393 my ($newname, $oldname, $subject, $from) = @$_;
394 if ($conf->exists($oldname, $agentnum)) {
395 my $new = new FS::msg_template({
396 'msgname' => $oldname,
397 'agentnum' => $agentnum,
398 'from_addr' => ($from && $conf->config($from, $agentnum)) ||
399 $conf->config('invoice_from', $agentnum),
400 'subject' => ($subject && $conf->config($subject, $agentnum)) || '',
401 'mime_type' => 'text/html',
402 'body' => join('<BR>',$conf->config($oldname, $agentnum)),
404 my $error = $new->insert;
405 die $error if $error;
406 $conf->set($newname, $new->msgnum, $agentnum);
407 $conf->delete($oldname, $agentnum);
408 $conf->delete($from, $agentnum) if $from;
409 $conf->delete($subject, $agentnum) if $subject;
416 # Every bit as pleasant as it sounds.
418 # We do this because Text::Template::Preprocess doesn't
419 # actually work. It runs the entire template through
420 # the preprocessor, instead of the code segments. Which
421 # is a shame, because Text::Template already contains
422 # the code to do this operation.
424 my (@outside, @inside);
427 while($body || $chunk) {
428 my ($first, $delim, $rest);
429 # put all leading non-delimiters into $first
431 ($body =~ /^((?:\\[{}]|[^{}])*)(.*)$/s);
433 # put a leading delimiter into $delim if there is one
435 ($rest =~ /^([{}]?)(.*)$/s);
437 if( $delim eq '{' ) {
440 push @outside, $chunk;
445 elsif( $delim eq '}' ) {
448 push @inside, $chunk;
456 push @outside, $chunk . $rest;
457 } # else ? something wrong
462 (\@outside, \@inside);
471 L<FS::Record>, schema.html from the base documentation.