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

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