<%doc>
###############################################################################

=head1 NAME

/workflow/manager/dhandler - asset search result page

=head1 VERSION

$LastChangedRevision$

=head1 DATE

$LastChangedDate$

=head1 SYNOPSIS

=head1 DESCRIPTION

=cut

</%doc>
<%once>;
my %users;
my $show_republish = 0;

my $pl_name = { map { $_ => get_class_info($_)->get_plural_name }
                qw(story media template) };

my ($start_desk_ok, $desk_chk);
my ($type, $work_id, $widg, $alt);

my $profile = sub {
    my $o = shift;
    my $u = $o->get_user__id;
    my $id = $o->get_id;
    my $ret;
    if (defined $u && $u == get_user_id && chk_authz($o, EDIT, 1)) {
        $ret = [['Edit', "/workflow/profile/$alt/$id?checkout=1&return=search", '']];
    } else {
        $ret = [['View', "/workflow/profile/$alt/$id?return=search", '']];
    }
    push @$ret, [ 'Log', "/admin/events/$type/$id", ''];
    return $ret;
};

my $select = sub {
    my $o = shift;
    my $id = $o->get_id;
    my $text = $lang->maketext($o->is_active ? 'Checkout' : 'Reactivate');
    my $u_id = $o->get_user__id;
    my $ret;
    if (defined $u_id) {
        # Just return the name of the user who has the asset checked out.
        return $users{$u_id} ||= Bric::Biz::Person::User->lookup({
          id => $u_id
      })->format_name;
    } elsif ($o->get_workflow_id && chk_authz($o, EDIT, 1)) {
        # Allow checkout if they can see the desk the asset is on.
        $ret = [[$text, "$widg\_prof|checkout_cb"]]
          if $desk_chk->{$o->get_desk_id}[0];
    } else {
        # Allow checkout only if they have recall permission to the asset and
        # access to the start desk.
        $ret = [[
            $text,
            "$widg\_prof|recall_cb",
            "$id|$work_id",
            {
                id          => "da_ck_$type\_$id",
                label_after => 0,
                js          => qq{onclick="uncheck('da_rp_$type\_$id')"}
            },
        ]] if $start_desk_ok && chk_authz($o, RECALL, 1);
    }

        # Add a republish checkbox
    if ($type ne 'template' && $o->get_publish_status
        && !$o->get_workflow_id && chk_authz($o, PUBLISH, 1)
    ) {
        push @$ret, [
            $lang->maketext('Republish'),
            "desk_asset|$type\_pub_ids",
            $o->get_version_id,
            {
                id          => "da_rp_$type\_$id",
                label_after => 0,
                js          => qq{onclick="uncheck('da_ck_$type\_$id')"}
            }
        ];
        $show_republish = 1;
    }
    return $ret;
};

my $addition = sub {
    if ($start_desk_ok && chk_authz($_[0], CREATE, 1)) {
        return ['Create', "/workflow/profile/$alt/new/$work_id"];
    }
    return;
};

my $thumburi = sub {
    my ($selfobj, $fieldname) = @_;
    if ($fieldname eq 'thumb') {
        # If we get a thumbnail URI, we have an image. Otherwise, simply return
        # a space to fill the column properly because we aren't an image object
        # but USE_THUMBNAILS is on.
        my $thumb_uri = $selfobj->thumbnail_uri or return '&nbsp;';
        return qq{ <img src="$thumb_uri" />};
    }

    elsif ($fieldname eq 'uri') {
        return $selfobj->get_uri;
    }

    return;
};

my $exclude = sub { ! chk_authz($_[0], READ, 1) };

my $alter = {
    publish_status => sub {
        my ($val, $obj) = @_;
        return unless defined $val;
        $m->comp('/widgets/desk/publish_status.mc', asset => $obj);
    },

    title => sub {
        my ($title, $obj) = @_;
        my $format = '%s';
        if (my $meth = $obj->can('get_alias_id')) {
            $format = '<span class="alias">%s</span>' if $obj->$meth;
        }
        sprintf $format, $m->comp(
            '/widgets/profile/preview_link.mc',
            type  => $type,
            value => $title,
            doc   => $obj,
        );
    }
};

my $fields = {
    template => [qw(uri version output_channel_name publish_status)],
    story    => [ qw(title version cover_date publish_status) ],
    media    => [ qw(title version cover_date publish_status) ],
};

unshift @{$fields->{'media'}}, 'thumb' if USE_THUMBNAILS;

</%once>
<%init>;
# Get the workflow ID.
($type, $work_id) = split('/', $m->dhandler_arg);
$alt = $widg = $type;
($type, $widg) = ('template', 'tmpl') if $type eq 'template';

# Grab the workflow and start desk permissions.
my $wf = Bric::Biz::Workflow->lookup({ id => $work_id });
$desk_chk = {
    map {
        $_->[0] => [
            chk_authz(0, READ, 1, @{$_->[1]}),
            chk_authz(0, EDIT, 1, @{$_->[1]}),
        ]
    } map {
        [$_->get_id => [$_->get_asset_grp, $_->get_grp_ids ]]
    } $wf->allowed_desks
};
$start_desk_ok = $desk_chk->{$wf->get_head_desk_id}[0];
my $wf_name = $wf->get_name;

# Get the list of sites.
my $sites = $c->get('__SITES__');
unless ($sites) {
    $sites = Bric::Biz::Site->list({ active => 1 });
    $c->set('__SITES__', $sites);
}

# Add "site" to the list of fields only if there is more than one site.
my @fields = @{$fields->{$type}};
push @fields, 'site'
  unless $type eq 'template' || @$sites <= 1;
my $sort_by = $type eq 'template' ? 'uri'
    : get_pref('Default Asset Sort') || 'title';
</%init>
<& '/widgets/wrappers/sharky/header.mc',
   title => "Find $pl_name->{$type}",
   context => "Workflow | &quot;$wf_name&quot; | Find $pl_name->{$type}"
&>

<& /widgets/search/search.mc,
   object    => $type,
   type      => $type,
   wf        => $wf,
   state_key => $type . $work_id,
&>
<form method="post" action="<% $r->uri %>" name="<% $type %>_manager">
<& '/widgets/wrappers/sharky/table_top.mc',
    caption => '%n Found',
    object  => $type
&>
<& '/widgets/listManager/listManager.mc',
    object    => $type,
    state_key => $type . $work_id,
    sortBy    => $sort_by,
    fields    => \@fields,
    alter     => $alter,
    exclude   => $exclude,
    profile   => $profile,
    addition  => $addition,
    select    => $select,
    behavior  => 'expand',
    field_values => $thumburi,
    cx_filter => ! $ARGS{site_id},
    field_titles => { version => $lang->maketext('V.'), thumb => $lang->maketext('Thumb')},
&>
<& '/widgets/wrappers/sharky/table_bottom.mc' &>

<& '/widgets/profile/button.mc',
    disp    => $lang->maketext('Checkout'),
    name    => 'Checkout',
    button  => 'checkout_red',
    useTable => 0
&>
% if ($show_republish) {
% $show_republish = 0;
<& /widgets/buttons/submit.mc,
   disp      => 'publish_checked_red',
   widget    => 'desk_asset',
   cb        => 'publish_cb',
   button    => 'publish_checked_red',
   useTable  => 0 &>
% }
</form>
<& /widgets/wrappers/sharky/footer.mc &>
<%cleanup>;
# Clear out the cache of user names.
%users = ();
</%cleanup>
