rt 4.2.16
[freeside.git] / rt / share / html / Elements / ColumnMap
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %#
3 %# COPYRIGHT:
4 %#
5 %# This software is Copyright (c) 1996-2019 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 <%ARGS>
49 $Class => 'RT__Ticket'
50 $Name
51 $Attr  => undef
52 </%ARGS>
53 <%ONCE>
54
55 use Scalar::Util;
56
57 my ($COLUMN_MAP, $WCOLUMN_MAP);
58 $WCOLUMN_MAP = $COLUMN_MAP = {
59     id => {
60         attribute => 'id',
61         title     => '#', # loc
62         align     => 'right',
63         value     => sub { return $_[0]->id }
64     },
65
66     Created => {
67         attribute => 'Created',
68         title     => 'Created', # loc
69         value     => sub { return $_[0]->CreatedObj },
70     },
71     CreatedRelative => {
72         attribute => 'Created',
73         title     => 'Created', # loc
74         value     => sub { return $_[0]->CreatedObj->AgeAsString }
75     },
76     CreatedBy => {
77         attribute => 'Creator',
78         title     => 'Created By', # loc
79         value     => sub { return $_[0]->CreatorObj->Name }
80     },
81     LastUpdated => {
82         attribute => 'LastUpdated',
83         title     => 'Last Updated', # loc
84         value     => sub { return $_[0]->LastUpdatedObj },
85     },
86     LastUpdatedRelative => {
87         attribute => 'LastUpdated',
88         title     => 'Last Updated', # loc
89         value     => sub { return $_[0]->LastUpdatedObj->AgeAsString }
90     },
91     LastUpdatedBy => {
92         attribute => 'LastUpdatedBy',
93         title     => 'Last Updated By', # loc
94         value     => sub { return $_[0]->LastUpdatedByObj->Name }
95     },
96
97     CustomField => {
98         attribute => sub { return shift @_ },
99         title     => sub { return pop @_ },
100         value     => sub {
101             my $self = $WCOLUMN_MAP->{CustomField};
102             my $cf   = $self->{load}->(@_);
103             return unless $cf->Id;
104             return $self->{render}->( $cf, $cf->ValuesForObject($_[0])->ItemsArrayRef );
105         },
106         load      => sub {
107             # Cache the CF object on a per-request basis, to avoid
108             # having to load it for every row
109             my $key = join("-","CF",
110                            $_[0]->CustomFieldLookupType,
111                            $_[0]->CustomFieldLookupId,
112                            $_[-1]);
113
114             my $cf = $m->notes($key);
115             unless ($cf) {
116                 $cf = $_[0]->LoadCustomFieldByIdentifier($_[-1]);
117                 RT->Logger->debug("Unable to load $_[-1] for ".$_[0]->CustomFieldLookupType." ".$_[0]->CustomFieldLookupId)
118                     unless $cf->Id;
119                 $m->notes($key, $cf);
120             }
121
122             return $cf;
123           },
124           render    => sub {
125             my ($cf, $ocfvs) = @_;
126             my $comp = $m->comp_exists("/Elements/ShowCustomField".$cf->Type)
127                      ? "/Elements/ShowCustomField".$cf->Type
128                      : undef;
129
130             my @values = map {
131                 $comp
132                     ? \($m->scomp( $comp, Object => $_ ))
133                     : $_->Content
134             } @$ocfvs;
135
136             if (@values > 1) {
137                 for my $value (splice @values) {
138                     push @values, \"<li>", $value, \"</li> \n";
139                 }
140                 @values = (\"<ul class='cf-values'>", @values, \"</ul>");
141             }
142             return @values;
143         },
144     },
145
146     CheckBox => {
147         title => sub {
148             my $name = $_[1] || 'SelectedTickets';
149             my $checked = $DECODED_ARGS->{ $name .'All' }? 'checked="checked"': '';
150
151             return \qq{<input type="checkbox" name="}, $name, \qq{All" value="1" $checked
152                               onclick="setCheckbox(this, },
153                               $m->interp->apply_escapes($name,'j'),
154                               \qq{)" />};
155         },
156         value => sub {
157             my $id = $_[0]->id;
158
159             my $name = $_[2] || 'SelectedTickets';
160             return \qq{<input type="checkbox" name="}, $name, \qq{" value="$id" checked="checked" />}
161                 if $DECODED_ARGS->{ $name . 'All'};
162
163             my $arg = $DECODED_ARGS->{ $name };
164             my $checked = '';
165             if ( $arg && ref $arg ) {
166                 $checked = 'checked="checked"' if grep $_ == $id, grep { defined and length } @$arg;
167             }
168             elsif ( $arg ) {
169                 $checked = 'checked="checked"' if $arg == $id;
170             }
171             return \qq{<input type="checkbox" name="}, $name, \qq{" value="$id" $checked />}
172         },
173     },
174     RadioButton => {
175         title => \'&nbsp;',
176         value => sub {
177             my $id = $_[0]->id;
178
179             my $name = $_[2] || 'SelectedTicket';
180             my $arg = $DECODED_ARGS->{ $name };
181             my $checked = '';
182             $checked = 'checked="checked"' if $arg && $arg == $id;
183             return \qq{<input type="radio" name="}, $name, \qq{" value="$id" $checked />};
184         },
185     },
186     (map {
187         my $value = RT->Config->Get($_);
188         $_ => { value => sub { return \$value } };
189     
190     } qw(WebPath WebBaseURL WebURL)),
191     WebRequestPath    => { value => sub { substr( $m->request_path, 1 ) } },
192     WebRequestPathDir => { value => sub { substr( $m->request_comp->dir_path, 1 ) } },
193     WebHomePath       => {
194         value => sub {
195             my $path = RT->Config->Get("WebPath");
196             if (not $session{CurrentUser}->Privileged) {
197                 $path .= "/SelfService";
198             }
199             return \$path;
200         },
201     },
202     CurrentUser       => { value => sub { $session{CurrentUser}->id } },
203     CurrentUserName   => { value => sub { $session{CurrentUser}->Name } },
204 };
205
206 $COLUMN_MAP->{'CF'} = $COLUMN_MAP->{'CustomField'};
207
208 Scalar::Util::weaken($WCOLUMN_MAP);
209
210 my $ROLE_MAP = {};
211
212 </%ONCE>
213 <%INIT>
214 $m->callback( COLUMN_MAP => $COLUMN_MAP, CallbackName => 'Once', CallbackOnce => 1 );
215
216 my $generic_with_roles;
217
218 # Add in roles
219 my $RecordClass = $Class;
220 $RecordClass =~ s/_/:/g;
221 if ($RecordClass->DOES("RT::Record::Role::Roles")) {
222     unless ($ROLE_MAP->{$RecordClass}) {
223         for my $role ($RecordClass->Roles) {
224             my $attrs = $RecordClass->Role($role);
225             $ROLE_MAP->{$RecordClass}{$role} = {
226                 title => $role,
227                 attribute => $attrs->{Column} || "$role.EmailAddress",
228                 value => sub { return \($m->scomp("/Elements/ShowPrincipal", Object => $_[0]->RoleGroup($role) ) ) },
229             };
230             $ROLE_MAP->{$RecordClass}{$role . "s"} = $ROLE_MAP->{$RecordClass}{$role}
231                 unless $attrs->{Single};
232         }
233     }
234     $generic_with_roles = { %{$COLUMN_MAP}, %{$ROLE_MAP->{$RecordClass}} };
235 } else {
236     $generic_with_roles = { %{$COLUMN_MAP} };
237 }
238
239 $m->callback( COLUMN_MAP => $generic_with_roles );
240
241 # first deal with class specific things
242 if (RT::Interface::Web->ComponentPathIsSafe($Class) and $m->comp_exists("/Elements/$Class/ColumnMap")) {
243     my $class_map = $m->comp("/Elements/$Class/ColumnMap", Attr => $Attr, Name => $Name, GenericMap => $generic_with_roles );
244     return $class_map if defined $class_map;
245 }
246
247 return GetColumnMapEntry( Map => $generic_with_roles, Name => $Name, Attribute => $Attr );
248
249 </%INIT>