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


<%ONCE>
my $COLUMN_MAP;

my $LinkCallback = sub {
    my $method = shift;

    my $mode            = $RTx::AssetTracker::Asset::LINKTYPEMAP{$method}{Mode};
    my $type            = $RTx::AssetTracker::Asset::LINKTYPEMAP{$method}{Type};
    my $other_mode      = ($mode eq "Target" ? "Base" : "Target");
    my $mode_uri        = $mode.'URI';
    my $local_type      = 'Local'.$mode;

    {
        export_style => 's24',
        export_value => sub {
            my @values = map {
                $_->$mode_uri->Resolver->URI,
                "\n",
            } @{ $_[0]->Links($other_mode,$type)->ItemsArrayRef };
            pop @values; # Remove that last \n
            return @values;
        },
        value => sub {
            map {
                \'<a href="',
                $_->$mode_uri->Resolver->HREF,
                \'">',
                $_->$mode_uri->Resolver->AsString,
                \'</a><br />',
            } @{ $_[0]->Links($other_mode,$type)->ItemsArrayRef }
        }
    }
};

$COLUMN_MAP = {
    IP => {
        attribute => 'IP',
        title     => 'IP Address',
        value     => sub { return map { ($_, \'<br>') } $_[0]->IPsAsList },
        export_value     => sub { $_[0]->_export_formatted_IPs() },
    },
    TypeName => {
        attribute => 'Type',
        title     => 'Type',
        value     => sub { return $_[0]->TypeObj->Name }
    },
    Name => {
        attribute => 'Name',
        title     => 'Name',
        value     => sub { return $_[0]->Name }
    },
    Status => {
        attribute => 'Status',
        title     => 'Status',
        value     => sub { return loc($_[0]->Status) }
    },
    Description => {
        attribute => 'Description',
        title     => 'Description',
        value     => sub { return $_[0]->Description || "(" . loc('No description') . ")" }
    },
    ExtendedStatus => {
        title     => 'Status',
        attribute => 'Status',
        value     => sub {
            my $Asset = shift;

            if ( $Asset->HasUnresolvedDependencies ) {
                if (   $Asset->HasUnresolvedDependencies( Type => 'approval' )
                    or $Asset->HasUnresolvedDependencies( Type => 'code' ) )
                {
                    return \'<em>', loc('(pending approval)'), \'</em>';
                }
                else {
                    return \'<em>', loc('(pending other Collection)'), \'</em>';
                }
            }
            else {
                return loc( $Asset->Status );
            }

          }
    },
    LastTransaction => {
        attribute => 'LastTransaction',
        title     => 'Last Transaction',
        value     => sub { return $_[0]->Transactions->Last->BriefDescription }
    },

    # Falls through to generic ColumnMap for other attributes
    id => {
        export_type  => 'Number',
        export_style => 's23',
    },
    Created => {
        export_type  => 'DateTime',
        export_style => 's21',
        export_value => sub { my $date = $_[0]->Created . '.000'; $date =~ s/ /T/; $date }
    },
    LastUpdated => {
        export_type  => 'DateTime',
        export_style => 's21',
        export_value => sub { my $date = $_[0]->Created . '.000'; $date =~ s/ /T/; $date }
    },
    CustomField => {
        export_style => 's24',
        export_value => 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]);
                $m->notes($key, $cf);
            }

            # Display custom field contents, separated by newlines.
            my $values = $cf->ValuesForObject( $_[0] );
            my @values = map {
                $_->Content,
                "\n",
            } @{ $values->ItemsArrayRef };
            pop @values; # Remove that last \n
            return @values;
        },
    },

    # Everything from LINKTYPEMAP
    (map {
        $_ => $LinkCallback->( $_ )
    } keys %RTx::AssetTracker::Asset::LINKTYPEMAP),

    '_CLASS' => {
        value => sub { return $_[1] % 2 ? 'oddline' : 'evenline' }
    },
    '_CHECKBOX' => {
        attribute => 'checkbox',
        title => 'Update',
        align     => 'right',
        value     => sub { return \('<input type="checkbox" class="checkbox" name="UpdateAsset'.$_[0]->id.'" value="1" checked="checked" />') }
    },

};

for my $role ( RTx::AssetTracker::Type->RoleGroupTypes() ) {
    my $group_method = $role . 'RoleGroup';
    my $export_method = $role . 'RoleGroupExportString';
    $COLUMN_MAP->{$role} = {
        attribute => "$role.EmailAddress",
        value => sub { return $_[0]->$group_method->MemberEmailAddressesAsString },
        export_value => sub { return $_[0]->$export_method },
    };
}
</%ONCE>
<%init>
$m->callback( COLUMN_MAP => $COLUMN_MAP, CallbackName => 'ColumnMap' );
return GetColumnMapEntry( Map => $COLUMN_MAP, Name => $Name, Attribute => $Attr );
</%init>
