RT# 82942 Replace DBI->connect() with FS::DBI->connect()
[freeside.git] / FS / FS / part_export / phone_sqlopensips.pm
index cb644a0..c281787 100644 (file)
@@ -8,6 +8,7 @@ use FS::part_export;
 use FS::svc_phone;
 use FS::export_svc;
 use LWP::UserAgent;
+use FS::DBI;
 
 @ISA = qw(FS::part_export);
 
@@ -16,66 +17,47 @@ tie %options, 'Tie::IxHash',
   'username' => { label=>'Database username' },
   'password' => { label=>'Database password' },
   'xmlrpc_url' => { label=>'XMLRPC URL' },
+  # XXX: in future, add non-agent-virtualized config, i.e. per-export setting of gwlist, routeid, description, etc.
+  # and/or setting description from the phone_name column
 ;
 
 %info = (
-  'svc'      => 'svc_phone',
-  'desc'     => 'Export DIDs to OpenSIPs dr_rules table',
-  'options'  => \%options,
-  'notes'    => 'Export DIDs to OpenSIPs dr_rules table',
+  'svc'        => 'svc_phone',
+  'desc'       => 'Export DIDs to OpenSIPs dr_rules table',
+  'options'    => \%options,
+  'no_machine' => 1,
+  'notes'      => 'Export DIDs to OpenSIPs dr_rules table',
 );
 
 sub rebless { shift; }
 
 sub _export_insert {
   my($self, $svc_x) = (shift, shift);
+
+  my $conf = new FS::Conf;
+  my $agentnum = $svc_x->cust_svc->cust_pkg->cust_main->agentnum || 0;
+  my $gwlist = $conf->config('opensips_gwlist',$agentnum) || '';
+  my $description = $conf->config('opensips_description',$agentnum) || '';
+  my $route = $conf->config('opensips_route',$agentnum) || '';
+
   my $dbh = $self->opensips_connect;
   my $sth = $dbh->prepare("insert into dr_rules ".
            "( groupid, prefix, timerec, routeid, gwlist, description ) ".
            " values ( ?, ?, ?, ?, ?, ? )") or die $dbh->errstr;
-  $sth->execute('0',$svc_x->phonenum,'',$svc_x->route,$svc_x->gwlist,
-               $svc_x->phone_name) or die $sth->errstr;
+  $sth->execute('0',$svc_x->phonenum,'',$route,$gwlist,$description)
+           or die $sth->errstr;
   $dbh->disconnect;
-  $self->dr_reload;
+  $self->dr_reload; # XXX: if this fails, do we delete what we just inserted?
 }
 
 sub opensips_connect {
     my $self = shift;
-    DBI->connect($self->option('datasrc'),$self->option('username'),
-                       $self->option('password')) or die $DBI::errstr;
+    FS::DBI->connect($self->option('datasrc'),$self->option('username'),
+                       $self->option('password')) or die $FS::DBI::errstr;
 }
 
 sub _export_replace {
-  my( $self, $new, $old ) = (shift, shift, shift);
-    my @update = ();
-    my @paramvalues = ();
-
-    if($old->route ne $new->route){
-       push @update, 'routeid = ?';
-       push @paramvalues, $new->route;
-    }
-
-    if($old->phone_name ne $new->phone_name) {
-       push @update, 'description = ?';
-       push @paramvalues, $new->phone_name;
-    }
-
-    if($old->gwlist ne $new->gwlist) {
-       push @update, 'gwlist = ?';
-       push @paramvalues, $new->gwlist;
-    }
-
-    if(scalar(@update)) {
-      my $update_str = join(' and ',@update);
-      my $dbh = $self->opensips_connect;
-      my $sth = $dbh->prepare("update dr_rules set $update_str " . 
-           " where prefix = ? ") or die $dbh->errstr;
-      push @paramvalues, $old->phonenum;
-      $sth->execute(@paramvalues) or die $sth->errstr;
-      $dbh->disconnect;
-      return $self->dr_reload;
-    }
-  '';
+   '';
 }
 
 sub _export_suspend {
@@ -95,7 +77,7 @@ sub _export_delete {
     or die $dbh->errstr;
   $sth->execute($svc_x->phonenum) or die $sth->errstr;
   $dbh->disconnect;
-  $self->dr_reload;
+  $self->dr_reload; # XXX: if this fails, do we re-insert what we just deleted?
 }
 
 sub dr_reload {
@@ -113,3 +95,4 @@ sub dr_reload {
     '';
 }
 
+1;