16aad6dcd655dfb5e5c4e57e75bfcf20cd4e6365
[freeside.git] / FS / FS / part_export.pm
1 package FS::part_export;
2
3 use strict;
4 use vars qw( @ISA @EXPORT_OK $DEBUG %exports );
5 use Exporter;
6 use Tie::IxHash;
7 use FS::Record qw( qsearch qsearchs dbh );
8 use FS::option_Common;
9 use FS::part_svc;
10 use FS::part_export_option;
11 use FS::export_svc;
12
13 #for export modules, though they should probably just use it themselves
14 use FS::queue;
15
16 @ISA = qw( FS::option_Common );
17 @EXPORT_OK = qw(export_info);
18
19 $DEBUG = 0;
20
21 =head1 NAME
22
23 FS::part_export - Object methods for part_export records
24
25 =head1 SYNOPSIS
26
27   use FS::part_export;
28
29   $record = new FS::part_export \%hash;
30   $record = new FS::part_export { 'column' => 'value' };
31
32   #($new_record, $options) = $template_recored->clone( $svcpart );
33
34   $error = $record->insert( { 'option' => 'value' } );
35   $error = $record->insert( \%options );
36
37   $error = $new_record->replace($old_record);
38
39   $error = $record->delete;
40
41   $error = $record->check;
42
43 =head1 DESCRIPTION
44
45 An FS::part_export object represents an export of Freeside data to an external
46 provisioning system.  FS::part_export inherits from FS::Record.  The following
47 fields are currently supported:
48
49 =over 4
50
51 =item exportnum - primary key
52
53 =item machine - Machine name 
54
55 =item exporttype - Export type
56
57 =item nodomain - blank or "Y" : usernames are exported to this service with no domain
58
59 =back
60
61 =head1 METHODS
62
63 =over 4
64
65 =item new HASHREF
66
67 Creates a new export.  To add the export to the database, see 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 # the new method can be inherited from FS::Record, if a table method is defined
75
76 sub table { 'part_export'; }
77
78 =cut
79
80 #=item clone SVCPART
81 #
82 #An alternate constructor.  Creates a new export by duplicating an existing
83 #export.  The given svcpart is assigned to the new export.
84 #
85 #Returns a list consisting of the new export object and a hashref of options.
86 #
87 #=cut
88 #
89 #sub clone {
90 #  my $self = shift;
91 #  my $class = ref($self);
92 #  my %hash = $self->hash;
93 #  $hash{'exportnum'} = '';
94 #  $hash{'svcpart'} = shift;
95 #  ( $class->new( \%hash ),
96 #    { map { $_->optionname => $_->optionvalue }
97 #        qsearch('part_export_option', { 'exportnum' => $self->exportnum } )
98 #    }
99 #  );
100 #}
101
102 =item insert HASHREF
103
104 Adds this record to the database.  If there is an error, returns the error,
105 otherwise returns false.
106
107 If a hash reference of options is supplied, part_export_option records are
108 created (see L<FS::part_export_option>).
109
110 =item delete
111
112 Delete this record from the database.
113
114 =cut
115
116 #foreign keys would make this much less tedious... grr dumb mysql
117 sub delete {
118   my $self = shift;
119   local $SIG{HUP} = 'IGNORE';
120   local $SIG{INT} = 'IGNORE';
121   local $SIG{QUIT} = 'IGNORE';
122   local $SIG{TERM} = 'IGNORE';
123   local $SIG{TSTP} = 'IGNORE';
124   local $SIG{PIPE} = 'IGNORE';
125
126   my $oldAutoCommit = $FS::UID::AutoCommit;
127   local $FS::UID::AutoCommit = 0;
128   my $dbh = dbh;
129
130   my $error = $self->SUPER::delete;
131   if ( $error ) {
132     $dbh->rollback if $oldAutoCommit;
133     return $error;
134   }
135
136   foreach my $export_svc ( $self->export_svc ) {
137     my $error = $export_svc->delete;
138     if ( $error ) {
139       $dbh->rollback if $oldAutoCommit;
140       return $error;
141     }
142   }
143
144   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
145
146   '';
147
148 }
149
150 =item check
151
152 Checks all fields to make sure this is a valid export.  If there is
153 an error, returns the error, otherwise returns false.  Called by the insert
154 and replace methods.
155
156 =cut
157
158 sub check {
159   my $self = shift;
160   my $error = 
161     $self->ut_numbern('exportnum')
162     || $self->ut_domain('machine')
163     || $self->ut_alpha('exporttype')
164   ;
165   return $error if $error;
166
167   $self->nodomain =~ /^(Y?)$/ or return "Illegal nodomain: ". $self->nodomain;
168   $self->nodomain($1);
169
170   $self->deprecated(1); #BLAH
171
172   #check exporttype?
173
174   $self->SUPER::check;
175 }
176
177 #=item part_svc
178 #
179 #Returns the service definition (see L<FS::part_svc>) for this export.
180 #
181 #=cut
182 #
183 #sub part_svc {
184 #  my $self = shift;
185 #  qsearchs('part_svc', { svcpart => $self->svcpart } );
186 #}
187
188 sub part_svc {
189   use Carp;
190   croak "FS::part_export::part_svc deprecated";
191   #confess "FS::part_export::part_svc deprecated";
192 }
193
194 =item svc_x
195
196 Returns a list of associated FS::svc_* records.
197
198 =cut
199
200 sub svc_x {
201   my $self = shift;
202   map { $_->svc_x } $self->cust_svc;
203 }
204
205 =item cust_svc
206
207 Returns a list of associated FS::cust_svc records.
208
209 =cut
210
211 sub cust_svc {
212   my $self = shift;
213   map { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
214     grep { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
215       $self->export_svc;
216 }
217
218 =item export_svc
219
220 Returns a list of associated FS::export_svc records.
221
222 =cut
223
224 sub export_svc {
225   my $self = shift;
226   qsearch('export_svc', { 'exportnum' => $self->exportnum } );
227 }
228
229 =item part_export_option
230
231 Returns all options as FS::part_export_option objects (see
232 L<FS::part_export_option>).
233
234 =cut
235
236 sub part_export_option {
237   my $self = shift;
238   $self->option_objects;
239 }
240
241 =item options 
242
243 Returns a list of option names and values suitable for assigning to a hash.
244
245 =item option OPTIONNAME
246
247 Returns the option value for the given name, or the empty string.
248
249 =item _rebless
250
251 Reblesses the object into the FS::part_export::EXPORTTYPE class, where
252 EXPORTTYPE is the object's I<exporttype> field.  There should be better docs
253 on how to create new exports, but until then, see L</NEW EXPORT CLASSES>.
254
255 =cut
256
257 sub _rebless {
258   my $self = shift;
259   my $exporttype = $self->exporttype;
260   my $class = ref($self). "::$exporttype";
261   eval "use $class;";
262   #die $@ if $@;
263   bless($self, $class) unless $@;
264   $self;
265 }
266
267 #these should probably all go away, just let the subclasses define em
268
269 =item export_insert SVC_OBJECT
270
271 =cut
272
273 sub export_insert {
274   my $self = shift;
275   #$self->rebless;
276   $self->_export_insert(@_);
277 }
278
279 #sub AUTOLOAD {
280 #  my $self = shift;
281 #  $self->rebless;
282 #  my $method = $AUTOLOAD;
283 #  #$method =~ s/::(\w+)$/::_$1/; #infinite loop prevention
284 #  $method =~ s/::(\w+)$/_$1/; #infinite loop prevention
285 #  $self->$method(@_);
286 #}
287
288 =item export_replace NEW OLD
289
290 =cut
291
292 sub export_replace {
293   my $self = shift;
294   #$self->rebless;
295   $self->_export_replace(@_);
296 }
297
298 =item export_delete
299
300 =cut
301
302 sub export_delete {
303   my $self = shift;
304   #$self->rebless;
305   $self->_export_delete(@_);
306 }
307
308 =item export_suspend
309
310 =cut
311
312 sub export_suspend {
313   my $self = shift;
314   #$self->rebless;
315   $self->_export_suspend(@_);
316 }
317
318 =item export_unsuspend
319
320 =cut
321
322 sub export_unsuspend {
323   my $self = shift;
324   #$self->rebless;
325   $self->_export_unsuspend(@_);
326 }
327
328 #fallbacks providing useful error messages intead of infinite loops
329 sub _export_insert {
330   my $self = shift;
331   return "_export_insert: unknown export type ". $self->exporttype;
332 }
333
334 sub _export_replace {
335   my $self = shift;
336   return "_export_replace: unknown export type ". $self->exporttype;
337 }
338
339 sub _export_delete {
340   my $self = shift;
341   return "_export_delete: unknown export type ". $self->exporttype;
342 }
343
344 #call svcdb-specific fallbacks
345
346 sub _export_suspend {
347   my $self = shift;
348   #warn "warning: _export_suspened unimplemented for". ref($self);
349   my $svc_x = shift;
350   my $new = $svc_x->clone_suspended;
351   $self->_export_replace( $new, $svc_x );
352 }
353
354 sub _export_unsuspend {
355   my $self = shift;
356   #warn "warning: _export_unsuspend unimplemented for ". ref($self);
357   my $svc_x = shift;
358   my $old = $svc_x->clone_kludge_unsuspend;
359   $self->_export_replace( $svc_x, $old );
360 }
361
362 =item export_links SVC_OBJECT ARRAYREF
363
364 Adds a list of web elements to ARRAYREF specific to this export and SVC_OBJECT.
365 The elements are displayed in the UI to lead the the operator to external
366 configuration, monitoring, and similar tools.
367
368 =cut
369
370 =back
371
372 =head1 SUBROUTINES
373
374 =over 4
375
376 =item export_info [ SVCDB ]
377
378 Returns a hash reference of the exports for the given I<svcdb>, or if no
379 I<svcdb> is specified, for all exports.  The keys of the hash are
380 I<exporttype>s and the values are again hash references containing information
381 on the export:
382
383   'desc'     => 'Description',
384   'options'  => {
385                   'option'  => { label=>'Option Label' },
386                   'option2' => { label=>'Another label' },
387                 },
388   'nodomain' => 'Y', #or ''
389   'notes'    => 'Additional notes',
390
391 =cut
392
393 sub export_info {
394   #warn $_[0];
395   return $exports{$_[0]} || {} if @_;
396   #{ map { %{$exports{$_}} } keys %exports };
397   my $r = { map { %{$exports{$_}} } keys %exports };
398 }
399
400 #=item exporttype2svcdb EXPORTTYPE
401 #
402 #Returns the applicable I<svcdb> for an I<exporttype>.
403 #
404 #=cut
405 #
406 #sub exporttype2svcdb {
407 #  my $exporttype = $_[0];
408 #  foreach my $svcdb ( keys %exports ) {
409 #    return $svcdb if grep { $exporttype eq $_ } keys %{$exports{$svcdb}};
410 #  }
411 #  '';
412 #}
413
414 #false laziness w/part_pkg & cdr
415 foreach my $INC ( @INC ) {
416   foreach my $file ( glob("$INC/FS/part_export/*.pm") ) {
417     warn "attempting to load export info from $file\n" if $DEBUG;
418     $file =~ /\/(\w+)\.pm$/ or do {
419       warn "unrecognized file in $INC/FS/part_export/: $file\n";
420       next;
421     };
422     my $mod = $1;
423     my $info = eval "use FS::part_export::$mod; ".
424                     "\\%FS::part_export::$mod\::info;";
425     if ( $@ ) {
426       die "error using FS::part_export::$mod (skipping): $@\n" if $@;
427       next;
428     }
429     unless ( keys %$info ) {
430       warn "no %info hash found in FS::part_export::$mod, skipping\n"
431         unless $mod =~ /^(passwdfile|null)$/; #hack but what the heck
432       next;
433     }
434     warn "got export info from FS::part_export::$mod: $info\n" if $DEBUG;
435     no strict 'refs';
436     foreach my $svc (
437       ref($info->{'svc'}) ? @{$info->{'svc'}} : $info->{'svc'}
438     ) {
439       unless ( $svc ) {
440         warn "blank svc for FS::part_export::$mod (skipping)\n";
441         next;
442       }
443       $exports{$svc}->{$mod} = $info;
444     }
445   }
446 }
447
448 =back
449
450 =head1 NEW EXPORT CLASSES
451
452 A module should be added in FS/FS/part_export/ (an example may be found in
453 eg/export_template.pm)
454
455 =head1 BUGS
456
457 Hmm... cust_export class (not necessarily a database table...) ... ?
458
459 deprecated column...
460
461 =head1 SEE ALSO
462
463 L<FS::part_export_option>, L<FS::export_svc>, L<FS::svc_acct>,
464 L<FS::svc_domain>,
465 L<FS::svc_forward>, L<FS::Record>, schema.html from the base documentation.
466
467 =cut
468
469 1;
470