Attachments.pm 5.96 KB
Newer Older
1
# BEGIN BPS TAGGED BLOCK {{{
Jesse Vincent's avatar
Jesse Vincent committed
2
#
3
# COPYRIGHT:
Jesse Vincent's avatar
Jesse Vincent committed
4
#
5
# This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
Kevin Falcone's avatar
Kevin Falcone committed
6
#                                          <sales@bestpractical.com>
Jesse Vincent's avatar
Jesse Vincent committed
7
#
8
# (Except where explicitly superseded by other copyright notices)
Jesse Vincent's avatar
Jesse Vincent committed
9
10
#
#
11
# LICENSE:
Jesse Vincent's avatar
Jesse Vincent committed
12
#
13
14
15
# This work is made available to you under the terms of Version 2 of
# the GNU General Public License. A copy of that license should have
# been provided with this software, but in any event can be snarfed
16
# from www.gnu.org.
Jesse Vincent's avatar
Jesse Vincent committed
17
#
18
19
20
21
# This work is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
Jesse Vincent's avatar
Jesse Vincent committed
22
#
23
24
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
Jesse Vincent's avatar
Jesse Vincent committed
25
26
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 or visit their web page on the internet at
Ruslan Zakirov's avatar
Ruslan Zakirov committed
27
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
Jesse Vincent's avatar
Jesse Vincent committed
28
29
#
#
30
# CONTRIBUTION SUBMISSION POLICY:
Jesse Vincent's avatar
Jesse Vincent committed
31
#
32
33
34
35
36
# (The following paragraph is not intended to limit the rights granted
# to you to modify and distribute this software under the terms of
# the GNU General Public License and is only of importance to you if
# you choose to contribute your changes and enhancements to the
# community by submitting them to Best Practical Solutions, LLC.)
Jesse Vincent's avatar
Jesse Vincent committed
37
#
38
39
40
41
42
43
44
45
# By intentionally submitting any modifications, corrections or
# derivatives to this work, or any other work intended for use with
# Request Tracker, to Best Practical Solutions, LLC, you confirm that
# you are the copyright holder for those contributions and you grant
# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
# royalty-free, perpetual, license to use, copy, create derivative
# works based on those contributions, and sublicense and distribute
# those contributions and any derivatives thereof.
Jesse Vincent's avatar
Jesse Vincent committed
46
#
47
# END BPS TAGGED BLOCK }}}
48

Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
=head1 NAME

  RT::Attachments - a collection of RT::Attachment objects

=head1 SYNOPSIS

  use RT::Attachments;

=head1 DESCRIPTION

This module should never be called directly by client code. it's an internal module which
should only be accessed through exported APIs in Ticket, Queue and other similar objects.


=head1 METHODS



=cut

69
70

package RT::Attachments;
71
use strict;
72
use warnings;
73

74
use base 'RT::SearchBuilder';
75
76
77
78

use RT::Attachment;

sub Table { 'Attachments'}
79

Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
80

Ruslan Zakirov's avatar
Ruslan Zakirov committed
81
82
use RT::Attachment;

Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
83
sub _Init   {
Ruslan Zakirov's avatar
Ruslan Zakirov committed
84
85
86
87
88
89
90
91
    my $self = shift;
    $self->{'table'} = "Attachments";
    $self->{'primary_key'} = "id";
    $self->OrderBy(
        FIELD => 'id',
        ORDER => 'ASC',
    );
    return $self->SUPER::_Init( @_ );
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
92
93
}

94
95
96
97
98
sub CleanSlate {
    my $self = shift;
    delete $self->{_sql_transaction_alias};
    return $self->SUPER::CleanSlate( @_ );
}
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
99
100


101
=head2 TransactionAlias
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
102

103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
Returns alias for transactions table with applied join condition.
Always return the same alias, so if you want to build some complex
or recursive joining then you have to create new alias youself.

=cut

sub TransactionAlias {
    my $self = shift;
    return $self->{'_sql_transaction_alias'}
        if $self->{'_sql_transaction_alias'};

    my $res = $self->NewAlias('Transactions');
    $self->Limit(
        ENTRYAGGREGATOR => 'AND',
        FIELD           => 'TransactionId',
        VALUE           => $res . '.id',
        QUOTEVALUE      => 0,
    );
    return $self->{'_sql_transaction_alias'} = $res;
}
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
123
124
125
126
127
128
129
130
131

=head2 ContentType (VALUE => 'text/plain', ENTRYAGGREGATOR => 'OR', OPERATOR => '=' ) 

Limit result set to attachments of ContentType 'TYPE'...

=cut


sub ContentType  {
132
133
134
135
136
137
138
    my $self = shift;
    my %args = (
        VALUE           => 'text/plain',
	    OPERATOR        => '=',
	    ENTRYAGGREGATOR => 'OR',
	    @_
    );
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
139

140
    return $self->Limit ( %args, FIELD => 'ContentType' );
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
141
142
143
144
145
146
147
148
149
150
}

=head2 ChildrenOf ID

Limit result set to children of Attachment ID

=cut


sub ChildrenOf  {
Ruslan Zakirov's avatar
Ruslan Zakirov committed
151
152
153
154
155
156
    my $self = shift;
    my $attachment = shift;
    return $self->Limit(
        FIELD => 'Parent',
        VALUE => $attachment
    );
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
157
}
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

=head2 LimitNotEmpty

Limit result set to attachments with not empty content.

=cut

sub LimitNotEmpty {
    my $self = shift;
    $self->Limit(
        ENTRYAGGREGATOR => 'AND',
        FIELD           => 'Content',
        OPERATOR        => 'IS NOT',
        VALUE           => 'NULL',
        QUOTEVALUE      => 0,
    );
174
175

    # http://rt3.fsck.com/Ticket/Display.html?id=12483
176
177
178
179
180
181
182
183
    if ( RT->Config->Get('DatabaseType') ne 'Oracle' ) {
        $self->Limit(
            ENTRYAGGREGATOR => 'AND',
            FIELD           => 'Content',
            OPERATOR        => '!=',
            VALUE           => '',
        );
    }
184
185
    return;
}
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
186

Ruslan Zakirov's avatar
Ruslan Zakirov committed
187
188
189
190
191
192
193
194
195
=head2 LimitByTicket $ticket_id

Limit result set to attachments of a ticket.

=cut

sub LimitByTicket {
    my $self = shift;
    my $tid = shift;
196
197

    my $transactions = $self->TransactionAlias;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
    $self->Limit(
        ENTRYAGGREGATOR => 'AND',
        ALIAS           => $transactions,
        FIELD           => 'ObjectType',
        VALUE           => 'RT::Ticket',
    );

    my $tickets = $self->NewAlias('Tickets');
    $self->Limit(
        ENTRYAGGREGATOR => 'AND',
        ALIAS           => $tickets,
        FIELD           => 'id',
        VALUE           => $transactions . '.ObjectId',
        QUOTEVALUE      => 0,
    );
    $self->Limit(
        ENTRYAGGREGATOR => 'AND',
        ALIAS           => $tickets,
        FIELD           => 'EffectiveId',
        VALUE           => $tid,
    );
    return;
}
Jesse Vincent's avatar
rt.2.1  
Jesse Vincent committed
221

222
# {{{ sub Next
223
224
sub Next {
    my $self = shift;
Ruslan Zakirov's avatar
Ruslan Zakirov committed
225
226
227
228

    my $Attachment = $self->SUPER::Next;
    return $Attachment unless $Attachment;

229
    if ( $Attachment->TransactionObj->CurrentUserCanSee ) {
Ruslan Zakirov's avatar
Ruslan Zakirov committed
230
        return $Attachment;
231
232
233
    } else {
        # If the user doesn't have the right to show this ticket
        return $self->Next;
234
235
236
    }
}

237
238
239
240
241
242
243
244
245
246
247

=head2 NewItem

Returns an empty new RT::Attachment item

=cut

sub NewItem {
    my $self = shift;
    return(RT::Attachment->new($self->CurrentUser));
}
248

249
250
RT::Base->_ImportOverlays();

Ruslan Zakirov's avatar
Ruslan Zakirov committed
251
1;