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

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