From e8d185b1c0e712790371c6c27c493c137d66c21d Mon Sep 17 00:00:00 2001 From: Frank Brehm Date: Wed, 26 Mar 2008 10:41:48 +0000 Subject: [PATCH] Kategorien eines Buches editierbar gemacht --- lib/FrBr/Books/Controller/Books.pm | 73 ++++++++++++++++- lib/FrBr/Books/Util/Category.pm | 124 +++++++++++++++++++++++++++++ root/src/books/book_form.tt2 | 22 +++++ 3 files changed, 218 insertions(+), 1 deletion(-) create mode 100644 lib/FrBr/Books/Util/Category.pm diff --git a/lib/FrBr/Books/Controller/Books.pm b/lib/FrBr/Books/Controller/Books.pm index d8b1036..d07e5c9 100644 --- a/lib/FrBr/Books/Controller/Books.pm +++ b/lib/FrBr/Books/Controller/Books.pm @@ -8,8 +8,9 @@ use warnings; use base 'Catalyst::Controller'; use FrBr::Common; -use FrBr::Books::Util::Book; use FrBr::Books::Util::Author; +use FrBr::Books::Util::Category; +use FrBr::Books::Util::Book; =head1 NAME @@ -179,6 +180,21 @@ sub bookdata_session2stash : Private { } $c->log->debug( get_output_string( $K . "Autoren, die fehlen: ", $c->stash->{'autor_list_no'} ) ); + my %Kategorie; + + $c->stash->{'kategorie_list_book'} = []; + for my $cat_id ( @{$c->session->{'book_data_edit'}{'kategorien'}} ) { + push @{$c->stash->{'kategorie_list_book'}}, $cat_id; + $Kategorie{$cat_id} = 1; + } + $c->log->debug( get_output_string( $K . "Kategorien des Buches: ", $c->stash->{'kategorie_list_book'} ) ); + + $c->stash->{'kategorie_list_no'} = []; + for my $cat_id ( @{$c->stash->{'kategorie_ids'}} ) { + push @{$c->stash->{'kategorie_list_no'}}, $cat_id unless $Kategorie{$cat_id}; + } + $c->log->debug( get_output_string( $K . "Kategorien, die fehlen: ", $c->stash->{'kategorie_list_no'} ) ); + $c->stash->{'book_edit'}{'title'} = $c->session->{'book_data_edit'}{'title'}; $c->stash->{'book_edit'}{'untertitel'} = $c->session->{'book_data_edit'}{'untertitel'}; $c->stash->{'book_edit'}{'original_title'} = $c->session->{'book_data_edit'}{'original_title'}; @@ -313,6 +329,48 @@ sub bookdata_cgi2session : Private { # Original-Titel $c->session->{'book_data_edit'}{'original_title'} = $c->request->params->{'book_original_title'} if defined $c->request->params->{'book_original_title'}; + # Kategorien + $c->session->{'book_data_edit'}{'kategorien'} = [] unless $c->session->{'book_data_edit'}{'kategorien'}; + + my %Kategorie; + $i = 1; + for my $cat_id ( @{$c->session->{'book_data_edit'}{'kategorien'}} ) { + $Kategorie{$cat_id} = $i; + $i++; + } + + if ( $c->request->params->{'wird_kategorie'} ) { + if ( $c->request->params->{'kategorie_not'} ) { + my $kategorien = ref($c->request->params->{'kategorie_not'}) eq 'ARRAY' ? $c->request->params->{'kategorie_not'} : [$c->request->params->{'kategorie_not'}]; + $c->log->debug( get_output_string( $K . "Fuege Kategorien hinzu: ", $kategorien ) ); + for my $cat_id ( @$kategorien ) { + next unless $cat_id; + unless ( $Kategorie{$cat_id} ) { + push @{$c->session->{'book_data_edit'}{'kategorien'}}, $cat_id; + $Kategorie{$cat_id} = scalar( @{$c->session->{'book_data_edit'}{'kategorien'}} ); + } + } + } + } + elsif ( $c->request->params->{'entf_kategorie'} ) { + if ( $c->request->params->{'kategorie_book'} ) { + my $kategorien = ref($c->request->params->{'kategorie_book'}) eq 'ARRAY' ? $c->request->params->{'kategorie_book'} : [$c->request->params->{'kategorie_book'}]; + $c->log->debug( get_output_string( $K . "Loesche Kategorien: ", $kategorien ) ); + for my $cat_id ( @$kategorien ) { + next unless $cat_id; + $i = $Kategorie{$cat_id}; + next unless $i; + splice( @{$c->session->{'book_data_edit'}{'kategorien'}}, $i - 1, 1 ); + $i = 1; + %Kategorie = (); + for my $id ( @{$c->session->{'book_data_edit'}{'kategorien'}} ) { + $Kategorie{$id} = $i; + $i++; + } + } + } + } + # ISBN $c->session->{'book_data_edit'}{'isbn'} = $c->request->params->{'book_isbn'} if defined $c->request->params->{'book_isbn'}; @@ -345,6 +403,7 @@ sub prepare_data_structures : Private { my ( $self, $c ) = @_; my $K = __PACKAGE__ . "::prepare_data_structures(): "; + # Autoren zusammensammeln my $autor_list_complete = get_author_list($c); $c->log->debug( get_output_string( $K . "Autoren gesamt: ", $autor_list_complete ) ); @@ -354,6 +413,18 @@ sub prepare_data_structures : Private { } $c->log->debug( get_output_string( $K . "Autoren-Hash: ", $c->stash->{'autor_list'} ) ); + # Kategorien zusammensammeln + my $cat_list_complete = get_category_list($c); + $c->log->debug( get_output_string( $K . "Kategorien gesamt: ", $cat_list_complete ) ); + + $c->stash->{'kategorie_list'} = {}; + $c->stash->{'kategorie_ids'} = []; + for my $cat ( @$cat_list_complete ) { + push @{$c->stash->{'kategorie_ids'}}, $cat->{'id'}; + $c->stash->{'kategorie_list'}{$cat->{'id'}} = $cat->{'name'}; + } + $c->log->debug( get_output_string( $K . "Kategorien: ", $c->stash->{'kategorie_list'} ) ); + return 1; } diff --git a/lib/FrBr/Books/Util/Category.pm b/lib/FrBr/Books/Util/Category.pm new file mode 100644 index 0000000..9b6f740 --- /dev/null +++ b/lib/FrBr/Books/Util/Category.pm @@ -0,0 +1,124 @@ +package FrBr::Books::Util::Category; + +# $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_category_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::Category - Modul fuer Funktionen rund um Kategorien + +=head1 METHODS + +=cut + +#----------------------------------------------------------------------------------- + +=head2 get_category_list( $c, $params ) + +Sammelt alle Kategorien zusammen. + +Folgende benannte Parameter koennen ueber $params uebergeben werden: + +Rueckgabe: Eine Array-Ref von Hash-Refs mit allen Kategorien, die den uebergebenen Suchkriterien entsprechen: + + $res = [ + { 'id' => 1, + 'name' => 'Science Fiction', + }, + { 'id' => 2, + ... + }, + ... + ]; + +Die Liste ist nach den Kategorie-Namen alphabetisch sortiert. + +=cut + +sub get_category_list { + + my $c = shift; + my $K = __PACKAGE__ . "::get_category_list(): "; + + $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'} = [ 'kategorie_name' ]; + $other_params->{'select'} = [ + 'id', + 'kategorie_name', + ]; + $other_params->{'as'} = [ + 'id', + 'kategorie_name', + ]; + + for my $cat_rs ( $c->model('Schema::Kategorien')->search( $search_params, $other_params )->all() ) { + my $cat = {}; + $cat->{'id'} = $cat_rs->id(); + $cat->{'name'} = $cat_rs->kategorie_name(); + push @$list, $cat; + } + + 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; diff --git a/root/src/books/book_form.tt2 b/root/src/books/book_form.tt2 index f350ffe..c366a29 100644 --- a/root/src/books/book_form.tt2 +++ b/root/src/books/book_form.tt2 @@ -47,6 +47,28 @@ Original-Titel: + + Kategorie(n): + + + + + + + + + + +
Verfügbare KategorienKategorien des Buchs


+

+ +
ISBN: -- 2.39.5