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