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 );
13 use Date::Format qw( time2str );
14 use HTML::Entities qw( decode_entities encode_entities ) ;
16 use HTML::TreeBuilder;
17 use vars qw( $DEBUG $conf );
19 FS::UID->install_callback( sub { $conf = new FS::Conf; } );
25 FS::msg_template - Object methods for msg_template records
31 $record = new FS::msg_template \%hash;
32 $record = new FS::msg_template { 'column' => 'value' };
34 $error = $record->insert;
36 $error = $new_record->replace($old_record);
38 $error = $record->delete;
40 $error = $record->check;
44 An FS::msg_template object represents a customer message template.
45 FS::msg_template inherits from FS::Record. The following fields are currently
60 Agent associated with this template. Can be NULL for a global template.
64 MIME type. Defaults to text/html.
72 The message subject line, in L<Text::Template> format.
76 The message body, as plain text or HTML, in L<Text::Template> format.
90 Creates a new template. To add the template to the database, see L<"insert">.
92 Note that this stores the hash reference, not a distinct copy of the hash it
93 points to. You can ask the object for a copy with the I<hash> method.
97 # the new method can be inherited from FS::Record, if a table method is defined
99 sub table { 'msg_template'; }
103 Adds this record to the database. If there is an error, returns the error,
104 otherwise returns false.
108 # the insert method can be inherited from FS::Record
112 Delete this record from the database.
116 # the delete method can be inherited from FS::Record
118 =item replace OLD_RECORD
120 Replaces the OLD_RECORD with this one in the database. If there is an error,
121 returns the error, otherwise returns false.
125 # the replace method can be inherited from FS::Record
129 Checks all fields to make sure this is a valid template. If there is
130 an error, returns the error, otherwise returns false. Called by the insert
135 # the check method should currently be supplied - FS::Record contains some
136 # data checking routines
142 $self->ut_numbern('msgnum')
143 || $self->ut_text('msgname')
144 || $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum')
145 || $self->ut_textn('mime_type')
146 || $self->ut_anything('subject')
147 || $self->ut_anything('body')
148 || $self->ut_enum('disabled', [ '', 'Y' ] )
149 || $self->ut_textn('from_addr')
151 return $error if $error;
153 $self->mime_type('text/html') unless $self->mime_type;
158 =item prepare OPTION => VALUE
160 Fills in the template and returns a hash of the 'from' address, 'to'
161 addresses, subject line, and body.
163 Options are passed as a list of name/value pairs:
169 Customer object (required).
173 Additional context object (currently, can be a cust_main, cust_pkg,
174 cust_bill, cust_pay, cust_pay_pending, or svc_(acct, phone, broadband,
175 domain) ). If the object is a svc_*, its cust_pkg will be fetched and
176 used for substitution.
178 As a special case, this may be an arrayref of two objects. Both
179 objects will be available for substitution, with their field names
180 prefixed with 'new_' and 'old_' respectively. This is used in the
181 rt_ticket export when exporting "replace" events.
185 Configuration option to use as the source address, based on the customer's
186 agentnum. If unspecified (or the named option is empty), 'invoice_from'
189 The I<from_addr> field in the template takes precedence over this.
193 Destination address. The default is to use the customer's
194 invoicing_list addresses. Multiple addresses may be comma-separated.
198 Set to true when preparing a message for previewing, rather than to actually
199 send it. This turns off logging.
206 my( $self, %opt ) = @_;
208 my $cust_main = $opt{'cust_main'};
209 my $object = $opt{'object'};
210 warn "preparing template '".$self->msgname."' to cust#".$cust_main->custnum."\n"
213 my $subs = $self->substitutions;
216 # create substitution table
219 my @objects = ($cust_main);
223 if( ref($object) eq 'ARRAY' ) {
224 # [new, old], for provisioning tickets
225 push @objects, $object->[0], $object->[1];
226 push @prefixes, 'new_', 'old_';
227 $svc = $object->[0] if $object->[0]->isa('FS::svc_Common');
230 push @objects, $object;
232 $svc = $object if $object->isa('FS::svc_Common');
236 push @objects, $svc->cust_svc->cust_pkg;
240 foreach my $obj (@objects) {
241 my $prefix = shift @prefixes;
242 foreach my $name (@{ $subs->{$obj->table} }) {
245 $hash{$prefix.$name} = $obj->$name();
247 elsif( ref($name) eq 'ARRAY' ) {
248 # [ foo => sub { ... } ]
249 $hash{$prefix.($name->[0])} = $name->[1]->($obj);
252 warn "bad msg_template substitution: '$name'\n";
257 $_ = encode_entities($_) foreach values(%hash);
263 my $subject_tmpl = new Text::Template (
265 SOURCE => $self->subject,
267 my $subject = $subject_tmpl->fill_in( HASH => \%hash );
269 my $body = $self->body;
270 my ($skin, $guts) = eviscerate($body);
272 $_ = decode_entities($_); # turn all punctuation back into itself
273 s/\r//gs; # remove \r's
274 s/<br[^>]*>/\n/gsi; # and <br /> tags
275 s/<p>/\n/gsi; # and <p>
276 s/<\/p>//gsi; # and </p>
277 s/\240/ /gs; # and
281 $body = '{ use Date::Format qw(time2str); "" }';
282 while(@$skin || @$guts) {
283 $body .= shift(@$skin) || '';
284 $body .= shift(@$guts) || '';
291 my $body_tmpl = new Text::Template (
296 $body = $body_tmpl->fill_in( HASH => \%hash );
303 if ( exists($opt{'to'}) ) {
304 @to = split(/\s*,\s*/, $opt{'to'});
307 @to = $cust_main->invoicing_list_emailonly;
309 # no warning when preparing with no destination
311 my $from_addr = $self->from_addr;
314 if ( $opt{'from_config'} ) {
315 $from_addr = scalar( $conf->config($opt{'from_config'},
316 $cust_main->agentnum) );
318 $from_addr ||= scalar( $conf->config('invoice_from',
319 $cust_main->agentnum) );
322 if ( $conf->exists('log_sent_mail') and !$opt{'preview'} ) {
323 my $cust_msg = FS::cust_msg->new({
324 'custnum' => $cust_main->custnum,
325 'msgnum' => $self->msgnum,
326 'status' => 'prepared',
329 @cust_msg = ('cust_msg' => $cust_msg);
333 'custnum' => $cust_main->custnum,
334 'msgnum' => $self->msgnum,
335 'from' => $from_addr,
337 'bcc' => $self->bcc_addr || undef,
338 'subject' => $subject,
339 'html_body' => $body,
340 'text_body' => HTML::FormatText->new(leftmargin => 0, rightmargin => 70
341 )->format( HTML::TreeBuilder->new_from_content($body) ),
347 =item send OPTION => VALUE
349 Fills in the template and sends it to the customer. Options are as for
354 # broken out from prepare() in case we want to queue the sending,
358 send_email(generate_email($self->prepare(@_)));
361 # helper sub for package dates
362 my $ymd = sub { $_[0] ? time2str('%Y-%m-%d', $_[0]) : '' };
364 #my $conf = new FS::Conf;
366 #return contexts and fill-in values
367 # If you add anything, be sure to add a description in
368 # httemplate/edit/msg_template.html.
370 { 'cust_main' => [qw(
371 display_custnum agentnum agent_name
374 name name_short contact contact_firstlast
375 address1 address2 city county state zip
380 ship_last ship_first ship_company
381 ship_name ship_name_short ship_contact ship_contact_firstlast
382 ship_address1 ship_address2 ship_city ship_county ship_state ship_zip
384 ship_daytime ship_night ship_fax
386 paymask payname paytype payip
387 num_cancelled_pkgs num_ncancelled_pkgs num_pkgs
388 classname categoryname
391 invoicing_list_emailonly
392 cust_status ucfirst_cust_status cust_statuscolor
398 # expdate is a special case
399 [ signupdate_ymd => sub { time2str('%Y-%m-%d', shift->signupdate) } ],
400 [ dundate_ymd => sub { time2str('%Y-%m-%d', shift->dundate) } ],
401 [ paydate_my => sub { sprintf('%02d/%04d', shift->paydate_monthyear) } ],
402 [ otaker_first => sub { shift->access_user->first } ],
403 [ otaker_last => sub { shift->access_user->last } ],
404 [ payby => sub { FS::payby->shortname(shift->payby) } ],
405 [ company_name => sub {
406 $conf->config('company_name', shift->agentnum)
408 [ company_address => sub {
409 $conf->config('company_address', shift->agentnum)
411 [ company_phonenum => sub {
412 $conf->config('company_phonenum', shift->agentnum)
417 pkgnum pkg_label pkg_label_long
421 start_date setup bill last_bill
425 [ pkg => sub { shift->part_pkg->pkg } ],
426 [ cancel => sub { shift->getfield('cancel') } ], # grrr...
427 [ start_ymd => sub { $ymd->(shift->getfield('start_date')) } ],
428 [ setup_ymd => sub { $ymd->(shift->getfield('setup')) } ],
429 [ next_bill_ymd => sub { $ymd->(shift->getfield('bill')) } ],
430 [ last_bill_ymd => sub { $ymd->(shift->getfield('last_bill')) } ],
431 [ adjourn_ymd => sub { $ymd->(shift->getfield('adjourn')) } ],
432 [ susp_ymd => sub { $ymd->(shift->getfield('susp')) } ],
433 [ expire_ymd => sub { $ymd->(shift->getfield('expire')) } ],
434 [ cancel_ymd => sub { $ymd->(shift->getfield('cancel')) } ],
440 #XXX not really thinking about cust_bill substitutions quite yet
442 # for welcome and limit warning messages
448 [ password => sub { shift->getfield('_password') } ],
455 my $registrar = qsearchs('registrar',
456 { registrarnum => shift->registrarnum} );
457 $registrar ? $registrar->registrarname : ''
461 my $svc_acct = qsearchs('svc_acct', { svcnum => shift->catchall });
462 $svc_acct ? $svc_acct->email : ''
473 'svc_broadband' => [qw(
481 # for payment receipts
486 [ paid => sub { sprintf("%.2f", shift->paid) } ],
487 # overrides the one in cust_main in cases where a cust_pay is passed
488 [ payby => sub { FS::payby->shortname(shift->payby) } ],
489 [ date => sub { time2str("%a %B %o, %Y", shift->_date) } ],
491 my $cust_pay = shift;
492 ($cust_pay->payby eq 'CARD' || $cust_pay->payby eq 'CHEK') ?
493 $cust_pay->paymask : $cust_pay->decrypt($cust_pay->payinfo)
496 # for payment decline messages
497 # try to support all cust_pay fields
498 # 'error' is a special case, it contains the raw error from the gateway
499 'cust_pay_pending' => [qw(
503 [ paid => sub { sprintf("%.2f", shift->paid) } ],
504 [ payby => sub { FS::payby->shortname(shift->payby) } ],
505 [ date => sub { time2str("%a %B %o, %Y", shift->_date) } ],
508 ($pending->payby eq 'CARD' || $pending->payby eq 'CHEK') ?
509 $pending->paymask : $pending->decrypt($pending->payinfo)
516 my ($self, %opts) = @_;
519 [ 'alerter_msgnum', 'alerter_template', '', '', '' ],
520 [ 'cancel_msgnum', 'cancelmessage', 'cancelsubject', '', '' ],
521 [ 'decline_msgnum', 'declinetemplate', '', '', '' ],
522 [ 'impending_recur_msgnum', 'impending_recur_template', '', '', 'impending_recur_bcc' ],
523 [ 'payment_receipt_msgnum', 'payment_receipt_email', '', '', '' ],
524 [ 'welcome_msgnum', 'welcome_email', 'welcome_email-subject', 'welcome_email-from', '' ],
525 [ 'warning_msgnum', 'warning_email', 'warning_email-subject', 'warning_email-from', '' ],
528 my @agentnums = ('', map {$_->agentnum} qsearch('agent', {}));
529 foreach my $agentnum (@agentnums) {
531 my ($newname, $oldname, $subject, $from, $bcc) = @$_;
532 if ($conf->exists($oldname, $agentnum)) {
533 my $new = new FS::msg_template({
534 'msgname' => $oldname,
535 'agentnum' => $agentnum,
536 'from_addr' => ($from && $conf->config($from, $agentnum)) ||
537 $conf->config('invoice_from', $agentnum),
538 'bcc_addr' => ($bcc && $conf->config($from, $agentnum)) || '',
539 'subject' => ($subject && $conf->config($subject, $agentnum)) || '',
540 'mime_type' => 'text/html',
541 'body' => join('<BR>',$conf->config($oldname, $agentnum)),
543 my $error = $new->insert;
544 die $error if $error;
545 $conf->set($newname, $new->msgnum, $agentnum);
546 $conf->delete($oldname, $agentnum);
547 $conf->delete($from, $agentnum) if $from;
548 $conf->delete($subject, $agentnum) if $subject;
555 # Every bit as pleasant as it sounds.
557 # We do this because Text::Template::Preprocess doesn't
558 # actually work. It runs the entire template through
559 # the preprocessor, instead of the code segments. Which
560 # is a shame, because Text::Template already contains
561 # the code to do this operation.
563 my (@outside, @inside);
566 while($body || $chunk) {
567 my ($first, $delim, $rest);
568 # put all leading non-delimiters into $first
570 ($body =~ /^((?:\\[{}]|[^{}])*)(.*)$/s);
572 # put a leading delimiter into $delim if there is one
574 ($rest =~ /^([{}]?)(.*)$/s);
576 if( $delim eq '{' ) {
579 push @outside, $chunk;
584 elsif( $delim eq '}' ) {
587 push @inside, $chunk;
595 push @outside, $chunk . $rest;
596 } # else ? something wrong
601 (\@outside, \@inside);
610 L<FS::Record>, schema.html from the base documentation.