root/branches/release-38/lib/MT/ArchiveType.pm @ 2355

Revision 2355, 4.4 kB (checked in by takayama, 19 months ago)

Applied ogawa-san's patch. thanks ogawa-san. BugId:79726

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;
8
9use strict;
10use MT::WeblogPublisher;
11
12sub new {
13    my $pkg  = shift;
14    my $self = {@_};
15    bless $self, $pkg;
16}
17
18sub _getset {
19    my $self = shift;
20    my $name = shift;
21    @_ ? $self->{$name} = $_[0] : $self->{$name};
22}
23
24sub _getset_coderef {
25    my ($obj, $key, @param) = @_;
26    # We only do this weird thing for MT::ArchiveType, which for MT 4
27    # allowed assignment of a coderef to the base MT::ArchiveType class.
28    # With MT 4.2 and later, this routine should be overridden by a
29    # subclass, and therefore, shouldn't do anything by itself.
30    if (ref($obj) eq __PACKAGE__) {
31        if (@param && ref($param[0]) eq 'CODE') {
32            $obj->_getset( $key, @param );
33            return;
34        }
35        if (my $code = $obj->_getset($key)) {
36            return $code->(@param);
37        }
38    }
39    return;
40}
41
42sub name { shift->_getset( 'name', @_ ) }
43
44# The base class routine should handle coderef assignment
45# calling date_range with other parameters invokes this routine
46# and returns the result
47sub archive_group_iter {
48    shift->_getset_coderef( 'archive_group_iter', @_ );
49}
50sub archive_group_entries {
51    shift->_getset_coderef( 'archive_group_entries', @_ );
52}
53sub archive_file {
54    shift->_getset_coderef( 'archive_file', @_ );
55}
56sub archive_title {
57    shift->_getset_coderef( 'archive_title', @_ );
58}
59sub archive_label {
60    shift->_getset_coderef( 'archive_label', @_ );
61}
62
63sub group_based {
64    my $obj = shift;
65    if (ref($obj) eq __PACKAGE__) {
66        return $obj->_getset( 'archive_group_entries' ) ? 1 : 0;
67    }
68    return 0;
69}
70
71sub default_archive_templates {
72    shift->_getset( 'default_archive_templates', @_ );
73}
74sub dynamic_template { shift->_getset( 'dynamic_template', @_ ) }
75sub entry_class      { shift->_getset( 'entry_class',      @_ ) || 'entry' }
76sub category_class   { shift->_getset( 'category_class',   @_ ) || 'category' }
77sub template_params  { shift->_getset('template_params') }
78
79sub dynamic_support  {
80    my $obj = shift;
81    if (ref $obj ne __PACKAGE__) {
82        return 1; # assume support unless overridden
83    }
84    $obj->_getset( 'dynamic_support',  @_ );
85}
86
87sub category_based {
88    my $obj = shift;
89    if (ref $obj ne __PACKAGE__) {
90        return $obj->isa('MT::ArchiveType::Category');
91    }
92    return $obj->_getset('category_based', @_);
93}
94
95sub entry_based {
96    my $obj = shift;
97    if (ref $obj ne __PACKAGE__) {
98        return $obj->isa('MT::ArchiveType::Individual');
99    }
100    return $obj->_getset('entry_based', @_);
101}
102
103sub date_based {
104    my $obj = shift;
105    if (ref $obj ne __PACKAGE__) {
106        return $obj->isa('MT::ArchiveType::Date');
107    }
108    return $obj->_getset('date_based', @_);
109}
110
111sub author_based {
112    my $obj = shift;
113    if (ref $obj ne __PACKAGE__) {
114        return $obj->isa('MT::ArchiveType::Author');
115    }
116    return $obj->_getset('author_based', @_);
117}
118
119sub archive_entries_count {
120    my $self = shift;
121
122    return $self->_getset_coderef( 'archive_entries_count', @_ )
123        if ref($self) eq __PACKAGE__;
124
125    my ($params) = @_;
126    my $blog     = $params->{Blog};
127    my $at       = $params->{ArchiveType};
128    my $ts       = $params->{Timestamp};
129    my $cat      = $params->{Category};
130    my $auth     = $params->{Author};
131    my ( $start, $end );
132    if ($ts) {
133        my $archiver = MT->publisher->archiver($at);
134        ( $start, $end ) = $archiver->date_range($ts) if $archiver;
135    }
136
137    my $count = MT->model('entry')->count(
138        {
139            blog_id => $blog->id,
140            status  => MT::Entry::RELEASE(),
141            ( $ts ? ( authored_on => [ $start, $end ] ) : () ),
142            ( $auth ? ( author_id => $auth->id ) : () ),
143        },
144        {
145            ( $ts ? ( range_incl => { authored_on => 1 } ) : () ),
146            (
147                $cat
148                ? (
149                    'join' => [
150                        'MT::Placement', 'entry_id',
151                        { category_id => $cat->id }
152                    ]
153                  )
154                : ()
155            ),
156        }
157    );
158    return $count;
159}
160
161# For user-defined date-based archive types
162sub date_range {
163    shift->_getset_coderef( 'date_range', @_ );
164}
165sub next_archive_entry {
166    shift->_getset_coderef( 'next_archive_entry', @_ );
167}
168sub previous_archive_entry {
169    shift->_getset_coderef( 'previous_archive_entry', @_ );
170}
171
1721;
Note: See TracBrowser for help on using the browser.