1 package FS::part_export;
5 use FS::Record qw( qsearch qsearchs dbh );
7 use FS::part_export_option;
10 @ISA = qw(FS::Record);
14 FS::part_export - Object methods for part_export records
20 $record = new FS::part_export \%hash;
21 $record = new FS::part_export { 'column' => 'value' };
23 #($new_record, $options) = $template_recored->clone( $svcpart );
25 $error = $record->insert( { 'option' => 'value' } );
26 $error = $record->insert( \%options );
28 $error = $new_record->replace($old_record);
30 $error = $record->delete;
32 $error = $record->check;
36 An FS::part_export object represents an export of Freeside data to an external
37 provisioning system. FS::part_export inherits from FS::Record. The following
38 fields are currently supported:
42 =item exportnum - primary key
44 =item machine - Machine name
46 =item exporttype - Export type
48 =item nodomain - blank or "Y" : usernames are exported to this service with no domain
58 Creates a new export. To add the export to the database, see L<"insert">.
60 Note that this stores the hash reference, not a distinct copy of the hash it
61 points to. You can ask the object for a copy with the I<hash> method.
65 # the new method can be inherited from FS::Record, if a table method is defined
67 sub table { 'part_export'; }
73 #An alternate constructor. Creates a new export by duplicating an existing
74 #export. The given svcpart is assigned to the new export.
76 #Returns a list consisting of the new export object and a hashref of options.
82 # my $class = ref($self);
83 # my %hash = $self->hash;
84 # $hash{'exportnum'} = '';
85 # $hash{'svcpart'} = shift;
86 # ( $class->new( \%hash ),
87 # { map { $_->optionname => $_->optionvalue }
88 # qsearch('part_export_option', { 'exportnum' => $self->exportnum } )
95 Adds this record to the database. If there is an error, returns the error,
96 otherwise returns false.
98 If a hash reference of options is supplied, part_export_option records are
99 created (see L<FS::part_export_option>).
103 #false laziness w/queue.pm
107 local $SIG{HUP} = 'IGNORE';
108 local $SIG{INT} = 'IGNORE';
109 local $SIG{QUIT} = 'IGNORE';
110 local $SIG{TERM} = 'IGNORE';
111 local $SIG{TSTP} = 'IGNORE';
112 local $SIG{PIPE} = 'IGNORE';
114 my $oldAutoCommit = $FS::UID::AutoCommit;
115 local $FS::UID::AutoCommit = 0;
118 my $error = $self->SUPER::insert;
120 $dbh->rollback if $oldAutoCommit;
124 foreach my $optionname ( keys %{$options} ) {
125 my $part_export_option = new FS::part_export_option ( {
126 'exportnum' => $self->exportnum,
127 'optionname' => $optionname,
128 'optionvalue' => $options->{$optionname},
130 $error = $part_export_option->insert;
132 $dbh->rollback if $oldAutoCommit;
137 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
145 Delete this record from the database.
149 #foreign keys would make this much less tedious... grr dumb mysql
152 local $SIG{HUP} = 'IGNORE';
153 local $SIG{INT} = 'IGNORE';
154 local $SIG{QUIT} = 'IGNORE';
155 local $SIG{TERM} = 'IGNORE';
156 local $SIG{TSTP} = 'IGNORE';
157 local $SIG{PIPE} = 'IGNORE';
159 my $oldAutoCommit = $FS::UID::AutoCommit;
160 local $FS::UID::AutoCommit = 0;
163 my $error = $self->SUPER::delete;
165 $dbh->rollback if $oldAutoCommit;
169 foreach my $part_export_option ( $self->part_export_option ) {
170 my $error = $part_export_option->delete;
172 $dbh->rollback if $oldAutoCommit;
177 foreach my $export_svc ( $self->export_svc ) {
178 my $error = $export_svc->delete;
180 $dbh->rollback if $oldAutoCommit;
185 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
191 =item replace OLD_RECORD HASHREF
193 Replaces the OLD_RECORD with this one in the database. If there is an error,
194 returns the error, otherwise returns false.
196 If a hash reference of options is supplied, part_export_option records are
197 created or modified (see L<FS::part_export_option>).
205 local $SIG{HUP} = 'IGNORE';
206 local $SIG{INT} = 'IGNORE';
207 local $SIG{QUIT} = 'IGNORE';
208 local $SIG{TERM} = 'IGNORE';
209 local $SIG{TSTP} = 'IGNORE';
210 local $SIG{PIPE} = 'IGNORE';
212 my $oldAutoCommit = $FS::UID::AutoCommit;
213 local $FS::UID::AutoCommit = 0;
216 my $error = $self->SUPER::replace($old);
218 $dbh->rollback if $oldAutoCommit;
222 foreach my $optionname ( keys %{$options} ) {
223 my $old = qsearchs( 'part_export_option', {
224 'exportnum' => $self->exportnum,
225 'optionname' => $optionname,
227 my $new = new FS::part_export_option ( {
228 'exportnum' => $self->exportnum,
229 'optionname' => $optionname,
230 'optionvalue' => $options->{$optionname},
232 $new->optionnum($old->optionnum) if $old;
233 my $error = $old ? $new->replace($old) : $new->insert;
235 $dbh->rollback if $oldAutoCommit;
240 #remove extraneous old options
242 grep { !exists $options->{$_->optionname} } $old->part_export_option
244 my $error = $opt->delete;
246 $dbh->rollback if $oldAutoCommit;
251 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
259 Checks all fields to make sure this is a valid export. If there is
260 an error, returns the error, otherwise returns false. Called by the insert
268 $self->ut_numbern('exportnum')
269 || $self->ut_domain('machine')
270 || $self->ut_alpha('exporttype')
272 return $error if $error;
274 warn $self->machine. "!!!\n";
276 $self->machine =~ /^([\w\-\.]*)$/
277 or return "Illegal machine: ". $self->machine;
280 $self->nodomain =~ /^(Y?)$/ or return "Illegal nodomain: ". $self->nodomain;
283 $self->deprecated(1); #BLAH
292 #Returns the service definition (see L<FS::part_svc>) for this export.
298 # qsearchs('part_svc', { svcpart => $self->svcpart } );
303 croak "FS::part_export::part_svc deprecated";
304 #confess "FS::part_export::part_svc deprecated";
309 Returns a list of associated FS::export_svc records.
315 qsearch('export_svc', { 'exportnum' => $self->exportnum } );
318 =item part_export_option
320 Returns all options as FS::part_export_option objects (see
321 L<FS::part_export_option>).
325 sub part_export_option {
327 qsearch('part_export_option', { 'exportnum' => $self->exportnum } );
332 Returns a list of option names and values suitable for assigning to a hash.
338 map { $_->optionname => $_->optionvalue } $self->part_export_option;
341 =item option OPTIONNAME
343 Returns the option value for the given name, or the empty string.
349 my $part_export_option =
350 qsearchs('part_export_option', {
351 exportnum => $self->exportnum,
354 $part_export_option ? $part_export_option->optionvalue : '';
359 Reblesses the object into the FS::part_export::EXPORTTYPE class, where
360 EXPORTTYPE is the object's I<exporttype> field. There should be better docs
361 on how to create new exports (and they should live in their own files and be
362 autoloaded-on-demand), but until then, see L</NEW EXPORT CLASSES>.
368 my $exporttype = $self->exporttype;
369 my $class = ref($self). "::$exporttype";
371 bless($self, $class);
374 =item export_insert SVC_OBJECT
381 $self->_export_insert(@_);
387 # my $method = $AUTOLOAD;
388 # #$method =~ s/::(\w+)$/::_$1/; #infinite loop prevention
389 # $method =~ s/::(\w+)$/_$1/; #infinite loop prevention
390 # $self->$method(@_);
393 =item export_replace NEW OLD
400 $self->_export_replace(@_);
410 $self->_export_delete(@_);
413 #fallbacks providing useful error messages intead of infinite loops
416 return "_export_insert: unknown export type ". $self->exporttype;
419 sub _export_replace {
421 return "_export_replace: unknown export type ". $self->exporttype;
426 return "_export_delete: unknown export type ". $self->exporttype;
431 =head1 NEW EXPORT CLASSES
433 Should be added to httemplate/edit/part_export.cgi and a module should
434 be FS/FS/part_export/ (an example may be found in eg/export_template.pm)
440 Hmm... cust_export class (not necessarily a database table...) ... ?
446 L<FS::part_export_option>, L<FS::export_svc>, L<FS::svc_acct>,
448 L<FS::svc_forward>, L<FS::Record>, schema.html from the base documentation.