root/branches/release-31/lib/MT/ArchiveType/AuthorWeekly.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::AuthorWeekly;
8
9use strict;
10use base qw( MT::ArchiveType::Author MT::ArchiveType::Weekly );
11use MT::Util qw( dirify start_end_week );
12
13sub name {
14    return 'Author-Weekly';
15}
16
17sub archive_label {
18    return MT->translate('AUTHOR-WEEKLY_ADV');
19}
20
21sub default_archive_templates {
22    return [
23        {
24            label => 'author-display-name/yyyy/mm/day-week/index.html',
25            template => '%-a/%y/%m/%d-week/%f',
26            default  => 1
27        },
28        {
29            label => 'author_display_name/yyyy/mm/day-week/index.html',
30            template => '%a/%y/%m/%d-week/%f'
31        },
32    ];
33}
34
35sub dynamic_template {
36    return 'author/<$MTEntryAuthorID$>/week/<$MTArchiveDate format="%Y%m%d"$>';
37}
38
39sub template_params {
40    return {
41        archive_class         => "author-weekly-archive",
42        author_weekly_archive => 1,
43        main_template         => 1,
44        archive_template      => 1,
45        archive_listing       => 1,
46    },
47}
48
49sub archive_title {
50    my $obj = shift;
51    my ( $ctx, $entry_or_ts ) = @_;
52    my $stamp = ref $entry_or_ts ? $entry_or_ts->authored_on : $entry_or_ts;
53    my ( $start, $end ) = start_end_week($stamp);
54    my $start_date =
55      MT::Template::Context::_hdlr_date( $ctx,
56        { ts => $start, 'format' => "%x" } );
57    my $end_date =
58      MT::Template::Context::_hdlr_date( $ctx,
59        { ts => $end, 'format' => "%x" } );
60    my $author = $obj->display_name($ctx);
61
62    sprintf( "%s%s - %s", $author, $start_date, $end_date );
63}
64
65sub archive_file {
66    my $obj = shift;
67    my ( $ctx, %param ) = @_;
68    my $timestamp = $param{Timestamp};
69    my $file_tmpl = $param{Template};
70    my $author    = $ctx->{__stash}{author};
71    my $entry     = $ctx->{__stash}{entry};
72    my $file;
73    my $this_author = $author ? $author : ( $entry ? $entry->author : undef );
74    return "" unless $this_author;
75    my $name = dirify( $this_author->nickname );
76
77    if ( $name eq '' || !$file_tmpl ) {
78        return "" unless $this_author;
79        $name = "author" . $this_author->id if $name !~ /\w/;
80        my $start = start_end_week($timestamp);
81        my ( $year, $month, $day ) = unpack 'A4A2A2', $start;
82        $file =
83          sprintf( "%s/%04d/%02d/%02d-week/index", $name, $year, $month, $day );
84    }
85    else {
86        ( $ctx->{current_timestamp}, $ctx->{current_timestamp_end} ) =
87          start_end_week($timestamp);
88    }
89    $file;
90}
91
92sub archive_group_iter {
93    my $obj = shift;
94    my ( $ctx, $args ) = @_;
95    my $blog = $ctx->stash('blog');
96    my $sort_order =
97      ( $args->{sort_order} || '' ) eq 'ascend' ? 'ascend' : 'descend';
98    my $auth_order = $args->{sort_order} ? $args->{sort_order} : 'ascend';
99    my $order = ( $sort_order eq 'ascend' ) ? 'asc' : 'desc';
100    my $limit = exists $args->{lastn} ? delete $args->{lastn} : undef;
101
102    my $tmpl  = $ctx->stash('template');
103    my @data  = ();
104    my $count = 0;
105
106    my $ts    = $ctx->{current_timestamp};
107    my $tsend = $ctx->{current_timestamp_end};
108
109    my $at       = $ctx->{archive_type};
110    my $archiver = MT->publisher->archiver($at);
111    my $author;
112
113    # if (($tmpl && $tmpl->type ne 'index') &&
114    #     ($archiver && $archiver->author_based))
115    # {
116    $author = $ctx->stash('author');
117
118    # }
119
120    require MT::Entry;
121    my $loop_sub = sub {
122        my $auth       = shift;
123        my $count_iter = MT::Entry->count_group_by(
124            {
125                blog_id   => $blog->id,
126                author_id => $auth->id,
127                status    => MT::Entry::RELEASE(),
128                ( $ts && $tsend ? ( authored_on => [ $ts, $tsend ] ) : () ),
129            },
130            {
131                ( $ts && $tsend ? ( range_incl => { authored_on => 1 } ) : () ),
132                group  => ["week_number"],
133                'sort' => "week_number $order"
134            }
135        ) or return $ctx->error("Couldn't get weekly archive list");
136
137        while ( my @row = $count_iter->() ) {
138            my ( $year, $week ) = unpack 'A4A2', $row[1];
139            my $hash = {
140                year   => $year,
141                week   => $week,
142                author => $auth,
143                count  => $row[0],
144            };
145            push( @data, $hash );
146            return $count + 1
147              if ( defined($limit) && ( $count + 1 ) == $limit );
148            $count++;
149        }
150        return $count;
151    };
152
153    # Count entry by author
154    if ($author) {
155        $loop_sub->($author);
156    }
157    else {
158
159        # load authors
160        require MT::Author;
161        my $iter;
162        $iter = MT::Author->load_iter(
163            undef,
164            {
165                sort      => 'name',
166                direction => $auth_order,
167                join      => [
168                    'MT::Entry', 'author_id',
169                    { status => MT::Entry::RELEASE(), blog_id => $blog->id },
170                    { unique => 1 }
171                ]
172            }
173        );
174
175        while ( my $a = $iter->() ) {
176            $loop_sub->($a);
177            last if ( defined($limit) && $count == $limit );
178        }
179    }
180
181    my $loop = @data;
182    my $curr = 0;
183
184    return sub {
185        if ( $curr < $loop ) {
186            my $date = sprintf( "%04d%02d%02d000000",
187                week2ymd( $data[$curr]->{year}, $data[$curr]->{week} ) );
188            my ( $start, $end ) = start_end_week($date);
189            my $count = $data[$curr]->{count};
190            my %hash  = (
191                author => $data[$curr]->{author},
192                year   => $data[$curr]->{year},
193                week   => $data[$curr]->{week},
194                start  => $start,
195                end    => $end
196            );
197            $curr++;
198            return ( $count, %hash );
199        }
200        undef;
201      }
202}
203
204sub archive_group_entries {
205    my $obj = shift;
206    my ( $ctx, %param ) = @_;
207    my $ts =
208        $param{year}
209    ? sprintf( "%04d%02d%02d000000", week2ymd( $param{year}, $param{week} ) )
210        : $ctx->stash('current_timestamp');
211    my $author = $param{author} || $ctx->stash('author');
212    my $limit = $param{limit};
213    $obj->dated_author_entries( $ctx, 'Author-Weekly', $author, $ts, $limit );
214}
215
216sub archive_entries_count {
217    my $obj = shift;
218    my ( $blog, $at, $entry ) = @_;
219    my $auth = $entry->author;
220    return $obj->SUPER::archive_entries_count(
221        {
222            Blog        => $blog,
223            ArchiveType => $at,
224            Timestamp   => $entry->authored_on,
225            Author      => $auth
226        }
227    );
228}
229
2301;
Note: See TracBrowser for help on using the browser.