From a7b6abe6afb412f1a3f9efb07d51c71ed3127bae Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@spreadspace.org>
Date: Tue, 6 Oct 2015 03:28:21 +0200
Subject: creation of show macro cart is working now as well


diff --git a/lib/RHRD/rddb.pm b/lib/RHRD/rddb.pm
index e1afd55..d87acb7 100755
--- a/lib/RHRD/rddb.pm
+++ b/lib/RHRD/rddb.pm
@@ -1002,7 +1002,7 @@ sub create_show_log
     return (undef, 'ERROR', "Log with name '" . $logname . "' already exists")
   }
 
-  my $sql = qq{insert into LOGS set NAME = ?, LOG_EXISTS='N', TYPE=0, DESCRIPTION = ?, ORIGIN_USER = ?, ORIGIN_DATETIME=NOW(), LINK_DATETIME=NOW(), SERVICE = ?};
+  my $sql = qq{insert into LOGS (NAME, LOG_EXISTS, TYPE, DESCRIPTION, ORIGIN_USER, ORIGIN_DATETIME, LINK_DATETIME, SERVICE) values (?, 'N', 0, ?, ?, NOW(), NOW(), ?)};
 
   my $sth = $ctx->{'dbh'}->prepare($sql)
     or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
@@ -1010,8 +1010,10 @@ sub create_show_log
   $sth->execute($logname, $logname . " log", $ctx->{'config'}{'shows'}{'defaultuser'}, $ctx->{'config'}{'shows'}{'service'})
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
 
+  $sth->finish();
+
   (my $result, $status, $errorstring) = create_log_table($ctx, $logname);
-  unless (defined $result && defined $status) {
+  unless (defined $result) {
     return (undef, $status, $errorstring);
   }
 
@@ -1039,9 +1041,58 @@ sub create_show_log
   $sth->execute($next_id, $logname)
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
 
+  $sth->finish();
+
   return (1, 'OK', 'success');
 }
 
+sub get_next_free_showid
+{
+  my ($ctx) = @_;
+
+  my ($low, $high, $type, undef) = RHRD::rddb::get_group_carts($ctx, $ctx->{'config'}{'specialgroups'}{'shows'});
+  unless(defined $low) {
+    return (undef, $high, $type);
+  }
+
+  my $sql = qq{select NUMBER from CART where GROUP_NAME = ? order by NUMBER};
+
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
+
+  $sth->execute($ctx->{'config'}{'specialgroups'}{'shows'})
+    or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
+
+  my $show_id = $low;
+  while(my ($cart) = $sth->fetchrow_array()) {
+    last if($show_id < $cart);
+    $show_id += 1;
+  }
+  $sth->finish();
+
+  return ($show_id, 'OK', 'success');
+}
+
+sub create_show_macro_cart
+{
+  my ($ctx, $logname, $title) = @_;
+  my $macro = 'LL 1 ' . $logname . ' 0!';
+
+  my ($number, $status, $errorstring) = get_next_free_showid($ctx);
+
+  my $sql = qq{insert into CART (NUMBER, TYPE, GROUP_NAME, TITLE, MACROS, VALIDITY, METADATA_DATETIME) values (?, 2, ?, ?, ?, 3, NOW())};
+
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
+
+  $sth->execute($number, $ctx->{'config'}{'specialgroups'}{'shows'}, $title, $macro)
+    or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
+
+  $sth->finish();
+
+  return ($number, 'OK', 'success');
+}
+
 ###########################  MUSICPOOL handling  ###########################
 
 sub get_musicpools_cart_range
diff --git a/utils/rd-show b/utils/rd-show
index 0ec08da..0a0bc1e 100755
--- a/utils/rd-show
+++ b/utils/rd-show
@@ -237,8 +237,14 @@ sub add
   }
   print " * created log with name: " . $name . "\n";
 
+  (my $show_id, $status, $errorstring) = RHRD::rddb::create_show_macro_cart($ctx, $name, $title);
+  unless(defined $result) {
+    print STDERR $status . ": " . $errorstring . "\n";
+    return 1;
+  }
+  print " * created macro cart -> new show-id = " . $show_id . "\n";
+
   # TODO:
-  #   create macro cart referencing log -> show-id
   #   create dropbox for: groupname, show-id, rhythm, dow, starttime, len
 
   print " * finished\n";
-- 
cgit v0.10.2