1 package Blog
::Controller
::Post
;
5 use base
'Catalyst::Controller';
8 use Blog
::Validator
::Post
;
12 sub list
: Path
('') Args
(0) { }
15 : Route
('archive/:year|(19|20)\d\d|=/:month|(0|1)?\d|=/:day|[0-3]?\d|=')
16 #sub archive_list : LocalRegex('archive/((?:19|20)?\d\d)/(?:((?:0|1)?\d)/())?')
18 my ( $self, $c ) = @_;
20 foreach ( qw
/ year month day / ) {
21 $c->stash->{ $_ } = $c->req->param( $_ );
25 sub view
: Local Args
(1) {
26 my ( $self, $c, $id ) = @_;
28 my $post = $c->model( 'Post' )->create( key
=> $id );
29 $post->load( i18n
=> $c->language,
33 $self->throw_not_found( $c ) if $post->not_found;
35 $c->stash->{ post
} = {
36 $post->column_value_pairs,
37 available_translations
=> $post->available_translations,
38 tags
=> [ $post->tags ]
42 sub add
: Local Args
(0) {
43 my ( $self, $c ) = @_;
45 my $form = Blog
::Form
::Post
->new( $c );
46 my $validator = Blog
::Validator
::Post
->new( $c->req->params );
48 if ( $form->was_submitted && $validator->is_valid ) {
49 my $post = $form->create_from_form( $c->model( 'Post' )->name->new(),
53 $c->flash->{ msg
} = $c->loc( 'Post was added successfully' );
55 $c->res->redirect( $c->uri_for( '/post/view', $post->key ) );
58 $c->stash->{ validator
} = $validator;
61 sub edit
: LocalRegex
('^edit/(\d+)$') {
62 my ( $self, $c ) = @_;
64 my $id = $c->req->captures->[ 0 ];
66 my $post = $c->model( 'Post' )->create( id
=> $id );
67 $post->load( i18n
=> $c->language,
70 $self->throw_not_found( $c ) if $post->not_found;
72 my $form = Blog
::Form
::Post
->new( $c );
73 my $validator = Blog
::Validator
::Post
->new( $c->req->params );
75 if ( $form->was_submitted
76 && $validator->is_valid( unique_except
=> $post->key ) )
78 $post = $form->update_from_form( $post, $validator->params );
79 $post->save( cascade
=> 1);
81 $c->flash->{ msg
} = $c->loc( 'Post was saved successfully' );
83 $c->res->redirect( $c->uri_for( '/post/view', $post->key ) );
86 $c->stash->{ validator
} = $validator;
88 $c->stash->{ post
} = {
89 $post->column_value_pairs,
90 tags
=> [ $post->tags ]
94 sub delete : LocalRegex
('^delete/(\d+)$') {
95 my ( $self, $c ) = @_;
97 my $object = $c->model( 'Post' )->create( id
=> $c->req->captures->[ 0 ] );
98 $object->load( speculative
=> 1, select => [ 'id' ] );
99 $self->throw_not_found( $c ) if $object->not_found;
101 $object->delete( cascade
=> 1 );
103 $c->flash->{ msg
} = $c->loc( 'Post was deleted successfully' );
105 $c->res->redirect( $c->uri_for( '/post' ) );
108 sub feed
: Local Args
(0) {
109 my ( $self, $c ) = @_;
111 my $items = $c->model( 'Post' )->manager->feed();
113 $c->stash->{ feed
} = {
114 description
=> $c->loc( 'Recent posts' ),
118 return unless @
$items;
123 title
=> $_->i18n->title,
124 description
=> $c->helper('BBCode')->render( $_->i18n->annot ),
125 category
=> [ map { { name
=> $_->name } } @
{ $_->tags } ],
126 pubDate
=> $_->addtime->epoch,
127 link => $c->uri_for( '/post/view', $_->key ),
128 comments
=> $c->uri_for( '/post/view', $_->key ) . '#comments',
129 author
=> $c->config->{ author
}
134 $c->res->headers->header(
135 'Last-Modified' => time2str
( $feed_items->[ 0 ]->{ pubDate
} ) );
137 $c->stash->{ feed
}->{ items
} = $feed_items;
140 sub throw_not_found
{
141 my ( $self, $c ) = @_;
143 $c->detach( '/default' );
152 This library is free software, you can redistribute it and/or modify
153 it under the same terms as Perl itself.