%# Captain Penny's Law:
%# 	You can fool all of the people some of the time, and some of
%# the people all of the time, but you Can't Fool Mom.
<%INIT>
if ($ARGS->{id} eq 'new' and length($ARGS->{Subject})) {
    my $Event = $QueueObj->CreateFunctionItem(Events => $ARGS);

    if (!$Event) {
        if (!length($ARGS->{Subject})) {
            $m->notes( error => loc('Missing mandatory fields') );
        }
        else {
            $m->notes( error => loc('Invalid date format') );
        }
        return;
    }

    my $Now = RT::Date->new($session{'CurrentUser'}); $Now->SetToNow;
    $Event->__Set( Field => 'Resolution', Value => $Parent->Id );

    if ($Parent->Id) {
        $Event->SetCustomFieldValue( Version => $Parent->Subject );
    }

    foreach my $field (qw( EventStarts EventDue )) {
        $Event->SetCustomFieldValue( $field => $Now->AsDate )
            if !$Event->CustomFieldValue($field);
    }
}
else {
    foreach my $key (sort keys %$ARGS) {
        $key =~ /^DeleteItem-(\d+)(?:\.x)?$/ or next;
        my $Item = $QueueObj->LoadFunctionItem(Events => $1) or next;
        $Item->Delete;
    }
}
</%INIT>
<%ARGS>
$Parent => undef
$QueueObj
$ARGS
</%ARGS>
