1 package FS::part_event_condition;
4 use vars qw( @ISA $DEBUG @SKIP_CONDITION_SQL );
6 use FS::Record qw( qsearch qsearchs );
8 use FS::part_event; #for order_conditions_sql...
10 @ISA = qw( FS::option_Common ); # FS::Record );
13 @SKIP_CONDITION_SQL = ();
17 FS::part_event_condition - Object methods for part_event_condition records
21 use FS::part_event_condition;
23 $record = new FS::part_event_condition \%hash;
24 $record = new FS::part_event_condition { 'column' => 'value' };
26 $error = $record->insert;
28 $error = $new_record->replace($old_record);
30 $error = $record->delete;
32 $error = $record->check;
36 An FS::part_event_condition object represents an event condition.
37 FS::part_event_condition inherits from FS::Record. The following fields are
42 =item eventconditionnum - primary key
44 =item eventpart - Event definition (see L<FS::part_event>)
46 =item conditionname - Condition name - defines which FS::part_event::Condition::I<conditionname> evaluates this condition
56 Creates a new event. To add the example to the database, see L<"insert">.
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.
63 # the new method can be inherited from FS::Record, if a table method is defined
65 sub table { 'part_event_condition'; }
67 =item insert [ HASHREF | OPTION => VALUE ... ]
69 Adds this record to the database. If there is an error, returns the error,
70 otherwise returns false.
72 If a list or hash reference of options is supplied, part_event_condition_option
73 records are created (see L<FS::part_event_condition_option>).
77 # the insert method can be inherited from FS::Record
81 Delete this record from the database.
85 # the delete method can be inherited from FS::Record
87 =item replace OLD_RECORD [ HASHREF | OPTION => VALUE ... ]
89 Replaces the OLD_RECORD with this one in the database. If there is an error,
90 returns the error, otherwise returns false.
92 If a list or hash reference of options is supplied, part_event_condition_option
93 records are created or modified (see L<FS::part_event_condition_option>).
97 # the replace method can be inherited from FS::Record
101 Checks all fields to make sure this is a valid example. If there is
102 an error, returns the error, otherwise returns false. Called by the insert
107 # the check method should currently be supplied - FS::Record contains some
108 # data checking routines
114 $self->ut_numbern('eventconditionnum')
115 || $self->ut_foreign_key('eventpart', 'part_event', 'eventpart')
116 || $self->ut_alpha('conditionname')
118 return $error if $error;
120 #XXX check conditionname to make sure a module exists?
121 # well it'll die in _rebless...
129 Reblesses the object into the FS::part_event::Condition::CONDITIONNAME class,
130 where CONDITIONNAME is the object's I<conditionname> field.
136 my $conditionname = $self->conditionname;
137 #my $class = ref($self). "::$conditionname";
138 my $class = "FS::part_event::Condition::$conditionname";
141 bless($self, $class); #unless $@;
151 =item conditions [ EVENTTABLE ]
153 Return information about the available conditions. If an eventtable is
154 specified, only return information about conditions available for that
157 Information is returned as key-value pairs. Keys are condition names. Values
158 are hashrefs with the following keys:
166 # =item default_weight
172 See L<FS::part_event::Condition> for more information.
176 #false laziness w/part_event.pm
177 #some false laziness w/part_export & part_pkg
179 foreach my $INC ( @INC ) {
180 foreach my $file ( glob("$INC/FS/part_event/Condition/*.pm") ) {
181 warn "attempting to load Condition from $file\n" if $DEBUG;
182 $file =~ /\/(\w+)\.pm$/ or do {
183 warn "unrecognized file in $INC/FS/part_event/Condition/: $file\n";
187 my $fullmod = "FS::part_event::Condition::$mod";
188 eval "use $fullmod;";
190 die "error using $fullmod (skipping): $@\n" if $@;
191 #warn "error using $fullmod (skipping): $@\n" if $@;
194 if ( $fullmod->disabled ) {
195 warn "$fullmod is disabled; skipping\n";
198 #my $full_condition_sql = $fullmod. '::condition_sql';
199 my $condition_sql_coderef = sub { $fullmod->condition_sql(@_) };
200 my $order_sql_coderef = $fullmod->can('order_sql')
201 ? sub { $fullmod->order_sql(@_) }
203 $conditions{$mod} = {
204 ( map { $_ => $fullmod->$_() }
205 qw( description eventtable_hashref
206 implicit_flag remove_warning
210 #option_fields_hashref
212 'option_fields' => [ $fullmod->option_fields() ],
213 'condition_sql' => $condition_sql_coderef,
214 'order_sql' => $order_sql_coderef,
220 my( $class, $eventtable ) = @_;
222 map { $_ => $conditions{$_} }
223 # sort { $conditions{$a}->{'default_weight'}<=>$conditions{$b}->{'default_weight'} }
225 $class->all_conditionnames( $eventtable )
230 =item all_conditionnames [ EVENTTABLE ]
232 Returns a list of just the condition names
236 sub all_conditionnames {
237 my ( $class, $eventtable ) = @_;
239 grep { !$eventtable || $conditions{$_}->{'eventtable_hashref'}{$eventtable} }
243 =item join_conditions_sql [ EVENTTABLE ]
245 Returns an SQL fragment selecting joining all condition options for an event as
246 tables titled "cond_I<conditionname>". Typically used in conjunction with
247 B<where_conditions_sql>.
251 sub join_conditions_sql {
252 my ( $class, $eventtable ) = @_;
253 my %conditions = $class->conditions( $eventtable );
257 "LEFT JOIN part_event_condition AS cond_$_".
258 " ON ( part_event.eventpart = cond_$_.eventpart".
259 " AND cond_$_.conditionname = ". dbh->quote($_).
267 =item where_conditions_sql [ EVENTTABLE [ , OPTION => VALUE, ... ] ]
269 Returns an SQL fragment to select events which have unsatisfied conditions.
270 Must be used in conjunction with B<join_conditions_sql>.
272 The only current option is "time", the current time (or "pretend" current time
273 as passed to freeside-daily), as a UNIX timestamp.
277 sub where_conditions_sql {
278 my ( $class, $eventtable, %options ) = @_;
280 my $time = $options{'time'};
282 my %conditions = $class->conditions( $eventtable );
284 my $where = join(' AND ',
286 my $conditionname = $_;
287 my $coderef = $conditions{$conditionname}->{condition_sql};
288 my $sql = &$coderef( $eventtable, 'time'=>$time );
289 die "$coderef is not a CODEREF" unless ref($coderef) eq 'CODE';
290 "( cond_$conditionname.conditionname IS NULL OR $sql )";
292 grep { my $cond = $_;
293 ! grep { $_ eq $cond } @SKIP_CONDITION_SQL
301 =item order_conditions_sql [ EVENTTABLE ]
303 Returns an SQL fragment to order selected events. Must be used in conjunction
304 with B<join_conditions_sql>.
308 sub order_conditions_sql {
309 my( $class, $eventtable ) = @_;
311 my %conditions = $class->conditions( $eventtable );
313 my $eventtables = join(' ', FS::part_event->eventtables_runorder);
315 my $order_by = join(', ',
316 "position( part_event.eventtable in ' $eventtables ')",
318 my $conditionname = $_;
319 my $coderef = $conditions{$conditionname}->{order_sql};
320 my $sql = &$coderef( $eventtable );
321 "CASE WHEN cond_$conditionname.conditionname IS NULL
327 sort { $conditions{$a}->{order_sql_weight}
328 <=> $conditions{$b}->{order_sql_weight}
330 grep { $conditions{$_}->{order_sql} }
336 "ORDER BY $order_by";
346 L<FS::part_event::Condition>, L<FS::part_event>, L<FS::Record>, schema.html from
347 the base documentation.