The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
%# People will do tomorrow what they did today because that is what they
%# did yesterday.

<& /Work/Elements/104Header, Title => 'My Approvals', Action => '/Work/Approvals/index.html' &>
<& /Work/Elements/SelectSearch, Categories => \%Categories, Category => $Category, %ARGS &>
<& /Work/Elements/List, List => $List, Begin => $Begin, URL => '/Work/Approvals/Display.html', ARGS => \%ARGS, Role => 'approver', IDMap => $IDMap &>
<& /Work/Elements/104Footer &>

<%INIT>
$session{'WorkPage'} = '/Work/Approvals/';
$session{'CcSet'} = 0;

my %Categories;
my $Queues = RT::Queues->new($session{'CurrentUser'});
$Queues->UnLimit;
my $List = [];

my $clause = "SELECT Id FROM Tickets WHERE Type = 'approval' AND ( Owner = ";
$clause .= $session{'CurrentUser'}->UserObj->Id;

# now, get additional owners (people that delegates to us)
my $Groups = RT::Groups->new( $session{'CurrentUser'} );
$Groups->Limit( FIELD => 'Domain', OPERATOR => '=', VALUE => 'Personal' );
$Groups->Limit(
    FIELD => 'Description',
    OPERATOR => '=',
    VALUE => $session{'CurrentUser'}->UserObj->PrincipalId
);

while (my $Group = $Groups->Next) {
    next if $Group->Disabled; 
    $clause .= " OR ( Owner = " . $Group->Instance
	     . " AND IssueStatement = " . $Group->Name . ")";
}

$clause .= ") AND ( " . join(
    ' OR ', map "Status = '$_'",
    (($Status eq 'unresolved') ? ('open') : (grep !/deleted/, RT::Queue->InactiveStatusArray))
);

if ($CreatedBefore) {
    $CreatedBefore =~ s!/!-!g;
    my $created_before = RT::Date->new( $session{'CurrentUser'} );
    $created_before->Set( Format => 'unknown', Value => $CreatedBefore );
    $clause .= ") AND ( CreatedBefore <= '" . $created_before->ISO . "' ";
}

if ($CreatedAfter) {
    $CreatedAfter =~ s!/!-!g;
    my $created_after = RT::Date->new( $session{'CurrentUser'} );
    $created_after->Set( Format => 'unknown', Value => $CreatedAfter );
    $clause .= ") AND ( CreatedAfter >= '" . $created_after->ISO . "' ";
}

# also consider AdminCcs as potential approvers.
#my $group_tickets = RT::Tickets->new( $session{'CurrentUser'} );
#$group_tickets->LimitWatcher( TYPE => 'AdminCC', VALUE => $session{'CurrentUser'}->UserObj->Id);

# if (length $Category) {
    my @queues;
    my $QueuesObj = RT::Queues->new($session{'CurrentUser'});
    $QueuesObj->UnLimit;
    while (my $queue = $QueuesObj->Next) {
	my $descr = $queue->Description;
	$Categories{$descr}++ if length $descr;
	next if length $Category and $descr ne $Category;
	push @queues, $queue->Id;
    }
    $clause .= ") AND ( " . join( ' OR ', map "IssueStatement = '$_'", @queues ) if length($Category);
# }

$clause .= ") ORDER BY Created DESC;";

my ($IDMap, %done);

my $tickets = $RT::Handle->dbh->selectcol_arrayref($clause);

my $ID;
foreach my $TicketId (@$tickets) {
    my $TicketObj = RT::Ticket->new($session{CurrentUser});
    $TicketObj->Load($TicketId);
    next if $done{$TicketId}++; # don't show duplicate tickets

    my $approving = $TicketObj->OriginObj or next;
    next if $approving->Status eq 'deleted';

    if ($RT::OIN104 and ($ID or $Requestor and $Requestor =~ /^[-\w]+$/)) {
	# if it looks like a employee_no, try to identify it as such.
	if ($ID ||= RT::User->no_to_id($Requestor)) {
	    $Requestor = undef;
	    my $ext_id = eval { $approving->FirstRequestor->ExternalAuthId } or next;
	    next unless $ext_id == $ID;
	}
    }

    if ($Requestor) {
	my $name = eval { $approving->FirstRequestor->Name } or next;
	next unless index($name, $Requestor) > -1;
    }
    # next if $Status eq 'unresolved' and $TicketObj->HasUnresolvedDependencies( Type => 'approval' );
    push @$List, $TicketObj;
    $IDMap->{$TicketId} = $approving;
}

</%INIT>
<%ARGS>
$Category	=> ''
$Begin		=> 0
$Status		=> 'unresolved'
$CreatedBefore	=> ''
$CreatedAfter	=> ''
$Requestor	=> ''
</%ARGS>