]> Frank Brehm's Git Trees - books.git/commitdiff
Bindungsart dazu, mit Speichern begonnen
authorFrank Brehm <frank@brehm-online.com>
Tue, 11 Nov 2008 22:01:41 +0000 (22:01 +0000)
committerFrank Brehm <frank@brehm-online.com>
Tue, 11 Nov 2008 22:01:41 +0000 (22:01 +0000)
lib/FrBr/Books/Controller/Books.pm
lib/FrBr/Books/Util/Bindungsart.pm [new file with mode: 0644]
lib/FrBr/Books/Util/Book.pm
root/src/books/book_form.tt2
root/src/books/save_success.tt2 [new file with mode: 0644]

index 517d90eec9ae70c52a8729d4b588a098f22402a2..6166a23f15b0e33101e26a3bd3a57547a92a972d 100644 (file)
@@ -10,6 +10,7 @@ use base 'Catalyst::Controller';
 use FrBr::Common;
 use FrBr::Books::Util::Author;
 use FrBr::Books::Util::Book;
+use FrBr::Books::Util::Bindungsart;
 use FrBr::Books::Util::Category;
 use FrBr::Books::Util::Serie;
 use FrBr::Books::Util::Ort;
@@ -158,10 +159,52 @@ sub form_new : Path('new') {
         return 1;
     }
 
+    return $self->do_save_book($c);
+
+}
+
+#-------------------------------------------------------
+
+sub do_save_book : Private {
+
+    my ( $self, $c ) = @_;
+    my $K = ( caller(0) )[3] . "(): ";
+
+    my $book = $c->stash->{'book_edit'};
+
+    $book->{'autoren'}    = $c->stash->{'autor_list_book'};
+    $book->{'kategorien'} = $c->stash->{'kategorie_list_book'};
+    $book->{'serien'}     = $c->stash->{'serienliste_book'};
+    $c->log->debug( get_output_string( $K . "Buchangaben, die gesichert werden sollen: ", $book ) );
+
+    return 1 unless $self->check_formparams( $c, $book );
+
+    return 1;
+
+}
+
+#-------------------------------------------------------
+
+sub check_formparams : Private {
+
+    my ( $self, $c, $book ) = @_;
+    my $K = ( caller(0) )[3] . "(): ";
+
+    unless ( $book->{'title'} ) {
+        $c->stash->{'error_message'} = "Kein Buchtitel angegeben.";
+        return undef;
+    }
+
+    unless ( scalar( @{$book->{'autoren'}} ) ) {
+        $c->stash->{'error_message'} = "Kein Buchautor angegeben.";
+        return undef;
+    }
+
     return 1;
 
 }
 
+
 #-------------------------------------------------------
 
 sub bookdata_session2stash : Private {
@@ -222,12 +265,17 @@ sub bookdata_session2stash : Private {
     $c->stash->{'book_edit'}{'untertitel'}     = $c->session->{'book_data_edit'}{'untertitel'};
     $c->stash->{'book_edit'}{'original_title'} = $c->session->{'book_data_edit'}{'original_title'};
     $c->stash->{'book_edit'}{'verlags_id'}     = $c->session->{'book_data_edit'}{'verlags_id'};
+    $c->stash->{'book_edit'}{'orts_id'}        = $c->session->{'book_data_edit'}{'orts_id'};
+    $c->stash->{'book_edit'}{'bindungsart_id'} = $c->session->{'book_data_edit'}{'bindungsart_id'};
     $c->stash->{'book_edit'}{'isbn'}           = $c->session->{'book_data_edit'}{'isbn'};
     $c->stash->{'book_edit'}{'book_nr'}        = $c->session->{'book_data_edit'}{'book_nr'};
     $c->stash->{'book_edit'}{'ausgabejahr'}    = $c->session->{'book_data_edit'}{'ausgabejahr'};
     $c->stash->{'book_edit'}{'druckjahr'}      = $c->session->{'book_data_edit'}{'druckjahr'};
     $c->stash->{'book_edit'}{'seiten'}         = $c->session->{'book_data_edit'}{'seiten'};
     $c->stash->{'book_edit'}{'preis'}          = $c->session->{'book_data_edit'}{'preis'};
+    $c->stash->{'book_edit'}{'waehrungs_id'}   = $c->session->{'book_data_edit'}{'waehrungs_id'};
+
+    $c->log->debug( get_output_string( $K . "Buch-Infos im Stash: ", $c->stash->{'book_edit'} ) );
 
     return 1;
 
@@ -345,17 +393,38 @@ sub bookdata_cgi2session : Private {
     }
 
     # Buchtitel
-    $c->session->{'book_data_edit'}{'title'} = $c->request->params->{'book_title'} if defined $c->request->params->{'book_title'};
+    if ( defined $c->request->params->{'book_title'} ) {
+        my $title = $c->request->params->{'book_title'};
+        $title =~ s/^\s+//;
+        $title =~ s/\s+$//;
+        $c->session->{'book_data_edit'}{'title'} = $title;
+    }
 
     # Untertitel
-    $c->session->{'book_data_edit'}{'untertitel'} = $c->request->params->{'book_untertitel'} if defined $c->request->params->{'book_untertitel'};
+    if ( defined $c->request->params->{'book_untertitel'} ) {
+        my $title = $c->request->params->{'book_untertitel'};
+        $title =~ s/^\s+//;
+        $title =~ s/\s+$//;
+        $c->session->{'book_data_edit'}{'untertitel'} = $title;
+    }
 
     # Original-Titel
-    $c->session->{'book_data_edit'}{'original_title'} = $c->request->params->{'book_original_title'} if defined $c->request->params->{'book_original_title'};
+    if ( defined $c->request->params->{'book_original_title'} ) {
+        my $title = $c->request->params->{'book_original_title'};
+        $title =~ s/^\s+//;
+        $title =~ s/\s+$//;
+        $c->session->{'book_data_edit'}{'original_title'} = $title;
+    }
 
     # Verlag
     $c->session->{'book_data_edit'}{'verlags_id'} = $c->request->params->{'verlags_id'} if defined $c->request->params->{'verlags_id'};
 
+    # Aufbewahrungsort
+    $c->session->{'book_data_edit'}{'orts_id'} = $c->request->params->{'orts_id'} if defined $c->request->params->{'orts_id'};
+
+    # Bindungsart
+    $c->session->{'book_data_edit'}{'bindungsart_id'} = $c->request->params->{'bindungsart_id'} if defined $c->request->params->{'bindungsart_id'};
+
     # Kategorien
     $c->session->{'book_data_edit'}{'kategorien'} = [] unless $c->session->{'book_data_edit'}{'kategorien'};
 
@@ -441,7 +510,12 @@ sub bookdata_cgi2session : Private {
     }
 
     # ISBN
-    $c->session->{'book_data_edit'}{'isbn'} = $c->request->params->{'book_isbn'} if defined $c->request->params->{'book_isbn'};
+    if ( defined $c->request->params->{'book_isbn'} ) {
+        my $isbn = $c->request->params->{'book_isbn'};
+        $isbn =~ s/^\s+//;
+        $isbn =~ s/\s+$//;
+        $c->session->{'book_data_edit'}{'isbn'} = $isbn;
+    }
 
     # Buch-Nr. (Buchklub usw.)
     $c->session->{'book_data_edit'}{'book_nr'} = $c->request->params->{'book_nr'} if defined $c->request->params->{'book_nr'};
@@ -455,6 +529,9 @@ sub bookdata_cgi2session : Private {
     # Seiten
     $c->session->{'book_data_edit'}{'seiten'} = $c->request->params->{'book_seiten'} if defined $c->request->params->{'book_seiten'};
 
+    # Waehrung
+    $c->session->{'book_data_edit'}{'waehrungs_id'} = $c->request->params->{'waehrungs_id'} if defined $c->request->params->{'waehrungs_id'};
+
     # Preis
     if ( defined $c->request->params->{'book_preis'} ) {
         my $preis = to_float($c->request->params->{'book_preis'});
@@ -516,6 +593,17 @@ sub prepare_data_structures : Private {
     }
     $c->log->debug( get_output_string( $K . "Aufbewahrungsorte: ", $c->stash->{'ortsliste'} ) );
 
+    # Bindungsarten zusammensammeln
+    my $bart_liste = get_bindungsarten_list($c);
+    $c->log->debug( get_output_string( $K . "Bindungsarten gesamt: ", $bart_liste ) );
+    $c->stash->{'bindungsartenliste'} = {};
+    $c->stash->{'bindungsarten_ids'}  = [];
+    for my $bart ( @$bart_liste ) {
+        push @{$c->stash->{'bindungsarten_ids'}}, $bart->{'id'};
+        $c->stash->{'bindungsartenliste'}{$bart->{'id'}} = $bart->{'name'};
+    }
+    $c->log->debug( get_output_string( $K . "Bindungsarten: ", $c->stash->{'bindungsartenliste'} ) );
+
     # Buchserien zusamensammeln
     my $serienliste = get_serienliste($c);
     $c->log->debug( get_output_string( $K . "Serien gesamt: ", $serienliste ) );
diff --git a/lib/FrBr/Books/Util/Bindungsart.pm b/lib/FrBr/Books/Util/Bindungsart.pm
new file mode 100644 (file)
index 0000000..cfee297
--- /dev/null
@@ -0,0 +1,127 @@
+package FrBr::Books::Util::Bindungsart;
+
+# $Id$
+# $URL$
+
+use strict;
+use warnings;
+
+use FrBr::Common;
+
+# Export-Deklarationen
+
+BEGIN {
+
+    use Exporter();
+    our ( $VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS );
+
+    # set the version for version checking
+    $VERSION = 0.1;
+    my ($rev) = '$Revision$' =~ /(\d+)/;
+    $VERSION = sprintf( $VERSION . ".%d", $rev );
+
+    @ISA    = qw(Exporter);
+    @EXPORT = qw(
+        &get_bindungsarten_list
+    );
+
+    #%EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
+
+    # your exported package globals go here,
+    # as well as any optionally exported functions
+    #@EXPORT_OK   = qw($Var1 %Hashit &func3);
+} ## end BEGIN
+
+our @EXPORT_OK;
+
+=head1 NAME
+
+FrBr::Books::Util::Bindungsart - Modul fuer Funktionen rund um Bindungsarten
+
+=head1 METHODS
+
+=cut
+
+#-----------------------------------------------------------------------------------
+
+=head2 get_bindungsarten_list( $c, $params )
+
+Sammelt alle Bindungsarten zusammen.
+
+Folgende benannte Parameter koennen ueber $params uebergeben werden:
+
+Rueckgabe: Eine Array-Ref von Hash-Refs mit allen Bindungsarten, die den uebergebenen Suchkriterien entsprechen:
+
+  $res = [
+    { 'id'     => 1,
+      'name'    => 'Hartcover',
+    },
+    { 'id'     => 2,
+      ...
+    },
+    ...
+  ];
+
+Die Liste ist nach den Bindungsart-Namen alphabetisch sortiert.
+
+=cut
+
+sub get_bindungsarten_list {
+
+    my $c = shift;
+    my $K = ( caller(0) )[3] . "(): ";
+
+    $c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2;
+
+    my $params = {};
+    if ( ref($_[0]) and ref($_[0]) eq 'HASH' ) {
+        $params = $_[0];
+    }
+    else {
+        %$params = @_;
+    }
+    $c->log->debug(  get_output_string( $K, "Uebergebene Parameter: ", $params ) ) if $c->stash->{'debug_level'} >= 2;
+
+    my $list = [];
+
+    my $search_params = undef;
+
+    my $other_params = {};
+    $other_params->{'order_by'} = [ 'art_name' ];
+    $other_params->{'select'} = [
+        'id',
+        'art_name',
+    ];
+    $other_params->{'as'} = [
+        'id',
+        'art_name',
+    ];
+    for my $art ( $c->model('Schema::Bindungsarten')->search( $search_params, $other_params )->all() ) {
+        my $bart = {};
+        $bart->{'id'}    = $art->id();
+        $bart->{'name'}  = $art->art_name();
+        push @$list, $bart;
+    }
+
+    return $list;
+}
+
+#-----------------------------------------------------------------------------------
+
+=head1 AUTHOR
+
+Frank Brehm
+
+=head1 LICENSE
+
+This library is free software, you can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
+1;
+
+__END__
+
+# vim: noai : ts=4 fenc=utf-8 filetype=perl expandtab :
index 7459baa0087ea653757357c90db168967299bd42..20bb7805274770703455101f029a118a05a5ba28 100644 (file)
@@ -23,6 +23,7 @@ BEGIN {
     @ISA    = qw(Exporter);
     @EXPORT = qw(
         &get_booklist
+        &save_book
     );
 
     #%EXPORT_TAGS = ( );     # eg: TAG => [ qw!name1 name2! ],
@@ -63,7 +64,7 @@ Rueckgabe: Eine Array-Ref mit allen Buchtiteln, die den uebergebenen Suchkriteri
 sub get_booklist {
 
     my $c = shift;
-    my $K = __PACKAGE__ . "::get_booklist(): ";
+    my $K  = ( caller(0) )[3] . "(): ";
 
     $c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2;
 
@@ -245,6 +246,146 @@ sub get_booklist {
 
 #-----------------------------------------------------------------------------------
 
+=head2 save_book( $c, $book )
+
+  $book = {
+    'ausgabejahr' => '2004',
+    'autoren' => [
+      '11'
+    ],
+    'bindungsart_id' => 4,
+    'book_nr' => '',
+    'druckjahr' => '2006',
+    'isbn' => '3-492-70076-4',
+    'kategorien' => [
+      '2'
+    ],
+    'original_title' => '',
+    'orts_id' => '2',
+    'preis' => 14,
+    'seiten' => '640',
+    'serien' => [
+      '4'
+    ],
+    'title' => 'Die Zwerge',
+    'untertitel' => '',
+    'verlags_id' => '8',
+    'waehrungs_id' => '1'
+  };
+
+=cut
+
+sub save_book {
+
+    my $c    = shift;
+    my $book = shift;
+    my $K    = ( caller(0) )[3] . "(): ";
+
+    $c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2;
+
+    my $storage = $c->stash->{'storage'};
+
+    my $save_func = sub {
+
+        my $storage = shift;
+        my $dbh     = shift;
+        my $buch    = shift;
+
+        my $sql = <<ENDE;
+INSERT INTO `buecher` (
+    `id`, `title`, `title_original`, `untertitel`, `verlags_id`, `orts_id`,
+    `bindungsart_id`, `isbn`, `buch_nr`, `ausgabejahr`, `druckjahr`, `seiten`, `preis`, `waehrungs_id` )
+  VALUES (
+    ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )
+  ON DUPLICATE KEY UPDATE
+    `id`             = LAST_INSERT_ID(`id`),
+    `title`          = ?,
+    `title_original` = ?,
+    `untertitel`     = ?,
+    `verlags_id`     = ?,
+    `orts_id`        = ?,
+    `bindungsart_id` = ?,
+    `isbn`           = ?,
+    `buch_nr`        = ?,
+    `ausgabejahr`    = ?,
+    `druckjahr`      = ?,
+    `seiten`         = ?,
+    `preis`          = ?,
+    `waehrungs_id`   = ?
+ENDE
+
+        my @params = ();
+        push @params, $buch->{'id'};
+        push @params, $buch->{'title'};
+        push @params, $buch->{'original_title'};
+        push @params, $buch->{'untertitel'};
+        push @params, $buch->{'verlags_id'};
+        push @params, $buch->{'orts_id'};
+        push @params, $buch->{'bindungsart_id'};
+        push @params, $buch->{'isbn'};
+        push @params, $buch->{'book_nr'};
+        push @params, $buch->{'ausgabejahr'};
+        push @params, $buch->{'druckjahr'};
+        push @params, $buch->{'seiten'};
+        push @params, $buch->{'preis'};
+        push @params, $buch->{'waehrungs_id'};
+
+        push @params, $buch->{'title'};
+        push @params, $buch->{'original_title'};
+        push @params, $buch->{'verlags_id'};
+        push @params, $buch->{'untertitel'};
+        push @params, $buch->{'orts_id'};
+        push @params, $buch->{'bindungsart_id'};
+        push @params, $buch->{'isbn'};
+        push @params, $buch->{'book_nr'};
+        push @params, $buch->{'ausgabejahr'};
+        push @params, $buch->{'druckjahr'};
+        push @params, $buch->{'seiten'};
+        push @params, $buch->{'preis'};
+        push @params, $buch->{'waehrungs_id'};
+
+        if ( $storage->debug() ) {
+            my $text = $sql;
+            $text =~ s/\s+$//;
+            $text .= ": " . join( ", ", map { $dbh->quote($_) } @params ) . "\n";
+            warn $text;
+        }
+
+        my $sth = $dbh->prepare($sql);
+        $sth->execute( @params );
+
+    };
+
+    $storage->dbh_do( $save_func, $book );
+
+    my $book_id = $storage->last_insert_id();
+
+    return undef unless $book_id;
+
+    return $book_id;
+
+}
+
+#-----------------------------------------------------------------------------------
+
+=head2 save_buch_autoren( $c, $book_id, @$autor_ids )
+
+=cut
+
+sub save_buch_autoren {
+
+    my $c         = shift;
+    my $book_id   = shift;
+    my $autor_ids = shift;
+
+    my $K    = ( caller(0) )[3] . "(): ";
+
+    
+
+}
+
+#-----------------------------------------------------------------------------------
+
 =head1 AUTHOR
 
 Frank Brehm
index 20192963c0c7c32b44eb9f89ed38e90d269c7f7f..b41615fb1d43516dcdf2cb755d3c2f9e34e60527 100644 (file)
@@ -92,6 +92,12 @@ function goto_new_ort() {
             <option value="">-- Aufbewahrungsort auswählen --</option>[% FOR ortsid IN orts_ids %]
             <option value="[% ortsid %]"[% IF book_edit.orts_id == ortsid %] selected[% END %]>[% ortsliste.$ortsid %]</option>[% END %]
           </select>&nbsp;<input type="button" name="neuer_aufbewahrungsort" value="*" class="shift_button" title="Neuer Aufbewahrungsort" onclick="goto_new_ort();" /></td>
+    </tr><tr>
+      <th>Bindungsart:</th>
+      <td><select name="bindungsart_id" size="1">
+            <option value="">-- Bindungsart auswählen --</option>[% FOR bart_id IN bindungsarten_ids %]
+            <option value="[% bart_id %]"[% IF book_edit.bindungsart_id == bart_id %] selected[% END %]>[% bindungsartenliste.$bart_id %]</option>[% END %]
+          </select></td>
     </tr><tr>
       <th>Kategorie(n):</th>
       <td>
diff --git a/root/src/books/save_success.tt2 b/root/src/books/save_success.tt2
new file mode 100644 (file)
index 0000000..523efbb
--- /dev/null
@@ -0,0 +1,17 @@
+[%#
+
+    save_success.tt2 - Template fuer erfolgreiches Speichern des Buches
+
+    vim: noai : ts=4 fenc=utf-8 filetype=html expandtab :
+
+    $Id$
+    $URL$
+
+-%]
+<div style="text-align: center">
+
+Das Buch <b>&quot;[% book_edit.title %]</b> wurde erfolgreich gespeichert.
+
+<h2><a href="[% path('/books') %]">OK</a></h2>
+
+</div>