root/branches/release-32/lib/MT/ArchiveType/Category.pm @ 1618

Revision 1618, 4.5 kB (checked in by takayama, 20 months ago)

Fixed BugId:70976 BugId:70966, BugId:69874
* Applied ogawa-san's patch.

Line 
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
7package MT::ArchiveType::Category;
8
9use strict;
10use base qw( MT::ArchiveType );
11
12sub name {
13    return 'Category';
14}
15
16sub archive_label {
17    return MT->translate("CATEGORY_ADV");
18}
19
20sub dynamic_template {
21    return 'category/<$MTCategoryID$>';
22}
23
24sub 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
38sub template_params {
39    return {
40        archive_class                      => "category-archive",
41        category_archive                   => 1,
42        main_template                      => 1,
43        archive_template                   => 1,
44        archive_listing                    => 1,
45        'module_category-monthly_archives' => 1,
46    };
47}
48
49sub archive_title {
50    my $obj = shift;
51    my ($ctx) = @_;
52    my $c = $ctx->stash('category');
53    $c ? $c->label : '';
54}
55
56sub archive_file {
57    my $obj = shift;
58    my ( $ctx, %param ) = @_;
59    my $timestamp = $param{Timestamp};
60    my $file_tmpl = $param{Template};
61    my $blog      = $ctx->{__stash}{blog};
62    my $cat       = $ctx->{__stash}{category};
63    my $entry     = $ctx->{__stash}{entry};
64    my $file;
65
66    my $this_cat = $cat ? $cat : ( $entry ? $entry->category : undef );
67    if ($file_tmpl) {
68        $ctx->stash( 'archive_category', $this_cat );
69        $ctx->{inside_mt_categories} = 1;
70        $ctx->{__stash}{category} = $this_cat;
71    }
72    else {
73        if ( !$this_cat ) {
74            return "";
75        }
76        my $label = '';
77        $label = dirify( $this_cat->label );
78        if ( $label !~ /\w/ ) {
79            $label = $this_cat ? "cat" . $this_cat->id : "";
80        }
81        $file = sprintf( "%s/index", $this_cat->category_path );
82    }
83    $file;
84}
85
86sub archive_group_iter {
87    my $obj = shift;
88    my ( $ctx, $args ) = @_;
89
90    my $blog_id = $ctx->stash('blog')->id;
91    require MT::Category;
92    my $iter = MT::Category->load_iter( { blog_id => $blog_id },
93        { 'sort' => 'label', direction => 'ascend' } );
94    require MT::Placement;
95    require MT::Entry;
96
97    return sub {
98        while ( my $c = $iter->() ) {
99            my @arguments = (
100                {
101                    blog_id => $blog_id,
102                    status  => MT::Entry::RELEASE()
103                },
104                {
105                    'join' => [
106                        'MT::Placement', 'entry_id', { category_id => $c->id }
107                    ]
108                }
109            );
110            my $count = MT::Entry->count( @arguments );
111            next unless $count || $args->{show_empty};
112            return ( $count, category => $c );
113        }
114        undef;
115      }
116}
117
118sub 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
143sub 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
158sub 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
175sub group_based {
176    return 1;
177}
178
1791;
Note: See TracBrowser for help on using the browser.