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

Revision 1486, 6.5 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::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        main_template                      => 1,
45        archive_template                   => 1,
46        archive_listing                    => 1,
47    };
48}
49
50sub archive_file {
51    my $obj = shift;
52    my ( $ctx, %param ) = @_;
53    my $timestamp = $param{Timestamp};
54    my $file_tmpl = $param{Template};
55    my $blog      = $ctx->{__stash}{blog};
56    my $cat       = $ctx->{__stash}{cat} || $ctx->{__stash}{category};
57    my $entry     = $ctx->{__stash}{entry};
58    my $file;
59
60    my $this_cat = $cat ? $cat : ( $entry ? $entry->category : undef );
61    if ($file_tmpl) {
62        ( $ctx->{current_timestamp}, $ctx->{current_timestamp_end} ) =
63          start_end_month( $timestamp, $blog );
64        $ctx->stash( 'archive_category', $this_cat );
65        $ctx->{inside_mt_categories} = 1;
66        $ctx->{__stash}{category} = $this_cat;
67    }
68    else {
69        if ( !$this_cat ) {
70            return "";
71        }
72        my $label = '';
73        $label = dirify( $this_cat->label );
74        if ( $label !~ /\w/ ) {
75            $label = $this_cat ? "cat" . $this_cat->id : "";
76        }
77        my $start = start_end_month( $timestamp, $blog );
78        my ( $year, $month ) = unpack 'A4A2', $start;
79        $file = sprintf( "%s/%04d/%02d/index",
80            $this_cat->category_path, $year, $month );
81    }
82    $file;
83}
84
85sub archive_title {
86    my $obj = shift;
87    my ( $ctx, $entry_or_ts ) = @_;
88    my $stamp = ref $entry_or_ts ? $entry_or_ts->authored_on : $entry_or_ts;
89    my $start = start_end_month( $stamp, $ctx->stash('blog') );
90    my $date =
91      MT::Template::Context::_hdlr_date( $ctx,
92        { ts => $start, 'format' => "%B %Y" } );
93    my $cat = $obj->display_name($ctx);
94
95    sprintf( "%s%s", $cat, $date );
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    my $ts    = $ctx->{current_timestamp};
112    my $tsend = $ctx->{current_timestamp_end};
113
114    require MT::Placement;
115    require MT::Entry;
116    my $loop_sub = sub {
117        my $c          = shift;
118        my $entry_iter = MT::Entry->count_group_by(
119            {
120                blog_id => $blog->id,
121                status  => MT::Entry::RELEASE(),
122                ( $ts && $tsend ? ( authored_on => [ $ts, $tsend ] ) : () ),
123            },
124            {
125                ( $ts && $tsend ? ( range_incl => { authored_on => 1 } ) : () ),
126                group => [
127                    "extract(year from authored_on)",
128                    "extract(month from authored_on)"
129                ],
130                sort => "extract(year from authored_on) $order,
131                              extract(month from authored_on) $order",
132                'join' =>
133                  [ 'MT::Placement', 'entry_id', { category_id => $c->id } ]
134            }
135        ) or return $ctx->error("Couldn't get yearly archive list");
136        while ( my @row = $entry_iter->() ) {
137            my $hash = {
138                year     => $row[1],
139                month    => $row[2],
140                category => $c,
141                count    => $row[0],
142            };
143            push( @data, $hash );
144            return $count + 1
145              if ( defined($limit) && ( $count + 1 ) == $limit );
146            $count++;
147        }
148    };
149
150    if ($cat) {
151        $loop_sub->($cat);
152    }
153    else {
154        require MT::Category;
155        my $iter = MT::Category->load_iter( { blog_id => $blog->id },
156            { 'sort' => 'label', direction => $cat_order } );
157        while ( my $category = $iter->() ) {
158            $loop_sub->($category);
159            last if ( defined($limit) && $count == $limit );
160        }
161    }
162
163    my $loop = @data;
164    my $curr = 0;
165
166    return sub {
167        if ( $curr < $loop ) {
168            my $date = sprintf(
169                "%04d%02d%02d000000",
170                $data[$curr]->{year},
171                $data[$curr]->{month}, 1
172            );
173            my ( $start, $end ) = start_end_month($date);
174            my $count = $data[$curr]->{count};
175            my %hash  = (
176                category => $data[$curr]->{category},
177                year     => $data[$curr]->{year},
178                month    => $data[$curr]->{month},
179                start    => $start,
180                end      => $end,
181            );
182            $curr++;
183            return ( $count, %hash );
184        }
185        undef;
186      }
187}
188
189sub archive_group_entries {
190    my $obj = shift;
191    my ( $ctx, %param ) = @_;
192    my $ts =
193        $param{year}
194    ? sprintf( "%04d%02d%02d000000", $param{year}, $param{month}, 1 )
195        : $ctx->stash('current_timestamp');
196    my $cat = $param{category} || $ctx->stash('archive_category');
197    my $limit = $param{limit};
198    $obj->dated_category_entries( $ctx, 'Category-Monthly', $cat, $ts, $limit );
199}
200
201sub archive_entries_count {
202    my $obj = shift;
203    my ( $blog, $at, $entry ) = @_;
204    my $cat = $entry->category;
205    return 0 unless $cat;
206    return $obj->SUPER::archive_entries_count(
207        {
208            Blog        => $blog,
209            ArchiveType => $at,
210            Timestamp   => $entry->authored_on,
211            Category    => $cat
212        }
213    );
214}
215
2161;
Note: See TracBrowser for help on using the browser.