rt 4.2.15
[freeside.git] / rt / lib / RT / Attachments.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2 #
3 # COPYRIGHT:
4 #
5 # This software is Copyright (c) 1996-2018 Best Practical Solutions, LLC
6 #                                          <sales@bestpractical.com>
7 #
8 # (Except where explicitly superseded by other copyright notices)
9 #
10 #
11 # LICENSE:
12 #
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
16 # from www.gnu.org.
17 #
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 # General Public License for more details.
22 #
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 # 02110-1301 or visit their web page on the internet at
27 # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 #
29 #
30 # CONTRIBUTION SUBMISSION POLICY:
31 #
32 # (The following paragraph is not intended to limit the rights granted
33 # to you to modify and distribute this software under the terms of
34 # the GNU General Public License and is only of importance to you if
35 # you choose to contribute your changes and enhancements to the
36 # community by submitting them to Best Practical Solutions, LLC.)
37 #
38 # By intentionally submitting any modifications, corrections or
39 # derivatives to this work, or any other work intended for use with
40 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 # you are the copyright holder for those contributions and you grant
42 # Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 # royalty-free, perpetual, license to use, copy, create derivative
44 # works based on those contributions, and sublicense and distribute
45 # those contributions and any derivatives thereof.
46 #
47 # END BPS TAGGED BLOCK }}}
48
49 =head1 NAME
50
51   RT::Attachments - a collection of RT::Attachment objects
52
53 =head1 SYNOPSIS
54
55   use RT::Attachments;
56
57 =head1 DESCRIPTION
58
59 This module should never be called directly by client code. it's an internal module which
60 should only be accessed through exported APIs in Ticket, Queue and other similar objects.
61
62
63 =head1 METHODS
64
65
66
67 =cut
68
69
70 package RT::Attachments;
71 use strict;
72 use warnings;
73
74 use base 'RT::SearchBuilder';
75
76 use RT::Attachment;
77
78 sub Table { 'Attachments'}
79
80
81 use RT::Attachment;
82
83 sub _Init   {
84     my $self = shift;
85     $self->{'table'} = "Attachments";
86     $self->{'primary_key'} = "id";
87     $self->OrderBy(
88         FIELD => 'id',
89         ORDER => 'ASC',
90     );
91     return $self->SUPER::_Init( @_ );
92 }
93
94 sub CleanSlate {
95     my $self = shift;
96     delete $self->{_sql_transaction_alias};
97     return $self->SUPER::CleanSlate( @_ );
98 }
99
100
101 =head2 TransactionAlias
102
103 Returns alias for transactions table with applied join condition.
104 Always return the same alias, so if you want to build some complex
105 or recursive joining then you have to create new alias youself.
106
107 =cut
108
109 sub TransactionAlias {
110     my $self = shift;
111     return $self->{'_sql_transaction_alias'}
112         if $self->{'_sql_transaction_alias'};
113
114     return $self->{'_sql_transaction_alias'} = $self->Join(
115         ALIAS1 => 'main',
116         FIELD1 => 'TransactionId',
117         TABLE2 => 'Transactions',
118         FIELD2 => 'id',
119     );
120 }
121
122 =head2 ContentType (VALUE => 'text/plain', ENTRYAGGREGATOR => 'OR', OPERATOR => '=' ) 
123
124 Limit result set to attachments of ContentType 'TYPE'...
125
126 =cut
127
128
129 sub ContentType  {
130     my $self = shift;
131     my %args = (
132         VALUE           => 'text/plain',
133         OPERATOR        => '=',
134         ENTRYAGGREGATOR => 'OR',
135         @_
136     );
137
138     return $self->Limit ( %args, FIELD => 'ContentType' );
139 }
140
141 =head2 ChildrenOf ID
142
143 Limit result set to children of Attachment ID
144
145 =cut
146
147
148 sub ChildrenOf  {
149     my $self = shift;
150     my $attachment = shift;
151     return $self->Limit(
152         FIELD => 'Parent',
153         VALUE => $attachment
154     );
155 }
156
157 =head2 LimitNotEmpty
158
159 Limit result set to attachments with not empty content.
160
161 =cut
162
163 sub LimitNotEmpty {
164     my $self = shift;
165     $self->Limit(
166         ENTRYAGGREGATOR => 'AND',
167         FIELD           => 'Content',
168         OPERATOR        => 'IS NOT',
169         VALUE           => 'NULL',
170         QUOTEVALUE      => 0,
171     );
172
173     # http://rt3.fsck.com/Ticket/Display.html?id=12483
174     if ( RT->Config->Get('DatabaseType') ne 'Oracle' ) {
175         $self->Limit(
176             ENTRYAGGREGATOR => 'AND',
177             FIELD           => 'Content',
178             OPERATOR        => '!=',
179             VALUE           => '',
180         );
181     }
182     return;
183 }
184
185 =head2 LimitByTicket $ticket_id
186
187 Limit result set to attachments of a ticket.
188
189 =cut
190
191 sub LimitByTicket {
192     my $self = shift;
193     my $tid = shift;
194
195     my $transactions = $self->TransactionAlias;
196     $self->Limit(
197         ENTRYAGGREGATOR => 'AND',
198         ALIAS           => $transactions,
199         FIELD           => 'ObjectType',
200         VALUE           => 'RT::Ticket',
201     );
202
203     my $tickets = $self->Join(
204         ALIAS1 => $transactions,
205         FIELD1 => 'ObjectId',
206         TABLE2 => 'Tickets',
207         FIELD2 => 'id',
208     );
209     $self->Limit(
210         ENTRYAGGREGATOR => 'AND',
211         ALIAS           => $tickets,
212         FIELD           => 'EffectiveId',
213         VALUE           => $tid,
214     );
215     return;
216 }
217
218 sub AddRecord {
219     my $self = shift;
220     my ($record) = @_;
221
222     return unless $record->TransactionObj->CurrentUserCanSee;
223     return $self->SUPER::AddRecord( $record );
224 }
225
226 RT::Base->_ImportOverlays();
227
228 1;