<%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 $pl_name = { map { $_ => get_class_info($_)->get_plural_name }
                qw(story media formatting) };

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 $u_id = $o->get_user__id;
    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.
        return ['Checkout', "$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.
        return ['Checkout', "$widg\_prof|recall_cb", $o->get_id.'|'.$work_id]
          if $start_desk_ok && chk_authz($o, RECALL, 1);
    }
    return;
};

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) = @_;
    return unless $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" />};
};

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 {
                  $m->comp('/widgets/profile/preview_link.mc',
                           type  => $type,
                           title => '<span class="l10n">' . shift() . '</span>',
                           doc   => shift)
              }
            };

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

my $sort_by = { formatting => 'file_name',
                story      => get_pref('Default Asset Sort') || 'title',
                media      => get_pref('Default Asset Sort') || 'title'  };

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) = ('formatting', 'tmpl') if $type eq 'templates';

# 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 'formatting' || @$sites <= 1;
</%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,
&>


<form method="post" action="<% $r->uri %>" name="<% $type %>_manager">
<& '/widgets/listManager/listManager.mc',
    object    => $type,
    sortBy    => $sort_by->{$type},
    fields    => \@fields,
    alter     => $alter,
    exclude   => $exclude,
    profile   => $profile,
    title     => $lang->maketext('%n Found'),
    addition  => $addition,
    select    => $select,
    behavior  => 'expand',
    field_values => $thumburi,
    cx_filter => ! $ARGS{site_id},
    field_titles => { version => $lang->maketext('V.'), thumb => $lang->maketext('Thumb')},
&>
<p></p>
<input type="image" src="/media/images/<% $lang_key %>/checkout_red.gif" border="0" name="Checkout" value="Checkout">
</form>
<& /widgets/wrappers/sharky/footer.mc &>
<%cleanup>;
# Clear out the cache of user names.
%users = ();
</%cleanup>
