From c253bf234deafa69ed2e462c8ec2d8f41b484e9e Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@spreadspace.org>
Date: Fri, 9 Oct 2015 19:40:46 +0200
Subject: added some checks before drastic changes of a group


diff --git a/lib/RHRD/rddb.pm b/lib/RHRD/rddb.pm
index 5aedef4..67b431f 100755
--- a/lib/RHRD/rddb.pm
+++ b/lib/RHRD/rddb.pm
@@ -907,15 +907,23 @@ sub get_shows_next_free_slot
 
 sub list_shows
 {
-  my ($ctx) = @_;
+  my ($ctx, $groupname) = @_;
 
-  my $sql = qq{select TO_CART,SET_USER_DEFINED from DROPBOXES where STATION_NAME=? order by TO_CART;};
+  my $sql = qq{select TO_CART,SET_USER_DEFINED from DROPBOXES where STATION_NAME = ? order by TO_CART;};
+  if(defined($groupname)) {
+    $sql = qq{select TO_CART,SET_USER_DEFINED from DROPBOXES where STATION_NAME = ? and GROUP_NAME = ? order by TO_CART;};
+  }
 
   my $sth = $ctx->{'dbh'}->prepare($sql)
     or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
-  $sth->execute($ctx->{'config'}{'dropboxes'}{'dropbox-pseudo-station'})
-    or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
+  unless(defined($groupname)) {
+    $sth->execute($ctx->{'config'}{'dropboxes'}{'dropbox-pseudo-station'})
+      or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
+  } else {
+    $sth->execute($ctx->{'config'}{'dropboxes'}{'dropbox-pseudo-station'}, $groupname)
+      or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
+  }
 
   my @show_dbs;
   while(my ($to_cart, $params, $lowcart, $highcart) = $sth->fetchrow_array()) {
diff --git a/utils/rhrd-group b/utils/rhrd-group
index 4f59c07..1de7e4b 100755
--- a/utils/rhrd-group
+++ b/utils/rhrd-group
@@ -81,6 +81,16 @@ sub remove
 {
   my ($ctx, $groupname) = @_;
 
+  my @carts = RHRD::rddb::get_show_group_carts_used($ctx, $groupname);
+  if(!defined $carts[0] && defined $carts[1]) {
+    print STDERR $carts[1] . ": " . $carts[2] . "\n";
+    return 1;
+  }
+  unless(scalar @carts == 0) {
+    print "won't remove group with active shows\n use 'rhrd-show list " . $groupname . "' to find out which show(s) use this group\n";
+    return 1;
+  }
+
   my @results = RHRD::rddb::remove_group($ctx, $groupname);
   if(!defined $results[0] && defined $results[2]) {
     print STDERR "$results[2]\n";
@@ -165,9 +175,23 @@ sub set_carts
 
   $low_cart = RHRD::rddb::RD_INVALID_CART unless($low_cart >= RHRD::rddb::RD_MIN_CART && $low_cart <= RHRD::rddb::RD_MAX_CART);
   $high_cart = RHRD::rddb::RD_INVALID_CART unless($high_cart >= RHRD::rddb::RD_MIN_CART && $high_cart <= RHRD::rddb::RD_MAX_CART);
-  $cart_type = 1 unless($cart_type == 1 || $cart_type == 2);
-  $enforce_cart_range = 'Y' if ($enforce_cart_range eq 'Y' || $enforce_cart_range eq 'y' || $enforce_cart_range eq '1');
-  $enforce_cart_range = 'N' unless ($enforce_cart_range eq 'Y');
+  $cart_type = 1 unless(defined($cart_type) && ($cart_type == 1 || $cart_type == 2));
+  $enforce_cart_range = 'Y' if(defined($enforce_cart_range) && ($enforce_cart_range eq 'Y' || $enforce_cart_range eq 'y' || $enforce_cart_range eq '1'));
+  $enforce_cart_range = 'N' unless(defined($enforce_cart_range) &&$enforce_cart_range eq 'Y');
+
+  my @carts = RHRD::rddb::get_show_group_carts_used($ctx, $groupname);
+  if(!defined $carts[0] && defined $carts[1]) {
+    print STDERR $carts[1] . ": " . $carts[2] . "\n";
+    return 1;
+  }
+  unless(scalar @carts == 0) {
+    for my $cart (@carts) {
+      if($cart < $low_cart || $cart > $high_cart) {
+        print "this group has active shows with carts outside of the new range\n use 'rhrd-show list " . $groupname . "' to find out which show(s) use this group\n";
+        return 1;
+      }
+    }
+  }
 
   my ($cnt, undef, $errorstring) = RHRD::rddb::set_group_cart_range($ctx, $groupname, $low_cart, $high_cart, $cart_type, $enforce_cart_range);
   unless(defined $cnt) {
diff --git a/utils/rhrd-show b/utils/rhrd-show
index f2a6aed..cc3b859 100755
--- a/utils/rhrd-show
+++ b/utils/rhrd-show
@@ -33,16 +33,16 @@ use RHRD::utils;
 
 sub print_usage
 {
-  print STDERR "Usage: rd-show list\n" .
+  print STDERR "Usage: rd-show list [ <group> ]\n" .
                "       rd-show (show|remove) <show-id>\n" .
                "       rd-show add <groupname> <name> <title> <num-carts> <rhythm> <dow> <starttime> <len>\n";
 }
 
 sub list
 {
-  my ($ctx) = @_;
+  my ($ctx, $group) = @_;
 
-  my @shows = RHRD::rddb::list_shows($ctx);
+  my @shows = RHRD::rddb::list_shows($ctx, $group);
   if(!defined $shows[0] && defined $shows[1]) {
     print STDERR "$shows[1]: $shows[2]";
     return 1;
@@ -219,11 +219,11 @@ my $ret = 0;
 my ($ctx, $status, $errorstring) = RHRD::rddb::init();
 if(defined $ctx) {
   if($cmd eq "list") {
-    if($num_args != 1) {
+    if($num_args > 2) {
       print_usage();
       $ret = 1;
     } else {
-      $ret = list($ctx)
+      $ret = list($ctx, $ARGV[1])
     }
   }
   elsif($cmd eq "show") {
-- 
cgit v0.10.2