root/branches/release-32/lib/MT/ArchiveType/Author.pm @ 1618

Revision 1618, 5.4 kB (checked in by takayama, 20 months ago)

Fixed BugId:70976 BugId:70966, BugId:69874
* Applied ogawa-san's patch.

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::Author;
8
9use strict;
10use base qw( MT::ArchiveType );
11
12sub name {
13    return 'Author';
14}
15
16sub archive_label {
17    return MT->translate('AUTHOR_ADV');
18}
19
20sub default_archive_templates {
21    return [
22        {
23            label    => 'author-display-name/index.html',
24            template => '%-a/%f',
25            default  => 1
26        },
27        {
28            label    => 'author_display_name/index.html',
29            template => '%a/%f'
30        },
31    ];
32}
33
34sub dynamic_template {
35    return 'author/<$MTEntryAuthorID$>/<$MTEntryID$>';
36}
37
38sub template_params {
39    return {
40        archive_class                    => "author-archive",
41        'module_author-monthly_archives' => 1,
42        main_template                    => 1,
43        author_archive                   => 1,
44        archive_template                 => 1,
45        archive_listing                  => 1,
46    };
47}
48
49sub author_archive_title {
50    my $obj = shift;
51    my ($ctx) = @_;
52    my $a = $ctx->stash('author');
53    $a ? $a->nickname || MT->translate( 'Author (#[_1])', $a->id ) : '';
54}
55
56sub archive_file {
57    my $obj = shift;
58    my ( $ctx, %param ) = @_;
59    my $file_tmpl = $param{Template};
60    my $author    = $ctx->{__stash}{author};
61    my $entry     = $ctx->{__stash}{entry};
62    my $file;
63
64    my $this_author = $author ? $author : ( $entry ? $entry->author : undef );
65    return "" unless $this_author;
66
67    if ( !$file_tmpl ) {
68        $file = sprintf( "%s/index", $this_author->basename );
69    }
70    $file;
71}
72
73sub archive_group_iter {
74    my $obj = shift;
75    my ( $ctx, $args ) = @_;
76    my $blog = $ctx->stash('blog');
77    my $sort_order =
78      ( $args->{sort_order} || '' ) eq 'ascend' ? 'ascend' : 'descend';
79    my $auth_order = $args->{sort_order} ? $args->{sort_order} : 'ascend';
80    my $order = ( $sort_order eq 'ascend' ) ? 'asc' : 'desc';
81    my $limit = exists $args->{lastn} ? delete $args->{lastn} : undef;
82    require MT::Entry;
83    require MT::Author;
84    my $auth_iter = MT::Author->load_iter(
85        undef,
86        {
87            sort      => 'name',
88            direction => $auth_order,
89            join      => [
90                'MT::Entry', 'author_id',
91                { status => MT::Entry::RELEASE(), blog_id => $blog->id },
92                { unique => 1 }
93            ]
94        }
95    );
96    my $i = 0;
97    return sub {
98
99        while ( my $a = $auth_iter->() ) {
100            last if defined($limit) && $i == $limit;
101            my $count = MT::Entry->count(
102                {
103                    blog_id   => $blog->id,
104                    status    => MT::Entry::RELEASE(),
105                    author_id => $a->id
106                }
107            );
108            next if $count == 0 && !$args->{show_empty};
109            $i++;
110            return ( $count, author => $a );
111        }
112        undef;
113    };
114}
115
116sub archive_group_entries {
117    my $obj = shift;
118    my ( $ctx, %param ) = @_;
119    my $blog = $ctx->stash('blog');
120    my $a = $param{author} || $ctx->stash('author');
121    my $limit = $param{limit};
122    if ( $limit eq 'auto' ) {
123        my $blog = $ctx->stash('blog');
124        $limit = $blog->entries_on_index if $blog;
125    }
126    return [] unless $a;
127    require MT::Entry;
128    my @entries = MT::Entry->load(
129        {
130            blog_id   => $blog->id,
131            author_id => $a->id,
132            status    => MT::Entry::RELEASE()
133        },
134        {
135            'sort'      => 'authored_on',
136            'direction' => 'descend',
137            ( $limit ? ( 'limit' => $limit ) : () ),
138        }
139    );
140    \@entries;
141}
142
143sub archive_entries_count {
144    my $obj = shift;
145    my ( $blog, $at, $entry ) = @_;
146    return $obj->SUPER::archive_entries_count(@_) unless $entry;
147    my $auth = $entry->author;
148    return $obj->SUPER::archive_entries_count(
149        {
150            Blog        => $blog,
151            ArchiveType => $at,
152            Author      => $auth
153        }
154    );
155}
156
157sub display_name {
158    my $obj = shift;
159    my ($ctx)    = shift;
160    my $tmpl     = $ctx->stash('template');
161    my $at       = $ctx->{archive_type};
162    my $author   = '';
163    if (   ( $tmpl && $tmpl->type eq 'index' )
164        || !$obj
165        || ( $obj && !$obj->author_based )
166        || !$ctx->{inside_archive_list} )
167    {
168        $author = $ctx->stash('author');
169        $author =
170            $author
171          ? $author->nickname
172          ? $author->nickname . ": "
173          : MT->translate( 'Author (#[_1])', $author->id )
174          : '';
175    }
176    return $author;
177}
178
179sub group_based {
180    return 1;
181}
182
183sub date_based_author_entries {
184    my $obj = shift;
185    my ( $ctx, $at, $author, $ts ) = @_;
186
187    my $blog     = $ctx->stash('blog');
188    my ( $start, $end );
189    if ($ts) {
190        ( $start, $end ) = $obj->date_range($ts);
191    }
192    else {
193        $start = $ctx->{current_timestamp};
194        $end   = $ctx->{current_timestamp_end};
195    }
196    my @entries = MT::Entry->load(
197        {
198            blog_id     => $blog->id,
199            author_id   => $author->id,
200            status      => MT::Entry::RELEASE(),
201            authored_on => [ $start, $end ]
202        },
203        {
204            range => { authored_on => 1 },
205            'sort' => 'authored_on',
206            'direction' => 'descend',
207        }
208    ) or return $ctx->error("Couldn't get $at archive list");
209    \@entries;
210}
211
2121;
Note: See TracBrowser for help on using the browser.