%# BEGIN BPS TAGGED BLOCK {{{
%#
%# COPYRIGHT:
%#
%# This software is Copyright (c) 1996-2024 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 }}}
<%ARGS>
$Class => 'RT__Ticket'
$Name
$Attr  => undef
</%ARGS>
<%ONCE>

use Scalar::Util;

my $role_value = sub {
    my $role   = shift;
    my $object = shift;

    # $[0] is the index number of current row
    my $field  = $_[1] || '';

    my ( $role_type, $attr, $cf_name );


    if ( $role eq 'CustomRole' ) {
        my $role_name;
        if ( $field =~ /^\{(.+)\}\.CustomField\.\{(.+)\}/ ) {

            # {test}.CustomField.{foo}
            $role_name = $1;
            $cf_name   = $2;
        }
        elsif ( $field =~ /^\{(.+)\}(?:\.(\w+))?$/ ) {

            # {test}.Name or {test}
            $role_name = $1;
            $attr      = $2;
        }

        # Cache the role object on a per-request basis, to avoid
        # having to load it for every row
        my $key = "RT::CustomRole-" . $role_name;

        my $role_obj = $m->notes($key);
        if ( !$role_obj ) {
            $role_obj = RT::CustomRole->new( $object->CurrentUser );
            $role_obj->Load($role_name);

            RT->Logger->notice("Unable to load custom role $role_name")
                unless $role_obj->Id;

            $m->notes( $key, $role_obj );
        }
        $role_type = $role_obj->GroupType;
    }
    else {
        if ( $field =~ /^CustomField\.\{(.+)\}/ ) {
            $cf_name = $1;
        }
        elsif ( $field =~ /^(\w+)$/ ) {
            $attr = $1;
        }
        $role_type = $role;
    }

    return if !$role_type;

    my $role_group = $object->RoleGroup($role_type);
    if ( $cf_name || $attr ) {
        # TODO Show direct members only?
        my $users = $role_group->UserMembersObj;
        my @values;

        while ( my $user = $users->Next ) {
            if ($cf_name) {
                my $key = join( "-", "CF", $user->CustomFieldLookupType, $cf_name );
                my $cf = $m->notes($key);
                if ( !$cf ) {
                    $cf = $user->LoadCustomFieldByIdentifier($cf_name);
                    RT->Logger->debug( "Unable to load $cf_name for " . $user->CustomFieldLookupType )
                        unless $cf->Id;
                    $m->notes( $key, $cf );
                }

                my $ocfvs = $cf->ValuesForObject($user)->ItemsArrayRef;
                my $comp
                    = $m->comp_exists( "/Elements/ShowCustomField" . $cf->Type )
                    ? "/Elements/ShowCustomField" . $cf->Type
                    : undef;

                push @values, map { $comp ? \( $m->scomp( $comp, Object => $_ ) ) : $_->Content } @$ocfvs;

            }
            elsif ( $user->_Accessible( $attr, 'read' ) ) {
                push @values, $user->$attr || ();
            }
        }
        return @values if @values <= 1;

        if ($cf_name) {
            @values = map { \"<li>", $_, \"</li> \n" } @values;
            @values = ( \"<ul class='cf-values'>", @values, \"</ul>" );
        }
        else {
            return join ', ', @values;
        }
    }
    else {
        return \( $m->scomp( "/Elements/ShowPrincipal", Object => $role_group ) );
    }
};

my ($COLUMN_MAP, $WCOLUMN_MAP);
$WCOLUMN_MAP = $COLUMN_MAP = {
    id => {
        attribute => 'id',
        title     => '#', # loc
        align     => 'right',
        value     => sub { return $_[0]->id }
    },

    Created => {
        attribute => 'Created',
        title     => 'Created', # loc
        value     => sub { return $_[0]->CreatedObj->AsString }
    },
    CreatedRelative => {
        attribute => 'Created',
        title     => 'Created', # loc
        value     => sub { return $_[0]->CreatedObj->AgeAsString }
    },
    CreatedBy => {
        attribute => 'Creator',
        title     => 'Created By', # loc
        value     => sub { return $_[0]->CreatorObj->Name }
    },
    LastUpdated => {
        attribute => 'LastUpdated',
        title     => 'Last Updated', # loc
        value     => sub { return $_[0]->LastUpdatedObj->AsString }
    },
    LastUpdatedRelative => {
        attribute => 'LastUpdated',
        title     => 'Last Updated', # loc
        value     => sub { return $_[0]->LastUpdatedObj->AgeAsString }
    },
    LastUpdatedBy => {
        attribute => 'LastUpdatedBy',
        title     => 'Last Updated By', # loc
        value     => sub { return $_[0]->LastUpdatedByObj->Name }
    },

    CustomField => {
        attribute => sub { return shift @_ },
        title     => sub { return pop @_ },
        value     => sub {
            my $self = $WCOLUMN_MAP->{CustomField};
            my $cf   = $self->{load}->(@_);
            return unless $cf->Id;
            return $self->{render}->( $cf, $cf->ValuesForObject($_[0])->ItemsArrayRef );
        },
        load      => sub {
            # Cache the CF object on a per-request basis, to avoid
            # having to load it for every row
            my $key = join("-","CF",
                           $_[0]->CustomFieldLookupType,
                           $_[0]->CustomFieldLookupId,
                           $_[-1]);

            my $cf = $m->notes($key);
            unless ($cf) {
                $cf = $_[0]->LoadCustomFieldByIdentifier($_[-1]);
                RT->Logger->debug("Unable to load $_[-1] for ".$_[0]->CustomFieldLookupType." ".$_[0]->CustomFieldLookupId)
                    unless $cf->Id;
                $m->notes($key, $cf);
            }
            return $cf;
        },
        render    => sub {
            my ($cf, $ocfvs) = @_;
            my $comp = $m->comp_exists("/Elements/ShowCustomField".$cf->Type)
                     ? "/Elements/ShowCustomField".$cf->Type
                     : undef;

            my @values = map {
                $comp
                    ? \($m->scomp( $comp, Object => $_ ))
                    : $_->Content
            } @$ocfvs;

            if (@values > 1) {
                for my $value (splice @values) {
                    push @values, \"<li>", $value, \"</li> \n";
                }
                @values = (\"<ul class='cf-values'>", @values, \"</ul>");
            }
            return @values;
        },
        edit      => sub {
            my $self = $WCOLUMN_MAP->{CustomField};
            my $cf = $self->{load}->(@_);
            return unless $cf->Id;

            # uploading files should be done on the modify page
            return if $cf->Type =~ /^(?:Binary|Image)$/;

            return \($m->scomp('/Elements/EditCustomField', CustomField => $cf, Object => $_[0]))
        },
    },
    CustomRole => {
        attribute => sub {
            my $field = $_[0];
            if ( $field =~ /^CustomRole\.\{.+\}\.\w+/ ) {
                return $field;
            }
            else {
                return "$field.Name";
            }
        },
        title     => sub {
            my $field = pop @_;
            if (   $field =~ /^\{(.+)\}\.CustomField\.\{(.+)\}/
                || $field =~ /^\{(.+)\}\.(.+)/ )
            {
                return "$1.$2";
            }
            elsif ( $field =~ /^\{(.+)\}$/ ) {
                return $1;
            }
            else {
                return $field;
            }
        },
        load      => sub {
            my $field = $_[2];
            my $role_name;
            if ( $field =~ /^\{(.+)\}\.CustomField\.\{(.+)\}/ ) {

                # {test}.CustomField.{foo}
                $role_name = $1;
            }
            elsif ( $field =~ /^\{(.+)\}(?:\.(\w+))?$/ ) {

                # {test}.Name or {test}
                $role_name = $1;
            }

            # Cache the role object on a per-request basis, to avoid
            # having to load it for every row
            my $key = "RT::CustomRole-" . $role_name;

            my $role_obj = $m->notes($key);
            if (!$role_obj) {
                $role_obj = RT::CustomRole->new($_[0]->CurrentUser);
                if ($role_name =~ /^\d+$/) {
                    $role_obj->Load($role_name);
                }
                else {
                    $role_obj->LoadByCols(Name => $role_name, LookupType => $_[0]->CustomFieldLookupType);
                }

                RT->Logger->notice("Unable to load custom role $role_name")
                    unless $role_obj->Id;

                $m->notes($key, $role_obj);
            }

            return $role_obj;
        },
        edit => sub {
            my $self = $WCOLUMN_MAP->{CustomRole};
            my $role   = $self->{load}->(@_);
            return unless $role->Id;
            if ($role->SingleValue) {
                if ( $_[0]->isa('RT::Ticket') ) {
                    return \($m->scomp("/Elements/SingleUserRoleInput", role => $role, Ticket => $_[0]));
                }
                elsif ( $_[0]->isa('RT::Asset') ) {
                    my $group = $_[0]->RoleGroup( $role->GroupType);
                    my $user       = $group->UserMembersObj()->First || RT->Nobody;
                    my $user_name  = $m->interp->apply_escapes( $user->Name, 'h' );
                    my $group_type = $role->GroupType;
                    return \qq{<input class="form-control" type="text" value="$user_name" name="SetRoleMember-$group_type" data-autocomplete="Users" data-autocomplete-return="Name" />};
                }
                else {
                    RT->Logger->warning( "Invalid object for custom roles: " . ref $_[0] );
                    return undef;
                }
            }
            else {
                return undef;
            }
        },
        value => sub { return $role_value->('CustomRole', @_) },
    },

    CheckBox => {
        title => sub {
            my $name = $_[1] || 'SelectedTickets';
            my $checked = $DECODED_ARGS->{ $name .'All' }? 'checked="checked"': '';

            my $escape_h_name = $m->interp->apply_escapes($name,'h');
            my $escape_j_name = $m->interp->apply_escapes($name,'j');

            return \qq{
<div class="custom-control custom-checkbox">
  <input type="checkbox" name="${escape_h_name}All" id="${escape_h_name}All" value="1" class="checkbox custom-control-input" $checked onclick="setCheckbox(this, $escape_j_name)" />
  <label class="custom-control-label" for="${escape_h_name}All"></label>
</div>};
        },
        value => sub {
            my $id = $_[0]->id;

            my $name = $_[2] || 'SelectedTickets';

            my $checked = '';
            if ( $DECODED_ARGS->{ $name . 'All'} ) {
                $checked = 'checked="checked"';
            }
            else {
                my $arg = $DECODED_ARGS->{ $name };
                if ( $arg && ref $arg ) {
                    $checked = 'checked="checked"' if grep $_ == $id, grep { defined and length } @$arg;
                }
                elsif ( $arg ) {
                    $checked = 'checked="checked"' if $arg == $id;
                }
            }

            my $escape_h_name = $m->interp->apply_escapes($name,'h');
            return \qq{
<div class="custom-control custom-checkbox">
  <input type="checkbox" name="$escape_h_name" id="$escape_h_name-$id" value="$id" class="checkbox custom-control-input" $checked />
  <label class="custom-control-label" for="$escape_h_name-$id"></label>
</div>};
        },
    },
    RadioButton => {
        title => \'&nbsp;',
        value => sub {
            my $id = $_[0]->id;

            my $name = $_[2] || 'SelectedTicket';
            my $arg = $DECODED_ARGS->{ $name };
            my $checked = '';
            $checked = 'checked="checked"' if $arg && $arg == $id;
            return \qq{<input type="radio" name="}, $name, \qq{" value="$id" $checked />};
        },
    },
    (map {
        my $value = RT->Config->Get($_);
        $_ => { value => sub { return \$value } };
    
    } qw(WebPath WebBaseURL WebURL)),
    WebRequestPath    => { value => sub { substr( $m->request_path, 1 ) } },
    WebRequestPathDir => { value => sub { substr( $m->request_comp->dir_path, 1 ) } },
    WebHomePath       => {
        value => sub {
            my $path = RT->Config->Get("WebPath");
            if (not $session{CurrentUser}->Privileged) {
                $path .= "/SelfService";
            }
            return \$path;
        },
    },
    CurrentUser       => { value => sub { $session{CurrentUser}->id } },
    CurrentUserName   => { value => sub { $session{CurrentUser}->Name } },
};

$COLUMN_MAP->{'CF'} = $COLUMN_MAP->{'CustomField'};

# Add a CustomFieldView column for custom fields, but with editing disabled
$COLUMN_MAP->{'CustomFieldView'} = {
    attribute => sub {
        my $attr = $_[0];
        $attr =~ s!CustomFieldView!CustomField!;
        return $attr;
    },
};

# We copy all keys from CF to CustomFieldView except for "edit" and overridden ones
foreach my $key ( keys( %{ $COLUMN_MAP->{'CF'} } ) ) {
    next if $key eq 'edit' || $COLUMN_MAP->{'CustomFieldView'}->{$key};
    $COLUMN_MAP->{'CustomFieldView'}->{$key} = $COLUMN_MAP->{'CF'}->{$key};
}

Scalar::Util::weaken($WCOLUMN_MAP);

my $ROLE_MAP = {};

</%ONCE>
<%INIT>
$m->callback( COLUMN_MAP => $COLUMN_MAP, CallbackName => 'Once', CallbackOnce => 1 );

my $generic_with_roles;

# Add in roles
my $RecordClass = $Class;
$RecordClass =~ s/_/:/g;
if ($RecordClass->DOES("RT::Record::Role::Roles")) {
    unless ($ROLE_MAP->{$RecordClass}) {
        # UserDefined => 1 is handled by the CustomRole mapping
        for my $role ($RecordClass->Roles(UserDefined => 0)) {
            my $attrs = $RecordClass->Role($role);
            $ROLE_MAP->{$RecordClass}{$role} = {
                attribute => sub {
                    my $field = $_[0];
                    if ( $field =~ /\.\w+/ ) {
                        return $field;
                    }
                    else {
                        return "$field.Name";
                    }
                },
                title => sub {
                    my $field = pop @_;
                    if (   $field =~ /^CustomField\.\{(.+)\}/
                        || $field =~ /^(?!$role)(.+)/ )
                    {
                        return "$role.$1";
                    }
                    else {
                        return $role;
                    }
                },
                value => sub { return $role_value->($role, @_, @_ == 2 ? '' : () ) },
                edit => sub {
                    if ($attrs->{Single} && $RecordClass eq 'RT::Asset' ) {
                        my $group      = $_[0]->RoleGroup($role);
                        my $user       = $group->UserMembersObj()->First || RT->Nobody;
                        my $user_name  = $m->interp->apply_escapes( $user->Name, 'h' );
                        return \qq{<input class="form-control" type="text" value="$user_name" name="SetRoleMember-$role" data-autocomplete="Users" data-autocomplete-return="Name" />};
                    }
                    else {
                        return undef;
                    }
                },
            };

            $ROLE_MAP->{$RecordClass}{$role . "s"} = $ROLE_MAP->{$RecordClass}{$role}
                unless $attrs->{Single};
        }
    }
    $generic_with_roles = { %{$COLUMN_MAP}, %{$ROLE_MAP->{$RecordClass}} };
} else {
    $generic_with_roles = { %{$COLUMN_MAP} };
}

$m->callback( COLUMN_MAP => $generic_with_roles );

# first deal with class specific things
if (RT::Interface::Web->ComponentPathIsSafe($Class) and $m->comp_exists("/Elements/$Class/ColumnMap")) {
    my $class_map = $m->comp("/Elements/$Class/ColumnMap", Attr => $Attr, Name => $Name, GenericMap => $generic_with_roles );
    return $class_map if defined $class_map;
}

return GetColumnMapEntry( Map => $generic_with_roles, Name => $Name, Attribute => $Attr );

</%INIT>
