root/branches/release-38/lib/MT/ArchiveType/AuthorYearly.pm @ 2372

Revision 2372, 6.0 kB (checked in by bchoate, 19 months ago)

Updates to remap date_range method to appropriate class. BugId:79796

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/author-display-name/yyyy/index.html',
25            template => '%-a/%y/%f',
26            default  => 1
27        },
28        {
29            label    => 'author/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        archive_template      => 1,
44        archive_listing       => 1,
45    };
46}
47
48sub archive_title {
49    my $obj = shift;
50    my ( $ctx, $entry_or_ts ) = @_;
51    my $stamp = ref $entry_or_ts ? $entry_or_ts->authored_on : $entry_or_ts;
52    my $start = start_end_year($stamp);
53    my $year =
54      MT::Template::Context::_hdlr_date( $ctx,
55        { ts => $start, 'format' => "%Y" } );
56    my $lang = lc MT->current_language || 'en_us';
57    $lang = 'ja' if lc($lang) eq 'jp';
58    my $author = $obj->display_name($ctx);
59
60    sprintf( "%s%s%s", $author, $year, ( $lang eq 'ja' ? '&#24180;' : '' ) );
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_year($timestamp);
79        my ($year) = unpack 'A4', $start;
80        $file = sprintf( "%s/%04d/index", $name, $year );
81    }
82    else {
83        ( $ctx->{current_timestamp}, $ctx->{current_timestamp_end} ) =
84          start_end_year($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    # if (($tmpl && $tmpl->type ne 'index') &&
108    #     ($archiver && $archiver->author_based))
109    # {
110    $author = $ctx->stash('author');
111
112    # }
113
114    require MT::Entry;
115    my $loop_sub = sub {
116        my $auth       = shift;
117        my $count_iter = MT::Entry->count_group_by(
118            {
119                blog_id   => $blog->id,
120                author_id => $auth->id,
121                status    => MT::Entry::RELEASE()
122            },
123            {
124                group  => ["extract(year from authored_on)"],
125                'sort' => "extract(year from authored_on) $order"
126            }
127        ) or return $ctx->error("Couldn't get monthly archive list");
128
129        while ( my @row = $count_iter->() ) {
130            my $hash = {
131                year   => $row[1],
132                author => $auth,
133                count  => $row[0],
134            };
135            push( @data, $hash );
136            return $count + 1
137              if ( defined($limit) && ( $count + 1 ) == $limit );
138            $count++;
139        }
140        return $count;
141    };
142
143    # Count entry by author
144    if ($author) {
145        $loop_sub->($author);
146    }
147    else {
148
149        # load authors
150        require MT::Author;
151        my $iter;
152        $iter = MT::Author->load_iter(
153            undef,
154            {
155                sort      => 'name',
156                direction => $auth_order,
157                join      => [
158                    'MT::Entry', 'author_id',
159                    { status => MT::Entry::RELEASE(), blog_id => $blog->id },
160                    { unique => 1 }
161                ]
162            }
163        );
164
165        while ( my $a = $iter->() ) {
166            $loop_sub->($a);
167            last if ( defined($limit) && $count == $limit );
168        }
169    }
170
171    my $loop = @data;
172    my $curr = 0;
173
174    return sub {
175        if ( $curr < $loop ) {
176            my $date =
177              sprintf( "%04d%02d%02d000000", $data[$curr]->{year}, 1, 1 );
178            my ( $start, $end ) = start_end_year($date);
179            my $count = $data[$curr]->{count};
180            my %hash  = (
181                author => $data[$curr]->{author},
182                year   => $data[$curr]->{year},
183                start  => $start,
184                end    => $end
185            );
186            $curr++;
187            return ( $count, %hash );
188        }
189        undef;
190      }
191}
192
193sub archive_group_entries {
194    my $obj = shift;
195    my ( $ctx, %param ) = @_;
196    my $ts =
197        $param{year}
198    ? sprintf( "%04d%02d%02d000000", $param{year}, 1, 1 )
199        : $ctx->stash('current_timestamp');
200    my $author = $param{author} || $ctx->stash('author');
201    my $limit = $param{limit};
202    $obj->date_based_author_entries( $ctx, 'Author-Yearly', $author, $ts, $limit );
203}
204
205sub archive_entries_count {
206    my $obj = shift;
207    my ( $blog, $at, $entry ) = @_;
208    my $auth = $entry->author;
209    return $obj->SUPER::archive_entries_count(
210        {
211            Blog        => $blog,
212            ArchiveType => $at,
213            Timestamp   => $entry->authored_on,
214            Author      => $auth
215        }
216    );
217}
218
219sub date_range {
220    MT::ArchiveType::Yearly::date_range(@_);
221}
222
2231;
Note: See TracBrowser for help on using the browser.