root/branches/release-31/lib/MT/ArchiveType/AuthorMonthly.pm @ 1486

Revision 1486, 6.6 kB (checked in by bchoate, 21 months ago)

Refactoring of archive type publishing code into separate modules.

Line 
1# Movable Type (r) Open Source (C) 2001-2008 Six Apart, Ltd.
2# This program is distributed under the terms of the
3# GNU General Public License, version 2.
4#
5# $Id$
6
7package MT::ArchiveType::AuthorMonthly;
8
9use strict;
10use base qw( MT::ArchiveType::Author MT::ArchiveType::Monthly );
11use MT::Util qw( dirify start_end_month );
12
13sub name {
14    return 'Author-Monthly';
15}
16
17sub archive_label {
18    return MT->translate('AUTHOR-MONTHLY_ADV');
19}
20
21sub default_archive_templates {
22    return [
23        {
24            label    => 'author-display-name/yyyy/mm/index.html',
25            template => '%-a/%y/%m/%f',
26            default  => 1
27        },
28        {
29            label    => 'author_display_name/yyyy/mm/index.html',
30            template => '%a/%y/%m/%f'
31        },
32    ];
33}
34
35sub dynamic_template {
36    return 'author/<$MTEntryAuthorID$>/<$MTArchiveDate format="%Y%m"$>';
37}
38
39sub template_params {
40    return {
41        archive_class                    => "author-monthly-archive",
42        author_monthly_archive           => 1,
43        'module_author-monthly_archives' => 1,
44        main_template                    => 1,
45        archive_template                 => 1,
46        archive_listing                  => 1,
47    };
48}
49
50sub archive_title {
51    my $obj = shift;
52    my ( $ctx, $entry_or_ts ) = @_;
53    my $stamp = ref $entry_or_ts ? $entry_or_ts->authored_on : $entry_or_ts;
54    my ($start) = start_end_month($stamp);
55    my $date =
56      MT::Template::Context::_hdlr_date( $ctx,
57        { ts => $start, 'format' => "%B %Y" } );
58    my $author = $obj->display_name($ctx);
59
60    sprintf( "%s%s", $author, $date );
61}
62
63sub archive_file {
64    my $obj = shift;
65    my ( $ctx, %param ) = @_;
66    my $timestamp = $param{Timestamp};
67    my $file_tmpl = $param{Template};
68    my $author    = $ctx->{__stash}{author};
69    my $entry     = $ctx->{__stash}{entry};
70    my $file;
71    my $this_author = $author ? $author : ( $entry ? $entry->author : undef );
72    return "" unless $this_author;
73    my $name = dirify( $this_author->nickname );
74
75    if ( $name eq '' || !$file_tmpl ) {
76        return "" unless $this_author;
77        $name = "author" . $this_author->id if $name !~ /\w/;
78        my ($start) = start_end_month($timestamp);
79        my ( $year, $month ) = unpack 'A4A2', $start;
80        $file = sprintf( "%s/%04d/%02d/index", $name, $year, $month );
81    }
82    else {
83        ( $ctx->{current_timestamp}, $ctx->{current_timestamp_end} ) =
84          start_end_month($timestamp);
85    }
86    $file;
87}
88
89sub archive_group_iter {
90    my $obj = shift;
91    my ( $ctx, $args ) = @_;
92    my $blog = $ctx->stash('blog');
93    my $sort_order =
94      ( $args->{sort_order} || '' ) eq 'ascend' ? 'ascend' : 'descend';
95    my $auth_order = $args->{sort_order} ? $args->{sort_order} : 'ascend';
96    my $order = ( $sort_order eq 'ascend' ) ? 'asc' : 'desc';
97    my $limit = exists $args->{lastn} ? delete $args->{lastn} : undef;
98
99    my $tmpl  = $ctx->stash('template');
100    my @data  = ();
101    my $count = 0;
102
103    my $at       = $ctx->{archive_type};
104    my $archiver = MT->publisher->archiver($at);
105    my $author;
106
107    my $ts    = $ctx->{current_timestamp};
108    my $tsend = $ctx->{current_timestamp_end};
109
110    # if (($tmpl && $tmpl->type ne 'index') &&
111    #     ($archiver && $archiver->author_based))
112    # {
113    $author = $ctx->stash('author');
114
115    # }
116
117    require MT::Entry;
118    my $loop_sub = sub {
119        my $auth       = shift;
120        my $count_iter = MT::Entry->count_group_by(
121            {
122                blog_id   => $blog->id,
123                author_id => $auth->id,
124                status    => MT::Entry::RELEASE(),
125                ( $ts && $tsend ? ( authored_on => [ $ts, $tsend ] ) : () ),
126            },
127            {
128                ( $ts && $tsend ? ( range_incl => { authored_on => 1 } ) : () ),
129                group => [
130                    "extract(year from authored_on)",
131                    "extract(month from authored_on)"
132                ],
133                'sort' => "extract(year from authored_on) $order,
134                         extract(month from authored_on) $order"
135            }
136        ) or return $ctx->error("Couldn't get monthly archive list");
137
138        while ( my @row = $count_iter->() ) {
139            my $hash = {
140                year   => $row[1],
141                month  => $row[2],
142                author => $auth,
143                count  => $row[0],
144            };
145            push( @data, $hash );
146            return $count + 1
147              if ( defined($limit) && ( $count + 1 ) == $limit );
148            $count++;
149        }
150        return $count;
151    };
152
153    # Count entry by author
154    if ($author) {
155        $loop_sub->($author);
156    }
157    else {
158
159        # load authors
160        require MT::Author;
161        my $iter;
162        $iter = MT::Author->load_iter(
163            undef,
164            {
165                sort      => 'name',
166                direction => $auth_order,
167                join      => [
168                    'MT::Entry', 'author_id',
169                    { status => MT::Entry::RELEASE(), blog_id => $blog->id },
170                    { unique => 1 }
171                ]
172            }
173        );
174
175        while ( my $a = $iter->() ) {
176            $loop_sub->($a);
177            last if ( defined($limit) && $count == $limit );
178        }
179    }
180
181    my $loop = @data;
182    my $curr = 0;
183
184    return sub {
185        if ( $curr < $loop ) {
186            my $date = sprintf(
187                "%04d%02d%02d000000",
188                $data[$curr]->{year},
189                $data[$curr]->{month}, 1
190            );
191            my ( $start, $end ) = start_end_month($date);
192            my $count = $data[$curr]->{count};
193            my %hash  = (
194                author => $data[$curr]->{author},
195                year   => $data[$curr]->{year},
196                month  => $data[$curr]->{month},
197                start  => $start,
198                end    => $end
199            );
200            $curr++;
201            return ( $count, %hash );
202        }
203        undef;
204      }
205}
206
207sub archive_group_entries {
208    my $obj = shift;
209    my ( $ctx, %param ) = @_;
210    Carp::confess("ctx is undef") unless defined $ctx;
211    my $ts =
212        $param{year}
213    ? sprintf( "%04d%02d%02d000000", $param{year}, $param{month}, 1 )
214        : $ctx->stash('current_timestamp');
215    my $author = $param{author} || $ctx->stash('author');
216    my $limit = $param{limit};
217    $obj->dated_author_entries( $ctx, 'Author-Monthly', $author, $ts, $limit );
218}
219
220sub archive_entries_count {
221    my $obj = shift;
222    my ( $blog, $at, $entry ) = @_;
223    my $auth = $entry->author;
224    return $obj->SUPER::archive_entries_count(
225        {
226            Blog        => $blog,
227            ArchiveType => $at,
228            Timestamp   => $entry->authored_on,
229            Author      => $auth
230        }
231    );
232}
233
2341;
Note: See TracBrowser for help on using the browser.