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

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