root/branches/release-34/lib/MT/ArchiveType/AuthorMonthly.pm @ 1892

Revision 1892, 6.6 kB (checked in by bchoate, 20 months ago)

By default, publish author archives into an 'author' path. bugid:75536

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