%# $File: //depot/RT/rt/local/html/Todo/index.html $ $Author: autrijus $
%# $Revision: #2 $ $Change: 6417 $ $DateTime: 2003/06/10 05:54:04 $
%# Personal to-do plugin
<& /Elements/Header, Title => loc($QueueDescription) &>
<& /Elements/Tabs, current_toptab => 'Todo/index.html' &>
<& /Elements/ListActions, actions => \@Actions &>
<% loc($QueueDescription) %>
| <&|/l&>Create new item&> | 
<& .DisplayTodo, (
    tickets	=> $tickets_cc,
    title	=> '[*,_1,group ticket]',	# loc
) &>
<& .DisplayTodo, (
    tickets	=> $tickets_with_due,
    title	=> '[*,_1,ticket] due',		# loc
) &>
<& .DisplayTodo, (
    tickets	=> $tickets_without_due,
    title	=> '[*,_1,unresolved ticket]',	# loc
) &>
<%INIT>
my @Actions;
my $QueueName		= '___todo';
my $QueueDescription	= 'Personal Todo'; # loc
my $queue = RT::Queue->new($session{'CurrentUser'});
my ($ok, $msg) = $queue->Load($QueueName);
unless ($ok) {
    my $new_entry = RT::Queue->new($RT::SystemUser);
    my ( $return, $msg ) = $new_entry->Create(
	Name		=> $QueueName,
	Description	=> $QueueDescription,
	Disabled	=> 1,
    );
    $queue->Load($QueueName);
}
if ($ARGS{'Subject'}) {
    my $due = '';
	
    if ($ARGS{'Due'}) {	
	$due = ParseDateToISO($ARGS{'Due'});
    }
    my $Ticket = RT::Ticket->new($session{'CurrentUser'});
    my ($id, $Trans, $ErrMsg)= $Ticket->Create(
	Queue	=> $ARGS{Queue},
	Subject	=> $ARGS{Subject},
	Owner	=> $session{'CurrentUser'}->Name,
	Type	=> 'todo',
	Due	=> $due,
    );         
    unless ($id && $Trans) {
        Abort($ErrMsg);
    }
    push(@Actions, $ErrMsg);
}
if ($ARGS{'id'}) {
    my $Ticket = RT::Ticket->new($session{'CurrentUser'});
    $Ticket->Load($ARGS{'id'});
    if ($ARGS{'Status'} ) {
	my ($val, $msg) = $Ticket->SetStatus($ARGS{'Status'});
	push @Actions, $msg;
    }
}
my $tickets_with_due = RT::Tickets->new($session{'CurrentUser'});
my $tickets_without_due = RT::Tickets->new($session{'CurrentUser'});
my $tickets_cc = RT::Tickets->new($session{'CurrentUser'});
foreach ($tickets_with_due, $tickets_without_due, $tickets_cc) {
    $_->Limit( FIELD      => 'Type', VALUE => 'todo' );
    $_->LimitQueue( VALUE => $queue->id, OPERATOR => '=' );
    $_->LimitStatus(VALUE => 'open');
    $_->LimitStatus(VALUE => 'new');
    $_->OrderBy(FIELD => 'Due', ALIAS => 'main', ORDER => 'DESC');
}
foreach ($tickets_with_due, $tickets_without_due) {
    $_->LimitOwner( VALUE => $session{'CurrentUser'}->Id );
}
$tickets_cc->LimitWatcher( TYPE => 'CC', VALUE => $session{'CurrentUser'}->EmailAddress );
$tickets_with_due->Limit(FIELD => 'Due', OPERATOR => '>', VALUE => '1970-01-01');
$tickets_without_due->Limit(FIELD => 'Due', OPERATOR => '=', VALUE => '1970-01-01');
%INIT>
<%DEF .DisplayTodo>
% return unless $tickets->Count;
| <% loc($title, $tickets->Count) %> | 
% my $i;
% while (my $ticket = $tickets->Next) {
>
| <&|/l, $ticket->Id, $ticket->Subject &>#[_1]: [_2]&> 
%  if ($ticket->Owner != $session{'CurrentUser'}->Id) { <&|/l&>Owner&>: <% $ticket->OwnerAsString %>
<&|/l&>Cc&>: <% $ticket->CcAddresses %>
%  } elsif (my $cc = $ticket->CcAddresses) {
 <&|/l&>Cc&>: <% $cc %>
%  }
 | %  if ($ticket->DueObj->Unix == -1) {
 
%  } elsif ($ticket->DueObj->Unix < (time + (86400*5))) {
<% $ticket->DueObj->AsString %>
%  } else {
<% $ticket->DueObj->AsString %>
%  } | %  if ($ticket->Owner == $session{'CurrentUser'}->Id) {
[<&|/l&>Done&>]
%  } else {
 
%  } | 
% }
<%ARGS>
$tickets
$title
%ARGS>
%DEF>