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