From c6c891b3921dce1cb3575e67a6f45f0fb24f722d Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@spreadspace.org>
Date: Fri, 9 Oct 2015 19:03:10 +0200
Subject: improved show deletion


diff --git a/lib/RHRD/rddb.pm b/lib/RHRD/rddb.pm
index 07e0f86..5aedef4 100755
--- a/lib/RHRD/rddb.pm
+++ b/lib/RHRD/rddb.pm
@@ -968,9 +968,32 @@ sub create_show_group
     return (undef, $status, $errorstring);
   }
 
+  for my $user (@{$ctx->{'config'}{'specialusers'}{'allshows'}}) {
+    ($cnt, $status, $errorstring) = RHRD::rddb::add_group_member($ctx, $groupname, $user);
+    return (undef, $status, $errorstring) unless(defined $cnt);
+  }
+
   return ($low_cart, 'OK', 'success');
 }
 
+sub get_show_group
+{
+  my ($ctx, $showid) = @_;
+
+  my $sql = qq{select GROUP_NAME from DROPBOXES where TO_CART = ? and DROPBOXES.STATION_NAME = ?;};
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
+
+  $sth->execute($showid, $ctx->{'config'}{'dropboxes'}{'dropbox-pseudo-station'})
+    or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
+
+  my ($groupname) = $sth->fetchrow_array();
+  unless(defined($groupname)) {
+    return (undef, 'ERROR', "Show not found");
+  }
+  return ($groupname, 'OK', 'success');
+}
+
 sub get_show_group_cart_range
 {
   my ($ctx, $showid) = @_;
@@ -1240,6 +1263,11 @@ sub remove_show
     return (undef, $status, $errorstring);
   }
 
+  my @show_carts = get_show_carts($ctx, $showid);
+  if(!defined $show_carts[0] && defined $show_carts[1]) {
+    return (undef, $show_carts[1], $show_carts[2]);
+  }
+
   my @actions = ({
       # Delete Dropbox
       sql => qq{delete from DROPBOXES where TO_CART = ?;},
@@ -1271,6 +1299,20 @@ sub remove_show
     $sth->finish();
   }
 
+  if(!defined $show_carts[0]) {
+    push @actions, { name => 'audio carts', cnt => 0 };
+  } else {
+    my $sth = $ctx->{'dbh'}->prepare(qq{delete from CART where NUMBER = ?;})
+      or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
+
+    my $cnt = 0;
+    for my $cart (@show_carts) {
+      $cnt += ($sth->execute($cart) or return (undef, 'ERROR', "Database Error: " . $sth->errstr));
+    }
+    $sth->finish();
+    push @actions, { name => 'audio carts', cnt => $cnt };
+  }
+
   (my $cnt, $status, $errorstring) = drop_log_table($ctx, $logname);
   unless (defined $cnt) {
     return (undef, $status, $errorstring);
diff --git a/utils/rhrd-sanity-check b/utils/rhrd-sanity-check
index 93d933e..6cfb2d7 100755
--- a/utils/rhrd-sanity-check
+++ b/utils/rhrd-sanity-check
@@ -79,6 +79,7 @@ sub check_groups__check_show_group
   }
   if(scalar @carts == 0) {
     print " group '" . $group . "': carts are not used by any show\n";
+    $errors++;
   }
 
   my ($nownext, $traffic, $music) = RHRD::rddb::get_group_reports($ctx, $group);
diff --git a/utils/rhrd-show b/utils/rhrd-show
index 7cb8422..f2a6aed 100755
--- a/utils/rhrd-show
+++ b/utils/rhrd-show
@@ -181,14 +181,30 @@ sub remove
 {
   my ($ctx, $show_id) = @_;
 
+  my ($group, $status, $errorstring) = RHRD::rddb::get_show_group($ctx, $show_id);
+  unless(defined $group) {
+    print STDERR $status . ": " . $errorstring . "\n";
+    return 1;
+  }
+
   my @results = RHRD::rddb::remove_show($ctx, $show_id);
   if(!defined $results[0] && defined $results[2]) {
-    print STDERR "$results[2]\n";
+    print STDERR $results[1] . ": " . $results[2] . "\n";
     return 1;
   }
   for my $href (@results) {
     print int($href->{cnt}) . " " . $href->{name} . " deleted\n";
   }
+
+  my @carts = RHRD::rddb::get_show_group_carts_used($ctx, $group);
+  if(!defined $carts[0] && defined $carts[1]) {
+    print STDERR $carts[1] . ": " . $carts[2] . "\n";
+    return 1;
+  }
+  if(scalar @carts == 0) {
+    print "group '" . $group . "' is now empty .. you should probably remove it!\n";
+  }
+
   return 0;
 }
 
-- 
cgit v0.10.2