1 package Blog
::Controller
::Post
;
5 use base
'Catalyst::Controller';
8 use Blog
::Validator
::Post
;
11 use Date
::Calc
'Add_Delta_YMD';
13 sub list
: Path
('') Args
(0) {
14 my ( $self, $c ) = @_;
16 my $page = $c->req->param( 'p' );
18 $page = 1 unless $page && $page =~ m/^\d+$/o;
20 my $count = $c->model( 'Post' )->count();
22 $c->stash->{ count
} = $count;
23 $c->stash->{ page_size
} = 20;
24 $c->stash->{ page
} = $page;
26 my $objects = $c->model( 'Post' )->manager->posts_full( page
=> $page );
28 $self->throw_not_found( $c ) unless ( $page == 1 || @
$objects );
30 $c->stash->{ 'posts' } = $objects;
33 sub archive
: Local Args
(0) { }
36 : Route
('archive/:year|(19|20)\d\d|/:month|(0|1)?\d|=/:day|[0-3]?\d|=')
37 #sub archive_list : LocalRegex('archive/((?:19|20)?\d\d)/(?:((?:0|1)?\d)/())?')
39 my ( $self, $c ) = @_;
41 my $year = $c->req->param('year');
42 my $month = $c->req->param('month');
43 my $day = $c->req->param('day');
46 eval { $from = timelocal
(0, 0, 0, $day || 1, $month ?
$month - 1 : 0, $year) };
48 $self->throw_not_found( $c ) if $@
;
50 my $page = $c->req->param( 'p' );
52 $page = 1 unless $page && $page =~ m/^\d+$/o;
54 my $count = $c->model( 'Post' )->count();
56 $c->stash->{ count
} = $count;
57 $c->stash->{ page_size
} = 20;
58 $c->stash->{ page
} = $page;
60 my ( $Dy, $Dm, $Dd ) = (0, 0, 0);
69 my ( $new_year, $new_month, $new_day ) =
70 Add_Delta_YMD
( $year, $month || 1, $day || 1, $Dy, $Dm, $Dd );
72 my $to = timelocal
(0, 0, 0, $new_day, $new_month - 1, $new_year);
73 warn 'SHIT' unless $to < $from;
75 my $posts = $c->model( 'Post' )->manager->posts_full(
76 query
=> [ addtime
=> { ge => $from }, addtime
=> { lt => $to } ],
80 $c->stash->{ template
} = 'post/archive.tt2';
89 sub view
: LocalRegex
('^(\d+)$') {
90 my ( $self, $c ) = @_;
92 my $id = $c->req->captures->[ 0 ];
94 my $object = $c->model( 'Post' )->create( id
=> $id );
97 select => [ qw
/ id addtime title content / ]
100 $self->throw_not_found( $c ) if $object->not_found;
102 $c->stash->{ post
} = $object;
106 my ( $self, $c ) = @_;
108 my $form = Blog
::Form
::Post
->new( $c );
109 my $validator = Blog
::Validator
::Post
->new( $c->req->params );
111 if ( $form->was_submitted && $validator->is_valid ) {
112 my $object = $form->object_from_form(
113 class => $c->model( 'Post' )->name,
114 params
=> $validator->params
118 $c->flash->{ msg
} = $c->loc( 'Post was added successfully' );
120 $c->res->redirect( '/post' );
123 $c->stash->{ validator
} = $validator;
126 sub edit
: LocalRegex
('^(\d+)/edit$') {
127 my ( $self, $c ) = @_;
129 my $id = $c->req->captures->[ 0 ];
131 my $post = $c->model( 'Post' )->create( id
=> $id );
132 $post->load( speculative
=> 1, with_objects
=> [ 'tags' ] );
133 $self->throw_not_found( $c ) if $post->not_found;
135 my $form = Blog
::Form
::Post
->new( $c );
136 my $validator = Blog
::Validator
::Post
->new( $c->req->params );
138 if ( $form->was_submitted && $validator->is_valid ) {
139 $post = $form->object_from_form(
141 params
=> $validator->params
145 $c->flash->{ msg
} = $c->loc( 'Post was saved successfully' );
147 $c->res->redirect( '/post' );
150 $c->stash->{ validator
} = $validator;
151 $c->stash->{ post
} = $post;
154 sub delete : LocalRegex
('^(\d+)/delete$') {
155 my ( $self, $c ) = @_;
157 my $object = $c->model( 'Post' )->create( id
=> $c->req->captures->[ 0 ] );
158 $object->load( speculative
=> 1, select => [ 'id' ] );
159 $self->throw_not_found( $c ) if $object->not_found;
161 $object->delete( cascade
=> 1 );
163 $c->flash->{ msg
} = $c->loc( 'Post was deleted successfully' );
165 $c->res->redirect( '/post' );
169 my ( $self, $c ) = @_;
171 my $items = $c->model( 'Post' )->search(
172 sort_by
=> 'addtime DESC',
174 with_objects
=> 'tags'
177 $c->stash->{ feed
} = { description
=> $c->loc( 'Recent posts' ) };
179 return unless @
$items;
181 $c->stash->{ feed
}->{ items
} = [
185 description
=> $c->bbcode( $_->annot ),
186 category
=> [ map { { name
=> $_->name } } @
{ $_->tags } ],
187 pubDate
=> $_->addtime,
188 link => $c->uri_for( '/post', $_->id ),
189 comments
=> $c->uri_for( '/post', $_->id ) . '#comments',
190 author
=> $c->config->{ author
}
196 sub throw_not_found
{
197 my ( $self, $c ) = @_;
199 $c->detach( '/default' );
208 This library is free software, you can redistribute it and/or modify
209 it under the same terms as Perl itself.