]> Frank Brehm's Git Trees - books.git/commitdiff
Kategorien angefangen
authorFrank Brehm <frank@brehm-online.com>
Mon, 8 Sep 2008 20:12:25 +0000 (20:12 +0000)
committerFrank Brehm <frank@brehm-online.com>
Mon, 8 Sep 2008 20:12:25 +0000 (20:12 +0000)
lib/FrBr/Books/Controller/Kategorie.pm [new file with mode: 0644]
lib/FrBr/Books/Util/Category.pm
root/src/category/index.tt2
root/src/category/new.tt2
root/src/index.tt2

diff --git a/lib/FrBr/Books/Controller/Kategorie.pm b/lib/FrBr/Books/Controller/Kategorie.pm
new file mode 100644 (file)
index 0000000..aaea194
--- /dev/null
@@ -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 : 
index cc365fcaacd1cf456aaeddcdaa1121eef395bfa9..b48731d606e63c61bdb8435d3f1f2ef22f3c0fe8 100644 (file)
@@ -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 = <<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
index 17c2876ffd58751b2c429255795688bd5bc06ede..faa75d41fefb952aea5bf9216c109e296777097e 100644 (file)
@@ -21,7 +21,7 @@
     <td class="button">
       &nbsp;</td>
     <td class="item">
-      <a href="[% path('/category/list') %]">Liste der Kategorien</a></td>
+      <a href="[% path('/kategorie/list') %]">Liste der Kategorien</a></td>
   </tr>
   <tr>
     <td colspan="2" class="empty">
@@ -31,7 +31,7 @@
     <td class="button">
       &nbsp;</td>
     <td class="item">
-      <a href="[% path('/category/new') %]">Neue Kategorie</a></td>
+      <a href="[% path('/kategorie/new') %]">Neue Kategorie</a></td>
   </tr>
   <tr>
     <td colspan="2" class="empty">
index 82f6b4b4b086e64abd6c3dde83be5d0476e6d6c0..3fd7d2cadb40cd379ec7148af3a41346cbdce0f3 100644 (file)
@@ -12,5 +12,5 @@
 [% PROCESS category/cat_form.tt2 %]
 
 <div class="back">
-<h2><a href="[% return_target_category_save %]">[% 'Zurück' %]</a></h2>
+<h2><a href="[% return_target_category_save %]">[% 'Zurück' %]</a></h2>
 </div>
index 590aca29ccd06a71a59c1d5f818eb90e843f0b6b..e962ffd2cb413bb5686769ec603f9d017dd6e4be 100644 (file)
 <table class="menu" cellspacing="0">
   <tr>
     <th colspan="2">Menü</th>
-  </tr>
-  <tr>
+  </tr><tr>
     <td colspan="2" class="empty">
       &nbsp;</td>
-  </tr>
-  <tr>
+  </tr><tr>
     <td class="button">
       &nbsp;</td>
     <td class="item">
       <a href="[% path('/books') %]">Bücher suchen</a></td>
+  </tr><tr>
+    <td colspan="2" class="empty">
+      &nbsp;</td>
+  </tr><tr>
+    <td class="button">
+      &nbsp;</td>
+    <td class="item">
+      <a href="[% path('/kategorie') %]">Kategorien</a></td>
   </tr>
 </table>
 </div>