File: Author.pm

package info (click to toggle)
movabletype-opensource 5.1.4%2Bdfsg-4%2Bdeb7u3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 32,996 kB
  • sloc: perl: 197,285; php: 62,405; sh: 166; xml: 117; makefile: 83; sql: 32
file content (204 lines) | stat: -rw-r--r-- 6,368 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# Movable Type (r) Open Source (C) 2001-2012 Six Apart, Ltd.
# This program is distributed under the terms of the
# GNU General Public License, version 2.
#
# $Id$

# Core Summary Object Framework functions for MT::Author

package MT::Summary::Author;

use strict;
use warnings;
use MT::Author;
use MT::Entry;

sub summarize_comment_count {
    my $author = shift;
    my ($terms) = @_;
    my %args;
    use MT::Comment;
    $args{join} = MT::Entry->join_on(
        undef,
        {   id     => \'= comment_entry_id',
            status => MT::Entry::RELEASE(),
        }
    );
    my $comment_count = MT::Comment->count(
        {   commenter_id => $author->id,
            visible      => 1,
        },
        \%args
    );
    return $comment_count;
}

sub expire_comment_count {
    my ( $parent_obj, $obj, $terms ) = @_;

    # action: save/remove
    # parent_obj => author, obj => the comment
    return unless ( $parent_obj and $parent_obj->id );
    $parent_obj->set_summary( $terms, 0 );
    $parent_obj->expire_summary($terms);
}

sub expire_comment_count_entry {
    my ( $parent_obj, $obj, $terms, $action, $orig ) = @_;
    use MT::Comment;
    if ( $action eq 'remove' ) {
        my $c_iter = MT::Comment->load_iter( { entry_id => $obj->id } );
        while ( my $c = $c_iter->() ) {
            if ( $c->commenter_id ) {
                my $a = MT::Author->load( $c->commenter_id );
                $a->expire_summary('comment_count');
                if ( !$a->summary_is_expired('comment_count') ) {
                    $a->set_summary( $terms, 0 );
                    $a->expire_summary('comment_count');
                }
            }
        }
    }
    elsif ( $action eq 'save' ) {
        if ( $obj->{changed_cols}->{status} ) {
            my $c_iter = MT::Comment->load_iter( { entry_id => $obj->id } );
            while ( my $c = $c_iter->() ) {
                if ( $c->commenter_id ) {
                    my $a = MT::Author->load( $c->commenter_id );
                    $a->expire_summary('comment_count');
                    if ( !$a->summary_is_expired('comment_count') ) {
                        $a->set_summary( $terms, 0 );
                        $a->expire_summary('comment_count');
                    }
                }
            }
        }
    }
}

sub summarize_entry_count {
    my $author = shift;
    my ($terms) = @_;

    return MT->model('entry')->count(
        {   author_id => $author->id,
            status    => MT::Entry::RELEASE(),
        },
    );
}

sub expire_entry_count {
    my ( $parent_obj, $obj, $terms, $action, $orig ) = @_;

    # action: save/remove
    # parent_obj => author, obj => the entry
    my $count = $parent_obj->summary($terms);
    if ( !defined $count || $count < 1 && $action eq 'remove' ) {
        $parent_obj->expire_summary($terms);
    }
    elsif ( $action eq 'remove' ) {
        if ( $obj->status == MT::Entry::RELEASE()
            and !$parent_obj->summary_is_expired($terms) )
        {
            $parent_obj->set_summary( $terms, $count - 1 );
        }
    }
    elsif ( $action eq 'save' ) {
        if ( $obj->{changed_cols}->{status}
            && ( ( $orig->{__orig_value}->{status} || 0 ) != $obj->status ) )
        {
            if ( ( $obj->status || 0 ) == MT::Entry::RELEASE()
                && !$parent_obj->summary_is_expired($terms) )
            {
                $parent_obj->set_summary( $terms, $count + 1 );
            }
            elsif ( ( $orig->{__orig_value}->{status} || 0 )
                == MT::Entry::RELEASE() )
            {
                my $orig_author;
                if ( $orig->{__orig_value}->{author_id} ) {
                    $orig_author = MT::Author->load(
                        $orig->{__orig_value}->{author_id} );
                }
                else {
                    $orig_author = $parent_obj;
                }
                if ( $orig_author
                    and !$orig_author->summary_is_expired($terms) )
                {
                    my $orig_count = $orig_author->summary($terms);
                    if ( !defined $orig_count || $orig_count < 1 ) {
                        $orig_author->expire_summary($terms);
                    }
                    else {
                        $orig_author->set_summary( $terms, $orig_count - 1 );
                    }
                }
            }
        }
        if (    $obj->status == MT::Entry::RELEASE()
            and $obj->{changed_cols}->{author_id}
            and !$obj->{changed_cols}->{id}
            and $orig->{__orig_value}->{author_id} )
        {
            my $orig_author
                = MT::Author->load( $orig->{__orig_value}->{author_id} );
            if ( $orig_author and !$orig_author->summary_is_expired($terms) )
            {
                my $orig_count = $orig_author->summary($terms);
                if ( !defined $orig_count || $orig_count < 1 ) {
                    $orig_author->expire_summary($terms);
                }
                else {
                    $orig_author->set_summary( $terms, $orig_count - 1 );
                }
            }
            if ( !$parent_obj->summary_is_expired($terms) ) {
                $parent_obj->set_summary( $terms, $count + 1 );
            }
        }
    }
    else {
        die "Incorrect action type '$action'; expected save/remove\n";
    }
}

# ============= tags ===============

###########################################################################

=head2 AuthorCommentCount

Returns number of comments written by the author specified by current context.

=for tags authors comment

=cut

sub _hdlr_author_comment_count {
    my ( $ctx, $args, $cond ) = @_;
    my $author = $ctx->stash('author')
        or return $ctx->_no_author_error('MTAuthorCommentCount');

    return $ctx->count_format( $author->summarize('comment_count'), $args );
}

###########################################################################

=head2 AuthorEntriesCount

Returns number of entries written by the author who specified by current context.

=for tags authors entries

=cut

sub _hdlr_author_entries_count {
    my ( $ctx, $args, $cond ) = @_;
    my $author = $ctx->stash('author')
        or return $ctx->_no_author_error('MTAuthorEntryCount');

    return $ctx->count_format( $author->summarize('entry_count'), $args );
}

1;