1 %# BEGIN BPS TAGGED BLOCK {{{
5 %# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
6 %# <jesse@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 }}}
48 <& /Elements/ListActions, actions => \@results &>
50 <form action="<%$RT::WebPath%><% $m->request_comp->path |n %>" method="post" name="EditCustomFields">
51 <input type="hidden" class="hidden" name="id" value="<% $Object->Id %>" />
52 <input type="hidden" class="hidden" name="ObjectType" value="<% $ObjectType %>" />
53 <input type="hidden" class="hidden" name="SubType" value="<% $SubType %>" />
54 <input type="hidden" class="hidden" name="UpdateCFs" value="1" />
57 <h2><&|/l&>Global Custom Fields</&></h2>
58 <& PickCustomFields, CustomFields => \@GlobalCFs, ReadOnly => 1, id => $id, SubType => $SubType &>
60 <h2><&|/l&>Selected Custom Fields</&></h2>
61 <& PickCustomFields, CustomFields => [$ObjectCFs->CustomFields], id => $id, Checked => 1, SubType => $SubType &>
62 <h2><&|/l&>Unselected Custom Fields</&></h2>
63 <& PickCustomFields, CustomFields => \@UnassignedCFs, id => $id, SubType => $SubType &>
65 <& /Elements/Submit, CheckAll => 1, ClearAll => 1 &>
70 my $CustomFields = RT::CustomFields->new($session{'CurrentUser'});
72 my (@GlobalCFs, @UnassignedCFs);
75 if ($id and !$Object->CurrentUserHasRight('AssignCustomFields')) {
76 $m->out('<p><i>', loc('(No custom fields)'), '</i></p>');
80 my $lookup = $ObjectType;
81 $lookup .= "-$SubType" if $SubType;
83 $CustomFields->LimitToLookupType($lookup);
84 $CustomFields->OrderBy( FIELD => 'Name' );
87 my ($GlobalCFs, $ObjectCFs);
88 $ObjectCFs = RT::ObjectCustomFields->new($session{'CurrentUser'});
90 $ObjectCFs->LimitToObjectId($id);
91 $ObjectCFs->LimitToLookupType($lookup);
93 # Check sanity of SortOrders
95 $SortOrders{$_->SortOrder}++
96 while ($_ = $ObjectCFs->Next);
98 # If there are duplicates, run though and squash them
99 if (grep {$_ > 1} values %SortOrders) {
101 while (my $ObjectCF = $ObjectCFs->Next) {
102 $ObjectCF->SetSortOrder($i++);
104 $ObjectCFs->GotoFirstItem;
107 # {{{ deal with moving sortorder of custom fields
108 if ($CustomField and $Move) {
109 my $SourceObj = RT::ObjectCustomField->new($session{'CurrentUser'});
110 $SourceObj->LoadByCols( ObjectId => $id, CustomField => $CustomField );
113 my $target_order = $SourceObj->SortOrder + $Move;
114 while (my $ObjectCF = $ObjectCFs->Next) {
115 my $this_order = $ObjectCF->SortOrder;
117 # if we have an exact match, finish the loop now
118 ($TargetObj = $ObjectCF, last) if $this_order == $target_order;
120 # otherwise, we need to apropos toward the general direction
121 # ... first, check the sign is correct
122 next unless ($this_order - $SourceObj->SortOrder) * $Move > 0;
124 # ... next, see if we already have a candidate
126 # ... if yes, compare the delta and choose the smaller one
127 my $orig_delta = abs($TargetObj->SortOrder - $target_order);
128 my $this_delta = abs($this_order - $target_order);
129 next if $orig_delta < $this_delta;
132 $TargetObj = $ObjectCF;
136 # swap their sort order
137 my ($s, $t) = ($SourceObj->SortOrder, $TargetObj->SortOrder);
138 $TargetObj->SetSortOrder($s);
139 $SourceObj->SetSortOrder($t);
140 # because order changed, we must redo search for subsequent uses
143 $ObjectCFs->GotoFirstItem;
148 $GlobalCFs = RT::ObjectCustomFields->new($session{'CurrentUser'});
149 $GlobalCFs->LimitToObjectId(0);
150 $GlobalCFs->LimitToLookupType($lookup);
153 while (my $cf = $CustomFields->Next) {
156 if ($GlobalCFs and $GlobalCFs->HasEntryForCustomField($cf_id)) {
157 push @GlobalCFs, $cf;
162 # Go through and delete all the custom field relationships that this object
164 my $key = "Object-$id-CF-$cf_id";
166 if (!$ObjectCFs->HasEntryForCustomField($cf_id)) {
167 my ($val, $msg) = $cf->AddToObject($Object);
168 push (@results, $msg);
169 push @UnassignedCFs, $cf if !$val;
173 push @UnassignedCFs, $cf;
174 if ($ObjectCFs->HasEntryForCustomField($cf_id)) {
175 my ($val, $msg) = $cf->RemoveFromObject($Object);
176 push (@results, $msg);
177 pop @UnassignedCFs if !$val;
181 elsif (!$ObjectCFs->HasEntryForCustomField($cf_id)) {
182 push @UnassignedCFs, $cf;
189 $ObjectCFs = RT::ObjectCustomFields->new($session{'CurrentUser'});
191 $ObjectCFs->LimitToObjectId($id);
192 $ObjectCFs->LimitToLookupType($lookup);
199 $CustomField => undef
200 $FindDisabledCustomFields => undef