[freeside-commits] freeside/FS/FS access_user.pm, 1.12, 1.13 option_Common.pm, 1.3, 1.4 m2m_Common.pm, 1.1, 1.2 Record.pm, 1.132, 1.133

Ivan,,, ivan at wavetail.420.am
Fri Jan 26 00:04:39 PST 2007


Update of /home/cvs/cvsroot/freeside/FS/FS
In directory wavetail:/tmp/cvs-serv23827/FS/FS

Modified Files:
	access_user.pm option_Common.pm m2m_Common.pm Record.pm 
Log Message:
top bar option!

Index: option_Common.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/option_Common.pm,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- option_Common.pm	5 Feb 2006 12:27:20 -0000	1.3
+++ option_Common.pm	26 Jan 2007 08:04:37 -0000	1.4
@@ -6,7 +6,7 @@
 
 @ISA = qw( FS::Record );
 
-$DEBUG = 0;
+$DEBUG = 3;
 
 =head1 NAME
 
@@ -18,6 +18,11 @@
 
 @ISA = qw( FS::option_Common );
 
+#optional for non-standard names
+sub _option_table    { 'table_name'; }  #defaults to ${table}_option
+sub _option_namecol  { 'column_name'; } #defaults to optionname
+sub _option_valuecol { 'column_name'; } #defaults to optionvalue
+
 =head1 DESCRIPTION
 
 FS::option_Common is intended as a base class for classes which have a
@@ -66,14 +71,17 @@
     return $error;
   }
 
-  my $pkey = $self->pkey;
+  my $pkey = $self->primary_key;
   my $option_table = $self->option_table;
 
+  my $namecol = $self->_option_namecol;
+  my $valuecol = $self->_option_valuecol;
+
   foreach my $optionname ( keys %{$options} ) {
     my $href = {
-      $pkey         => $self->get($pkey),
-      'optionname'  => $optionname,
-      'optionvalue' => $options->{$optionname},
+      $pkey     => $self->get($pkey),
+      $namecol  => $optionname,
+      $valuecol => $options->{$optionname},
     };
 
     #my $option_record = eval "new FS::$option_table \$href";
@@ -123,7 +131,7 @@
     return $error;
   }
   
-  my $pkey = $self->pkey;
+  my $pkey = $self->primary_key;
   #my $option_table = $self->option_table;
 
   foreach my $obj ( $self->option_objects ) {
@@ -140,7 +148,7 @@
 
 }
 
-=item replace OLD_RECORD [ HASHREF | OPTION => VALUE ... ]
+=item replace [ OLD_RECORD ] [ HASHREF | OPTION => VALUE ... ]
 
 Replaces the OLD_RECORD with this one in the database.  If there is an error,
 returns the error, otherwise returns false.
@@ -152,12 +160,16 @@
 
 sub replace {
   my $self = shift;
-  my $old = shift;
+
+  my $old = ( ref($_[0]) eq ref($self) )
+              ? shift
+              : $self->replace_old;
+
   my $options = 
     ( ref($_[0]) eq 'HASH' )
       ? shift
       : { @_ };
-  warn "FS::option_Common::insert called on $self with options ".
+  warn "FS::option_Common::replace called on $self with options ".
        join(', ', map "$_ => ". $options->{$_}, keys %$options)
     if $DEBUG;
 
@@ -178,30 +190,44 @@
     return $error;
   }
 
-  my $pkey = $self->pkey;
+  my $pkey = $self->primary_key;
   my $option_table = $self->option_table;
 
+  my $namecol = $self->_option_namecol;
+  my $valuecol = $self->_option_valuecol;
+
   foreach my $optionname ( keys %{$options} ) {
-    my $old = qsearchs( $option_table, {
-        $pkey         => $self->get($pkey),
-        'optionname'  => $optionname,
+
+    warn "FS::option_Common::replace: inserting or replacing option: $optionname"
+      if $DEBUG > 1;
+
+    my $oldopt = qsearchs( $option_table, {
+        $pkey    => $self->get($pkey),
+        $namecol => $optionname,
     } );
 
     my $href = {
-        $pkey         => $self->get($pkey),
-        'optionname'  => $optionname,
-        'optionvalue' => $options->{$optionname},
+        $pkey     => $self->get($pkey),
+        $namecol  => $optionname,
+        $valuecol => $options->{$optionname},
     };
 
-    #my $new = eval "new FS::$option_table \$href";
+    #my $newopt = eval "new FS::$option_table \$href";
     #if ( $@ ) {
     #  $dbh->rollback if $oldAutoCommit;
     #  return $@;
     #}
-    my $new = "FS::$option_table"->new($href);
+    my $newopt = "FS::$option_table"->new($href);
 
-    $new->optionnum($old->optionnum) if $old;
-    my $error = $old ? $new->replace($old) : $new->insert;
+    my $opt_pkey = $newopt->primary_key;
+
+    $newopt->$opt_pkey($oldopt->$opt_pkey) if $oldopt;
+    warn $oldopt;
+    warn "FS::option_Common::replace: ".
+         ( $oldopt ? "$newopt -> replace($oldopt)" : "$newopt -> insert" )
+      if $DEBUG > 2;
+    my $error = $oldopt ? $newopt->replace($oldopt) : $newopt->insert;
+    warn $error;
     if ( $error ) {
       $dbh->rollback if $oldAutoCommit;
       return $error;
@@ -210,7 +236,7 @@
 
   #remove extraneous old options
   foreach my $opt (
-    grep { !exists $options->{$_->optionname} } $old->option_objects
+    grep { !exists $options->{$_->$namecol()} } $old->option_objects
   ) {
     my $error = $opt->delete;
     if ( $error ) {
@@ -233,7 +259,7 @@
 
 sub option_objects {
   my $self = shift;
-  my $pkey = $self->pkey;
+  my $pkey = $self->primary_key;
   my $option_table = $self->option_table;
   qsearch($option_table, { $pkey => $self->get($pkey) } );
 }
@@ -246,7 +272,9 @@
 
 sub options {
   my $self = shift;
-  map { $_->optionname => $_->optionvalue } $self->option_objects;
+  my $namecol = $self->_option_namecol;
+  my $valuecol = $self->_option_valuecol;
+  map { $_->$namecol() => $_->$valuecol() } $self->option_objects;
 }
 
 =item option OPTIONNAME
@@ -257,30 +285,35 @@
 
 sub option {
   my $self = shift;
-  my $pkey = $self->pkey;
+  my $pkey = $self->primary_key;
   my $option_table = $self->option_table;
-  my $obj =
-    qsearchs($option_table, {
-      $pkey      => $self->get($pkey),
-      optionname => shift,
-  } );
-  $obj ? $obj->optionvalue : '';
+  my $namecol = $self->_option_namecol;
+  my $valuecol = $self->_option_valuecol;
+  my $hashref = {
+      $pkey    => $self->get($pkey),
+      $namecol => shift,
+  };
+  warn "$self -> option: searching for ".
+         join(' / ', map { "$_ => ". $hashref->{$_} } keys %$hashref )
+    if $DEBUG;
+  my $obj = qsearchs($option_table, $hashref);
+  $obj ? $obj->$valuecol() : '';
 }
 
 
-sub pkey {
-  my $self = shift;
-  my $pkey = $self->dbdef_table->primary_key;
-}
-
 sub option_table {
   my $self = shift;
-  my $option_table = $self->table . '_option';
+  my $option_table = $self->_option_table;
   eval "use FS::$option_table";
   die $@ if $@;
   $option_table;
 }
 
+#defaults
+sub _option_table    { shift->table .'_option'; }
+sub _option_namecol  { 'optionname'; }
+sub _option_valuecol { 'optionvalue'; }
+
 =back
 
 =head1 BUGS

Index: access_user.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/access_user.pm,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -d -r1.12 -r1.13
--- access_user.pm	14 Dec 2006 09:27:26 -0000	1.12
+++ access_user.pm	26 Jan 2007 08:04:36 -0000	1.13
@@ -6,10 +6,12 @@
 use FS::Conf;
 use FS::Record qw( qsearch qsearchs dbh );
 use FS::m2m_Common;
+use FS::option_Common;
 use FS::access_usergroup;
 use FS::agent;
 
- at ISA = qw( FS::m2m_Common FS::Record );
+ at ISA = qw( FS::m2m_Common FS::option_Common FS::Record );
+#@ISA = qw( FS::m2m_Common FS::option_Common );
 
 #kludge htpasswd for now (i hope this bootstraps okay)
 FS::UID->install_callback( sub {
@@ -74,6 +76,10 @@
 
 sub table { 'access_user'; }
 
+sub _option_table    { 'access_user_pref'; }
+sub _option_namecol  { 'prefname'; }
+sub _option_valuecol { 'prefvalue'; }
+
 =item insert
 
 Adds this record to the database.  If there is an error, returns the error,
@@ -177,7 +183,11 @@
 =cut
 
 sub replace {
-  my($new, $old) = ( shift, shift );
+  my $new = shift;
+
+  my $old = ( ref($_[0]) eq ref($new) )
+              ? shift
+              : $new->replace_old;
 
   local $SIG{HUP} = 'IGNORE';
   local $SIG{INT} = 'IGNORE';

Index: m2m_Common.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/m2m_Common.pm,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- m2m_Common.pm	14 May 2006 16:47:31 -0000	1.1
+++ m2m_Common.pm	26 Jan 2007 08:04:37 -0000	1.2
@@ -3,25 +3,26 @@
 use strict;
 use vars qw( @ISA $DEBUG );
 use FS::Schema qw( dbdef );
-use FS::Record qw( qsearch qsearchs ); #dbh );
+use FS::Record qw( qsearch qsearchs dbh );
 
- at ISA = qw( FS::Record );
+#hmm.  well.  we seem to be used as a mixin.
+#@ISA = qw( FS::Record );
 
 $DEBUG = 0;
 
 =head1 NAME
 
-FS::m2m_Common - Base class for classes in a many-to-many relationship
+FS::m2m_Common - Mixin class for classes in a many-to-many relationship
 
 =head1 SYNOPSIS
 
 use FS::m2m_Common;
 
- at ISA = qw( FS::m2m_Common );
+ at ISA = qw( FS::m2m_Common FS::Record );
 
 =head1 DESCRIPTION
 
-FS::m2m_Common is intended as a base class for classes which have a
+FS::m2m_Common is intended as a mixin class for classes which have a
 many-to-many relationship with another table (via a linking table).
 
 Note: It is currently assumed that the link table contains two fields
@@ -31,7 +32,17 @@
 
 =over 4
 
-=item process_m2m
+=item process_m2m OPTION => VALUE, ...
+
+Available options:
+
+link_table (required) - 
+
+target_table (required) - 
+
+params (required) - hashref; keys are primary key values in target_table (values are boolean).  For convenience, keys may optionally be prefixed with the name
+of the primary key, as in agentnum54 instead of 54, or passed as an arrayref
+of values.
 
 =cut
 
@@ -39,41 +50,64 @@
   my( $self, %opt ) = @_;
 
   my $self_pkey = $self->dbdef_table->primary_key;
+  my %hash = ( $self_pkey => $self->$self_pkey() );
 
   my $link_table = $self->_load_table($opt{'link_table'});
 
   my $target_table = $self->_load_table($opt{'target_table'});
   my $target_pkey = dbdef->table($target_table)->primary_key;
 
-  foreach my $target_obj ( qsearch($target_table, {} ) ) {
-
-    my $targetnum = $target_obj->$target_pkey();
+  if ( ref($opt{'params'}) eq 'ARRAY' ) {
+    $opt{'params'} = { map { $_=>1 } @{$opt{'params'}} };
+  }
 
-    my $link_obj = qsearchs( $link_table, {
-        $self_pkey   => $self->$self_pkey(),
-        $target_pkey => $targetnum,
-    });
+  local $SIG{HUP} = 'IGNORE';
+  local $SIG{INT} = 'IGNORE';
+  local $SIG{QUIT} = 'IGNORE';
+  local $SIG{TERM} = 'IGNORE';
+  local $SIG{TSTP} = 'IGNORE';
+  local $SIG{PIPE} = 'IGNORE';
 
-    if ( $link_obj && ! $opt{'params'}->{"$target_pkey$targetnum"} ) {
+  my $oldAutoCommit = $FS::UID::AutoCommit;
+  local $FS::UID::AutoCommit = 0;
+  my $dbh = dbh;
 
-      my $d_link_obj = $link_obj; #need to save $link_obj for below.
-      my $error = $d_link_obj->delete;
-      die $error if $error;
+  foreach my $del_obj (
+    grep { 
+           my $targetnum = $_->$target_pkey();
+           (    ! $opt{'params'}->{$targetnum}
+             && ! $opt{'params'}->{"$target_pkey$targetnum"}
+           );
+         }
+         qsearch( $link_table, \%hash )
+  ) {
+    my $error = $del_obj->delete;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return $error;
+    }
+  }
 
-    } elsif ( $opt{'params'}->{"$target_pkey$targetnum"} && ! $link_obj ) {
+  foreach my $add_targetnum (
+    grep { ! qsearchs( $link_table, { %hash, $target_pkey => $_ } ) }
+    map  { /^($target_pkey)?(\d+)$/; $2; }
+    grep { /^($target_pkey)?(\d+)$/ }
+    grep { $opt{'params'}->{$_} }
+    keys %{ $opt{'params'} }
+  ) {
 
-      #ok to clobber it now (but bad form nonetheless?)
-      #$link_obj = new "FS::$link_table" ( {
-      $link_obj = "FS::$link_table"->new( {
-        $self_pkey   => $self->$self_pkey(),
-        $target_pkey => $targetnum,
-      });
-      my $error = $link_obj->insert;
-      die $error if $error;
+    my $add_obj = "FS::$link_table"->new( {
+      %hash, 
+      $target_pkey => $add_targetnum,
+    });
+    my $error = $add_obj->insert;
+    if ( $error ) {
+      $dbh->rollback if $oldAutoCommit;
+      return $error;
     }
-
   }
 
+  $dbh->commit or die $dbh->errstr if $oldAutoCommit;
   '';
 }
 

Index: Record.pm
===================================================================
RCS file: /home/cvs/cvsroot/freeside/FS/FS/Record.pm,v
retrieving revision 1.132
retrieving revision 1.133
diff -u -d -r1.132 -r1.133
--- Record.pm	23 Jan 2007 06:51:54 -0000	1.132
+++ Record.pm	26 Jan 2007 08:04:37 -0000	1.133
@@ -26,7 +26,7 @@
 #export dbdef for now... everything else expects to find it here
 @EXPORT_OK = qw(dbh fields hfields qsearch qsearchs dbdef jsearch);
 
-$DEBUG = 0;
+$DEBUG = 3;
 $me = '[FS::Record]';
 
 $nowarn_identical = 0;
@@ -563,6 +563,17 @@
   dbdef->table($table);
 }
 
+=item primary_key
+
+Returns the primary key for the table.
+
+=cut
+
+sub primary_key {
+  my $self = shift;
+  my $pkey = $self->dbdef_table->primary_key;
+}
+
 =item get, getfield COLUMN
 
 Returns the value of the column/field/key COLUMN.
@@ -688,6 +699,8 @@
   my $self = shift;
   my $saved = {};
 
+  warn "$self -> insert" if $DEBUG;
+
   my $error = $self->check;
   return $error if $error;
 
@@ -784,8 +797,7 @@
         dbh->rollback if $FS::UID::AutoCommit;
         return dbh->errstr;
       };
-      #$i_sth->execute($oid) or do {
-      $i_sth->execute() or do {
+      $i_sth->execute() or do { #$i_sth->execute($oid)
         dbh->rollback if $FS::UID::AutoCommit;
         return $i_sth->errstr;
       };



More information about the freeside-commits mailing list