--- /dev/null
+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 :
@ISA = qw(Exporter);
@EXPORT = qw(
&get_category_list
+ &save_category
);
#%EXPORT_TAGS = ( ); # eg: TAG => [ qw!name1 name2! ],
#-----------------------------------------------------------------------------------
+=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 = <<ENDE;
+INSERT INTO `kategorien` (
+ `id`, `kategorie_name` )
+ VALUES (
+ ?, ? )
+ ON DUPLICATE KEY UPDATE
+ `id` = LAST_INSERT_ID(`id`), `kategorie_name` = ?
+ENDE
+
+ if ( $storage->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