1 package FS::part_export;
4 use vars qw( @ISA @EXPORT_OK $DEBUG %exports );
7 use FS::Record qw( qsearch qsearchs dbh );
10 use FS::part_export_option;
13 @ISA = qw( FS::option_Common );
14 @EXPORT_OK = qw(export_info);
20 FS::part_export - Object methods for part_export records
26 $record = new FS::part_export \%hash;
27 $record = new FS::part_export { 'column' => 'value' };
29 #($new_record, $options) = $template_recored->clone( $svcpart );
31 $error = $record->insert( { 'option' => 'value' } );
32 $error = $record->insert( \%options );
34 $error = $new_record->replace($old_record);
36 $error = $record->delete;
38 $error = $record->check;
42 An FS::part_export object represents an export of Freeside data to an external
43 provisioning system. FS::part_export inherits from FS::Record. The following
44 fields are currently supported:
48 =item exportnum - primary key
50 =item machine - Machine name
52 =item exporttype - Export type
54 =item nodomain - blank or "Y" : usernames are exported to this service with no domain
64 Creates a new export. To add the export to the database, see L<"insert">.
66 Note that this stores the hash reference, not a distinct copy of the hash it
67 points to. You can ask the object for a copy with the I<hash> method.
71 # the new method can be inherited from FS::Record, if a table method is defined
73 sub table { 'part_export'; }
79 #An alternate constructor. Creates a new export by duplicating an existing
80 #export. The given svcpart is assigned to the new export.
82 #Returns a list consisting of the new export object and a hashref of options.
88 # my $class = ref($self);
89 # my %hash = $self->hash;
90 # $hash{'exportnum'} = '';
91 # $hash{'svcpart'} = shift;
92 # ( $class->new( \%hash ),
93 # { map { $_->optionname => $_->optionvalue }
94 # qsearch('part_export_option', { 'exportnum' => $self->exportnum } )
101 Adds this record to the database. If there is an error, returns the error,
102 otherwise returns false.
104 If a hash reference of options is supplied, part_export_option records are
105 created (see L<FS::part_export_option>).
109 Delete this record from the database.
113 #foreign keys would make this much less tedious... grr dumb mysql
116 local $SIG{HUP} = 'IGNORE';
117 local $SIG{INT} = 'IGNORE';
118 local $SIG{QUIT} = 'IGNORE';
119 local $SIG{TERM} = 'IGNORE';
120 local $SIG{TSTP} = 'IGNORE';
121 local $SIG{PIPE} = 'IGNORE';
123 my $oldAutoCommit = $FS::UID::AutoCommit;
124 local $FS::UID::AutoCommit = 0;
127 my $error = $self->SUPER::delete;
129 $dbh->rollback if $oldAutoCommit;
133 foreach my $export_svc ( $self->export_svc ) {
134 my $error = $export_svc->delete;
136 $dbh->rollback if $oldAutoCommit;
141 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
149 Checks all fields to make sure this is a valid export. If there is
150 an error, returns the error, otherwise returns false. Called by the insert
158 $self->ut_numbern('exportnum')
159 || $self->ut_domain('machine')
160 || $self->ut_alpha('exporttype')
162 return $error if $error;
164 $self->nodomain =~ /^(Y?)$/ or return "Illegal nodomain: ". $self->nodomain;
167 $self->deprecated(1); #BLAH
176 #Returns the service definition (see L<FS::part_svc>) for this export.
182 # qsearchs('part_svc', { svcpart => $self->svcpart } );
187 croak "FS::part_export::part_svc deprecated";
188 #confess "FS::part_export::part_svc deprecated";
193 Returns a list of associated FS::svc_* records.
199 map { $_->svc_x } $self->cust_svc;
204 Returns a list of associated FS::cust_svc records.
210 map { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
211 grep { qsearch('cust_svc', { 'svcpart' => $_->svcpart } ) }
217 Returns a list of associated FS::export_svc records.
223 qsearch('export_svc', { 'exportnum' => $self->exportnum } );
226 =item part_export_option
228 Returns all options as FS::part_export_option objects (see
229 L<FS::part_export_option>).
233 sub part_export_option {
235 $self->option_objects;
240 Returns a list of option names and values suitable for assigning to a hash.
242 =item option OPTIONNAME
244 Returns the option value for the given name, or the empty string.
248 Reblesses the object into the FS::part_export::EXPORTTYPE class, where
249 EXPORTTYPE is the object's I<exporttype> field. There should be better docs
250 on how to create new exports, but until then, see L</NEW EXPORT CLASSES>.
256 my $exporttype = $self->exporttype;
257 my $class = ref($self). "::$exporttype";
260 bless($self, $class) unless $@;
264 #these should probably all go away, just let the subclasses define em
266 =item export_insert SVC_OBJECT
273 $self->_export_insert(@_);
279 # my $method = $AUTOLOAD;
280 # #$method =~ s/::(\w+)$/::_$1/; #infinite loop prevention
281 # $method =~ s/::(\w+)$/_$1/; #infinite loop prevention
282 # $self->$method(@_);
285 =item export_replace NEW OLD
292 $self->_export_replace(@_);
302 $self->_export_delete(@_);
312 $self->_export_suspend(@_);
315 =item export_unsuspend
319 sub export_unsuspend {
322 $self->_export_unsuspend(@_);
325 #fallbacks providing useful error messages intead of infinite loops
328 return "_export_insert: unknown export type ". $self->exporttype;
331 sub _export_replace {
333 return "_export_replace: unknown export type ". $self->exporttype;
338 return "_export_delete: unknown export type ". $self->exporttype;
341 #call svcdb-specific fallbacks
343 sub _export_suspend {
345 #warn "warning: _export_suspened unimplemented for". ref($self);
347 my $new = $svc_x->clone_suspended;
348 $self->_export_replace( $new, $svc_x );
351 sub _export_unsuspend {
353 #warn "warning: _export_unsuspend unimplemented for ". ref($self);
355 my $old = $svc_x->clone_kludge_unsuspend;
356 $self->_export_replace( $svc_x, $old );
365 =item export_info [ SVCDB ]
367 Returns a hash reference of the exports for the given I<svcdb>, or if no
368 I<svcdb> is specified, for all exports. The keys of the hash are
369 I<exporttype>s and the values are again hash references containing information
372 'desc' => 'Description',
374 'option' => { label=>'Option Label' },
375 'option2' => { label=>'Another label' },
377 'nodomain' => 'Y', #or ''
378 'notes' => 'Additional notes',
384 return $exports{$_[0]} || {} if @_;
385 #{ map { %{$exports{$_}} } keys %exports };
386 my $r = { map { %{$exports{$_}} } keys %exports };
389 #=item exporttype2svcdb EXPORTTYPE
391 #Returns the applicable I<svcdb> for an I<exporttype>.
395 #sub exporttype2svcdb {
396 # my $exporttype = $_[0];
397 # foreach my $svcdb ( keys %exports ) {
398 # return $svcdb if grep { $exporttype eq $_ } keys %{$exports{$svcdb}};
403 foreach my $INC ( @INC ) {
404 foreach my $file ( glob("$INC/FS/part_export/*.pm") ) {
405 warn "attempting to load export info from $file\n" if $DEBUG;
406 $file =~ /\/(\w+)\.pm$/ or do {
407 warn "unrecognized file in $INC/FS/part_export/: $file\n";
411 my $info = eval "use FS::part_export::$mod; ".
412 "\\%FS::part_export::$mod\::info;";
414 die "error using FS::part_export::$mod (skipping): $@\n" if $@;
417 unless ( keys %$info ) {
418 warn "no %info hash found in FS::part_export::$mod, skipping\n"
419 unless $mod =~ /^(passwdfile|null)$/; #hack but what the heck
422 warn "got export info from FS::part_export::$mod: $info\n" if $DEBUG;
425 ref($info->{'svc'}) ? @{$info->{'svc'}} : $info->{'svc'}
428 warn "blank svc for FS::part_export::$mod (skipping)\n";
431 $exports{$svc}->{$mod} = $info;
438 =head1 NEW EXPORT CLASSES
440 A module should be added in FS/FS/part_export/ (an example may be found in
441 eg/export_template.pm)
445 Hmm... cust_export class (not necessarily a database table...) ... ?
451 L<FS::part_export_option>, L<FS::export_svc>, L<FS::svc_acct>,
453 L<FS::svc_forward>, L<FS::Record>, schema.html from the base documentation.