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 base qw( FS::m2name_Common FS::Record );
23 FS::m2name_Common is intended as a mixin class for classes which have a
24 related table that lists names.
30 =item process_m2name OPTION => VALUE, ...
34 link_table (required) - Table into which the records are inserted.
36 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.
38 name_col (required) - Name of the column in link_table that stores the string names.
40 names_list (required) - List reference of the possible string name values.
42 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.
44 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.
46 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.
51 my( $self, %opt ) = @_;
53 my $self_pkey = $self->dbdef_table->primary_key;
54 my $link_sourcekey = $opt{'num_col'} || $self_pkey;
56 my $link_table = $self->_load_table($opt{'link_table'});
58 my $link_static = $opt{'link_static'} || {};
60 warn "$me processing m2name from ". $self->table. ".$link_sourcekey".
64 foreach my $name ( @{ $opt{'names_list'} } ) {
66 warn "$me checking $name\n" if $DEBUG;
68 my $name_col = $opt{'name_col'};
70 my $obj = qsearchs( $link_table, {
71 $link_sourcekey => $self->$self_pkey(),
78 if ( $opt{'param_style'} =~ /link_table.value\s+checkboxes/i ) {
79 #access_group.html style
80 my $paramname = "$link_table.$name";
81 $param = $opt{'params'}->{$paramname};
82 } elsif ( $opt{'param_style'} =~ /name_colN values/i ) {
83 #part_event.html style
85 my @fields = grep { /^$name_col\d+$/ }
86 keys %{$opt{'params'}};
88 $param = grep { $name eq $opt{'params'}->{$_} } @fields;
91 #this depends on their being one condition per name...
92 #which needs to be enforced on the edit page...
93 #(it is on part_event and access_group edit)
94 foreach my $field (@fields) {
95 $prefix = "$field." if $name eq $opt{'params'}->{$field};
97 warn "$me prefix $prefix\n" if $DEBUG;
100 croak "unknown param_style: ". $opt{'param_style'};
101 $param = $opt{'params'}->{$name};
104 if ( $obj && ! $param ) {
106 warn "$me deleting $name\n" if $DEBUG;
108 my $d_obj = $obj; #need to save $obj for below.
109 my $error = $d_obj->delete;
110 die "error deleting $d_obj for $link_table.$name: $error" if $error;
112 } elsif ( $param && ! $obj ) {
114 warn "$me inserting $name\n" if $DEBUG;
116 #ok to clobber it now (but bad form nonetheless?)
117 #$obj = new "FS::$link_table" ( {
118 $obj = "FS::$link_table"->new( {
119 $link_sourcekey => $self->$self_pkey(),
120 $opt{'name_col'} => $name,
125 if ( $opt{'args_callback'} ) { #edit/process/part_event.html
126 &{ $opt{'args_callback'} }( $obj,
133 my $error = $obj->insert( @args );
134 die "error inserting $obj for $link_table.$name: $error" if $error;
136 } elsif ( $param && $obj && $opt{'args_callback'} ) {
139 if ( $opt{'args_callback'} ) { #edit/process/part_event.html
140 &{ $opt{'args_callback'} }( $obj,
147 my $error = $obj->replace( $obj, @args );
148 die "error replacing $obj for $link_table.$name: $error" if $error;
158 my( $self, $table ) = @_;
159 eval "use FS::$table";