From 62488862215502f7e96fa7e40ab7ac66d0766a13 Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@spreadspace.org>
Date: Thu, 17 Dec 2015 17:26:17 +0100
Subject: setting music pool clock works now and get returns title as well


diff --git a/lib/RHRD/rddb.pm b/lib/RHRD/rddb.pm
index f70a87a..cd0374a 100755
--- a/lib/RHRD/rddb.pm
+++ b/lib/RHRD/rddb.pm
@@ -1454,7 +1454,7 @@ sub is_musicpools_user
 {
   my ($ctx, $username) = @_;
 
-  return 0 if $username = '';
+  return 0 if $username eq '';
 
   for my $groupuser (@{$ctx->{'config'}{'specialusers'}{'allpools'}}) {
     if ($username eq $groupuser) {
@@ -1478,8 +1478,7 @@ sub get_musicpools_clocks
     }
   }
 
-  my $sql = 'select GRID.DOW,GRID.HOUR,CLOCKS.NAME,CLOCKS.SHORT_NAME,CLOCKS.COLOR from CLOCKS,(' . join(' UNION ' , @reqs) . ") as GRID where GRID.CLOCK = CLOCKS.NAME;";
-  print $sql;
+  my $sql = 'select GRID.DOW,GRID.HOUR,CLOCKS.SHORT_NAME,CLOCKS.COLOR,GROUPS.DESCRIPTION from DROPBOXES,GROUPS,CLOCKS,(' . join(' UNION ' , @reqs) . ") as GRID where GRID.CLOCK = CLOCKS.NAME and CLOCKS.SHORT_NAME = DROPBOXES.PATH and DROPBOXES.GROUP_NAME = GROUPS.NAME;";
 
   my $sth = $ctx->{'dbh'}->prepare($sql)
     or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
@@ -1488,13 +1487,13 @@ sub get_musicpools_clocks
     or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
 
   my @clocks;
-  while(my ($dow, $hour, $name, $color) = $sth->fetchrow_array()) {
+  while(my ($dow, $hour, $shortname, $color, $title) = $sth->fetchrow_array()) {
     my $clock = {};
     $clock->{'DOW'} = $dow;
     $clock->{'HOUR'} = $hour;
-    $clock->{'NAME'} = $name;
+    $clock->{'SHORTNAME'} = $shortname;
     $clock->{'COLOR'} = $color;
-    $clock->{'TITLE'} = "unknown title"; # TODO: fetch title from Group Description
+    $clock->{'TITLE'} = $title;
     push @clocks, $clock;
   }
 
@@ -1505,15 +1504,29 @@ sub get_musicpools_clocks
 
 sub set_musicpools_clock
 {
-  my ($ctx, $dow, $hour, $name) = @_;
+  my ($ctx, $dow, $hour, $shortname) = @_;
+
+  my $sql = qq{select NAME from CLOCKS where SHORT_NAME = ?;};
+
+  my $sth = $ctx->{'dbh'}->prepare($sql)
+    or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
+
+  $sth->execute($shortname)
+    or return (undef, 'ERROR', "Database Error: " . $sth->errstr);
+
+  my ($name) = $sth->fetchrow_array;
+  $sth->finish();
+
+  if(!defined $name) {
+    return (undef, 'ERROR', "clock '" . $shortname . "' does not exist");
+  }
 
   my $day = $dow - 1;
   $day = 6 if $dow == 0;
   my $idx = (($day*24) + $hour);
+  $sql = 'update SERVICES set CLOCK' . $idx . ' = ? where NAME = ?';
 
-  my $sql = 'update SERVICES set CLOCK' . $idx . ' = (select NAME from CLOCKS where SHORT_NAME = ?) where NAME = ?'; # TODO: only set if CLOCKS.NAME is not null
-
-  my $rows = $ctx->{'dbh'}->do($sql, $name, $ctx->{'config'}{'shows'}{'service'})
+  my $rows = $ctx->{'dbh'}->do($sql, undef, $name, $ctx->{'config'}{'shows'}{'service'})
     or return (undef, 'ERROR', "Database Error: " . $ctx->{'dbh'}->errstr);
 
   unless($rows == 1) {
diff --git a/test/get-music-clocks b/test/get-music-clocks
index ca0a9f4..5b06c48 100755
--- a/test/get-music-clocks
+++ b/test/get-music-clocks
@@ -44,5 +44,5 @@ if(defined $ctx) {
 
 print "Clocks:\n";
 for my $href (@clocks) {
-  print " * " . $href->{'DOW'} . "/" . $href->{'HOUR'} . ": " . $href->{'NAME'} . ", " . $href->{'COLOR'} . "\n";
+  print " * " . $href->{'DOW'} . "/" . $href->{'HOUR'} . ": " . $href->{'SHORTNAME'} . ", " . $href->{'COLOR'} . ", " . $href->{'TITLE'} . "\n";
 }
-- 
cgit v0.10.2