add stack backtrace to fatal problems in virtual field check
[freeside.git] / FS / FS / svc_www.pm
1 package FS::svc_www;
2
3 use strict;
4 use vars qw(@ISA $conf $apacheip);
5 #use FS::Record qw( qsearch qsearchs );
6 use FS::Record qw( qsearchs dbh );
7 use FS::svc_Common;
8 use FS::cust_svc;
9 use FS::domain_record;
10 use FS::svc_acct;
11 use FS::svc_domain;
12
13 @ISA = qw( FS::svc_Common );
14
15 #ask FS::UID to run this stuff for us later
16 $FS::UID::callback{'FS::svc_www'} = sub { 
17   $conf = new FS::Conf;
18   $apacheip = $conf->config('apacheip');
19 };
20
21 =head1 NAME
22
23 FS::svc_www - Object methods for svc_www records
24
25 =head1 SYNOPSIS
26
27   use FS::svc_www;
28
29   $record = new FS::svc_www \%hash;
30   $record = new FS::svc_www { 'column' => 'value' };
31
32   $error = $record->insert;
33
34   $error = $new_record->replace($old_record);
35
36   $error = $record->delete;
37
38   $error = $record->check;
39
40   $error = $record->suspend;
41
42   $error = $record->unsuspend;
43
44   $error = $record->cancel;
45
46 =head1 DESCRIPTION
47
48 An FS::svc_www object represents an web virtual host.  FS::svc_www inherits
49 from FS::svc_Common.  The following fields are currently supported:
50
51 =over 4
52
53 =item svcnum - primary key
54
55 =item recnum - DNS `A' record corresponding to this web virtual host. (see L<FS::domain_record>)
56
57 =item usersvc - account (see L<FS::svc_acct>) corresponding to this web virtual host.
58
59 =back
60
61 =head1 METHODS
62
63 =over 4
64
65 =item new HASHREF
66
67 Creates a new web virtual host.  To add the record to the database, see
68 L<"insert">.
69
70 Note that this stores the hash reference, not a distinct copy of the hash it
71 points to.  You can ask the object for a copy with the I<hash> method.
72
73 =cut
74
75 sub table { 'svc_www'; }
76
77 =item insert [ , OPTION => VALUE ... ]
78
79 Adds this record to the database.  If there is an error, returns the error,
80 otherwise returns false.
81
82 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be 
83 defined.  An FS::cust_svc record will be created and inserted.
84
85 Currently available options are: I<depend_jobnum>
86
87 If I<depend_jobnum> is set (to a scalar jobnum or an array reference of
88 jobnums), all provisioning jobs will have a dependancy on the supplied
89 jobnum(s) (they will not run until the specific job(s) complete(s)).
90
91
92 =cut
93
94 sub insert {
95   my $self = shift;
96
97   my $error = $self->check;
98   return $error if $error;
99
100   local $SIG{HUP} = 'IGNORE';
101   local $SIG{INT} = 'IGNORE';
102   local $SIG{QUIT} = 'IGNORE';
103   local $SIG{TERM} = 'IGNORE';
104   local $SIG{TSTP} = 'IGNORE';
105   local $SIG{PIPE} = 'IGNORE';
106
107   my $oldAutoCommit = $FS::UID::AutoCommit;
108   local $FS::UID::AutoCommit = 0;
109   my $dbh = dbh;
110
111   #if ( $self->recnum =~ /^([\w\-]+|\@)\.(([\w\.\-]+\.)+\w+)$/ ) {
112   if ( $self->recnum =~ /^([\w\-]+|\@)\.(\d+)$/ ) {
113     my( $reczone, $domain_svcnum ) = ( $1, $2 );
114     unless ( $apacheip ) {
115       $dbh->rollback if $oldAutoCommit;
116       return "Configuration option apacheip not set; can't autocreate A record";
117              #"for $reczone". $svc_domain->domain;
118     }
119     my $domain_record = new FS::domain_record {
120       'svcnum'  => $domain_svcnum,
121       'reczone' => $reczone,
122       'recaf'   => 'IN',
123       'rectype' => 'A',
124       'recdata' => $apacheip,
125     };
126     $error = $domain_record->insert;
127     if ( $error ) {
128       $dbh->rollback if $oldAutoCommit;
129       return $error;
130     }
131     $self->recnum($domain_record->recnum);
132   }
133
134   $error = $self->SUPER::insert(@_);
135   if ( $error ) {
136     $dbh->rollback if $oldAutoCommit;
137     return $error;
138   }
139
140   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
141   '';
142 }
143
144 =item delete
145
146 Delete this record from the database.
147
148 =cut
149
150 sub delete {
151   my $self = shift;
152   my $error;
153
154   $error = $self->SUPER::delete;
155   return $error if $error;
156
157   '';
158 }
159
160 =item replace OLD_RECORD
161
162 Replaces the OLD_RECORD with this one in the database.  If there is an error,
163 returns the error, otherwise returns false.
164
165 =cut
166
167 sub replace {
168   my ( $new, $old ) = ( shift, shift );
169   my $error;
170
171   $error = $new->SUPER::replace($old);
172   return $error if $error;
173
174   '';
175 }
176
177 =item suspend
178
179 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
180
181 =item unsuspend
182
183 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
184
185 =item cancel
186
187 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
188
189 =item check
190
191 Checks all fields to make sure this is a valid web virtual host.  If there is
192 an error, returns the error, otherwise returns false.  Called by the insert
193 and repalce methods.
194
195 =cut
196
197 sub check {
198   my $self = shift;
199
200   my $x = $self->setfixed;
201   return $x unless ref($x);
202   #my $part_svc = $x;
203
204   my $error =
205     $self->ut_numbern('svcnum')
206 #    || $self->ut_number('recnum')
207     || $self->ut_number('usersvc')
208   ;
209   return $error if $error;
210
211   if ( $self->recnum =~ /^(\d+)$/ ) {
212   
213     $self->recnum($1);
214     return "Unknown recnum: ". $self->recnum
215       unless qsearchs('domain_record', { 'recnum' => $self->recnum } );
216
217   } elsif ( $self->recnum =~ /^([\w\-]+|\@)\.(([\w\.\-]+\.)+\w+)$/ ) {
218
219     my( $reczone, $domain ) = ( $1, $2 );
220
221     my $svc_domain = qsearchs( 'svc_domain', { 'domain' => $domain } )
222       or return "unknown domain $domain (recnum $1.$2)";
223
224     my $domain_record = qsearchs( 'domain_record', {
225       'reczone' => $reczone,
226       'svcnum' => $svc_domain->svcnum,
227     });
228
229     if ( $domain_record ) {
230       $self->recnum($domain_record->recnum);
231     } else {
232       #insert will create it
233       #$self->recnum("$reczone.$domain");
234       $self->recnum("$reczone.". $svc_domain->svcnum);
235     }
236
237   } else {
238     return "Illegal recnum: ". $self->recnum;
239   }
240
241   return "Unknown usersvc (svc_acct.svcnum): ". $self->usersvc
242     unless qsearchs('svc_acct', { 'svcnum' => $self->usersvc } );
243
244   $self->SUPER::check;
245
246 }
247
248 =item domain_record
249
250 Returns the FS::domain_record record for this web virtual host's zone (see
251 L<FS::domain_record>).
252
253 =cut
254
255 sub domain_record {
256   my $self = shift;
257   qsearchs('domain_record', { 'recnum' => $self->recnum } );
258 }
259
260 =item svc_acct
261
262 Returns the FS::svc_acct record for this web virtual host's owner (see
263 L<FS::svc_acct>).
264
265 =cut
266
267 sub svc_acct {
268   my $self = shift;
269   qsearchs('svc_acct', { 'svcnum' => $self->usersvc } );
270 }
271
272 =back
273
274 =head1 BUGS
275
276 =head1 SEE ALSO
277
278 L<FS::svc_Common>, L<FS::Record>, L<FS::domain_record>, L<FS::cust_svc>,
279 L<FS::part_svc>, L<FS::cust_pkg>, schema.html from the base documentation.
280
281 =cut
282
283 1;
284