]> Frank Brehm's Git Trees - books.git/commitdiff
Speichern eines Buches abgeschlossen
authorFrank Brehm <frank@brehm-online.com>
Fri, 14 Nov 2008 16:12:58 +0000 (16:12 +0000)
committerFrank Brehm <frank@brehm-online.com>
Fri, 14 Nov 2008 16:12:58 +0000 (16:12 +0000)
lib/FrBr/Books/Controller/Books.pm
lib/FrBr/Books/Util/Book.pm

index 6166a23f15b0e33101e26a3bd3a57547a92a972d..17239e27738a7778879b248c2908e7ec430e5ea8 100644 (file)
@@ -155,7 +155,7 @@ sub form_new : Path('new') {
     $self->bookdata_session2stash($c);
     $c->stash->{'book_edit'}{'title'} = "Neues Buch" unless $c->stash->{'book_edit'}{'title'};
 
-    unless ( $c->request->params->{'book_form_sent'} ) {
+    unless ( $c->request->params->{'book_form_sent'} and $c->request->params->{'do_save'} ) {
         return 1;
     }
 
@@ -179,6 +179,17 @@ sub do_save_book : Private {
 
     return 1 unless $self->check_formparams( $c, $book );
 
+    eval {
+        die "Speichern des Buchs mißlungen." unless save_book( $c, $book );
+    };
+    if ( $@ ) {
+        $c->stash->{'error_message'} = $@;
+        return undef;
+    }
+
+    $c->stash->{'template'} = 'books/save_success.tt2';
+    delete $c->session->{'book_data_edit'} if exists $c->session->{'book_data_edit'};
+
     return 1;
 
 }
index 20bb7805274770703455101f029a118a05a5ba28..75669eaa20c76c3b13efebc60913568f9344072f 100644 (file)
@@ -284,6 +284,7 @@ sub save_book {
     $c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2;
 
     my $storage = $c->stash->{'storage'};
+    $c->log->debug( get_output_string( $K . "Storage: ", $storage ) ) if $c->stash->{'debug_level'} > 4;
 
     my $save_func = sub {
 
@@ -362,6 +363,10 @@ ENDE
 
     return undef unless $book_id;
 
+    save_buch_autoren( $c, $book_id, $book->{'autoren'} );
+    save_buch_kategorien( $c, $book_id, $book->{'kategorien'} );
+    save_buch_serien( $c, $book_id, $book->{'serien'} );
+
     return $book_id;
 
 }
@@ -380,7 +385,230 @@ sub save_buch_autoren {
 
     my $K    = ( caller(0) )[3] . "(): ";
 
-    
+    my $storage = $c->stash->{'storage'};
+    $c->log->debug( get_output_string( $K . "Storage: ", $storage ) ) if $c->stash->{'debug_level'} > 4;
+    my $dbh = $storage->dbh();
+    $c->log->debug( get_output_string( $K . "DBH: ", $dbh ) ) if $c->stash->{'debug_level'} > 2;
+
+    my $anzahl = 0;
+    $anzahl = scalar( @$autor_ids ) if $autor_ids and ref($autor_ids) and ref($autor_ids) eq 'ARRAY';
+
+    my $in = '';
+    $in = '(' . join( ", ", map { $dbh->quote($_) } @$autor_ids ) . ")" if $anzahl;
+
+    my $sql;
+    $sql = <<ENDE;
+DELETE FROM `autor2buch`
+ WHERE `buch_id` = ?
+ENDE
+    $sql .= "   AND `autor_id` NOT IN " . $in . "\n" if $anzahl;
+
+    my $qparams = [];
+    push @$qparams, $book_id;
+
+    if ( $storage->debug() ) {
+        my $text = $sql;
+        $text =~ s/\s+$//;
+        $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n";
+        warn $text;
+    }
+
+    my $sth = $dbh->prepare($sql);
+    $sth->execute( @$qparams );
+
+    return 1 unless $anzahl;
+
+    my $i = 0;
+
+    $sql = <<ENDE;
+INSERT INTO `autor2buch` (
+    `buch_id`, `autor_id`, `ord_num`
+  ) VALUES (
+    ?, ?, ? )
+  ON DUPLICATE KEY UPDATE
+    `ord_num` = ?
+ENDE
+
+    $sth = $dbh->prepare($sql);
+
+    for my $autor_id ( @$autor_ids ) {
+
+        $qparams = [];
+        push @$qparams, $book_id;
+        push @$qparams, $autor_id;
+        push @$qparams, $i;
+        push @$qparams, $i;
+
+        if ( $storage->debug() ) {
+            my $text = $sql;
+            $text =~ s/\s+$//;
+            $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n";
+            warn $text;
+        }
+
+        $sth->execute( @$qparams );
+
+        $i++;
+    }
+
+}
+
+#-----------------------------------------------------------------------------------
+
+=head2 save_buch_serien( $c, $book_id, @$serien_ids )
+
+=cut
+
+sub save_buch_serien {
+
+    my $c          = shift;
+    my $book_id    = shift;
+    my $serien_ids = shift;
+
+    my $K    = ( caller(0) )[3] . "(): ";
+
+    my $storage = $c->stash->{'storage'};
+    $c->log->debug( get_output_string( $K . "Storage: ", $storage ) ) if $c->stash->{'debug_level'} > 4;
+    my $dbh = $storage->dbh();
+    $c->log->debug( get_output_string( $K . "DBH: ", $dbh ) ) if $c->stash->{'debug_level'} > 2;
+
+    my $anzahl = 0;
+    $anzahl = scalar( @$serien_ids ) if $serien_ids and ref($serien_ids) and ref($serien_ids) eq 'ARRAY';
+
+    my $in = '';
+    $in = '(' . join( ", ", map { $dbh->quote($_) } @$serien_ids ) . ")" if $anzahl;
+
+    my $sql;
+    $sql = <<ENDE;
+DELETE FROM `buch2serie`
+ WHERE `buch_id` = ?
+ENDE
+    $sql .= "   AND `serien_id` NOT IN " . $in . "\n" if $anzahl;
+
+    my $qparams = [];
+    push @$qparams, $book_id;
+
+    if ( $storage->debug() ) {
+        my $text = $sql;
+        $text =~ s/\s+$//;
+        $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n";
+        warn $text;
+    }
+
+    my $sth = $dbh->prepare($sql);
+    $sth->execute( @$qparams );
+
+    return 1 unless $anzahl;
+
+    my $i = 1;
+
+    $sql = <<ENDE;
+INSERT INTO `buch2serie` (
+    `buch_id`, `serien_id`, `ord_num`
+  ) VALUES (
+    ?, ?, ? )
+  ON DUPLICATE KEY UPDATE
+    `ord_num` = IFNULL( `ord_num`, ? )
+ENDE
+
+    $sth = $dbh->prepare($sql);
+
+    for my $serien_id ( @$serien_ids ) {
+
+        $qparams = [];
+        push @$qparams, $book_id;
+        push @$qparams, $serien_id;
+        push @$qparams, $i;
+        push @$qparams, $i;
+
+        if ( $storage->debug() ) {
+            my $text = $sql;
+            $text =~ s/\s+$//;
+            $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n";
+            warn $text;
+        }
+
+        $sth->execute( @$qparams );
+
+        $i++;
+    }
+
+}
+
+#-----------------------------------------------------------------------------------
+
+=head2 save_buch_kategorien( $c, $book_id, @$kategorie_ids )
+
+=cut
+
+sub save_buch_kategorien {
+
+    my $c             = shift;
+    my $book_id       = shift;
+    my $kategorie_ids = shift;
+
+    my $K    = ( caller(0) )[3] . "(): ";
+
+    my $storage = $c->stash->{'storage'};
+    $c->log->debug( get_output_string( $K . "Storage: ", $storage ) ) if $c->stash->{'debug_level'} > 4;
+    my $dbh = $storage->dbh();
+    $c->log->debug( get_output_string( $K . "DBH: ", $dbh ) ) if $c->stash->{'debug_level'} > 2;
+
+    my $anzahl = 0;
+    $anzahl = scalar( @$kategorie_ids ) if $kategorie_ids and ref($kategorie_ids) and ref($kategorie_ids) eq 'ARRAY';
+
+    my $in = '';
+    $in = '(' . join( ", ", map { $dbh->quote($_) } @$kategorie_ids ) . ")" if $anzahl;
+
+    my $sql;
+    $sql = <<ENDE;
+DELETE FROM `buch2kategorie`
+ WHERE `buch_id` = ?
+ENDE
+    $sql .= "   AND `kategorie_id` NOT IN " . $in . "\n" if $anzahl;
+
+    my $qparams = [];
+    push @$qparams, $book_id;
+
+    if ( $storage->debug() ) {
+        my $text = $sql;
+        $text =~ s/\s+$//;
+        $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n";
+        warn $text;
+    }
+
+    my $sth = $dbh->prepare($sql);
+    $sth->execute( @$qparams );
+
+    return 1 unless $anzahl;
+
+    $sql = <<ENDE;
+INSERT INTO `buch2kategorie` (
+    `buch_id`, `kategorie_id`
+  ) VALUES (
+    ?, ? )
+  ON DUPLICATE KEY UPDATE
+    `kategorie_id` = `kategorie_id`
+ENDE
+
+    $sth = $dbh->prepare($sql);
+
+    for my $kategorie_id ( @$kategorie_ids ) {
+
+        $qparams = [];
+        push @$qparams, $book_id;
+        push @$qparams, $kategorie_id;
+
+        if ( $storage->debug() ) {
+            my $text = $sql;
+            $text =~ s/\s+$//;
+            $text .= ": " . join( ", ", map { $dbh->quote($_) } @$qparams ) . "\n";
+            warn $text;
+        }
+
+        $sth->execute( @$qparams );
+
+    }
 
 }