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 );
12 FS::msg_template - Object methods for msg_template records
18 $record = new FS::msg_template \%hash;
19 $record = new FS::msg_template { 'column' => 'value' };
21 $error = $record->insert;
23 $error = $new_record->replace($old_record);
25 $error = $record->delete;
27 $error = $record->check;
31 An FS::msg_template object represents a customer message template.
32 FS::msg_template inherits from FS::Record. The following fields are currently
70 Creates a new template. To add the template to the database, see L<"insert">.
72 Note that this stores the hash reference, not a distinct copy of the hash it
73 points to. You can ask the object for a copy with the I<hash> method.
77 # the new method can be inherited from FS::Record, if a table method is defined
79 sub table { 'msg_template'; }
83 Adds this record to the database. If there is an error, returns the error,
84 otherwise returns false.
88 # the insert method can be inherited from FS::Record
92 Delete this record from the database.
96 # the delete method can be inherited from FS::Record
98 =item replace OLD_RECORD
100 Replaces the OLD_RECORD with this one in the database. If there is an error,
101 returns the error, otherwise returns false.
105 # the replace method can be inherited from FS::Record
109 Checks all fields to make sure this is a valid template. If there is
110 an error, returns the error, otherwise returns false. Called by the insert
115 # the check method should currently be supplied - FS::Record contains some
116 # data checking routines
122 $self->ut_numbern('msgnum')
123 || $self->ut_text('msgname')
124 || $self->ut_foreign_keyn('agentnum', 'agent', 'agentnum')
125 || $self->ut_textn('mime_type')
126 || $self->ut_anything('body')
127 || $self->ut_enum('disabled', [ '', 'Y' ] )
129 return $error if $error;
131 $self->mime_type('text/html') unless $self->mime_type;
136 =item send OPTION => VALUE, ...
138 Fills in the template and emails it to the customer.
140 Options are passed as a list of name/value pairs:
146 Customer object (required).
150 Additional context object (currently, can be a cust_main object, cust_pkg
151 object, or cust_bill object).
158 my( $self, %opt ) = @_;
160 my $cust_main = $opt{'cust_main'};
161 my $object = $opt{'object'};
167 my $subs = $self->substitutions;
169 #XXX html escape this stuff
170 my %hash = map { $_ => $cust_main->$_() } @{ $subs->{'cust_main'} };
171 unless ( ! $object || $object->table eq 'cust_main' ) {
172 %hash = ( %hash, map { $_ => $object->$_() } @{ $subs->{$object->table} } );
175 my $subject_tmpl = new Text::Template (
177 SOURCE => $self->subject,
179 my $subject = $subject_tmpl->fill_in( HASH => \%hash );
181 my $body_tmpl = new Text::Template (
183 SOURCE => $self->body,
185 my $body = $body_tmpl->fill_in( HASH => \%hash );
191 my @to = $cust_main->invoicing_list_emailonly;
192 #unless (@to) { #XXX do something }
194 my $conf = new FS::Conf;
198 #XXX override from in event?
199 'from' => scalar( $conf->config('invoice_from', $cust_main->agentnum) ),
201 'subject' => $subject,
202 'html_body' => $body,
203 #XXX auto-make a text copy w/HTML::FormatText?
204 # alas, us luddite mutt/pine users just aren't that big a deal
210 #return contexts and fill-in values
212 { 'cust_main' => [qw(
213 display_custnum agentnum agent_name
216 name name_short contact contact_firstlast
217 address1 address2 city county state zip
222 ship_last ship_first ship_company
223 ship_name ship_name_short ship_contact ship_contact_firstlast
224 ship_address1 ship_address2 ship_city ship_county ship_state ship_zip
226 ship_daytime ship_night ship_fax
228 payby paymask payname paytype payip
229 num_cancelled_pkgs num_ncancelled_pkgs num_pkgs
230 classname categoryname
232 invoicing_list_emailonly
233 cust_status ucfirst_cust_status cust_statuscolor
235 #XXX make these pretty: signupdate dundate paydate_monthyear usernum
240 #XXX these are going to take more pretty-ing up
245 #XXX not really thinking about cust_bill substitutions quite yet
256 L<FS::Record>, schema.html from the base documentation.