| 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; |
|---|
| 8 | |
|---|
| 9 | use strict; |
|---|
| 10 | use MT::WeblogPublisher; |
|---|
| 11 | |
|---|
| 12 | sub new { |
|---|
| 13 | my $pkg = shift; |
|---|
| 14 | my $self = {@_}; |
|---|
| 15 | bless $self, $pkg; |
|---|
| 16 | } |
|---|
| 17 | |
|---|
| 18 | sub _getset { |
|---|
| 19 | my $self = shift; |
|---|
| 20 | my $name = shift; |
|---|
| 21 | @_ ? $self->{$name} = $_[0] : $self->{$name}; |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | sub _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 | |
|---|
| 42 | sub 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 |
|---|
| 47 | sub archive_group_iter { |
|---|
| 48 | shift->_getset_coderef( 'archive_group_iter', @_ ); |
|---|
| 49 | } |
|---|
| 50 | sub archive_group_entries { |
|---|
| 51 | shift->_getset_coderef( 'archive_group_entries', @_ ); |
|---|
| 52 | } |
|---|
| 53 | sub archive_file { |
|---|
| 54 | shift->_getset_coderef( 'archive_file', @_ ); |
|---|
| 55 | } |
|---|
| 56 | sub archive_title { |
|---|
| 57 | shift->_getset_coderef( 'archive_title', @_ ); |
|---|
| 58 | } |
|---|
| 59 | sub archive_label { |
|---|
| 60 | shift->_getset_coderef( 'archive_label', @_ ); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | sub 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 | |
|---|
| 71 | sub default_archive_templates { |
|---|
| 72 | shift->_getset( 'default_archive_templates', @_ ); |
|---|
| 73 | } |
|---|
| 74 | sub dynamic_template { shift->_getset( 'dynamic_template', @_ ) } |
|---|
| 75 | sub entry_class { shift->_getset( 'entry_class', @_ ) || 'entry' } |
|---|
| 76 | sub category_class { shift->_getset( 'category_class', @_ ) || 'category' } |
|---|
| 77 | sub template_params { shift->_getset('template_params') } |
|---|
| 78 | |
|---|
| 79 | sub 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 | |
|---|
| 87 | sub 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 | |
|---|
| 95 | sub 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 | |
|---|
| 103 | sub 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 | |
|---|
| 111 | sub 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 | |
|---|
| 119 | sub 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 |
|---|
| 162 | sub date_range { |
|---|
| 163 | shift->_getset_coderef( 'date_range', @_ ); |
|---|
| 164 | } |
|---|
| 165 | sub next_archive_entry { |
|---|
| 166 | shift->_getset_coderef( 'next_archive_entry', @_ ); |
|---|
| 167 | } |
|---|
| 168 | sub previous_archive_entry { |
|---|
| 169 | shift->_getset_coderef( 'previous_archive_entry', @_ ); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | 1; |
|---|