| 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::CategoryDaily; |
|---|
| 8 | |
|---|
| 9 | use strict; |
|---|
| 10 | use base qw( MT::ArchiveType::Category MT::ArchiveType::Daily ); |
|---|
| 11 | use MT::Util qw( dirify start_end_day ); |
|---|
| 12 | |
|---|
| 13 | sub name { |
|---|
| 14 | return 'Category-Daily'; |
|---|
| 15 | } |
|---|
| 16 | |
|---|
| 17 | sub archive_label { |
|---|
| 18 | return MT->translate('CATEGORY-DAILY_ADV'); |
|---|
| 19 | } |
|---|
| 20 | |
|---|
| 21 | sub default_archive_templates { |
|---|
| 22 | return [ |
|---|
| 23 | { |
|---|
| 24 | label => 'category/sub-category/yyyy/mm/dd/index.html', |
|---|
| 25 | template => '%-c/%y/%m/%d/%i', |
|---|
| 26 | default => 1 |
|---|
| 27 | }, |
|---|
| 28 | { |
|---|
| 29 | label => 'category/sub_category/yyyy/mm/dd/index.html', |
|---|
| 30 | template => '%c/%y/%m/%d/%i' |
|---|
| 31 | }, |
|---|
| 32 | ]; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | sub dynamic_template { |
|---|
| 36 | return 'category/<$MTCategoryID$>/<$MTArchiveDate format="%Y%m%d"$>'; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | sub template_params { |
|---|
| 40 | return { |
|---|
| 41 | archive_class => "category-daily-archive", |
|---|
| 42 | category_daily_archive => 1, |
|---|
| 43 | main_template => 1, |
|---|
| 44 | archive_template => 1, |
|---|
| 45 | archive_listing => 1, |
|---|
| 46 | }; |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | sub archive_file { |
|---|
| 50 | my $obj = shift; |
|---|
| 51 | my ( $ctx, %param ) = @_; |
|---|
| 52 | my $timestamp = $param{Timestamp}; |
|---|
| 53 | my $file_tmpl = $param{Template}; |
|---|
| 54 | my $blog = $ctx->{__stash}{blog}; |
|---|
| 55 | my $cat = $ctx->{__stash}{cat} || $ctx->{__stash}{category}; |
|---|
| 56 | my $entry = $ctx->{__stash}{entry}; |
|---|
| 57 | my $file; |
|---|
| 58 | |
|---|
| 59 | my $this_cat = $cat ? $cat : ( $entry ? $entry->category : undef ); |
|---|
| 60 | if ($file_tmpl) { |
|---|
| 61 | ( $ctx->{current_timestamp}, $ctx->{current_timestamp_end} ) = |
|---|
| 62 | start_end_day( $timestamp, $blog ); |
|---|
| 63 | $ctx->stash( 'archive_category', $this_cat ); |
|---|
| 64 | $ctx->{inside_mt_categories} = 1; |
|---|
| 65 | $ctx->{__stash}{category} = $this_cat; |
|---|
| 66 | } |
|---|
| 67 | else { |
|---|
| 68 | if ( !$this_cat ) { |
|---|
| 69 | return ""; |
|---|
| 70 | } |
|---|
| 71 | my $label = ''; |
|---|
| 72 | $label = dirify( $this_cat->label ); |
|---|
| 73 | if ( $label !~ /\w/ ) { |
|---|
| 74 | $label = $this_cat ? "cat" . $this_cat->id : ""; |
|---|
| 75 | } |
|---|
| 76 | my $start = start_end_day( $timestamp, $blog ); |
|---|
| 77 | my ( $year, $month, $day ) = unpack 'A4A2A2', $start; |
|---|
| 78 | $file = sprintf( "%s/%04d/%02d/%02d/index", |
|---|
| 79 | $this_cat->category_path, $year, $month, $day ); |
|---|
| 80 | } |
|---|
| 81 | $file; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | sub archive_title { |
|---|
| 85 | my $obj = shift; |
|---|
| 86 | my ( $ctx, $entry_or_ts ) = @_; |
|---|
| 87 | my $stamp = ref $entry_or_ts ? $entry_or_ts->authored_on : $entry_or_ts; |
|---|
| 88 | my $start = start_end_day( $stamp, $ctx->stash('blog') ); |
|---|
| 89 | my $date = |
|---|
| 90 | MT::Template::Context::_hdlr_date( $ctx, |
|---|
| 91 | { ts => $start, 'format' => "%x" } ); |
|---|
| 92 | my $cat = $obj->display_name($ctx); |
|---|
| 93 | |
|---|
| 94 | sprintf( "%s%s", $cat, $date ); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | sub archive_group_iter { |
|---|
| 98 | my $obj = shift; |
|---|
| 99 | my ( $ctx, $args ) = @_; |
|---|
| 100 | my $blog = $ctx->stash('blog'); |
|---|
| 101 | my $sort_order = |
|---|
| 102 | ( $args->{sort_order} || '' ) eq 'ascend' ? 'ascend' : 'descend'; |
|---|
| 103 | my $cat_order = $args->{sort_order} ? $args->{sort_order} : 'ascend'; |
|---|
| 104 | my $order = ( $sort_order eq 'ascend' ) ? 'asc' : 'desc'; |
|---|
| 105 | my $limit = exists $args->{lastn} ? delete $args->{lastn} : undef; |
|---|
| 106 | my $tmpl = $ctx->stash('template'); |
|---|
| 107 | my $cat = $ctx->stash('archive_category') || $ctx->stash('category'); |
|---|
| 108 | my @data = (); |
|---|
| 109 | my $count = 0; |
|---|
| 110 | my $ts = $ctx->{current_timestamp}; |
|---|
| 111 | my $tsend = $ctx->{current_timestamp_end}; |
|---|
| 112 | |
|---|
| 113 | require MT::Placement; |
|---|
| 114 | require MT::Entry; |
|---|
| 115 | my $loop_sub = sub { |
|---|
| 116 | my $c = shift; |
|---|
| 117 | my $entry_iter = MT::Entry->count_group_by( |
|---|
| 118 | { |
|---|
| 119 | blog_id => $blog->id, |
|---|
| 120 | status => MT::Entry::RELEASE(), |
|---|
| 121 | ( $ts && $tsend ? ( authored_on => [ $ts, $tsend ] ) : () ), |
|---|
| 122 | }, |
|---|
| 123 | { |
|---|
| 124 | ( $ts && $tsend ? ( range_incl => { authored_on => 1 } ) : () ), |
|---|
| 125 | group => [ |
|---|
| 126 | "extract(year from authored_on)", |
|---|
| 127 | "extract(month from authored_on)", |
|---|
| 128 | "extract(day from authored_on)" |
|---|
| 129 | ], |
|---|
| 130 | sort => "extract(year from authored_on) $order, |
|---|
| 131 | extract(month from authored_on) $order, |
|---|
| 132 | extract(day from authored_on) $order", |
|---|
| 133 | 'join' => |
|---|
| 134 | [ 'MT::Placement', 'entry_id', { category_id => $c->id } ] |
|---|
| 135 | } |
|---|
| 136 | ) or return $ctx->error("Couldn't get yearly archive list"); |
|---|
| 137 | while ( my @row = $entry_iter->() ) { |
|---|
| 138 | my $hash = { |
|---|
| 139 | year => $row[1], |
|---|
| 140 | month => $row[2], |
|---|
| 141 | day => $row[3], |
|---|
| 142 | category => $c, |
|---|
| 143 | count => $row[0], |
|---|
| 144 | }; |
|---|
| 145 | push( @data, $hash ); |
|---|
| 146 | return $count + 1 |
|---|
| 147 | if ( defined($limit) && ( $count + 1 ) == $limit ); |
|---|
| 148 | $count++; |
|---|
| 149 | } |
|---|
| 150 | }; |
|---|
| 151 | |
|---|
| 152 | if ($cat) { |
|---|
| 153 | $loop_sub->($cat); |
|---|
| 154 | } |
|---|
| 155 | else { |
|---|
| 156 | require MT::Category; |
|---|
| 157 | my $iter = MT::Category->load_iter( { blog_id => $blog->id }, |
|---|
| 158 | { 'sort' => 'label', direction => $cat_order } ); |
|---|
| 159 | while ( my $category = $iter->() ) { |
|---|
| 160 | $loop_sub->($category); |
|---|
| 161 | last if ( defined($limit) && $count == $limit ); |
|---|
| 162 | } |
|---|
| 163 | } |
|---|
| 164 | |
|---|
| 165 | my $loop = @data; |
|---|
| 166 | my $curr = 0; |
|---|
| 167 | |
|---|
| 168 | return sub { |
|---|
| 169 | if ( $curr < $loop ) { |
|---|
| 170 | my $date = sprintf( |
|---|
| 171 | "%04d%02d%02d000000", |
|---|
| 172 | $data[$curr]->{year}, |
|---|
| 173 | $data[$curr]->{month}, |
|---|
| 174 | $data[$curr]->{day} |
|---|
| 175 | ); |
|---|
| 176 | my ( $start, $end ) = start_end_day($date); |
|---|
| 177 | my $count = $data[$curr]->{count}; |
|---|
| 178 | my %hash = ( |
|---|
| 179 | category => $data[$curr]->{category}, |
|---|
| 180 | year => $data[$curr]->{year}, |
|---|
| 181 | month => $data[$curr]->{month}, |
|---|
| 182 | day => $data[$curr]->{day}, |
|---|
| 183 | start => $start, |
|---|
| 184 | end => $end, |
|---|
| 185 | ); |
|---|
| 186 | $curr++; |
|---|
| 187 | return ( $count, %hash ); |
|---|
| 188 | } |
|---|
| 189 | undef; |
|---|
| 190 | } |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | sub archive_group_entries { |
|---|
| 194 | my $obj = shift; |
|---|
| 195 | my ( $ctx, %param ) = @_; |
|---|
| 196 | my $ts = |
|---|
| 197 | $param{year} |
|---|
| 198 | ? sprintf( "%04d%02d%02d000000", $param{year}, $param{month}, |
|---|
| 199 | $param{day} ) |
|---|
| 200 | : $ctx->stash('current_timestamp'); |
|---|
| 201 | my $cat = $param{category} || $ctx->stash('archive_category'); |
|---|
| 202 | my $limit = $param{limit}; |
|---|
| 203 | $obj->dated_category_entries( $ctx, 'Category-Daily', $cat, $ts, $limit ); |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | sub archive_entries_count { |
|---|
| 207 | my $obj = shift; |
|---|
| 208 | my ( $blog, $at, $entry ) = @_; |
|---|
| 209 | my $cat = $entry->category; |
|---|
| 210 | return 0 unless $cat; |
|---|
| 211 | return $obj->SUPER::archive_entries_count( |
|---|
| 212 | { |
|---|
| 213 | Blog => $blog, |
|---|
| 214 | ArchiveType => $at, |
|---|
| 215 | Timestamp => $entry->authored_on, |
|---|
| 216 | Category => $cat |
|---|
| 217 | } |
|---|
| 218 | ); |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | 1; |
|---|