communigate provisioning phase 2: add svc_domain.trailer -> communigate TrailerText...
[freeside.git] / FS / FS / part_event_option.pm
1 package FS::part_event_option;
2
3 use strict;
4 use vars qw( @ISA );
5 use Scalar::Util qw( blessed );
6 use FS::UID qw( dbh );
7 use FS::Record qw( qsearch qsearchs );
8 use FS::part_event;
9 use FS::reason;
10
11 @ISA = qw(FS::Record);
12
13 =head1 NAME
14
15 FS::part_event_option - Object methods for part_event_option records
16
17 =head1 SYNOPSIS
18
19   use FS::part_event_option;
20
21   $record = new FS::part_event_option \%hash;
22   $record = new FS::part_event_option { 'column' => 'value' };
23
24   $error = $record->insert;
25
26   $error = $new_record->replace($old_record);
27
28   $error = $record->delete;
29
30   $error = $record->check;
31
32 =head1 DESCRIPTION
33
34 An FS::part_event_option object represents an event definition option (action
35 option).  FS::part_event_option inherits from FS::Record.  The following fields
36 are currently supported:
37
38 =over 4
39
40 =item optionnum - primary key
41
42 =item eventpart - Event definition (see L<FS::part_event>)
43
44 =item optionname - Option name
45
46 =item optionvalue - Option value
47
48 =back
49
50 =head1 METHODS
51
52 =over 4
53
54 =item new HASHREF
55
56 Creates a new record.  To add the record to the database, see L<"insert">.
57
58 Note that this stores the hash reference, not a distinct copy of the hash it
59 points to.  You can ask the object for a copy with the I<hash> method.
60
61 =cut
62
63 # the new method can be inherited from FS::Record, if a table method is defined
64
65 sub table { 'part_event_option'; }
66
67 =item insert
68
69 Adds this record to the database.  If there is an error, returns the error,
70 otherwise returns false.
71
72 =cut
73
74 sub insert {
75   my $self = shift;
76
77   local $SIG{HUP} = 'IGNORE';
78   local $SIG{INT} = 'IGNORE';
79   local $SIG{QUIT} = 'IGNORE';
80   local $SIG{TERM} = 'IGNORE';
81   local $SIG{TSTP} = 'IGNORE';
82   local $SIG{PIPE} = 'IGNORE';
83
84   my $oldAutoCommit = $FS::UID::AutoCommit;
85   local $FS::UID::AutoCommit = 0;
86   my $dbh = dbh;
87
88   if ( $self->optionname eq 'reasonnum' && $self->optionvalue eq 'HASH' ) {
89
90     my $error = $self->insert_reason(@_);
91     if ( $error ) {
92       $dbh->rollback if $oldAutoCommit;
93       return $error;
94     }
95
96   }
97
98   my $error = $self->SUPER::insert;
99   if ( $error ) {
100     $dbh->rollback if $oldAutoCommit;
101     return $error;
102   }
103
104   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
105
106   '';
107
108 }
109
110 =item delete
111
112 Delete this record from the database.
113
114 =cut
115
116 # the delete method can be inherited from FS::Record
117
118 =item replace [ OLD_RECORD ]
119
120 Replaces the OLD_RECORD with this one in the database.  If there is an error,
121 returns the error, otherwise returns false.
122
123 =cut
124
125 sub replace {
126   my $self = shift;
127
128   local $SIG{HUP} = 'IGNORE';
129   local $SIG{INT} = 'IGNORE';
130   local $SIG{QUIT} = 'IGNORE';
131   local $SIG{TERM} = 'IGNORE';
132   local $SIG{TSTP} = 'IGNORE';
133   local $SIG{PIPE} = 'IGNORE';
134
135   my $oldAutoCommit = $FS::UID::AutoCommit;
136   local $FS::UID::AutoCommit = 0;
137   my $dbh = dbh;
138
139   my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
140               ? shift
141               : $self->replace_old;
142
143   if ( $self->optionname eq 'reasonnum' ) {
144     warn "reasonnum: ". $self->optionvalue;
145   }
146   if ( $self->optionname eq 'reasonnum' && $self->optionvalue eq 'HASH' ) {
147
148     my $error = $self->insert_reason(@_);
149     if ( $error ) {
150       $dbh->rollback if $oldAutoCommit;
151       return $error;
152     }
153
154   }
155
156   my $error = $self->SUPER::replace($old);
157   if ( $error ) {
158     $dbh->rollback if $oldAutoCommit;
159     return $error;
160   }
161
162   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
163
164   '';
165
166 }
167
168 =item check
169
170 Checks all fields to make sure this is a valid record.  If there is
171 an error, returns the error, otherwise returns false.  Called by the insert
172 and replace methods.
173
174 =cut
175
176 # the check method should currently be supplied - FS::Record contains some
177 # data checking routines
178
179 sub check {
180   my $self = shift;
181
182   my $error = 
183     $self->ut_numbern('optionnum')
184     || $self->ut_foreign_key('eventpart', 'part_event', 'eventpart' )
185     || $self->ut_text('optionname')
186     || $self->ut_textn('optionvalue')
187   ;
188   return $error if $error;
189
190   $self->SUPER::check;
191 }
192
193 sub insert_reason {
194   my( $self, $reason ) = @_;
195
196   my $reason_obj = new FS::reason({
197     'reason_type' => $reason->{'typenum'},
198     'reason'      => $reason->{'reason'},
199   });
200
201   $reason_obj->insert or $self->optionvalue( $reason_obj->reasonnum ) and '';
202
203 }
204
205 =back
206
207 =head1 SEE ALSO
208
209 L<FS::part_event>, L<FS::Record>, schema.html from the base documentation.
210
211 =cut
212
213 1;
214