Merge branch 'master' of git.freeside.biz:/home/git/freeside
[freeside.git] / rt / share / html / Admin / Elements / EditCustomFields
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %#
3 %# COPYRIGHT:
4 %#
5 %# This software is Copyright (c) 1996-2015 Best Practical Solutions, LLC
6 %#                                          <sales@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/licenses/old-licenses/gpl-2.0.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 <& /Elements/ListActions, actions => \@results &>
49
50 <form action="<%RT->Config->Get('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
55 <h2><&|/l&>Selected Custom Fields</&></h2>
56 <& /Elements/CollectionList,
57     %ARGS,
58     Collection    => $added_cfs,
59     Rows          => 0,
60     Page          => 1,
61     Format        => $format,
62     DisplayFormat => $display_format,
63     AllowSorting  => 0,
64     ShowEmpty     => 0,
65     PassArguments => [
66         qw(Page Order OrderBy),
67         qw(id ObjectType SubType),
68     ],
69 &>
70
71 <h2><&|/l&>Unselected Custom Fields</&></h2>
72 <& /Elements/CollectionList,
73     OrderBy       => 'Name',
74     Order         => 'ASC',
75     %ARGS,
76     Collection    => $not_added_cfs,
77     Rows          => $rows,
78     Format        => $format,
79     DisplayFormat => "'__CheckBox.{AddCustomField}__',". $format,
80     AllowSorting  => 1,
81     ShowEmpty     => 0,
82     PassArguments => [
83         qw(Page Order OrderBy),
84         qw(id ObjectType SubType),
85     ],
86 &>
87
88 <& /Elements/Submit, Name => 'UpdateCFs' &>
89 </form>
90
91
92 <%INIT>
93 my $id = $Object->Id || 0;
94 if ($id and !$Object->CurrentUserHasRight('AssignCustomFields')) {
95     $m->out('<p><i>', loc('(No custom fields)'), '</i></p>');
96     return;
97 }
98
99 my @results;
100
101 my $lookup = $ObjectType;
102 $lookup .= "-$SubType" if $SubType;
103
104 ## deal with moving sortorder of custom fields
105 if ( $MoveCustomFieldUp ) { {
106     my $record = RT::ObjectCustomField->new( $session{'CurrentUser'} );
107     $record->LoadByCols( ObjectId => $id, CustomField => $MoveCustomFieldUp );
108     unless ( $record->id ) {
109         push @results, loc("Custom field #[_1] is not applied to this object", $MoveCustomFieldUp);
110         last;
111     }
112
113     my ($status, $msg) = $record->MoveUp;
114     push @results, $msg;
115 } }
116 if ( $MoveCustomFieldDown ) { {
117     my $record = RT::ObjectCustomField->new( $session{'CurrentUser'} );
118     $record->LoadByCols( ObjectId => $id, CustomField => $MoveCustomFieldDown );
119     unless ( $record->id ) {
120         push @results, loc("Custom field #[_1] is not applied to this object", $MoveCustomFieldDown);
121         last;
122     }
123
124     my ($status, $msg) = $record->MoveDown;
125     push @results, $msg;
126 } }
127
128 if ( $UpdateCFs ) {
129     foreach my $cf_id ( @AddCustomField ) {
130         my $CF = RT::CustomField->new( $session{'CurrentUser'} );
131         $CF->SetContextObject( $Object );
132         $CF->Load( $cf_id );
133         unless ( $CF->id ) {
134             push @results, loc("Couldn't load CustomField #[_1]", $cf_id);
135             next;
136         }
137         my ($status, $msg) = $CF->AddToObject( $Object );
138         push @results, $msg;
139     }
140     foreach my $cf_id ( @RemoveCustomField ) {
141         my $CF = RT::CustomField->new( $session{'CurrentUser'} );
142         $CF->SetContextObject( $Object );
143         $CF->Load( $cf_id );
144         unless ( $CF->id ) {
145             push @results, loc("Couldn't load CustomField #[_1]", $cf_id);
146             next;
147         }
148         my ($status, $msg) = $CF->RemoveFromObject( $Object );
149         push @results, $msg;
150     }
151 }
152
153 $m->callback(CallbackName => 'UpdateExtraFields', Results => \@results, Object => $Object, %ARGS);
154
155 my $added_cfs = RT::CustomFields->new( $session{'CurrentUser'} );
156 $added_cfs->LimitToLookupType($lookup);
157 $added_cfs->LimitToGlobalOrObjectId($id);
158 $added_cfs->SetContextObject( $Object );
159 $added_cfs->ApplySortOrder;
160
161 my $not_added_cfs = RT::CustomFields->new( $session{'CurrentUser'} );
162 $not_added_cfs->LimitToLookupType($lookup);
163 $not_added_cfs->LimitToNotAdded( $id ? ($id, 0) : (0) );
164
165 my $format = RT->Config->Get('AdminSearchResultFormat')->{'CustomFields'};
166 my $rows = RT->Config->Get('AdminSearchResultRows')->{'CustomFields'} || 50;
167
168 my $display_format = $id
169             ? ("'__RemoveCheckBox.{$id}__',". $format .", '__MoveCF.{$id}__'")
170             : ("'__CheckBox.{RemoveCustomField}__',". $format .", '__MoveCF.{$id}__'");
171 $m->callback(CallbackName => 'EditDisplayFormat', DisplayFormat => \$display_format, id => $id);
172
173 </%INIT>
174 <%ARGS>
175 $Object
176 $ObjectType
177 $SubType => ''
178
179 $UpdateCFs           => undef
180 @RemoveCustomField   => ()
181 @AddCustomField      => ()
182 $MoveCustomFieldUp   => undef
183 $MoveCustomFieldDown => undef
184 </%ARGS>