1 package FS::m2name_Common;
4 use vars qw( $DEBUG $me );
6 use FS::Schema qw( dbdef );
7 use FS::Record qw( qsearchs ); #qsearch dbh );
11 $me = '[FS::m2name_Common]';
15 FS::m2name_Common - Mixin class for tables with a related table listing names
19 use FS::m2name_Common;
21 @ISA = qw( FS::m2name_Common FS::Record );
25 FS::m2name_Common is intended as a mixin class for classes which have a
26 related table that lists names.
32 =item process_m2name OPTION => VALUE, ...
36 link_table (required) - Table into which the records are inserted.
38 num_col (optional) - Column in link_table which links to the primary key of the base table. If not specified, it is assumed this has the same name.
40 name_col (required) - Name of the column in link_table that stores the string names.
42 names_list (required) - List reference of the possible string name values.
44 params (required) - Hashref of keys and values, often passed as C<scalar($cgi->Vars)> from a form. Processing is controlled by the B<param_style param> option.
46 param_style (required) - Controls processing of B<params>. I<'link_table.value checkboxes'> specifies that parameters keys are in the form C<link_table.name>, and the values are booleans controlling whether or not to insert that name into link_table. I<'name_colN values'> specifies that parameter keys are in the form C<name_col0>, C<name_col1>, and so on, and values are the names inserted into link_table.
48 args_callback (optional) - Coderef. Optional callback that may modify arguments for insert and replace operations. The callback is run with four arguments: the first argument is object being inserted or replaced (i.e. FS::I<link_table> object), the second argument is a prefix to use when retreiving CGI arguements from the params hashref, the third argument is the params hashref (see above), and the final argument is a listref of arguments that the callback should modify.
53 my( $self, %opt ) = @_;
55 my $self_pkey = $self->dbdef_table->primary_key;
56 my $link_sourcekey = $opt{'num_col'} || $self_pkey;
58 my $link_table = $self->_load_table($opt{'link_table'});
60 my $link_static = $opt{'link_static'} || {};
62 warn "$me processing m2name from ". $self->table. ".$link_sourcekey".
66 foreach my $name ( @{ $opt{'names_list'} } ) {
68 warn "$me checking $name\n" if $DEBUG;
70 my $name_col = $opt{'name_col'};
72 my $obj = qsearchs( $link_table, {
73 $link_sourcekey => $self->$self_pkey(),
80 if ( $opt{'param_style'} =~ /link_table.value\s+checkboxes/i ) {
81 #access_group.html style
82 my $paramname = "$link_table.$name";
83 $param = $opt{'params'}->{$paramname};
84 } elsif ( $opt{'param_style'} =~ /name_colN values/i ) {
85 #part_event.html style
87 my @fields = grep { /^$name_col\d+$/ }
88 keys %{$opt{'params'}};
90 $param = grep { $name eq $opt{'params'}->{$_} } @fields;
93 #this depends on their being one condition per name...
94 #which needs to be enforced on the edit page...
95 #(it is on part_event and access_group edit)
96 foreach my $field (@fields) {
97 $prefix = "$field." if $name eq $opt{'params'}->{$field};
99 warn "$me prefix $prefix\n" if $DEBUG;
102 croak "unknown param_style: ". $opt{'param_style'};
103 $param = $opt{'params'}->{$name};
106 if ( $obj && ! $param ) {
108 warn "$me deleting $name\n" if $DEBUG;
110 my $d_obj = $obj; #need to save $obj for below.
111 my $error = $d_obj->delete;
112 die "error deleting $d_obj for $link_table.$name: $error" if $error;
114 } elsif ( $param && ! $obj ) {
116 warn "$me inserting $name\n" if $DEBUG;
118 #ok to clobber it now (but bad form nonetheless?)
119 #$obj = new "FS::$link_table" ( {
120 $obj = "FS::$link_table"->new( {
121 $link_sourcekey => $self->$self_pkey(),
122 $opt{'name_col'} => $name,
127 if ( $opt{'args_callback'} ) { #edit/process/part_event.html
128 &{ $opt{'args_callback'} }( $obj,
135 my $error = $obj->insert( @args );
136 die "error inserting $obj for $link_table.$name: $error" if $error;
138 } elsif ( $param && $obj && $opt{'args_callback'} ) {
141 if ( $opt{'args_callback'} ) { #edit/process/part_event.html
142 &{ $opt{'args_callback'} }( $obj,
149 my $error = $obj->replace( $obj, @args );
150 die "error replacing $obj for $link_table.$name: $error" if $error;
160 my( $self, $table ) = @_;
161 eval "use FS::$table";