forked from jayallen/melody
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Author.pm
260 lines (217 loc) · 7.01 KB
/
Author.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# Movable Type (r) Open Source (C) 2001-2010 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$
package MT::ArchiveType::Author;
use strict;
use base qw( MT::ArchiveType );
use MT::Util qw( remove_html encode_html );
sub name {
return 'Author';
}
sub archive_label {
return MT->translate('AUTHOR_ADV');
}
sub default_archive_templates {
return [ {
label => 'author/author-basename/index.html',
template => 'author/%-a/%f',
default => 1
},
{
label => 'author/author_basename/index.html',
template => 'author/%a/%f'
},
];
}
sub dynamic_template {
return 'author/<$MTEntryAuthorID$>/<$MTEntryID$>';
}
sub template_params {
return {
archive_class => "author-archive",
'module_author-monthly_archives' => 1,
author_archive => 1,
archive_template => 1,
archive_listing => 1,
};
}
sub archive_title {
my $obj = shift;
my ($ctx) = @_;
my $a = $ctx->stash('author');
encode_html(
remove_html(
$a ? $a->nickname || MT->translate('(Display Name not set)') : ''
)
);
}
sub archive_file {
my $obj = shift;
my ( $ctx, %param ) = @_;
my $file_tmpl = $param{Template};
my $author = $ctx->{__stash}{author};
my $entry = $ctx->{__stash}{entry};
my $file;
my $this_author = $author ? $author : ( $entry ? $entry->author : undef );
return "" unless $this_author;
if ( !$file_tmpl ) {
$file = sprintf( "%s/index", $this_author->basename );
}
$file;
}
sub archive_group_iter {
my $obj = shift;
my ( $ctx, $args ) = @_;
my $blog = $ctx->stash('blog');
my $sort_order
= ( $args->{sort_order} || '' ) eq 'ascend' ? 'ascend' : 'descend';
my $auth_order = $args->{sort_order} ? $args->{sort_order} : 'ascend';
my $order = ( $sort_order eq 'ascend' ) ? 'asc' : 'desc';
my $limit = exists $args->{lastn} ? delete $args->{lastn} : undef;
require MT::Entry;
require MT::Author;
my $auth_iter =
MT::Author->load_iter(
undef,
{
sort => 'name',
direction => $auth_order,
join => [
'MT::Entry',
'author_id',
{
status => MT::Entry::RELEASE(),
blog_id => $blog->id
},
{ unique => 1 }
]
}
);
my $i = 0;
return sub {
while ( my $a = $auth_iter->() ) {
last if defined($limit) && $i == $limit;
my $count =
MT::Entry->count( {
blog_id => $blog->id,
status => MT::Entry::RELEASE(),
author_id => $a->id
}
);
next if $count == 0 && !$args->{show_empty};
$i++;
return ( $count, author => $a );
}
undef;
};
} ## end sub archive_group_iter
sub archive_group_entries {
my $obj = shift;
my ( $ctx, %param ) = @_;
my $blog = $ctx->stash('blog');
my $a = $param{author} || $ctx->stash('author');
my $limit = $param{limit};
if ( $limit && ( $limit eq 'auto' ) ) {
my $blog = $ctx->stash('blog');
$limit = $blog->entries_on_index if $blog;
}
return [] unless $a;
require MT::Entry;
my @entries = MT::Entry->load( {
blog_id => $blog->id,
author_id => $a->id,
status => MT::Entry::RELEASE()
},
{
'sort' => 'authored_on',
'direction' => 'descend',
( $limit ? ( 'limit' => $limit ) : () ),
}
);
\@entries;
} ## end sub archive_group_entries
sub archive_entries_count {
my $obj = shift;
my ( $blog, $at, $entry ) = @_;
return $obj->SUPER::archive_entries_count(@_) unless $entry;
my $auth = $entry->author;
return $obj->SUPER::archive_entries_count(
{ Blog => $blog, ArchiveType => $at, Author => $auth } );
}
sub display_name {
my $obj = shift;
my ($ctx) = shift;
my $tmpl = $ctx->stash('template');
my $at = $ctx->{archive_type};
my $author = '';
if ( ( $tmpl && $tmpl->type eq 'index' )
|| !$obj
|| ( $obj && !$obj->author_based )
|| !$ctx->{inside_archive_list} )
{
$author = $ctx->stash('author');
$author
= $author
? $author->nickname
? $author->nickname . ": "
: MT->translate('(Display Name not set)')
: '';
}
return $author;
} ## end sub display_name
sub author_based {
return 1;
}
sub group_based {
return 1;
}
sub date_based_author_entries {
my $obj = shift;
my ( $ctx, $at, $author, $ts ) = @_;
my $blog = $ctx->stash('blog');
my ( $start, $end );
if ($ts) {
( $start, $end ) = $obj->date_range($ts);
}
else {
$start = $ctx->{current_timestamp};
$end = $ctx->{current_timestamp_end};
}
my @entries = MT::Entry->load( {
blog_id => $blog->id,
author_id => $author->id,
status => MT::Entry::RELEASE(),
authored_on => [ $start, $end ]
},
{
range => { authored_on => 1 },
'sort' => 'authored_on',
'direction' => 'descend',
}
) or return $ctx->error("Couldn't get $at archive list");
\@entries;
} ## end sub date_based_author_entries
1;
__END__
=head1 NAME
MT::ArchiveType::Author
=head1 METHODS
=head2 archive_entries_count
=head2 archive_file
=head2 archive_group_entries
=head2 archive_group_iter
=head2 archive_label
=head2 archive_title
=head2 author_based
=head2 date_based_author_entries
=head2 default_archive_templates
=head2 display_name
=head2 dynamic_template
=head2 group_based
=head2 name
=head2 template_params
=head1 AUTHOR & COPYRIGHT
Please see L<MT/AUTHOR & COPYRIGHT>.
=cut