~ubuntu-branches/debian/squeeze/movabletype-opensource/squeeze

« back to all changes in this revision

Viewing changes to lib/MT/ArchiveType/Author.pm

  • Committer: Bazaar Package Importer
  • Author(s): Dominic Hargreaves
  • Date: 2008-06-13 23:28:40 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080613232840-ya4jfxv1jgl45a3d
Tags: 4.2~rc2-1
* New upstream release candidate
* Update Standards-Version (no changes)
* Ensure that schema upgrade message is always seen

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
7
package MT::ArchiveType::Author;
 
8
 
 
9
use strict;
 
10
use base qw( MT::ArchiveType );
 
11
 
 
12
sub name {
 
13
    return 'Author';
 
14
}
 
15
 
 
16
sub archive_label {
 
17
    return MT->translate('AUTHOR_ADV');
 
18
}
 
19
 
 
20
sub default_archive_templates {
 
21
    return [
 
22
        {
 
23
            label    => 'author/author-display-name/index.html',
 
24
            template => 'author/%-a/%f',
 
25
            default  => 1
 
26
        },
 
27
        {
 
28
            label    => 'author/author_display_name/index.html',
 
29
            template => 'author/%a/%f'
 
30
        },
 
31
    ];
 
32
}
 
33
 
 
34
sub dynamic_template {
 
35
    return 'author/<$MTEntryAuthorID$>/<$MTEntryID$>';
 
36
}
 
37
 
 
38
sub template_params {
 
39
    return {
 
40
        archive_class                    => "author-archive",
 
41
        'module_author-monthly_archives' => 1,
 
42
        author_archive                   => 1,
 
43
        archive_template                 => 1,
 
44
        archive_listing                  => 1,
 
45
    };
 
46
}
 
47
 
 
48
sub archive_title {
 
49
    my $obj = shift;
 
50
    my ($ctx) = @_;
 
51
    my $a = $ctx->stash('author');
 
52
    $a ? $a->nickname || MT->translate( '(Display Name not set)' ) : '';
 
53
}
 
54
 
 
55
sub archive_file {
 
56
    my $obj = shift;
 
57
    my ( $ctx, %param ) = @_;
 
58
    my $file_tmpl = $param{Template};
 
59
    my $author    = $ctx->{__stash}{author};
 
60
    my $entry     = $ctx->{__stash}{entry};
 
61
    my $file;
 
62
 
 
63
    my $this_author = $author ? $author : ( $entry ? $entry->author : undef );
 
64
    return "" unless $this_author;
 
65
 
 
66
    if ( !$file_tmpl ) {
 
67
        $file = sprintf( "%s/index", $this_author->basename );
 
68
    }
 
69
    $file;
 
70
}
 
71
 
 
72
sub archive_group_iter {
 
73
    my $obj = shift;
 
74
    my ( $ctx, $args ) = @_;
 
75
    my $blog = $ctx->stash('blog');
 
76
    my $sort_order =
 
77
      ( $args->{sort_order} || '' ) eq 'ascend' ? 'ascend' : 'descend';
 
78
    my $auth_order = $args->{sort_order} ? $args->{sort_order} : 'ascend';
 
79
    my $order = ( $sort_order eq 'ascend' ) ? 'asc' : 'desc';
 
80
    my $limit = exists $args->{lastn} ? delete $args->{lastn} : undef;
 
81
    require MT::Entry;
 
82
    require MT::Author;
 
83
    my $auth_iter = MT::Author->load_iter(
 
84
        undef,
 
85
        {
 
86
            sort      => 'name',
 
87
            direction => $auth_order,
 
88
            join      => [
 
89
                'MT::Entry', 'author_id',
 
90
                { status => MT::Entry::RELEASE(), blog_id => $blog->id },
 
91
                { unique => 1 }
 
92
            ]
 
93
        }
 
94
    );
 
95
    my $i = 0;
 
96
    return sub {
 
97
 
 
98
        while ( my $a = $auth_iter->() ) {
 
99
            last if defined($limit) && $i == $limit;
 
100
            my $count = MT::Entry->count(
 
101
                {
 
102
                    blog_id   => $blog->id,
 
103
                    status    => MT::Entry::RELEASE(),
 
104
                    author_id => $a->id
 
105
                }
 
106
            );
 
107
            next if $count == 0 && !$args->{show_empty};
 
108
            $i++;
 
109
            return ( $count, author => $a );
 
110
        }
 
111
        undef;
 
112
    };
 
113
}
 
114
 
 
115
sub archive_group_entries {
 
116
    my $obj = shift;
 
117
    my ( $ctx, %param ) = @_;
 
118
    my $blog = $ctx->stash('blog');
 
119
    my $a = $param{author} || $ctx->stash('author');
 
120
    my $limit = $param{limit};
 
121
    if ( $limit eq 'auto' ) {
 
122
        my $blog = $ctx->stash('blog');
 
123
        $limit = $blog->entries_on_index if $blog;
 
124
    }
 
125
    return [] unless $a;
 
126
    require MT::Entry;
 
127
    my @entries = MT::Entry->load(
 
128
        {
 
129
            blog_id   => $blog->id,
 
130
            author_id => $a->id,
 
131
            status    => MT::Entry::RELEASE()
 
132
        },
 
133
        {
 
134
            'sort'      => 'authored_on',
 
135
            'direction' => 'descend',
 
136
            ( $limit ? ( 'limit' => $limit ) : () ),
 
137
        }
 
138
    );
 
139
    \@entries;
 
140
}
 
141
 
 
142
sub archive_entries_count {
 
143
    my $obj = shift;
 
144
    my ( $blog, $at, $entry ) = @_;
 
145
    return $obj->SUPER::archive_entries_count(@_) unless $entry;
 
146
    my $auth = $entry->author;
 
147
    return $obj->SUPER::archive_entries_count(
 
148
        {
 
149
            Blog        => $blog,
 
150
            ArchiveType => $at,
 
151
            Author      => $auth
 
152
        }
 
153
    );
 
154
}
 
155
 
 
156
sub display_name {
 
157
    my $obj = shift;
 
158
    my ($ctx)    = shift;
 
159
    my $tmpl     = $ctx->stash('template');
 
160
    my $at       = $ctx->{archive_type};
 
161
    my $author   = '';
 
162
    if (   ( $tmpl && $tmpl->type eq 'index' )
 
163
        || !$obj
 
164
        || ( $obj && !$obj->author_based )
 
165
        || !$ctx->{inside_archive_list} )
 
166
    {
 
167
        $author = $ctx->stash('author');
 
168
        $author =
 
169
            $author
 
170
          ? $author->nickname
 
171
          ? $author->nickname . ": "
 
172
          : MT->translate( '(Display Name not set)' )
 
173
          : '';
 
174
    }
 
175
    return $author;
 
176
}
 
177
 
 
178
sub author_based {
 
179
    return 1;
 
180
}
 
181
 
 
182
sub group_based {
 
183
    return 1;
 
184
}
 
185
 
 
186
sub date_based_author_entries {
 
187
    my $obj = shift;
 
188
    my ( $ctx, $at, $author, $ts ) = @_;
 
189
 
 
190
    my $blog     = $ctx->stash('blog');
 
191
    my ( $start, $end );
 
192
    if ($ts) {
 
193
        ( $start, $end ) = $obj->date_range($ts);
 
194
    }
 
195
    else {
 
196
        $start = $ctx->{current_timestamp};
 
197
        $end   = $ctx->{current_timestamp_end};
 
198
    }
 
199
    my @entries = MT::Entry->load(
 
200
        {
 
201
            blog_id     => $blog->id,
 
202
            author_id   => $author->id,
 
203
            status      => MT::Entry::RELEASE(),
 
204
            authored_on => [ $start, $end ]
 
205
        },
 
206
        {
 
207
            range => { authored_on => 1 },
 
208
            'sort' => 'authored_on',
 
209
            'direction' => 'descend',
 
210
        }
 
211
    ) or return $ctx->error("Couldn't get $at archive list");
 
212
    \@entries;
 
213
}
 
214
 
 
215
1;