| 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::Monthly; |
|---|
| 8 | |
|---|
| 9 | use strict; |
|---|
| 10 | use base qw( MT::ArchiveType::Date ); |
|---|
| 11 | use MT::Util qw( start_end_month ); |
|---|
| 12 | |
|---|
| 13 | sub name { |
|---|
| 14 | return 'Monthly'; |
|---|
| 15 | } |
|---|
| 16 | |
|---|
| 17 | sub archive_label { |
|---|
| 18 | return MT->translate("MONTHLY_ADV"); |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | sub dynamic_template { |
|---|
| 22 | 'archives/<$MTArchiveDate format="%Y%m"$>'; |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | sub default_archive_templates { |
|---|
| 26 | return [ |
|---|
| 27 | { |
|---|
| 28 | label => MT->translate('yyyy/mm/index.html'), |
|---|
| 29 | template => '%y/%m/%i', |
|---|
| 30 | default => 1 |
|---|
| 31 | }, |
|---|
| 32 | ]; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | sub template_params { |
|---|
| 36 | return { |
|---|
| 37 | datebased_only_archive => 1, |
|---|
| 38 | datebased_monthly_archive => 1, |
|---|
| 39 | main_template => 1, |
|---|
| 40 | archive_template => 1, |
|---|
| 41 | archive_listing => 1, |
|---|
| 42 | archive_class => "datebased-monthly-archive", |
|---|
| 43 | }; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | sub archive_file { |
|---|
| 47 | my $obj = shift; |
|---|
| 48 | my ( $ctx, %param ) = @_; |
|---|
| 49 | my $timestamp = $param{Timestamp}; |
|---|
| 50 | my $file_tmpl = $param{Template}; |
|---|
| 51 | my $blog = $ctx->{__stash}{blog}; |
|---|
| 52 | |
|---|
| 53 | my $file; |
|---|
| 54 | if ($file_tmpl) { |
|---|
| 55 | ( $ctx->{current_timestamp}, $ctx->{current_timestamp_end} ) = |
|---|
| 56 | start_end_month( $timestamp, $blog ); |
|---|
| 57 | } |
|---|
| 58 | else { |
|---|
| 59 | my $start = start_end_month( $timestamp, $blog ); |
|---|
| 60 | my ( $year, $mon ) = unpack 'A4A2', $start; |
|---|
| 61 | $file = sprintf( "%04d/%02d/index", $year, $mon ); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | $file; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | sub archive_title { |
|---|
| 68 | my $obj = shift; |
|---|
| 69 | my ( $ctx, $entry_or_ts ) = @_; |
|---|
| 70 | my $stamp = ref $entry_or_ts ? $entry_or_ts->authored_on : $entry_or_ts; |
|---|
| 71 | Carp::confess("ctx is undef") if !defined($ctx); |
|---|
| 72 | my $start = start_end_month( $stamp, $ctx->stash('blog') ); |
|---|
| 73 | MT::Template::Context::_hdlr_date( $ctx, |
|---|
| 74 | { ts => $start, 'format' => "%B %Y" } ); |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | sub date_range { |
|---|
| 78 | my $obj = shift; |
|---|
| 79 | start_end_month(@_); |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | sub archive_group_iter { |
|---|
| 83 | my $obj = shift; |
|---|
| 84 | my ( $ctx, $args ) = @_; |
|---|
| 85 | my $blog = $ctx->stash('blog'); |
|---|
| 86 | my $iter; |
|---|
| 87 | my $sort_order = |
|---|
| 88 | ( $args->{sort_order} || '' ) eq 'ascend' ? 'ascend' : 'descend'; |
|---|
| 89 | my $order = ( $sort_order eq 'ascend' ) ? 'asc' : 'desc'; |
|---|
| 90 | |
|---|
| 91 | my $ts = $ctx->{current_timestamp}; |
|---|
| 92 | my $tsend = $ctx->{current_timestamp_end}; |
|---|
| 93 | |
|---|
| 94 | require MT::Entry; |
|---|
| 95 | $iter = MT::Entry->count_group_by( |
|---|
| 96 | { |
|---|
| 97 | blog_id => $blog->id, |
|---|
| 98 | status => MT::Entry::RELEASE(), |
|---|
| 99 | ( $ts && $tsend ? ( authored_on => [ $ts, $tsend ] ) : () ), |
|---|
| 100 | }, |
|---|
| 101 | { |
|---|
| 102 | ( $ts && $tsend ? ( range_incl => { authored_on => 1 } ) : () ), |
|---|
| 103 | group => [ |
|---|
| 104 | "extract(year from authored_on)", |
|---|
| 105 | "extract(month from authored_on)" |
|---|
| 106 | ], |
|---|
| 107 | $args->{lastn} ? ( limit => $args->{lastn} ) : (), |
|---|
| 108 | sort => "extract(year from authored_on) $order, |
|---|
| 109 | extract(month from authored_on) $order" |
|---|
| 110 | } |
|---|
| 111 | ) or return $ctx->error("Couldn't get monthly archive list"); |
|---|
| 112 | |
|---|
| 113 | return sub { |
|---|
| 114 | while ( my @row = $iter->() ) { |
|---|
| 115 | my $date = sprintf( "%04d%02d%02d000000", $row[1], $row[2], 1 ); |
|---|
| 116 | my ( $start, $end ) = start_end_month($date); |
|---|
| 117 | return ( |
|---|
| 118 | $row[0], |
|---|
| 119 | year => $row[1], |
|---|
| 120 | month => $row[2], |
|---|
| 121 | start => $start, |
|---|
| 122 | end => $end |
|---|
| 123 | ); |
|---|
| 124 | } |
|---|
| 125 | undef; |
|---|
| 126 | }; |
|---|
| 127 | } |
|---|
| 128 | |
|---|
| 129 | sub archive_group_entries { |
|---|
| 130 | my $obj = shift; |
|---|
| 131 | my ( $ctx, %param ) = @_; |
|---|
| 132 | my $ts = |
|---|
| 133 | $param{year} |
|---|
| 134 | ? sprintf( "%04d%02d%02d000000", $param{year}, $param{month}, 1 ) |
|---|
| 135 | : undef; |
|---|
| 136 | my $limit = $param{limit}; |
|---|
| 137 | $obj->dated_group_entries( $ctx, 'Monthly', $ts, $limit ); |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | sub archive_entries_count { |
|---|
| 141 | my $obj = shift; |
|---|
| 142 | my ( $blog, $at, $entry ) = @_; |
|---|
| 143 | return $obj->SUPER::archive_entries_count( |
|---|
| 144 | { |
|---|
| 145 | Blog => $blog, |
|---|
| 146 | ArchiveType => $at, |
|---|
| 147 | Timestamp => $entry->authored_on |
|---|
| 148 | } |
|---|
| 149 | ); |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | 1; |
|---|