From 963e198bd05da3e5ba5affbff759c0ec50456b5e Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Fri, 29 Jul 2016 19:02:10 +0200
Subject: implemented create musicpool group


diff --git a/README b/README
index 13b99f0..0c004d7 100644
--- a/README
+++ b/README
@@ -44,7 +44,13 @@ CART RANGES:
   800000 --+----------+
            |          |
            |          |
-           |  200000  |  Music Pools (1000 Carts each)
+           |  100000  |  Music Pools (1000 Carts each)
+           |          |
+           |          |
+  900000 --+----------+
+           |          |
+           |          |
+           |  100000  |  <unused>
            |          |
            |          |
   999999 --+----------+
diff --git a/lib/RHRD/rddb.pm b/lib/RHRD/rddb.pm
index c209f81..5df0b6c 100755
--- a/lib/RHRD/rddb.pm
+++ b/lib/RHRD/rddb.pm
@@ -167,7 +167,7 @@ sub get_next_free_slot
     # print " --> " . $slot_name . ": " . $slot_low_cart . " - " . $slot_high_cart . " (" . $slot_chunksize . ")\n";;
     if($slot_chunksize != $group_chunksize) {
       $sth->finish();
-      return (undef, 'ERROR', "show group " . $slot_name . " has wrong chunksize " . $slot_chunksize . " != " . $group_chunksize);
+      return (undef, 'ERROR', "group " . $slot_name . " has wrong chunksize " . $slot_chunksize . " != " . $group_chunksize);
     }
 
     if($high_cart < $slot_low_cart) {
@@ -177,6 +177,10 @@ sub get_next_free_slot
   }
   $sth->finish();
 
+  if($high_cart > $group_high_cart) {
+    return (undef, 'ERROR', "there are no more free group cart slots");
+  }
+
   return ($low_cart, $high_cart);
 }
 
@@ -1755,6 +1759,23 @@ sub get_musicpools_next_free_slot
   return get_next_free_slot($ctx, $ctx->{'config'}{'specialgroups'}{'allpools'});
 }
 
+sub get_musicpool_num_from_cart
+{
+  my ($ctx, $cart) = @_;
+
+  my ($group_low_cart, $group_high_cart, $group_chunksize) = get_musicpools_cart_range($ctx);
+  unless (defined $group_low_cart) {
+    return ($group_low_cart, $group_high_cart, $group_chunksize);
+  }
+
+  if ($cart < $group_low_cart || $cart > $group_high_cart) {
+    return (undef, 'ERROR', "cart '$cart' is outside of musicpool range");
+  }
+
+  my $num = int(($cart - $group_low_cart) / $group_chunksize);
+  return ($num, 'OK', 'success');
+}
+
 sub list_musicpools
 {
   my ($ctx) = @_;
@@ -1798,10 +1819,10 @@ sub get_musicpool_info
 
   my ($group, $title, $low_cart, $high_cart, $params) = $sth->fetchrow_array();
   $sth->finish();
-  return (undef, 'ERROR', "pool $shortname does not exist") unless (defined $params);
+  return (undef, 'ERROR', "pool '$shortname' does not exist") unless (defined $params);
 
   my @p = split(';', $params);
-  return (undef, 'ERROR', "pool $shortname does not exist") unless ('M' eq $p[0]);
+  return (undef, 'ERROR', "pool '$shortname' does not exist") unless ('M' eq $p[0]);
 
   my $entry = {};
   $entry->{'SHORTNAME'} = $shortname;
@@ -1813,6 +1834,55 @@ sub get_musicpool_info
   return ($entry, 'OK', 'success');
 }
 
+sub create_musicpool_group
+{
+  my ($ctx, $groupname) = @_;
+
+  my ($cnt, $status, $errorstring) = add_group($ctx, $groupname);
+  unless(defined $cnt) {
+    return (undef, $status, $errorstring);
+  }
+
+  (my $low_cart, my $high_cart, $errorstring) = get_musicpools_next_free_slot($ctx);
+  if(!$low_cart) {
+    return (undef, $high_cart, $errorstring);
+  }
+  (my $num, $status, $errorstring) = get_musicpool_num_from_cart($ctx, $low_cart);
+  unless(defined $num) {
+    return (undef, $status, $errorstring);
+  }
+
+  ($cnt, $status, $errorstring) = set_group_cart_range($ctx, $groupname, $low_cart, $high_cart, 1, 'Y');
+  unless(defined $cnt) {
+    return (undef, $status, $errorstring);
+  }
+
+  ($cnt, $status, $errorstring) = RHRD::rddb::set_group_reports($ctx, $groupname, 'Y', 'N', 'Y');
+  unless(defined $cnt) {
+    return (undef, $status, $errorstring);
+  }
+
+  my $sql = qq{insert into AUDIO_PERMS (GROUP_NAME, SERVICE_NAME) values (?, ?);};
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
+
+  $sth->execute($groupname, $ctx->{'config'}{'shows'}{'service'})
+    or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
+  $sth->finish();
+
+  my @users = get_group_members($ctx, $ctx->{'config'}{'specialgroups'}{'allpools'});
+  if(!defined $users[0] && defined $users[1]) {
+    return (undef, $users[1], $users[2]);
+  }
+  for my $user (@users) {
+    ($cnt, $status, $errorstring) = RHRD::rddb::add_group_member($ctx, $groupname, $user);
+    return (undef, $status, $errorstring) unless(defined $cnt);
+  }
+
+  return ($num, 'OK', 'success');
+}
+
+
 sub is_musicpools_user
 {
   my ($ctx, $username) = @_;
diff --git a/lib/RHRD/utils.pm b/lib/RHRD/utils.pm
index d06e1ce..bcdac56 100755
--- a/lib/RHRD/utils.pm
+++ b/lib/RHRD/utils.pm
@@ -72,7 +72,7 @@ use constant {
   CMDLINE_DOW_HINT => "must be one of MO, TU, WE, TH, FR, SA, SU",
 
   # this is a subset of the colors from: stackoverflow.com/questions/2328339
-  POOL_COLORS => ["FFFF00", "1CE6FF", "FF34FF", "FF4A46", "008941", "006FA6", "A30059",
+  POOL_COLORS => ["FFFFFF", "FFFF00", "1CE6FF", "FF34FF", "FF4A46", "008941", "006FA6",
                   "FFDBE5", "7A4900", "0000A6", "63FFAC", "B79762", "004D43", "8FB0FF",
                   "5A0007", "809693", "FEFFE6", "4FC601", "3B5DFF", "4A3B53", "FF2F80",
                   "61615A", "BA0900", "6B7900", "00C2A0", "FFAA92", "FF90C9", "B903AA",
@@ -81,7 +81,7 @@ use constant {
                   "00489C", "6F0062", "0CBD66", "EEC3FF", "B77B68", "7A87A1", "788D66",
                   "885578", "FAD09F", "FF8A9A", "D157A0", "BEC459", "0086ED", "886F4C",
                   "549E79", "FFF69F", "72418F", "BC23FF", "99ADC0", "3A2465", "922329",
-                  "5B4534", "FDE8DC", "404E55", "0089A3", "CB7E98", "A4E804", "324E72",
+                  "5B4534", "404E55", "0089A3", "CB7E98", "A4E804", "324E72", "A30059",
                   "B4A8BD", "452C2C", "636375", "A3C8C9", "9B9700", "D0AC94", "FF6832",
                   "575329", "00FECF", "B05B6F", "8CD0FF", "1E6E00", "66E1D3", "CFCDAC",
                   "A77500", "6367A9", "A05837", "772600", "D790FF", "5B4E51", "8ADBB4",
@@ -89,6 +89,14 @@ use constant {
                   "DA007C", "FF1A59"]
 };
 
+sub get_musicpool_color
+{
+  my ($num) = @_;
+
+  $num = 0 if($num < 0 || $num > $#{+POOL_COLORS});
+  return POOL_COLORS->[$num];
+}
+
 sub dropbox_param_type_ok
 {
   my ($type) = @_;
diff --git a/utils/rhrd-pool b/utils/rhrd-pool
index 69bc702..618c3e7 100755
--- a/utils/rhrd-pool
+++ b/utils/rhrd-pool
@@ -30,7 +30,7 @@ sub print_usage
 {
   print STDERR "Usage: rhrd-pool list\n" .
                "       rhrd-pool (show|remove) <short-name>\n" .
-               "       rhrd-pool add <short-name> <groupname> <title>\n" .
+               "       rhrd-pool add <groupname> <title>\n" .
                "       rhrd-pool edit <short-name> <title>\n";
 }
 
@@ -65,7 +65,7 @@ sub show
 
 sub add__check_arguments
 {
-  my ($groupname, $shortname, $title) = @_;
+  my ($groupname, $title) = @_;
 
   if($groupname !~ m/^[-a-zA-Z0-9_]{1,10}$/) {
     print STDERR "name '" . $groupname . "' contains illegal characters or is too long/short\n";
@@ -73,24 +73,19 @@ sub add__check_arguments
     return 1;
   }
 
-  if($shortname !~ m/^P([0-9]{2})$/) {
-    print STDERR "short name '" . $shortname . "' must be a value between P00 and P99\n";
-    return 1;
-  }
-  my $color = RHRD::utils::POOL_COLORS->[int($1)];
-  return 0, $color;
+  return 0;
 }
 
 sub add
 {
-  my ($ctx, $shortname, $groupname, $title) = @_;
+  my ($ctx, $groupname, $title) = @_;
 
-  my ($ret, $color) = add__check_arguments($groupname, $shortname, $title);
+  my ($ret) = add__check_arguments($groupname, $title);
   if($ret) {
     return $ret;
   }
 
-  print " * creating pool: '" . $title . "' (" . $shortname . ") with group '" . $groupname . "'\n";
+  print " * creating pool: '" . $title . "' with group '" . $groupname . "'\n";
 
   my ($result, $status, $errorstring) = RHRD::rddb::check_group($ctx, $groupname);
   unless(defined $result) {
@@ -102,12 +97,14 @@ sub add
     return 1
   }
 
-  print " * creating group '" . $groupname . "'\n";
-  ($result, $status, $errorstring) = RHRD::rddb::create_musicpool_group($ctx, $groupname);
-  unless(defined $result) {
+  (my $num, $status, $errorstring) = RHRD::rddb::create_musicpool_group($ctx, $groupname);
+  unless(defined $num) {
     print STDERR $status . ": " . $errorstring . "\n";
     return 1;
   }
+  my $shortname = sprintf("P%02d", $num);
+  my $color = RHRD::utils::get_musicpool_color($num);
+  print " * created group '" . $groupname . "' --> pool shortname / color: " . $shortname . " / " . $color . "\n";
 
   ($result, $status, $errorstring) = RHRD::rddb::create_musicpool_event($ctx, $shortname, $groupname);
   unless(defined $result) {
@@ -205,11 +202,11 @@ if(defined $ctx) {
     }
   }
   elsif($cmd eq "add") {
-    if($num_args != 4) {
+    if($num_args != 3) {
       print_usage();
       $ret = 1;
     } else {
-      $ret = add($ctx, $ARGV[1], $ARGV[2], $ARGV[3]);
+      $ret = add($ctx, $ARGV[1], $ARGV[2]);
     }
   }
   elsif($cmd eq "edit") {
-- 
cgit v0.10.2