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