import rt 3.6.6
[freeside.git] / rt / lib / RT / Attributes_Overlay.pm
1 # BEGIN BPS TAGGED BLOCK {{{
2
3 # COPYRIGHT:
4 #  
5 # This software is Copyright (c) 1996-2007 Best Practical Solutions, LLC 
6 #                                          <jesse@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/copyleft/gpl.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 =head1 NAME
49
50   RT::Attributes - collection of RT::Attribute objects
51
52 =head1 SYNOPSIS
53
54   use RT::Attributes;
55 my $Attributes = new RT::Attributes($CurrentUser);
56
57 =head1 DESCRIPTION
58
59
60 =head1 METHODS
61
62 =cut
63
64
65 package RT::Attributes;
66
67 use strict;
68 no warnings qw(redefine);
69
70
71 sub _DoSearch {
72     my $self = shift;
73     $self->SUPER::_DoSearch();
74     $self->_BuildAccessTable();
75 }
76
77
78 sub _BuildAccessTable {
79     my $self = shift;
80     delete $self->{'attr'};
81     while (my $attr = $self->Next) {
82         push @{$self->{'attr'}->{$attr->Name}}, $attr;
83     }
84 }
85
86
87 sub _AttrHash {
88     my $self = shift;
89     $self->_DoSearch if ($self->{'must_redo_search'});
90     unless ($self->{'attr'}) {
91         $self->{'attr'}->{'__none'} = RT::Attribute->new($self->CurrentUser);
92     }
93     return ($self->{'attr'});
94 }
95
96 =head2 Names
97
98 Returns a list of the Names of all attributes for this object. 
99
100 =cut
101
102 sub Names {
103     my $self = shift;
104     my @keys =  keys %{$self->_AttrHash};
105     return(@keys);
106
107
108 }
109
110 =head2 Named STRING
111
112 Returns an array of all the RT::Attribute objects with the name STRING
113
114 =cut
115
116 sub Named {
117     my $self = shift;
118     my $name = shift;
119     my @attributes; 
120     if ($self->_AttrHash) {
121         @attributes = @{($self->_AttrHash->{$name}||[])};
122     }
123     return (@attributes);   
124 }
125
126 =head2 WithId ID
127
128 Returns the RT::Attribute objects with the id ID
129
130 XXX TODO XXX THIS NEEDS A BETTER ACL CHECK
131
132 =cut
133
134 sub WithId {
135     my $self = shift;
136     my $id = shift;
137
138     my $attr = RT::Attribute->new($self->CurrentUser);
139     $attr->LoadByCols( id => $id );
140     return($attr);
141 }
142
143 =head2 DeleteEntry { Name =>   Content => , id => }
144
145 Deletes attributes with
146     the matching name 
147  and the matching content or id
148
149 If Content and id are both undefined, delete all attributes with
150 the matching name.
151
152 =cut
153
154
155 sub DeleteEntry {
156     my $self = shift;
157     my %args = ( Name => undef,
158                  Content => undef,
159                  id => undef,
160                  @_);
161     my $found = 0;
162     foreach my $attr ($self->Named($args{'Name'})){ 
163       if ((!defined $args{'id'} and !defined $args{'Content'})
164           or (defined $args{'id'} and $attr->id eq $args{'id'})
165           or (defined $args{'Content'} and $attr->Content eq $args{'Content'})) {
166         my ($id, $msg) = $attr->Delete;
167         return ($id, $msg) unless $id;
168         $found = 1;
169       }
170     }
171     return (0, "No entry found") unless $found;
172     $self->_DoSearch();
173     return (1, $self->loc('Attribute Deleted'));
174 }
175
176
177 # {{{ LimitToObject 
178
179 =head2 LimitToObject $object
180
181 Limit the Attributes to rights for the object $object. It needs to be an RT::Record class.
182
183 =cut
184
185 sub LimitToObject {
186     my $self = shift;
187     my $obj = shift;
188     unless (defined($obj) && ref($obj) && UNIVERSAL::can($obj, 'id') && $obj->id) {
189     return undef;
190     }
191     $self->Limit(FIELD => 'ObjectType', OPERATOR=> '=', VALUE => ref($obj), ENTRYAGGREGATOR => 'OR');
192     $self->Limit(FIELD => 'ObjectId', OPERATOR=> '=', VALUE => $obj->id, ENTRYAGGREGATOR => 'OR', QUOTEVALUE => 0);
193
194 }
195
196 # }}}
197
198 1;