import rt 3.4.6
[freeside.git] / rt / html / Admin / Elements / EditCustomFields
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %# 
3 %# COPYRIGHT:
4 %#  
5 %# This software is Copyright (c) 1996-2005 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., 675 Mass Ave, Cambridge, MA 02139, USA.
26 %# 
27 %# 
28 %# CONTRIBUTION SUBMISSION POLICY:
29 %# 
30 %# (The following paragraph is not intended to limit the rights granted
31 %# to you to modify and distribute this software under the terms of
32 %# the GNU General Public License and is only of importance to you if
33 %# you choose to contribute your changes and enhancements to the
34 %# community by submitting them to Best Practical Solutions, LLC.)
35 %# 
36 %# By intentionally submitting any modifications, corrections or
37 %# derivatives to this work, or any other work intended for use with
38 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
39 %# you are the copyright holder for those contributions and you grant
40 %# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
41 %# royalty-free, perpetual, license to use, copy, create derivative
42 %# works based on those contributions, and sublicense and distribute
43 %# those contributions and any derivatives thereof.
44 %# 
45 %# END BPS TAGGED BLOCK }}}
46 <& /Elements/ListActions, actions => \@results &>
47
48 <FORM ACTION="<%$RT::WebPath%><% $m->request_comp->path |n %>" METHOD="POST" NAME="EditCustomFields">
49 <INPUT TYPE=HIDDEN NAME="id" VALUE="<% $Object->Id %>"/>
50 <INPUT TYPE=HIDDEN NAME="ObjectType" VALUE="<% $ObjectType %>"/>
51 <INPUT TYPE=HIDDEN NAME="SubType" VALUE="<% $SubType %>"/>
52 <INPUT TYPE=HIDDEN NAME="UpdateCFs" VALUE="1"/>
53
54 % if ($Object->Id) {
55 <h2><&|/l&>Global Custom Fields</&></h2>
56 <& PickCustomFields, CustomFields => \@GlobalCFs, ReadOnly => 1, id => $id, SubType => $SubType &>
57 % }
58 <h2><&|/l&>Selected Custom Fields</&></h2>
59 <& PickCustomFields, CustomFields => [$ObjectCFs->CustomFields], id => $id, Checked => 1, SubType => $SubType &>
60 <h2><&|/l&>Unselected Custom Fields</&></h2>
61 <& PickCustomFields, CustomFields => \@UnassignedCFs, id => $id, SubType => $SubType &>
62
63 <& /Elements/Submit, CheckAll => 1, ClearAll => 1 &>
64 </FORM>
65
66
67 <%INIT>
68 my $CustomFields = RT::CustomFields->new($session{'CurrentUser'});
69 my @results;
70 my (@GlobalCFs, @UnassignedCFs);
71
72 my $id = $Object->Id;
73 if ($id and !$Object->CurrentUserHasRight('AssignCustomFields')) {
74     $m->out('<P><i>', loc('(No custom fields)'), '</i></P>');
75     return;
76 }
77
78 my $lookup = $ObjectType;
79 $lookup .= "-$SubType" if $SubType;
80
81 $CustomFields->LimitToLookupType($lookup);
82 $CustomFields->OrderBy( FIELD => 'Name' );
83
84
85 my ($GlobalCFs, $ObjectCFs);
86 $ObjectCFs = RT::ObjectCustomFields->new($session{'CurrentUser'});
87 $ObjectCFs->UnLimit;
88 $ObjectCFs->LimitToObjectId($id);
89 $ObjectCFs->LimitToLookupType($lookup);
90
91 # {{{ deal with moving sortorder of custom fields
92 if ($CustomField and $Move) {
93     my $SourceObj = RT::ObjectCustomField->new($session{'CurrentUser'});
94     $SourceObj->LoadByCols( ObjectId => $id, CustomField => $CustomField );
95
96     my $TargetObj;
97     my $target_order = $SourceObj->SortOrder + $Move;
98     while (my $ObjectCF = $ObjectCFs->Next) { 
99         my $this_order = $ObjectCF->SortOrder;
100
101         # if we have an exact match, finish the loop now
102         ($TargetObj = $ObjectCF, last) if $this_order == $target_order;
103
104         # otherwise, we need to apropos toward the general direction
105         # ... first, check the sign is correct
106         next unless ($this_order - $SourceObj->SortOrder) * $Move > 0;
107
108         # ... next, see if we already have a candidate
109         if ($TargetObj) {
110             # ... if yes, compare the delta and choose the smaller one
111             my $orig_delta = abs($TargetObj->SortOrder - $target_order);
112             my $this_delta = abs($this_order - $target_order);
113             next if $orig_delta < $this_delta;
114         }
115
116         $TargetObj = $ObjectCF;
117     }
118
119     if ($TargetObj) {
120         # swap their sort order
121         my ($s, $t) = ($SourceObj->SortOrder, $TargetObj->SortOrder);
122         $TargetObj->SetSortOrder($s);
123         $SourceObj->SetSortOrder($t);
124         # because order changed, we must redo search for subsequent uses
125     }
126
127     $ObjectCFs->GotoFirstItem;
128 }
129 # }}}
130
131 if ($id) {
132     $GlobalCFs = RT::ObjectCustomFields->new($session{'CurrentUser'});
133     $GlobalCFs->LimitToObjectId(0);
134     $GlobalCFs->LimitToLookupType($lookup);
135 }
136
137 while (my $cf = $CustomFields->Next) { 
138     my $cf_id = $cf->Id;
139
140     if ($GlobalCFs and $GlobalCFs->HasEntryForCustomField($cf_id)) {
141         push @GlobalCFs, $cf;
142         next;
143     }
144
145     if ($UpdateCFs) {
146         # Go through and delete all the custom field relationships that this object
147         # no longer has
148         my $key = "Object-$id-CF-$cf_id";
149         if ($ARGS{$key}) {
150             if (!$ObjectCFs->HasEntryForCustomField($cf_id)) {
151                 my ($val, $msg) = $cf->AddToObject($Object);
152                 push (@results, $msg);
153                 push @UnassignedCFs, $cf if !$val;
154             }
155         }
156         else {
157             push @UnassignedCFs, $cf;
158             if ($ObjectCFs->HasEntryForCustomField($cf_id)) {
159                 my ($val, $msg) = $cf->RemoveFromObject($Object);
160                 push (@results, $msg);
161                 pop @UnassignedCFs if !$val;
162             }
163         }
164     }
165     elsif (!$ObjectCFs->HasEntryForCustomField($cf_id)) {
166         push @UnassignedCFs, $cf;
167     }
168         else {
169         }
170 }
171
172 # redo search...
173 $ObjectCFs = RT::ObjectCustomFields->new($session{'CurrentUser'});
174 $ObjectCFs->UnLimit;
175 $ObjectCFs->LimitToObjectId($id);
176 $ObjectCFs->LimitToLookupType($lookup);
177
178 </%INIT>
179 <%ARGS>
180 $title => undef
181 $Move => undef
182 $Source => undef
183 $CustomField => undef
184 $FindDisabledCustomFields => undef
185 $UpdateCFs => 0
186 $Object
187 $ObjectType
188 $SubType => ''
189 </%ARGS>