root/branches/release-31/lib/MT/ArchiveType/CategoryYearly.pm @ 1486

Revision 1486, 5.9 kB (checked in by bchoate, 21 months ago)

Refactoring of archive type publishing code into separate modules.

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::CategoryYearly;
8
9use strict;
10use base qw( MT::ArchiveType::Category MT::ArchiveType::Yearly );
11use MT::Util qw( dirify start_end_year );
12
13sub name {
14    return 'Category-Yearly';
15}
16
17sub archive_label {
18    return MT->translate('CATEGORY-YEARLY_ADV');
19}
20
21sub default_archive_templates {
22    return [
23        {
24            label    => 'category/sub-category/yyyy/index.html',
25            template => '%-c/%y/%i',
26            default  => 1
27        },
28        {
29            label    => 'category/sub_category/yyyy/index.html',
30            template => '%c/%y/%i'
31        },
32    ];
33}
34
35sub dynamic_template {
36    return 'category/<$MTCategoryID$>/<$MTArchiveDate format="%Y"$>';
37}
38
39sub template_params {
40    return {
41        archive_class           => "category-yearly-archive",
42        category_yearly_archive => 1,
43        main_template           => 1,
44        archive_template        => 1,
45        archive_listing         => 1,
46    };
47}
48
49sub 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_year( $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_year( $timestamp, $blog );
77        my ($year) = unpack 'A4', $start;
78        $file = sprintf( "%s/%04d/index", $this_cat->category_path, $year );
79    }
80    $file;
81}
82
83sub archive_title {
84    my $obj = shift;
85    my ( $ctx, $entry_or_ts ) = @_;
86    my $stamp = ref $entry_or_ts ? $entry_or_ts->authored_on : $entry_or_ts;
87    my $start = start_end_year( $stamp, $ctx->stash('blog') );
88    my $year =
89      MT::Template::Context::_hdlr_date( $ctx,
90        { ts => $start, 'format' => "%Y" } );
91    my $lang = lc MT->current_language || 'en_us';
92    $lang = 'ja' if lc($lang) eq 'jp';
93    my $cat = $obj->display_name($ctx);
94
95    sprintf( "%s%s%s", $cat, $year, ( $lang eq 'ja' ? '&#24180;' : '' ) );
96}
97
98sub archive_group_iter {
99    my $obj = shift;
100    my ( $ctx, $args ) = @_;
101    my $blog = $ctx->stash('blog');
102    my $sort_order =
103      ( $args->{sort_order} || '' ) eq 'ascend' ? 'ascend' : 'descend';
104    my $cat_order = $args->{sort_order} ? $args->{sort_order} : 'ascend';
105    my $order = ( $sort_order eq 'ascend' ) ? 'asc'                 : 'desc';
106    my $limit = exists $args->{lastn}       ? delete $args->{lastn} : undef;
107    my $tmpl  = $ctx->stash('template');
108    my $cat   = $ctx->stash('archive_category') || $ctx->stash('category');
109    my @data  = ();
110    my $count = 0;
111
112    require MT::Placement;
113    require MT::Entry;
114    my $loop_sub = sub {
115        my $c          = shift;
116        my $entry_iter = MT::Entry->count_group_by(
117            {
118                blog_id => $blog->id,
119                status  => MT::Entry::RELEASE()
120            },
121            {
122                group => ["extract(year from authored_on)"],
123                sort  => "extract(year from authored_on) $order",
124                'join' =>
125                  [ 'MT::Placement', 'entry_id', { category_id => $c->id } ]
126            }
127        ) or return $ctx->error("Couldn't get yearly archive list");
128        while ( my @row = $entry_iter->() ) {
129            my $hash = {
130                year     => $row[1],
131                category => $c,
132                count    => $row[0],
133            };
134            push( @data, $hash );
135            return $count + 1
136              if ( defined($limit) && ( $count + 1 ) == $limit );
137            $count++;
138        }
139    };
140
141    if ($cat) {
142        $loop_sub->($cat);
143    }
144    else {
145        require MT::Category;
146        my $iter = MT::Category->load_iter( { blog_id => $blog->id },
147            { 'sort' => 'label', direction => $cat_order } );
148        while ( my $category = $iter->() ) {
149            $loop_sub->($category);
150            last if ( defined($limit) && $count == $limit );
151        }
152    }
153
154    my $loop = @data;
155    my $curr = 0;
156
157    return sub {
158        if ( $curr < $loop ) {
159            my $date =
160              sprintf( "%04d%02d%02d000000", $data[$curr]->{year}, 1, 1 );
161            my ( $start, $end ) = start_end_year($date);
162            my $count = $data[$curr]->{count};
163            my %hash  = (
164                category => $data[$curr]->{category},
165                year     => $data[$curr]->{year},
166                start    => $start,
167                end      => $end,
168            );
169            $curr++;
170            return ( $count, %hash );
171        }
172        undef;
173      }
174}
175
176sub archive_group_entries {
177    my $obj = shift;
178    my ( $ctx, %param ) = @_;
179    my $ts =
180        $param{year}
181    ? sprintf( "%04d%02d%02d000000", $param{year}, 1, 1 )
182        : $ctx->stash('current_timestamp');
183    my $cat = $param{category} || $ctx->stash('archive_category');
184    my $limit = $param{limit};
185    $obj->dated_category_entries( $ctx, 'Category-Yearly', $cat, $ts, $limit );
186}
187
188sub archive_entries_count {
189    my $obj = shift;
190    my ( $blog, $at, $entry ) = @_;
191    my $cat = $entry->category;
192    return 0 unless $cat;
193    return $obj->SUPER::archive_entries_count(
194        {
195            Blog        => $blog,
196            ArchiveType => $at,
197            Timestamp   => $entry->authored_on,
198            Category    => $cat
199        }
200    );
201}
202
2031;
Note: See TracBrowser for help on using the browser.