| 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::Category; |
|---|
| 8 | |
|---|
| 9 | use strict; |
|---|
| 10 | use base qw( MT::ArchiveType ); |
|---|
| 11 | |
|---|
| 12 | sub name { |
|---|
| 13 | return 'Category'; |
|---|
| 14 | } |
|---|
| 15 | |
|---|
| 16 | sub archive_label { |
|---|
| 17 | return MT->translate("CATEGORY_ADV"); |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | sub dynamic_template { |
|---|
| 21 | return 'category/<$MTCategoryID$>'; |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | sub default_archive_templates { |
|---|
| 25 | return [ |
|---|
| 26 | { |
|---|
| 27 | label => MT->translate('category/sub-category/index.html'), |
|---|
| 28 | template => '%-c/%i', |
|---|
| 29 | default => 1 |
|---|
| 30 | }, |
|---|
| 31 | { |
|---|
| 32 | label => MT->translate('category/sub_category/index.html'), |
|---|
| 33 | template => '%c/%i' |
|---|
| 34 | } |
|---|
| 35 | ]; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | sub template_params { |
|---|
| 39 | return { |
|---|
| 40 | archive_class => "category-archive", |
|---|
| 41 | category_archive => 1, |
|---|
| 42 | archive_template => 1, |
|---|
| 43 | archive_listing => 1, |
|---|
| 44 | 'module_category_archives' => 1, |
|---|
| 45 | }; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | sub archive_title { |
|---|
| 49 | my $obj = shift; |
|---|
| 50 | my ($ctx) = @_; |
|---|
| 51 | my $c = $ctx->stash('category'); |
|---|
| 52 | $c ? $c->label : ''; |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | sub archive_file { |
|---|
| 56 | my $obj = shift; |
|---|
| 57 | my ( $ctx, %param ) = @_; |
|---|
| 58 | my $timestamp = $param{Timestamp}; |
|---|
| 59 | my $file_tmpl = $param{Template}; |
|---|
| 60 | my $blog = $ctx->{__stash}{blog}; |
|---|
| 61 | my $cat = $ctx->{__stash}{category}; |
|---|
| 62 | my $entry = $ctx->{__stash}{entry}; |
|---|
| 63 | my $file; |
|---|
| 64 | |
|---|
| 65 | my $this_cat = $cat ? $cat : ( $entry ? $entry->category : undef ); |
|---|
| 66 | if ($file_tmpl) { |
|---|
| 67 | $ctx->stash( 'archive_category', $this_cat ); |
|---|
| 68 | $ctx->{inside_mt_categories} = 1; |
|---|
| 69 | $ctx->{__stash}{category} = $this_cat; |
|---|
| 70 | } |
|---|
| 71 | else { |
|---|
| 72 | if ( !$this_cat ) { |
|---|
| 73 | return ""; |
|---|
| 74 | } |
|---|
| 75 | my $label = ''; |
|---|
| 76 | $label = dirify( $this_cat->label ); |
|---|
| 77 | if ( $label !~ /\w/ ) { |
|---|
| 78 | $label = $this_cat ? "cat" . $this_cat->id : ""; |
|---|
| 79 | } |
|---|
| 80 | $file = sprintf( "%s/index", $this_cat->category_path ); |
|---|
| 81 | } |
|---|
| 82 | $file; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | sub archive_group_iter { |
|---|
| 86 | my $obj = shift; |
|---|
| 87 | my ( $ctx, $args ) = @_; |
|---|
| 88 | |
|---|
| 89 | my $blog_id = $ctx->stash('blog')->id; |
|---|
| 90 | require MT::Category; |
|---|
| 91 | my $iter = MT::Category->load_iter( { blog_id => $blog_id }, |
|---|
| 92 | { 'sort' => 'label', direction => 'ascend' } ); |
|---|
| 93 | require MT::Placement; |
|---|
| 94 | require MT::Entry; |
|---|
| 95 | |
|---|
| 96 | # issue a single count_group_by for all categories |
|---|
| 97 | my $cnt_iter = MT::Placement->count_group_by({ |
|---|
| 98 | blog_id => $blog_id, |
|---|
| 99 | }, { |
|---|
| 100 | group => [ 'category_id' ], |
|---|
| 101 | join => MT::Entry->join_on('id', { status => MT::Entry::RELEASE() }), |
|---|
| 102 | }); |
|---|
| 103 | my %counts; |
|---|
| 104 | while (my ($count, $cat_id) = $cnt_iter->()) { |
|---|
| 105 | $counts{$cat_id} = $count; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | return sub { |
|---|
| 109 | while ( my $c = $iter->() ) { |
|---|
| 110 | my $count = $counts{$c->id}; |
|---|
| 111 | next unless $count || $args->{show_empty}; |
|---|
| 112 | return ( $count, category => $c ); |
|---|
| 113 | } |
|---|
| 114 | return (); |
|---|
| 115 | }; |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | sub archive_group_entries { |
|---|
| 119 | my $obj = shift; |
|---|
| 120 | my ( $ctx, %param ) = @_; |
|---|
| 121 | my $limit = $param{limit}; |
|---|
| 122 | if ( $limit && ($limit eq 'auto') ) { |
|---|
| 123 | my $blog = $ctx->stash('blog'); |
|---|
| 124 | $limit = $blog->entries_on_index if $blog; |
|---|
| 125 | } |
|---|
| 126 | my $c = $ctx->stash('archive_category') || $ctx->stash('category'); |
|---|
| 127 | require MT::Entry; |
|---|
| 128 | my @entries = MT::Entry->load( |
|---|
| 129 | { status => MT::Entry::RELEASE() }, |
|---|
| 130 | { |
|---|
| 131 | join => [ |
|---|
| 132 | 'MT::Placement', 'entry_id', |
|---|
| 133 | { category_id => $c->id }, { unqiue => 1 } |
|---|
| 134 | ], |
|---|
| 135 | 'sort' => 'authored_on', |
|---|
| 136 | 'direction' => 'descend', |
|---|
| 137 | ( $limit ? ( 'limit' => $limit ) : () ), |
|---|
| 138 | } |
|---|
| 139 | ); |
|---|
| 140 | \@entries; |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | sub archive_entries_count { |
|---|
| 144 | my $obj = shift; |
|---|
| 145 | my ( $blog, $at, $entry ) = @_; |
|---|
| 146 | return $obj->SUPER::archive_entries_count(@_) unless $entry; |
|---|
| 147 | my $cat = $entry->category; |
|---|
| 148 | return 0 unless $cat; |
|---|
| 149 | return $obj->SUPER::archive_entries_count( |
|---|
| 150 | { |
|---|
| 151 | Blog => $blog, |
|---|
| 152 | ArchiveType => $at, |
|---|
| 153 | Category => $cat |
|---|
| 154 | } |
|---|
| 155 | ); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | sub display_name { |
|---|
| 159 | my $archiver = shift; |
|---|
| 160 | my $ctx = shift; |
|---|
| 161 | my $tmpl = $ctx->stash('template'); |
|---|
| 162 | my $cat = ''; |
|---|
| 163 | if ( !$tmpl |
|---|
| 164 | || $tmpl->type eq 'index' |
|---|
| 165 | || !$archiver |
|---|
| 166 | || ( $archiver && !$archiver->category_based ) |
|---|
| 167 | || !$ctx->{inside_archive_list} ) |
|---|
| 168 | { |
|---|
| 169 | $cat = $ctx->stash('archive_category') || $ctx->stash('category'); |
|---|
| 170 | $cat = $cat ? $cat->label . ': ' : ''; |
|---|
| 171 | } |
|---|
| 172 | return $cat; |
|---|
| 173 | } |
|---|
| 174 | |
|---|
| 175 | sub group_based { |
|---|
| 176 | return 1; |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | 1; |
|---|