ObjectCustomFieldValues.pm 4.18 KB
Newer Older
1
# BEGIN BPS TAGGED BLOCK {{{
Jesse Vincent's avatar
Jesse Vincent committed
2
#
Jesse Vincent's avatar
Jesse Vincent committed
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
#
Jesse Vincent's avatar
Jesse Vincent committed
8
# (Except where explicitly superseded by other copyright notices)
Jesse Vincent's avatar
Jesse Vincent committed
9
10
#
#
Jesse Vincent's avatar
Jesse Vincent committed
11
# LICENSE:
Jesse Vincent's avatar
Jesse Vincent committed
12
#
13
14
15
16
# 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
# 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
#
Jesse Vincent's avatar
Jesse Vincent committed
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
#
#
Jesse Vincent's avatar
Jesse Vincent committed
30
# CONTRIBUTION SUBMISSION POLICY:
Jesse Vincent's avatar
Jesse Vincent committed
31
#
Jesse Vincent's avatar
Jesse Vincent committed
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
#
Jesse Vincent's avatar
Jesse Vincent committed
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

49
50
package RT::ObjectCustomFieldValues;

51
use strict;
52
53
use warnings;

54
use base 'RT::SearchBuilder';
55

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use RT::ObjectCustomFieldValue;

sub Table { 'ObjectCustomFieldValues'}

sub _Init {
    my $self = shift;

  # By default, order by SortOrder
  $self->OrderByCols(
	 { ALIAS => 'main',
	   FIELD => 'SortOrder',
	   ORDER => 'ASC' },
	 { ALIAS => 'main',
	   FIELD => 'id',
	   ORDER => 'ASC' },
     );

    return ( $self->SUPER::_Init(@_) );
}


# {{{ sub LimitToCustomField
78
79
80
81
82
83
84
85
86
87

=head2 LimitToCustomField FIELD

Limits the returned set to values for the custom field with Id FIELD

=cut
  
sub LimitToCustomField {
    my $self = shift;
    my $cf = shift;
Ruslan Zakirov's avatar
minor    
Ruslan Zakirov committed
88
89
90
91
    return $self->Limit(
        FIELD => 'CustomField',
        VALUE => $cf,
    );
92
93
94
95
}



96
=head2 LimitToObject OBJECT
97

98
Limits the returned set to values for the given OBJECT
99
100
101

=cut

102
103
104
sub LimitToObject {
    my $self = shift;
    my $object = shift;
Ruslan Zakirov's avatar
minor    
Ruslan Zakirov committed
105
106
107
108
109
110
111
112
    $self->Limit(
        FIELD => 'ObjectType',
        VALUE => ref($object),
    );
    return $self->Limit(
        FIELD => 'ObjectId',
        VALUE => $object->Id,
    );
113
114
115

}

116

Ruslan Zakirov's avatar
fix pod    
Ruslan Zakirov committed
117
=head2 HasEntry VALUE
118

119
120
If this collection has an entry with content that eq VALUE then
returns the entry, otherwise returns undef.
121
122
123
124
125
126
127

=cut


sub HasEntry {
    my $self = shift;
    my $value = shift;
128
    return undef unless defined $value && length $value;
129
130

    #TODO: this could cache and optimize a fair bit.
Ruslan Zakirov's avatar
minor    
Ruslan Zakirov committed
131
    foreach my $item ( @{$self->ItemsArrayRef} ) {
132
        return $item if lc $item->Content eq lc $value;
133
134
135
136
    }
    return undef;
}

137
138
139
sub _DoSearch {
    my $self = shift;
    
Ruslan Zakirov's avatar
minor    
Ruslan Zakirov committed
140
    # unless we really want to find disabled rows,
141
    # make sure we're only finding enabled ones.
Ruslan Zakirov's avatar
minor    
Ruslan Zakirov committed
142
    unless ( $self->{'find_expired_rows'} ) {
143
        $self->LimitToEnabled();
144
145
    }
    
Ruslan Zakirov's avatar
minor    
Ruslan Zakirov committed
146
    return $self->SUPER::_DoSearch(@_);
147
148
}

Linda Julien's avatar
Linda Julien committed
149
150
151
sub _DoCount {
    my $self = shift;
    
Ruslan Zakirov's avatar
minor    
Ruslan Zakirov committed
152
    # unless we really want to find disabled rows,
153
    # make sure we're only finding enabled ones.
Ruslan Zakirov's avatar
minor    
Ruslan Zakirov committed
154
    unless ( $self->{'find_expired_rows'} ) {
155
        $self->LimitToEnabled();
Linda Julien's avatar
Linda Julien committed
156
157
    }
    
Ruslan Zakirov's avatar
minor    
Ruslan Zakirov committed
158
    return $self->SUPER::_DoCount(@_);
Linda Julien's avatar
Linda Julien committed
159
160
}

161

162
163
164
165
166
167
168
169
170
171
172
173
174
=head2 NewItem

Returns an empty new RT::ObjectCustomFieldValue item

=cut

sub NewItem {
    my $self = shift;
    return(RT::ObjectCustomFieldValue->new($self->CurrentUser));
}
RT::Base->_ImportOverlays();

1;