Attributes.pm 5.34 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
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
#
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

49
50
=head1 NAME

Ruslan Zakirov's avatar
Ruslan Zakirov committed
51
RT::Attributes - collection of RT::Attribute objects
52
53
54

=head1 SYNOPSIS

Ruslan Zakirov's avatar
Ruslan Zakirov committed
55
    use RT::Attributes;
56
    my $Attributes = RT::Attributes->new($CurrentUser);
57
58
59
60
61

=head1 DESCRIPTION


=head1 METHODS
62

63
64
=cut

65
66
67

package RT::Attributes;

68
use strict;
69
70
use warnings;

71
use base 'RT::SearchBuilder';
72

73
74
75
76
use RT::Attribute;

sub Table { 'Attributes'}

77

Jesse Vincent's avatar
Jesse Vincent committed
78
79
80
sub _DoSearch {
    my $self = shift;
    $self->SUPER::_DoSearch();
81
82
83
84
85
86
87
88
89
90
# if _DoSearch doesn't fully succeed, 'must_redo_search' will be true
# and call _BuildAccessTable then will result in a deep recursion
    if ( $self->{'must_redo_search'} ) {
        $RT::Logger->crit(
"_DoSearch is not so successful as it still needs redo search, won't call _BuildAccessTable"
        );
    }
    else {
        $self->_BuildAccessTable();
    }
Jesse Vincent's avatar
Jesse Vincent committed
91
92
93
94
95
}


sub _BuildAccessTable {
    my $self = shift;
96
    delete $self->{'attr'};
Jesse Vincent's avatar
Jesse Vincent committed
97
98
99
100
101
102
103
104
    while (my $attr = $self->Next) {
        push @{$self->{'attr'}->{$attr->Name}}, $attr;
    }
}


sub _AttrHash {
    my $self = shift;
105
    $self->_DoSearch if ($self->{'must_redo_search'} && $self->{'is_limited'});
106
107
108
    unless ($self->{'attr'}) {
        $self->{'attr'}->{'__none'} = RT::Attribute->new($self->CurrentUser);
    }
Jesse Vincent's avatar
Jesse Vincent committed
109
110
111
112
113
114
115
116
117
118
119
    return ($self->{'attr'});
}

=head2 Names

Returns a list of the Names of all attributes for this object. 

=cut

sub Names {
    my $self = shift;
120
121
122
123
    my @keys =  keys %{$self->_AttrHash};
    return(@keys);


Jesse Vincent's avatar
Jesse Vincent committed
124
}
125

Jesse Vincent's avatar
Jesse Vincent committed
126
127
128
=head2 Named STRING

Returns an array of all the RT::Attribute objects with the name STRING
129
130
131

=cut

Jesse Vincent's avatar
Jesse Vincent committed
132
133
134
sub Named {
    my $self = shift;
    my $name = shift;
135
136
137
138
139
    my @attributes; 
    if ($self->_AttrHash) {
        @attributes = @{($self->_AttrHash->{$name}||[])};
    }
    return (@attributes);   
Jesse Vincent's avatar
Jesse Vincent committed
140
141
}

142
143
=head2 DeleteEntry { Name =>   Content => , id => }

144
145
Deletes attributes with
    the matching name 
146
 and the matching content or id
147
148
149
150

If Content and id are both undefined, delete all attributes with
the matching name.

151
152
153
154
155
=cut


sub DeleteEntry {
    my $self = shift;
Ruslan Zakirov's avatar
indent    
Ruslan Zakirov committed
156
157
158
159
160
161
    my %args = (
        Name    => undef,
        Content => undef,
        id      => undef,
        @_
    );
162
    my $found = 0;
Ruslan Zakirov's avatar
indent    
Ruslan Zakirov committed
163
164
165
166
167
168
169
170
171
    foreach my $attr ( $self->Named( $args{'Name'} ) ) {
        if ( ( !defined $args{'id'} and !defined $args{'Content'} )
             or ( defined $args{'id'} and $attr->id eq $args{'id'} )
             or ( defined $args{'Content'} and $attr->Content eq $args{'Content'} ) )
        {
            my ($id, $msg) = $attr->Delete;
            return ($id, $msg) unless $id;
            $found = 1;
        }
172
    }
173
    return (0, "No entry found") unless $found;
174
    $self->RedoSearch;
175
176
177
    # XXX: above string must work but because of bug in DBIx::SB it doesn't,
    # to reproduce delete string below and run t/api/attribute-tests.t
    $self->_DoSearch;
178
179
    return (1, $self->loc('Attribute Deleted'));
}
Jesse Vincent's avatar
Jesse Vincent committed
180

181
182
183
184
185
186
187
188
189
190
191


=head2 LimitToObject $object

Limit the Attributes to rights for the object $object. It needs to be an RT::Record class.

=cut

sub LimitToObject {
    my $self = shift;
    my $obj = shift;
192
193
    unless (eval { $obj->id} ){
        return undef;
194
195
196
197
198
199
200
    }
    $self->Limit(FIELD => 'ObjectType', OPERATOR=> '=', VALUE => ref($obj), ENTRYAGGREGATOR => 'OR');
    $self->Limit(FIELD => 'ObjectId', OPERATOR=> '=', VALUE => $obj->id, ENTRYAGGREGATOR => 'OR', QUOTEVALUE => 0);

}


201
202
203
204
205
206
207
208
209
210
211
212
213

=head2 NewItem

Returns an empty new RT::Attribute item

=cut

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

214
1;