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

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