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('subject')
127 || $self->ut_anything('body')
128 || $self->ut_enum('disabled', [ '', 'Y' ] )
130 return $error if $error;
132 $self->mime_type('text/html') unless $self->mime_type;
137 =item send OPTION => VALUE, ...
139 Fills in the template and emails it to the customer.
141 Options are passed as a list of name/value pairs:
147 Customer object (required).
151 Additional context object (currently, can be a cust_main object, cust_pkg
152 object, or cust_bill object).
159 my( $self, %opt ) = @_;
161 my $cust_main = $opt{'cust_main'};
162 my $object = $opt{'object'};
168 my $subs = $self->substitutions;
170 #XXX html escape this stuff
171 my %hash = map { $_ => $cust_main->$_() } @{ $subs->{'cust_main'} };
172 unless ( ! $object || $object->table eq 'cust_main' ) {
173 %hash = ( %hash, map { $_ => $object->$_() } @{ $subs->{$object->table} } );
176 my $subject_tmpl = new Text::Template (
178 SOURCE => $self->subject,
180 my $subject = $subject_tmpl->fill_in( HASH => \%hash );
182 my $body_tmpl = new Text::Template (
184 SOURCE => $self->body,
186 my $body = $body_tmpl->fill_in( HASH => \%hash );
192 my @to = $cust_main->invoicing_list_emailonly;
193 #unless (@to) { #XXX do something }
195 my $conf = new FS::Conf;
199 #XXX override from in event?
200 'from' => scalar( $conf->config('invoice_from', $cust_main->agentnum) ),
202 'subject' => $subject,
203 'html_body' => $body,
204 #XXX auto-make a text copy w/HTML::FormatText?
205 # alas, us luddite mutt/pine users just aren't that big a deal
211 #return contexts and fill-in values
213 { 'cust_main' => [qw(
214 display_custnum agentnum agent_name
217 name name_short contact contact_firstlast
218 address1 address2 city county state zip
223 ship_last ship_first ship_company
224 ship_name ship_name_short ship_contact ship_contact_firstlast
225 ship_address1 ship_address2 ship_city ship_county ship_state ship_zip
227 ship_daytime ship_night ship_fax
229 payby paymask payname paytype payip
230 num_cancelled_pkgs num_ncancelled_pkgs num_pkgs
231 classname categoryname
233 invoicing_list_emailonly
234 cust_status ucfirst_cust_status cust_statuscolor
236 #XXX make these pretty: signupdate dundate paydate_monthyear usernum
241 #XXX these are going to take more pretty-ing up
246 #XXX not really thinking about cust_bill substitutions quite yet
257 L<FS::Record>, schema.html from the base documentation.