1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2017 Best Practical Solutions, LLC
6 # <sales@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
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
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.
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.
30 # CONTRIBUTION SUBMISSION POLICY:
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.)
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.
47 # END BPS TAGGED BLOCK }}}
52 package RT::SearchBuilder::AddAndSort;
53 use base 'RT::SearchBuilder';
57 RT::SearchBuilder::AddAndSort - base class for 'add and sort' collections
61 Base class for collections where records can be added to objects with order.
62 See also L<RT::Record::AddAndSort>. Used by L<RT::ObjectScrips> and
63 L<RT::ObjectCustomFields>.
65 As it's about sorting then collection is sorted by SortOrder field.
74 # By default, order by SortOrder
84 return $self->SUPER::_Init(@_);
87 =head2 LimitToObjectId
89 Takes id of an object and limits collection.
96 $self->Limit( FIELD => 'ObjectId', VALUE => $id );
99 =head1 METHODS FOR TARGETS
101 Rather than implementing a base class for targets (L<RT::Scrip>,
102 L<RT::CustomField>) and its collections. This class provides
103 class methods to limit target collections.
105 =head2 LimitTargetToNotAdded
107 Takes a collection object and optional list of object ids. Limits the
108 collection to records not added to listed objects or if the list is
109 empty then any object. Use 0 (zero) to mean global.
113 sub LimitTargetToNotAdded {
115 my $collection = shift;
118 my $alias = $self->JoinTargetToAdded($collection => @ids);
121 ENTRYAGGREGATOR => 'AND',
130 =head2 LimitTargetToAdded
132 L</LimitTargetToNotAdded> with reverse meaning. Takes the same
137 sub LimitTargetToAdded {
139 my $collection = shift;
142 my $alias = $self->JoinTargetToAdded($collection => @ids);
145 ENTRYAGGREGATOR => 'AND',
148 OPERATOR => 'IS NOT',
154 =head2 JoinTargetToAdded
156 Joins collection to this table using left join, limits joined table
157 by ids if those are provided.
159 Returns alias of the joined table. Join is cached and re-used for
164 sub JoinTargetToAdded {
166 my $collection = shift;
169 my $alias = $self->JoinTargetToThis( $collection, New => 0, Left => 1 );
170 return $alias unless @ids;
172 # XXX: we need different EA in join clause, but DBIx::SB
173 # doesn't support them, use IN (X) instead
174 my $dbh = $self->_Handle->dbh;
186 =head2 JoinTargetToThis
188 Joins target collection to this table using TargetField.
190 Takes New and Left arguments. Use New to avoid caching and re-using
191 this join. Use Left to create LEFT JOIN rather than inner.
195 sub JoinTargetToThis {
197 my $collection = shift;
198 my %args = ( New => 0, Left => 0, Distinct => 0, @_ );
200 my $table = $self->Table;
201 my $key = "_sql_${table}_alias";
203 return $collection->{ $key } if $collection->{ $key } && !$args{'New'};
205 my $alias = $collection->Join(
206 $args{'Left'} ? (TYPE => 'LEFT') : (),
210 FIELD2 => $self->RecordClass->TargetField,
211 DISTINCT => $args{Distinct},
213 return $alias if $args{'New'};
214 return $collection->{ $key } = $alias;
217 RT::Base->_ImportOverlays();