| 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::AuthorDaily; |
|---|
| 8 | |
|---|
| 9 | use strict; |
|---|
| 10 | use base qw( MT::ArchiveType::Author MT::ArchiveType::Daily ); |
|---|
| 11 | use MT::Util qw( dirify start_end_day ); |
|---|
| 12 | |
|---|
| 13 | sub name { |
|---|
| 14 | return 'Author-Daily'; |
|---|
| 15 | } |
|---|
| 16 | |
|---|
| 17 | sub archive_label { |
|---|
| 18 | return MT->translate('AUTHOR-DAILY_ADV'); |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | sub default_archive_templates { |
|---|
| 22 | return [ |
|---|
| 23 | { |
|---|
| 24 | label => 'author-display-name/yyyy/mm/dd/index.html', |
|---|
| 25 | template => '%-a/%y/%m/%d/%f', |
|---|
| 26 | default => 1 |
|---|
| 27 | }, |
|---|
| 28 | { |
|---|
| 29 | label => 'author_display_name/yyyy/mm/dd/index.html', |
|---|
| 30 | template => '%a/%y/%m/%d/%f' |
|---|
| 31 | }, |
|---|
| 32 | ]; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | sub dynamic_template { |
|---|
| 36 | return 'author/<$MTEntryAuthorID$>/<$MTArchiveDate format="%Y%m%d"$>'; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | sub template_params { |
|---|
| 40 | return { |
|---|
| 41 | archive_class => "author-daily-archive", |
|---|
| 42 | author_daily_archive => 1, |
|---|
| 43 | archive_template => 1, |
|---|
| 44 | archive_listing => 1, |
|---|
| 45 | }; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | sub 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_day($stamp); |
|---|
| 53 | my $date = |
|---|
| 54 | MT::Template::Context::_hdlr_date( $ctx, |
|---|
| 55 | { ts => $start, 'format' => "%x" } ); |
|---|
| 56 | my $author = $obj->display_name($ctx); |
|---|
| 57 | sprintf( "%s%s", $author, $date ); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | sub archive_file { |
|---|
| 61 | my $obj = shift; |
|---|
| 62 | my ( $ctx, %param ) = @_; |
|---|
| 63 | my $timestamp = $param{Timestamp}; |
|---|
| 64 | my $file_tmpl = $param{Template}; |
|---|
| 65 | my $author = $ctx->{__stash}{author}; |
|---|
| 66 | my $entry = $ctx->{__stash}{entry}; |
|---|
| 67 | my $file; |
|---|
| 68 | my $this_author = $author ? $author : ( $entry ? $entry->author : undef ); |
|---|
| 69 | return "" unless $this_author; |
|---|
| 70 | my $name = dirify( $this_author->nickname ); |
|---|
| 71 | |
|---|
| 72 | if ( $name eq '' || !$file_tmpl ) { |
|---|
| 73 | return "" unless $this_author; |
|---|
| 74 | $name = "author" . $this_author->id if $name !~ /\w/; |
|---|
| 75 | my $start = start_end_day($timestamp); |
|---|
| 76 | my ( $year, $month, $day ) = unpack 'A4A2A2', $start; |
|---|
| 77 | $file = |
|---|
| 78 | sprintf( "%s/%04d/%02d/%02d/index", $name, $year, $month, $day ); |
|---|
| 79 | } |
|---|
| 80 | else { |
|---|
| 81 | ( $ctx->{current_timestamp}, $ctx->{current_timestamp_end} ) = |
|---|
| 82 | start_end_day($timestamp); |
|---|
| 83 | } |
|---|
| 84 | $file; |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | sub archive_group_iter { |
|---|
| 88 | my $obj = shift; |
|---|
| 89 | my ( $ctx, $args ) = @_; |
|---|
| 90 | my $blog = $ctx->stash('blog'); |
|---|
| 91 | my $sort_order = |
|---|
| 92 | ( $args->{sort_order} || '' ) eq 'ascend' ? 'ascend' : 'descend'; |
|---|
| 93 | my $auth_order = $args->{sort_order} ? $args->{sort_order} : 'ascend'; |
|---|
| 94 | my $order = ( $sort_order eq 'ascend' ) ? 'asc' : 'desc'; |
|---|
| 95 | my $limit = exists $args->{lastn} ? delete $args->{lastn} : undef; |
|---|
| 96 | |
|---|
| 97 | my $tmpl = $ctx->stash('template'); |
|---|
| 98 | my @data = (); |
|---|
| 99 | my $count = 0; |
|---|
| 100 | |
|---|
| 101 | my $ts = $ctx->{current_timestamp}; |
|---|
| 102 | my $tsend = $ctx->{current_timestamp_end}; |
|---|
| 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 | ( $ts && $tsend ? ( authored_on => [ $ts, $tsend ] ) : () ), |
|---|
| 124 | }, |
|---|
| 125 | { |
|---|
| 126 | ( $ts && $tsend ? ( range_incl => { authored_on => 1 } ) : () ), |
|---|
| 127 | group => [ |
|---|
| 128 | "extract(year from authored_on)", |
|---|
| 129 | "extract(month from authored_on)", |
|---|
| 130 | "extract(day from authored_on)" |
|---|
| 131 | ], |
|---|
| 132 | 'sort' => "extract(year from authored_on) $order, |
|---|
| 133 | extract(month from authored_on) $order, |
|---|
| 134 | extract(day from authored_on) $order" |
|---|
| 135 | } |
|---|
| 136 | ) or return $ctx->error("Couldn't get monthly archive list"); |
|---|
| 137 | |
|---|
| 138 | while ( my @row = $count_iter->() ) { |
|---|
| 139 | my $hash = { |
|---|
| 140 | year => $row[1], |
|---|
| 141 | month => $row[2], |
|---|
| 142 | day => $row[3], |
|---|
| 143 | author => $auth, |
|---|
| 144 | count => $row[0], |
|---|
| 145 | }; |
|---|
| 146 | push( @data, $hash ); |
|---|
| 147 | return $count + 1 |
|---|
| 148 | if ( defined($limit) && ( $count + 1 ) == $limit ); |
|---|
| 149 | $count++; |
|---|
| 150 | } |
|---|
| 151 | return $count; |
|---|
| 152 | }; |
|---|
| 153 | |
|---|
| 154 | # Count entry by author |
|---|
| 155 | if ($author) { |
|---|
| 156 | $loop_sub->($author); |
|---|
| 157 | } |
|---|
| 158 | else { |
|---|
| 159 | |
|---|
| 160 | # load authors |
|---|
| 161 | require MT::Author; |
|---|
| 162 | my $iter; |
|---|
| 163 | $iter = MT::Author->load_iter( |
|---|
| 164 | undef, |
|---|
| 165 | { |
|---|
| 166 | sort => 'name', |
|---|
| 167 | direction => $auth_order, |
|---|
| 168 | join => [ |
|---|
| 169 | 'MT::Entry', 'author_id', |
|---|
| 170 | { status => MT::Entry::RELEASE(), blog_id => $blog->id }, |
|---|
| 171 | { unique => 1 } |
|---|
| 172 | ] |
|---|
| 173 | } |
|---|
| 174 | ); |
|---|
| 175 | |
|---|
| 176 | while ( my $a = $iter->() ) { |
|---|
| 177 | $loop_sub->($a); |
|---|
| 178 | last if ( defined($limit) && $count == $limit ); |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | |
|---|
| 182 | my $loop = @data; |
|---|
| 183 | my $curr = 0; |
|---|
| 184 | |
|---|
| 185 | return sub { |
|---|
| 186 | if ( $curr < $loop ) { |
|---|
| 187 | my $date = sprintf( |
|---|
| 188 | "%04d%02d%02d000000", |
|---|
| 189 | $data[$curr]->{year}, |
|---|
| 190 | $data[$curr]->{month}, |
|---|
| 191 | $data[$curr]->{day} |
|---|
| 192 | ); |
|---|
| 193 | my ( $start, $end ) = start_end_day($date); |
|---|
| 194 | my $count = $data[$curr]->{count}; |
|---|
| 195 | my %hash = ( |
|---|
| 196 | author => $data[$curr]->{author}, |
|---|
| 197 | year => $data[$curr]->{year}, |
|---|
| 198 | month => $data[$curr]->{month}, |
|---|
| 199 | day => $data[$curr]->{day}, |
|---|
| 200 | start => $start, |
|---|
| 201 | end => $end |
|---|
| 202 | ); |
|---|
| 203 | $curr++; |
|---|
| 204 | return ( $count, %hash ); |
|---|
| 205 | } |
|---|
| 206 | undef; |
|---|
| 207 | } |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | sub archive_group_entries { |
|---|
| 211 | my $obj = shift; |
|---|
| 212 | my ( $ctx, %param ) = @_; |
|---|
| 213 | my $ts = |
|---|
| 214 | $param{year} |
|---|
| 215 | ? sprintf( "%04d%02d%02d000000", $param{year}, $param{month}, |
|---|
| 216 | $param{day} ) |
|---|
| 217 | : $ctx->stash('current_timestamp'); |
|---|
| 218 | my $author = $param{author} || $ctx->stash('author'); |
|---|
| 219 | my $limit = $param{limit}; |
|---|
| 220 | $obj->dated_author_entries( $ctx, 'Author-Daily', $author, $ts, $limit ); |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | sub archive_entries_count { |
|---|
| 224 | my $obj = shift; |
|---|
| 225 | my ( $blog, $at, $entry ) = @_; |
|---|
| 226 | my $auth = $entry->author; |
|---|
| 227 | return $obj->SUPER::archive_entries_count( |
|---|
| 228 | { |
|---|
| 229 | Blog => $blog, |
|---|
| 230 | ArchiveType => $at, |
|---|
| 231 | Timestamp => $entry->authored_on, |
|---|
| 232 | Author => $auth |
|---|
| 233 | } |
|---|
| 234 | ); |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | *date_range = \&MT::ArchiveType::Daily::date_range; |
|---|
| 238 | *next_archive_entry = \&MT::ArchiveType::Date::next_archive_entry; |
|---|
| 239 | *previous_archive_entry = \&MT::ArchiveType::Date::previous_archive_entry; |
|---|
| 240 | |
|---|
| 241 | 1; |
|---|