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

=head1 NAME

/workflow/active/dhandler - Show active assets.

=head1 VERSION

$LastChangedRevision$

=head1 DATE

$LastChangedDate$

=head1 SYNOPSIS

=head1 DESCRIPTION

=cut

</%doc>
<%init>;
my ($type, $work_id) = split('/', $m->dhandler_arg);
my ($fields, $obj_type, $previewer);
if ($type eq 'template') {
    $fields = [qw(file_name version priority desk)];
    $obj_type = 'template';
} else {
    if (USE_THUMBNAILS && $type eq 'media') {
        $fields = [qw(thumb title version priority desk)];
    } else {
        $fields = [qw(title version priority desk)];
    }
    $obj_type = $type;
    $previewer = sub {
        my ($title, $media) = @_;
        $m->comp('/widgets/profile/preview_link.mc',
                 type  => $type,
                 value => qq{<span class="l10n">$title</span>},
                 doc   => $media)
    };
}

# Grab the workflow and start desk permissions.
my $wf = Bric::Biz::Workflow->lookup({ id => $work_id });
my $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
               };
$wf = $wf->get_name;
</%init>

<& '/widgets/wrappers/sharky/header.mc',
    title => "Active $pl_name->{$obj_type}",
    no_hist => 1,
    context => "Workflow | &quot;$wf&quot; | Active $pl_name->{$obj_type}"
&>

<form method="post" action="<% $r->uri %>" name="theForm">
  <& '/widgets/wrappers/sharky/table_top.mc',
      caption => 'Active %n',
      object  => $obj_type
  &>
  <& '/widgets/listManager/listManager.mc',
     object       => $obj_type,
     fields       => $fields,
     sortBy       => 'priority',
     exclude      => $exclude->($desk_chk),
     addition     => undef,
     select       => $select->($type, $work_id, $desk_chk),
     profile      => $profile->($type),
     alter        => {priority => sub { $pri[$_[0]-1] },
                      title    => $previewer },
     field_titles => { desk    => $lang->maketext('Desk'),
                       version => $lang->maketext('V.'),
                       thumb   => $lang->maketext('Thumb'),
                     },
     field_values => $values,
     constrain    => {'workflow__id' => $work_id},
  &>
  <& '/widgets/wrappers/sharky/table_bottom.mc' &>
 
<& /widgets/buttons/submit.mc,
   disp      => 'Checkout',
   name      => 'checkout',
   button    => 'checkout_red',
   useTable  => 0 &>
<& /widgets/buttons/back.mc,
   disp      => 'Return',
   name      => 'return',
   uri       => '/workflow/profile/workspace/',
   button    => 'return_dgreen',
   useTable  => 0 &>

  </form>
<& /widgets/wrappers/sharky/footer.mc &>
<%cleanup>;
# Clear out the cache of user names.
%users = ();
</%cleanup>
<%once>
my $pl_name = { map { $_ => get_class_info($_)->get_plural_name }
                qw(story media template) };

my @pri = ('High', 'Medium High', 'Normal', 'Medium Low', 'Low');
my %users;

my $profile = sub {
    my ($type) = @_;

    # Return an anonymous sub with closure on $type
    return sub {
        my $o = shift;
        my $u = $o->get_user__id;
        my $id = $o->get_id;
        if (defined $u && $u == get_user_id && chk_authz($o, EDIT, 1)) {
            return ['Edit', "/workflow/profile/$type/$id?checkout=1&return=active", ''];
        } else {
            return ['View', "/workflow/profile/$type/$id?return=active", ''];
        }
    }
};

my $select = sub {
    my ($type, $work_id, $desk_chk) = @_;
    my $widget = $type eq 'template' ? 'tmpl_prof' : $type.'_prof';

    # Return an anonymous sub with closure on $type
    return sub {
        my $o = shift;
        my $u = $o->get_user__id;
        if (defined $u) {
            # Show the name of the person who has the asset checked out.
            return $users{$u} ||= Bric::Biz::Person::User->lookup({
              id => $u
            })->format_name;
        } elsif ($desk_chk->{$o->get_desk_id}[0] && chk_authz($o, EDIT, 1)) {
            # Allow access if they have permission and have READ access to the
            # desk the asset is on.
            return ['Checkout', $widget.'|checkout_cb'];
        }
        return;
    }
};

my $exclude = sub {
    my $desk_chk = shift;
    return sub {
        my $o = shift;
        return 1 unless $desk_chk->{$o->get_desk_id}[0];
        return ! chk_authz($o, READ, 1);
    };
};

my $values = sub {
    my ($o, $f) = @_;
    if ($f eq 'desk') {
        my $d = $o->get_current_desk;
        my $w_id = $o->get_workflow_id;
        my $d_id = $d->get_id;
        my $name = $d->get_name;

        return qq{<a href="/workflow/profile/desk/$w_id/$d_id/" class=redLink>$name</a>};
    } elsif ($f 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 = $o->thumbnail_uri or return '&nbsp;';
        return qq{ <img src="$thumb_uri" />};
    }
    return;
};
</%once>



