root/branches/release-39/lib/MT/ArchiveType/Yearly.pm @ 2500

Revision 2500, 3.6 kB (checked in by fumiakiy, 18 months ago)

Modernized how sort argument is specified in group_by query. BugId:79977. The legacy way of specifying it is still allowed but discouraged.

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        archive_template         => 1,
41        archive_listing          => 1,
42        archive_class            => "datebased-yearly-archive",
43    };
44}
45
46sub archive_file {
47    my $obj = shift;
48    my ( $ctx, %param ) = @_;
49    my $timestamp = $param{Timestamp};
50    my $file_tmpl = $param{Template};
51    my $blog      = $ctx->{__stash}{blog};
52
53    my $file;
54    if ($file_tmpl) {
55        ( $ctx->{current_timestamp}, $ctx->{current_timestamp_end} ) =
56          start_end_year( $timestamp, $blog );
57    }
58    else {
59        my $start = start_end_year( $timestamp, $blog );
60        my ( $year ) = unpack 'A4', $start;
61        $file = sprintf( "%04d/index", $year );
62    }
63
64    $file;
65}
66
67sub archive_title {
68    my $obj = shift;
69    my ( $ctx, $entry_or_ts ) = @_;
70    my $stamp = ref $entry_or_ts ? $entry_or_ts->authored_on : $entry_or_ts;
71    my $start = start_end_year( $stamp, $ctx->stash('blog') );
72    require MT::Template::Context;
73    my $year =
74      MT::Template::Context::_hdlr_date( $ctx,
75        { ts => $start, 'format' => "%Y" } );
76    my $lang = lc MT->current_language || 'en_us';
77    $lang = 'ja' if lc($lang) eq 'jp';
78
79    sprintf( "%s%s", $year, ( $lang eq 'ja' ? '&#24180;' : '' ) );
80}
81
82sub date_range {
83    my $obj = shift;
84    start_end_year(@_);
85}
86
87sub archive_group_iter {
88    my $obj = shift;
89    my ( $ctx, $args ) = @_;
90    my $blog = $ctx->stash('blog');
91    my $iter;
92    my $sort_order =
93      ( $args->{sort_order} || '' ) eq 'ascend' ? 'ascend' : 'descend';
94    my $order = ( $sort_order eq 'ascend' ) ? 'asc' : 'desc';
95
96    require MT::Entry;
97    $iter = MT::Entry->count_group_by(
98        {
99            blog_id => $blog->id,
100            status  => MT::Entry::RELEASE()
101        },
102        {
103            group => ["extract(year from authored_on)"],
104            $args->{lastn} ? ( limit => $args->{lastn} ) : (),
105            sort => [ { column => "extract(year from authored_on)", desc => $order } ],
106        }
107    ) or return $ctx->error("Couldn't get yearly archive list");
108
109    return sub {
110        while ( my @row = $iter->() ) {
111            my $date = sprintf( "%04d%02d%02d000000", $row[1], 1, 1 );
112            my ( $start, $end ) = start_end_year($date);
113            return ( $row[0], year => $row[1], start => $start, end => $end );
114        }
115        undef;
116    };
117}
118
119sub archive_group_entries {
120    my $obj = shift;
121    my ( $ctx, %param ) = @_;
122    my $ts =
123        $param{year}
124    ? sprintf( "%04d%02d%02d000000", $param{year}, 1, 1 )
125        : undef;
126    my $limit = $param{limit};
127    $obj->dated_group_entries( $ctx, 'Yearly', $ts, $limit );
128}
129
130sub archive_entries_count {
131    my $obj = shift;
132    my ( $blog, $at, $entry ) = @_;
133    return $obj->SUPER::archive_entries_count(
134        {
135            Blog        => $blog,
136            ArchiveType => $at,
137            Timestamp   => $entry->authored_on
138        }
139    );
140}
141
1421;
Note: See TracBrowser for help on using the browser.