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

Revision 1486, 6.0 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::AuthorYearly;
8
9use strict;
10use base qw( MT::ArchiveType::Author MT::ArchiveType::Yearly );
11use MT::Util qw( dirify start_end_year );
12
13sub name {
14    return 'Author-Yearly';
15}
16
17sub archive_label {
18    return MT->translate('AUTHOR-YEARLY_ADV');
19}
20
21sub default_archive_templates {
22    return [
23        {
24            label    => 'author-display-name/yyyy/index.html',
25            template => '%-a/%y/%f',
26            default  => 1
27        },
28        {
29            label    => 'author_display_name/yyyy/index.html',
30            template => '%a/%y/%f'
31        },
32    ];
33}
34
35sub dynamic_template {
36    return 'author/<$MTEntryAuthorID$>/<$MTArchiveDate format="%Y"$>';
37}
38
39sub template_params {
40    return {
41        archive_class         => "author-yearly-archive",
42        author_yearly_archive => 1,
43        main_template         => 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_year($stamp);
54    my $year =
55      MT::Template::Context::_hdlr_date( $ctx,
56        { ts => $start, 'format' => "%Y" } );
57    my $lang = lc MT->current_language || 'en_us';
58    $lang = 'ja' if lc($lang) eq 'jp';
59    my $author = $obj->display_name($ctx);
60
61    sprintf( "%s%s%s", $author, $year, ( $lang eq 'ja' ? '&#24180;' : '' ) );
62}
63
64sub archive_file {
65    my $obj = shift;
66    my ( $ctx, %param ) = @_;
67    my $timestamp = $param{Timestamp};
68    my $file_tmpl = $param{Template};
69    my $author    = $ctx->{__stash}{author};
70    my $entry     = $ctx->{__stash}{entry};
71    my $file;
72    my $this_author = $author ? $author : ( $entry ? $entry->author : undef );
73    return "" unless $this_author;
74    my $name = dirify( $this_author->nickname );
75
76    if ( $name eq '' || !$file_tmpl ) {
77        return "" unless $this_author;
78        $name = "author" . $this_author->id if $name !~ /\w/;
79        my $start = start_end_year($timestamp);
80        my ($year) = unpack 'A4', $start;
81        $file = sprintf( "%s/%04d/index", $name, $year );
82    }
83    else {
84        ( $ctx->{current_timestamp}, $ctx->{current_timestamp_end} ) =
85          start_end_year($timestamp);
86    }
87    $file;
88}
89
90sub archive_group_iter {
91    my $obj = shift;
92    my ( $ctx, $args ) = @_;
93    my $blog = $ctx->stash('blog');
94    my $sort_order =
95      ( $args->{sort_order} || '' ) eq 'ascend' ? 'ascend' : 'descend';
96    my $auth_order = $args->{sort_order} ? $args->{sort_order} : 'ascend';
97    my $order = ( $sort_order eq 'ascend' ) ? 'asc' : 'desc';
98    my $limit = exists $args->{lastn} ? delete $args->{lastn} : undef;
99
100    my $tmpl  = $ctx->stash('template');
101    my @data  = ();
102    my $count = 0;
103
104    my $at       = $ctx->{archive_type};
105    my $archiver = MT->publisher->archiver($at);
106    my $author;
107
108    # if (($tmpl && $tmpl->type ne 'index') &&
109    #     ($archiver && $archiver->author_based))
110    # {
111    $author = $ctx->stash('author');
112
113    # }
114
115    require MT::Entry;
116    my $loop_sub = sub {
117        my $auth       = shift;
118        my $count_iter = MT::Entry->count_group_by(
119            {
120                blog_id   => $blog->id,
121                author_id => $auth->id,
122                status    => MT::Entry::RELEASE()
123            },
124            {
125                group  => ["extract(year from authored_on)"],
126                'sort' => "extract(year from authored_on) $order"
127            }
128        ) or return $ctx->error("Couldn't get monthly archive list");
129
130        while ( my @row = $count_iter->() ) {
131            my $hash = {
132                year   => $row[1],
133                author => $auth,
134                count  => $row[0],
135            };
136            push( @data, $hash );
137            return $count + 1
138              if ( defined($limit) && ( $count + 1 ) == $limit );
139            $count++;
140        }
141        return $count;
142    };
143
144    # Count entry by author
145    if ($author) {
146        $loop_sub->($author);
147    }
148    else {
149
150        # load authors
151        require MT::Author;
152        my $iter;
153        $iter = MT::Author->load_iter(
154            undef,
155            {
156                sort      => 'name',
157                direction => $auth_order,
158                join      => [
159                    'MT::Entry', 'author_id',
160                    { status => MT::Entry::RELEASE(), blog_id => $blog->id },
161                    { unique => 1 }
162                ]
163            }
164        );
165
166        while ( my $a = $iter->() ) {
167            $loop_sub->($a);
168            last if ( defined($limit) && $count == $limit );
169        }
170    }
171
172    my $loop = @data;
173    my $curr = 0;
174
175    return sub {
176        if ( $curr < $loop ) {
177            my $date =
178              sprintf( "%04d%02d%02d000000", $data[$curr]->{year}, 1, 1 );
179            my ( $start, $end ) = start_end_year($date);
180            my $count = $data[$curr]->{count};
181            my %hash  = (
182                author => $data[$curr]->{author},
183                year   => $data[$curr]->{year},
184                start  => $start,
185                end    => $end
186            );
187            $curr++;
188            return ( $count, %hash );
189        }
190        undef;
191      }
192}
193
194sub archive_group_entries {
195    my $obj = shift;
196    my ( $ctx, %param ) = @_;
197    my $ts =
198        $param{year}
199    ? sprintf( "%04d%02d%02d000000", $param{year}, 1, 1 )
200        : $ctx->stash('current_timestamp');
201    my $author = $param{author} || $ctx->stash('author');
202    my $limit = $param{limit};
203    $obj->date_based_author_entries( $ctx, 'Author-Yearly', $author, $ts, $limit );
204}
205
206sub archive_entries_count {
207    my $obj = shift;
208    my ( $blog, $at, $entry ) = @_;
209    my $auth = $entry->author;
210    return $obj->SUPER::archive_entries_count(
211        {
212            Blog        => $blog,
213            ArchiveType => $at,
214            Timestamp   => $entry->authored_on,
215            Author      => $auth
216        }
217    );
218}
219
2201;
Note: See TracBrowser for help on using the browser.