root/branches/release-34/lib/MT/ArchiveType/AuthorWeekly.pm @ 1892

Revision 1892, 6.5 kB (checked in by bchoate, 20 months ago)

By default, publish author archives into an 'author' path. bugid:75536

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