1 package FS::part_export;
4 use vars qw( @ISA @EXPORT_OK %exports );
6 use FS::Record qw( qsearch qsearchs dbh );
8 use FS::part_export_option;
11 @ISA = qw(FS::Record);
12 @EXPORT_OK = qw(export_info);
16 FS::part_export - Object methods for part_export records
22 $record = new FS::part_export \%hash;
23 $record = new FS::part_export { 'column' => 'value' };
25 #($new_record, $options) = $template_recored->clone( $svcpart );
27 $error = $record->insert( { 'option' => 'value' } );
28 $error = $record->insert( \%options );
30 $error = $new_record->replace($old_record);
32 $error = $record->delete;
34 $error = $record->check;
38 An FS::part_export object represents an export of Freeside data to an external
39 provisioning system. FS::part_export inherits from FS::Record. The following
40 fields are currently supported:
44 =item exportnum - primary key
46 =item machine - Machine name
48 =item exporttype - Export type
50 =item nodomain - blank or "Y" : usernames are exported to this service with no domain
60 Creates a new export. To add the export to the database, see L<"insert">.
62 Note that this stores the hash reference, not a distinct copy of the hash it
63 points to. You can ask the object for a copy with the I<hash> method.
67 # the new method can be inherited from FS::Record, if a table method is defined
69 sub table { 'part_export'; }
75 #An alternate constructor. Creates a new export by duplicating an existing
76 #export. The given svcpart is assigned to the new export.
78 #Returns a list consisting of the new export object and a hashref of options.
84 # my $class = ref($self);
85 # my %hash = $self->hash;
86 # $hash{'exportnum'} = '';
87 # $hash{'svcpart'} = shift;
88 # ( $class->new( \%hash ),
89 # { map { $_->optionname => $_->optionvalue }
90 # qsearch('part_export_option', { 'exportnum' => $self->exportnum } )
97 Adds this record to the database. If there is an error, returns the error,
98 otherwise returns false.
100 If a hash reference of options is supplied, part_export_option records are
101 created (see L<FS::part_export_option>).
105 #false laziness w/queue.pm
109 local $SIG{HUP} = 'IGNORE';
110 local $SIG{INT} = 'IGNORE';
111 local $SIG{QUIT} = 'IGNORE';
112 local $SIG{TERM} = 'IGNORE';
113 local $SIG{TSTP} = 'IGNORE';
114 local $SIG{PIPE} = 'IGNORE';
116 my $oldAutoCommit = $FS::UID::AutoCommit;
117 local $FS::UID::AutoCommit = 0;
120 my $error = $self->SUPER::insert;
122 $dbh->rollback if $oldAutoCommit;
126 foreach my $optionname ( keys %{$options} ) {
127 my $part_export_option = new FS::part_export_option ( {
128 'exportnum' => $self->exportnum,
129 'optionname' => $optionname,
130 'optionvalue' => $options->{$optionname},
132 $error = $part_export_option->insert;
134 $dbh->rollback if $oldAutoCommit;
139 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
147 Delete this record from the database.
151 #foreign keys would make this much less tedious... grr dumb mysql
154 local $SIG{HUP} = 'IGNORE';
155 local $SIG{INT} = 'IGNORE';
156 local $SIG{QUIT} = 'IGNORE';
157 local $SIG{TERM} = 'IGNORE';
158 local $SIG{TSTP} = 'IGNORE';
159 local $SIG{PIPE} = 'IGNORE';
161 my $oldAutoCommit = $FS::UID::AutoCommit;
162 local $FS::UID::AutoCommit = 0;
165 my $error = $self->SUPER::delete;
167 $dbh->rollback if $oldAutoCommit;
171 foreach my $part_export_option ( $self->part_export_option ) {
172 my $error = $part_export_option->delete;
174 $dbh->rollback if $oldAutoCommit;
179 foreach my $export_svc ( $self->export_svc ) {
180 my $error = $export_svc->delete;
182 $dbh->rollback if $oldAutoCommit;
187 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
193 =item replace OLD_RECORD HASHREF
195 Replaces the OLD_RECORD with this one in the database. If there is an error,
196 returns the error, otherwise returns false.
198 If a hash reference of options is supplied, part_export_option records are
199 created or modified (see L<FS::part_export_option>).
207 local $SIG{HUP} = 'IGNORE';
208 local $SIG{INT} = 'IGNORE';
209 local $SIG{QUIT} = 'IGNORE';
210 local $SIG{TERM} = 'IGNORE';
211 local $SIG{TSTP} = 'IGNORE';
212 local $SIG{PIPE} = 'IGNORE';
214 my $oldAutoCommit = $FS::UID::AutoCommit;
215 local $FS::UID::AutoCommit = 0;
218 my $error = $self->SUPER::replace($old);
220 $dbh->rollback if $oldAutoCommit;
224 foreach my $optionname ( keys %{$options} ) {
225 my $old = qsearchs( 'part_export_option', {
226 'exportnum' => $self->exportnum,
227 'optionname' => $optionname,
229 my $new = new FS::part_export_option ( {
230 'exportnum' => $self->exportnum,
231 'optionname' => $optionname,
232 'optionvalue' => $options->{$optionname},
234 $new->optionnum($old->optionnum) if $old;
235 my $error = $old ? $new->replace($old) : $new->insert;
237 $dbh->rollback if $oldAutoCommit;
242 #remove extraneous old options
244 grep { !exists $options->{$_->optionname} } $old->part_export_option
246 my $error = $opt->delete;
248 $dbh->rollback if $oldAutoCommit;
253 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
261 Checks all fields to make sure this is a valid export. If there is
262 an error, returns the error, otherwise returns false. Called by the insert
270 $self->ut_numbern('exportnum')
271 || $self->ut_domain('machine')
272 || $self->ut_alpha('exporttype')
274 return $error if $error;
276 warn $self->machine. "!!!\n";
278 $self->machine =~ /^([\w\-\.]*)$/
279 or return "Illegal machine: ". $self->machine;
282 $self->nodomain =~ /^(Y?)$/ or return "Illegal nodomain: ". $self->nodomain;
285 $self->deprecated(1); #BLAH
294 #Returns the service definition (see L<FS::part_svc>) for this export.
300 # qsearchs('part_svc', { svcpart => $self->svcpart } );
305 croak "FS::part_export::part_svc deprecated";
306 #confess "FS::part_export::part_svc deprecated";
311 Returns a list of associated FS::export_svc records.
317 qsearch('export_svc', { 'exportnum' => $self->exportnum } );
320 =item part_export_option
322 Returns all options as FS::part_export_option objects (see
323 L<FS::part_export_option>).
327 sub part_export_option {
329 qsearch('part_export_option', { 'exportnum' => $self->exportnum } );
334 Returns a list of option names and values suitable for assigning to a hash.
340 map { $_->optionname => $_->optionvalue } $self->part_export_option;
343 =item option OPTIONNAME
345 Returns the option value for the given name, or the empty string.
351 my $part_export_option =
352 qsearchs('part_export_option', {
353 exportnum => $self->exportnum,
356 $part_export_option ? $part_export_option->optionvalue : '';
361 Reblesses the object into the FS::part_export::EXPORTTYPE class, where
362 EXPORTTYPE is the object's I<exporttype> field. There should be better docs
363 on how to create new exports (and they should live in their own files and be
364 autoloaded-on-demand), but until then, see L</NEW EXPORT CLASSES>.
370 my $exporttype = $self->exporttype;
371 my $class = ref($self). "::$exporttype";
373 bless($self, $class);
376 =item export_insert SVC_OBJECT
383 $self->_export_insert(@_);
389 # my $method = $AUTOLOAD;
390 # #$method =~ s/::(\w+)$/::_$1/; #infinite loop prevention
391 # $method =~ s/::(\w+)$/_$1/; #infinite loop prevention
392 # $self->$method(@_);
395 =item export_replace NEW OLD
402 $self->_export_replace(@_);
412 $self->_export_delete(@_);
415 #fallbacks providing useful error messages intead of infinite loops
418 return "_export_insert: unknown export type ". $self->exporttype;
421 sub _export_replace {
423 return "_export_replace: unknown export type ". $self->exporttype;
428 return "_export_delete: unknown export type ". $self->exporttype;
437 =item export_info [ SVCDB ]
439 Returns a hash reference of the exports for the given I<svcdb>, or if no
440 I<svcdb> is specified, for all exports. The keys of the hash are
441 I<exporttype>s and the values are again hash references containing information
444 'desc' => 'Description',
446 'option' => { label=>'Option Label' },
447 'option2' => { label=>'Another label' },
449 'nodomain' => 'Y', #or ''
450 'notes' => 'Additional notes',
456 return $exports{$_[0]} if @_;
457 #{ map { %{$exports{$_}} } keys %exports };
458 my $r = { map { %{$exports{$_}} } keys %exports };
461 =item exporttype2svcdb EXPORTTYPE
463 Returns the applicable I<svcdb> for an I<exporttype>.
467 sub exporttype2svcdb {
468 my $exporttype = $_[0];
469 foreach my $svcdb ( keys %exports ) {
470 return $svcdb if grep { $exporttype eq $_ } keys %{$exports{$svcdb}};
479 'Batch export of /etc/passwd and /etc/shadow files (Linux/SysV)',
484 'Batch export of /etc/passwd and /etc/master.passwd files (BSD)',
489 # 'Batch export of /etc/global/passwd and /etc/global/shadow for NIS ',
494 'Batch export of /etc/passwd and /etc/master.passwd files (BSD)',
498 'desc' => 'Batch export of a text /etc/raddb/users file (Livingston, Cistron)',
501 'desc' => 'Real-time export to SQL-backed RADIUS (ICRADIUS, FreeRADIUS)',
503 'datasrc' => { label=>'DBI data source' },
504 'username' => { label=>'Database username' },
505 'password' => { label=>'Database password' },
508 'notes' => 'Not specifying datasrc will export to the freeside database? (no... notes on MySQL replication, DBI::Proxy, etc., from Conf.pm && export.html etc., reset with bin/sqlradius_reset',
511 'desc' => 'Real-time export to Cyrus IMAP server',
514 'desc' => 'Real-time export to Critical Path Account Provisioning Protocol',
517 'desc' => 'Real-time export to InfoStreet streetSmartAPI',
519 'url' => { label=>'XML-RPC Access URL', },
520 'login' => { label=>'InfoStreet login', },
521 'password' => { label=>'InfoStreet password', },
522 'groupID' => { label=>'InfoStreet groupID', },
525 'notes' => 'Real-time export to <a href="http://www.infostreet.com/">InfoStreet</a> streetSmartAPI. Requires installation of <a href="http://search.cpan.org/search?dist=Frontier-Client">Frontier::Client</a> from CPAN.',
541 =head1 NEW EXPORT CLASSES
543 Should be added to the %export hash here, and a module should be added in
544 FS/FS/part_export/ (an example may be found in eg/export_template.pm)
550 Hmm... cust_export class (not necessarily a database table...) ... ?
556 L<FS::part_export_option>, L<FS::export_svc>, L<FS::svc_acct>,
558 L<FS::svc_forward>, L<FS::Record>, schema.html from the base documentation.