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