%# BEGIN BPS TAGGED BLOCK {{{
%#
%# COPYRIGHT:
%#
%# This software is Copyright (c) 1996-2021 Best Practical Solutions, LLC
%#                                          <sales@bestpractical.com>
%#
%# (Except where explicitly superseded by other copyright notices)
%#
%#
%# LICENSE:
%#
%# This work is made available to you under the terms of Version 2 of
%# the GNU General Public License. A copy of that license should have
%# been provided with this software, but in any event can be snarfed
%# from www.gnu.org.
%#
%# This work is distributed in the hope that it will be useful, but
%# WITHOUT ANY WARRANTY; without even the implied warranty of
%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%# General Public License for more details.
%#
%# You should have received a copy of the GNU General Public License
%# along with this program; if not, write to the Free Software
%# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
%# 02110-1301 or visit their web page on the internet at
%# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
%#
%#
%# CONTRIBUTION SUBMISSION POLICY:
%#
%# (The following paragraph is not intended to limit the rights granted
%# to you to modify and distribute this software under the terms of
%# the GNU General Public License and is only of importance to you if
%# you choose to contribute your changes and enhancements to the
%# community by submitting them to Best Practical Solutions, LLC.)
%#
%# By intentionally submitting any modifications, corrections or
%# derivatives to this work, or any other work intended for use with
%# Request Tracker, to Best Practical Solutions, LLC, you confirm that
%# you are the copyright holder for those contributions and you grant
%# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
%# royalty-free, perpetual, license to use, copy, create derivative
%# works based on those contributions, and sublicense and distribute
%# those contributions and any derivatives thereof.
%#
%# END BPS TAGGED BLOCK }}}
<%perl>
for my $group ( @Groupings ) {
    my $CustomFields = $Object->CustomFields;
    $m->callback(
        Table        => 1, # default is true
        %ARGS,
        CallbackPage => '/Elements/ShowCustomFields',
        CallbackName => 'MassageCustomFields',
        Object       => $Object,
        CustomFields => $CustomFields,
    );

    $CustomFields->LimitToGrouping( $Object => $group );
    next unless $CustomFields->First;

    my $modify_url = $title_href ? "$title_href?id=".$Object->id.($group?";Grouping=".$m->interp->apply_escapes($group,'u')."#".CSSClass("$css_class-$group") : "#".$css_class) : undef;
    my $modify_inline
        = '<a class="inline-edit-toggle edit" href="'
        . $m->interp->apply_escapes( ( $modify_url || '#' ), 'h' ) . '">'
        . qq{<span class="fas fa-pencil-alt icon-bordered fa-2x" alt="$edit_label" data-toggle="tooltip" data-placement="top" data-original-title="$edit_label"></span>}
        . '</a>'
        . '<a class="inline-edit-toggle cancel hidden" href="#">'
        . qq{<span class="fas fa-times icon-bordered fa-2x" alt="$cancel_label" data-toggle="tooltip" data-placement="top" data-original-title="$cancel_label"></span>}
        . '</a>';
    my $modify_behavior = $InlineEdit ? ($inline_edit_behavior{$group} || $inline_edit_behavior{_default} || 'link') : 'hide';
    my @class = $css_class;
    push @class, CSSClass("$css_class-$group") if $group;
    push @class, 'editing' if $modify_behavior eq 'always';

    my %grouping_args = (
        title => $group? loc($group) : loc('Custom Fields'),
        class => (join " ", @class),
        hide_empty => 1,
        title_href => $modify_url,
        ($modify_behavior =~ /^(link|click)$/ ? (titleright_raw => $modify_inline) : ()),
        data => { 'inline-edit-behavior' => $modify_behavior },
        %$TitleBoxARGS,
    );
    $m->callback( CallbackName => 'TitleBox', Object => $Object, Grouping => $group, ARGSRef => \%grouping_args );
</%perl>
<&| /Widgets/TitleBox, %grouping_args &>
% unless ($modify_behavior eq 'always') {
  <div class="inline-edit-display">
    <& ShowCustomFields, %ARGS, Object => $Object, Grouping => $group &>
  </div>
% }
% if ($modify_behavior ne 'hide') {
  <form class="inline-edit" action="<% $ActionURL %>" method="post" enctype="multipart/form-data">
    <input type="hidden" class="hidden" name="id" value="<% $Object->id %>" />
    <& /Elements/EditCustomFields, Object => $Object, Grouping => $group, InTable => 0 &>
    <div class="form-row">
      <div class="col-12 text-right">
        <input type="submit" class="button btn btn-primary" value="<&|/l&>Save</&>" />
      </div>
    </div>
  </form>
% }
</&>
% }
<%ARGS>
$Object
$title_href => ""
$InlineEdit => 0
@Groupings  => ()
$ActionURL  => RT->Config->Get('WebPath')."/Ticket/Display.html"
</%ARGS>
<%INIT>
my $css_class = lc(ref($Object)||$Object);
$css_class =~ s/^rt:://;
$css_class =~ s/::/-/g;
$css_class = CSSClass($css_class);
$css_class .= '-info-cfs';

my $TitleBoxARGS = delete $ARGS{TitleBoxARGS} || {};

$InlineEdit = 0 unless $Object->isa('RT::Ticket');
my %inline_edit_behavior;
if ( my $config = RT->Config->Get('InlineEditPanelBehavior') ) {
    %inline_edit_behavior = %{ $config->{ RT::CustomField->_GroupingClass($Object) } || $config->{'RT::Ticket'} || {} };
}

my $edit_label   = $m->interp->apply_escapes( loc("Edit"), 'h' );
my $cancel_label = $m->interp->apply_escapes( loc("Cancel"), 'h' );

@Groupings = (RT::CustomField->CustomGroupings( $Object ), '') unless @Groupings;
</%INIT>
