From 9ca564354715514ad7199294749fb80085d9fec9 Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Wed, 3 Aug 2016 19:01:47 +0200
Subject: added clear command to rhrd-pool


diff --git a/lib/RHRD/rddb.pm b/lib/RHRD/rddb.pm
index 5e6289e..7c5bae3 100755
--- a/lib/RHRD/rddb.pm
+++ b/lib/RHRD/rddb.pm
@@ -2296,6 +2296,41 @@ sub update_musicpool_title
   return ($cnt, 'OK', 'success');
 }
 
+sub clear_musicpool
+{
+  my ($ctx, $shortname) = @_;
+
+  my ($groupname, $status, $errorstring) = get_musicpool_group($ctx, $shortname);
+  unless (defined $groupname) {
+    return (undef, $status, $errorstring);
+  }
+
+  my @actions = ({
+    # Delete Member Cuts
+    sql => qq{delete from CUTS where CART_NUMBER IN (select NUMBER from CART where GROUP_NAME = ?);},
+    name => 'member cuts',
+    cnt => 0
+  }, {
+    # Delete Member Carts
+    sql => qq{delete from CART where GROUP_NAME = ?;},
+    name => 'member carts',
+    cnt => 0
+  });
+
+  for my $href (@actions) {
+    my $sth = $ctx->{'dbh'}->prepare($href->{sql})
+      or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
+    delete($href->{sql});
+
+    $href->{cnt} = $sth->execute($groupname)
+      or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
+
+    $sth->finish();
+  }
+
+  return @actions;
+}
+
 sub remove_musicpool
 {
   my ($ctx, $shortname) = @_;
diff --git a/utils/rhrd-pool b/utils/rhrd-pool
index acaff65..ada9b6f 100755
--- a/utils/rhrd-pool
+++ b/utils/rhrd-pool
@@ -29,7 +29,7 @@ sub print_usage
 {
   print STDERR "Usage: rhrd-pool list\n" .
                "       rhrd-pool show <short-name>\n" .
-               "       rhrd-pool remove <short-name> [--force]\n" .
+               "       rhrd-pool remove|clear <short-name> [--force]\n" .
                "       rhrd-pool add <groupname> <title>\n" .
                "       rhrd-pool edit <short-name> <title>\n";
 }
@@ -209,6 +209,38 @@ sub remove
   return 0;
 }
 
+sub clear
+{
+  my ($ctx, $shortname, $force) = @_;
+
+  if(defined($force) && $force ne "--force") {
+    print_usage();
+    return 1;
+  }
+
+  my @slots = RHRD::rddb::get_musicpool_clock_usage($ctx, $shortname);
+  if(!defined $slots[0] && defined $slots[1]) {
+    print STDERR "$slots[1]: $slots[2]";
+    return 1;
+  }
+  if(scalar(@slots) > 0) {
+    print STDERR "musicpool is still in use (" . scalar(@slots) . " grid entries)\n";
+    return 1 unless(defined($force));
+    print STDERR "  *** forced clearing ***\n"
+  }
+
+  my @results = RHRD::rddb::clear_musicpool($ctx, $shortname);
+  if(!defined $results[0] && defined $results[2]) {
+    print STDERR $results[1] . ": " . $results[2] . "\n";
+    return 1;
+  }
+  for my $href (@results) {
+    print int($href->{cnt}) . " " . $href->{name} . " deleted\n";
+  }
+
+  return 0;
+}
+
 my $num_args = $#ARGV + 1;
 if($num_args < 1) {
   print_usage();
@@ -243,6 +275,14 @@ if(defined $ctx) {
       $ret = remove($ctx, $ARGV[1], $ARGV[2]);
     }
   }
+  elsif($cmd eq "clear") {
+    if($num_args < 2 || $num_args > 3) {
+      print_usage();
+      $ret = 1;
+    } else {
+      $ret = clear($ctx, $ARGV[1], $ARGV[2]);
+    }
+  }
   elsif($cmd eq "add") {
     if($num_args != 3) {
       print_usage();
-- 
cgit v0.10.2