From: Frank Brehm Date: Mon, 8 Sep 2008 20:12:25 +0000 (+0000) Subject: Kategorien angefangen X-Git-Url: https://git.uhu-banane.net/?a=commitdiff_plain;h=f38f0db1e184bc653029e4a863643cb00c349fe9;p=books.git Kategorien angefangen --- diff --git a/lib/FrBr/Books/Controller/Kategorie.pm b/lib/FrBr/Books/Controller/Kategorie.pm new file mode 100644 index 0000000..aaea194 --- /dev/null +++ b/lib/FrBr/Books/Controller/Kategorie.pm @@ -0,0 +1,235 @@ +package FrBr::Books::Controller::Kategorie; + +# $Id$ +# $URL$ + +use strict; +use warnings; +use base 'Catalyst::Controller'; + +use FrBr::Common; +use FrBr::Books::Util::Category; + +=head1 NAME + +FrBr::Books::Controller::Kategorie - Catalyst Controller fuer alles rund um Kategorien + +=head1 DESCRIPTION + +Catalyst Controller. + +=head1 METHODS + +=cut + +#------------------------------------------------------- + +=head2 auto + +=cut + +sub auto : Private { + + my ( $self, $c ) = @_; + my $K = ( caller(0) )[3] . "(): "; + + $c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2; + + $c->stash->{'menu_path'} = [] unless $c->stash->{'menu_path'}; + push @{ $c->stash->{'menu_path'} }, { + 'path' => $c->web_path("/kategorie"), + 'name' => "Kategorie" + }; + + $c->stash->{'cssfiles'} = [] unless $c->stash->{'cssfiles'}; + push @{$c->stash->{'cssfiles'}}, 'category/styles.css'; + + 1; + +} ## end sub auto : + +#------------------------------------------------------- + +=head2 index + +=cut + +sub index : Private { + my ( $self, $c ) = @_; + my $K = ( caller(0) )[3] . "(): "; + + $c->stash->{'template'} = 'category/index.tt2'; +} + +#------------------------------------------------------- + +=head2 default + +=cut + +sub default : Private { + + my ( $self, $c ) = @_; + my $K = ( caller(0) )[3] . "(): "; + + $c->stash->{'template'} = 'not_implemented.tt2'; + + push @{ $c->stash->{'menu_path'} }, { + 'path' => $c->web_path("/kategorie/default"), + 'name' => "Nicht Implementiert" + }; + +} ## end sub default : + +#------------------------------------------------------- + +=head2 form_new( ) + +Erstellen eines neuen Verlages. + +=cut + +sub form_new : Path('new') { + + my ( $self, $c ) = @_; + my $K = ( caller(0) )[3] . "(): "; + + $c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2; + + $c->stash->{'menu_path'} = [] unless $c->stash->{'menu_path'}; + push @{ $c->stash->{'menu_path'} }, { + 'path' => $c->web_path("/kategorie/new"), + 'name' => "Neu" + }; + + $c->stash->{'template'} = 'category/new.tt2'; + push @{$c->stash->{'cssfiles'}}, 'category/form.css'; + + $c->stash->{'error_message'} = ''; + +# $self->prepare_data_structures($c); + + $self->category_cgi2session($c); + + $c->stash->{'verlag_edit'} = {} unless $c->stash->{'verlag_edit'}; + $self->category_session2stash($c); + $c->stash->{'category_edit'}{'name'} = "Neue Kategorie" unless $c->stash->{'category_edit'}{'name'}; + + return 1 unless $c->request->params->{'category_form_sent'} and $c->request->params->{'do_save'}; + + return $self->do_save_category($c); + +} + +#------------------------------------------------------- + +sub do_save_category : Private { + + my ( $self, $c ) = @_; + my $K = ( caller(0) )[3] . "(): "; + + return 1 unless $self->check_formparams($c); + + eval { + die "Speichern des Verlags misslungen." unless save_category( $c, $c->stash->{'category_edit'} ); + }; + if ( $@ ) { + $c->stash->{'error_message'} = $@; + return undef; + } + + $c->stash->{'template'} = 'category/save_success.tt2'; + delete $c->session->{'category_data_edit'} if exists $c->session->{'category_data_edit'}; + delete $c->session->{'return_target_category_save'} if exists $c->session->{'return_target_category_save'}; + + return 1; + +} + +#------------------------------------------------------- + +sub check_formparams : Private { + + my ( $self, $c ) = @_; + my $K = ( caller(0) )[3] . "(): "; + + unless ( $c->stash->{'category_edit'} ) { + $c->stash->{'error_message'} = "Interner Fehler"; + return undef; + } + + unless ( $c->stash->{'category_edit'}{'name'} ) { + $c->stash->{'error_message'} = "Kein Name der Kategorie angegeben."; + return undef; + } + + return 1; + +} + +#------------------------------------------------------- + +sub category_cgi2session : Private { + + my ( $self, $c ) = @_; + my $K = ( caller(0) )[3] . "(): "; + + $c->session->{'return_target_category_save'} = $c->request->params->{'return_target_form'} if $c->request->params->{'return_target_form'}; + + return 1 unless $c->request->params->{'category_form_sent'}; + + # Basis anlegen, wenn notwendig + $c->session->{'category_data_edit'} = {} unless $c->session->{'category_data_edit'}; + + # Kategorie-Id eintragen, wenn notwendig + $c->session->{'category_data_edit'}{'id'} = $c->request->params->{'category_id'} if $c->request->params->{'category_id'}; + + # Name der Kategorie + if ( defined $c->request->params->{'category_name'} ) { + my $name = $c->request->params->{'category_name'}; + $name =~ s/^\s+//; + $name =~ s/\s+$//; + $c->session->{'category_data_edit'}{'name'} = $name; + } + + return 1; + +} + +#------------------------------------------------------- + +sub category_session2stash : Private { + + my ( $self, $c ) = @_; + my $K = ( caller(0) )[3] . "(): "; + + $c->log->debug( get_output_string( $K . "Kategorie in der Session:", $c->session->{'category_data_edit'} ) ) if $c->stash->{'debug_level'} > 2; + + $c->stash->{'category_edit'} = {} unless $c->stash->{'category_edit'}; + $c->stash->{'category_edit'}{'id'} = $c->session->{'category_data_edit'}{'id'} if $c->session->{'category_data_edit'}{'id'}; + $c->stash->{'category_edit'}{'name'} = $c->session->{'category_data_edit'}{'name'} if $c->session->{'category_data_edit'}{'name'}; + + $c->stash->{'return_target_category_save'} = $c->session->{'return_target_category_save'} || $c->web_path('/kategorie'); + + return 1; + +} + +#------------------------------------------------------- + +=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 : diff --git a/lib/FrBr/Books/Util/Category.pm b/lib/FrBr/Books/Util/Category.pm index cc365fc..b48731d 100644 --- a/lib/FrBr/Books/Util/Category.pm +++ b/lib/FrBr/Books/Util/Category.pm @@ -23,6 +23,7 @@ BEGIN { @ISA = qw(Exporter); @EXPORT = qw( &get_category_list + &save_category ); #%EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ], @@ -110,6 +111,50 @@ sub get_category_list { #----------------------------------------------------------------------------------- +=head2 save_category( $c, $category ) + +=cut + +sub save_category { + + my $c = shift; + my $cat = shift; + my $K = __PACKAGE__ . "::save_category(): "; + + $c->log->debug( $K . "aufgerufen." ) if $c->stash->{'debug_level'} > 2; + + my $storage = $c->stash->{'storage'}; + + my $save_func = sub { + my ( $storage, $dbh, $category_id, $cat_name ) = @_; + my $sql = <debug() ) { + my $text = $sql; + $text =~ s/\s+$//; + $text .= ": " . join( ", ", map { $dbh->quote($_) } ( $category_id, $cat_name, $cat_name ) ) . "\n"; + warn $text; + } + + my $sth = $dbh->prepare($sql); + $sth->execute( $category_id, $cat_name, $cat_name ); + }; + + $storage->dbh_do( $save_func, $cat->{'id'}, $cat-->{'name'} ); + + return $storage->last_insert_id(); + +} + +#----------------------------------------------------------------------------------- + =head1 AUTHOR Frank Brehm diff --git a/root/src/category/index.tt2 b/root/src/category/index.tt2 index 17c2876..faa75d4 100644 --- a/root/src/category/index.tt2 +++ b/root/src/category/index.tt2 @@ -21,7 +21,7 @@   - Liste der Kategorien + Liste der Kategorien @@ -31,7 +31,7 @@   - Neue Kategorie + Neue Kategorie diff --git a/root/src/category/new.tt2 b/root/src/category/new.tt2 index 82f6b4b..3fd7d2c 100644 --- a/root/src/category/new.tt2 +++ b/root/src/category/new.tt2 @@ -12,5 +12,5 @@ [% PROCESS category/cat_form.tt2 %] diff --git a/root/src/index.tt2 b/root/src/index.tt2 index 590aca2..e962ffd 100644 --- a/root/src/index.tt2 +++ b/root/src/index.tt2 @@ -14,16 +14,22 @@ - - + - - + + + + + +