%## Default elements from RT. Header and the ticket elements. Both this and the
%## html printed after the <%INIT>
<& /Elements/Header,
Title => loc("#[_1]: [_2]", $TicketObj->Id, $TicketObj->Subject),
LinkRel => \%link_rel &>
<& /Elements/Tabs &>
%## The html with %-prefixed perl code.
Ticket to export
%print "$mail_head";
%print "$mail";
<%ARGS>
$id => undef
$TicketObj => undef
$email => undef
%ARGS>
<%INIT>
my (@Actions, $Tickets);
unless ($id || $TicketObj) {
Abort('No ticket specified');
}
$TicketObj ||= LoadTicket($ARGS{'id'});
unless ($TicketObj->CurrentUserHasRight('ShowTicket')) {
Abort("No permission to view ticket");
}
# used to convert html-formated email -> plain text for the body.
use HTML::Parse;
use HTML::FormatText;
use Encode;
my $formater = HTML::FormatText->new(leftmargin => 0, rightmargin => 75);
# using buildtin methods to get the attachments - all nice and cronological.
my $attachments = $m->comp('Elements/FindAttachments', Ticket => $TicketObj, Tickets => $Tickets);
my $attachment_content = $m->comp('Elements/LoadTextAttachments', Ticket => $TicketObj);
#Get some information.
my $queue = RT::Queue->new($RT::SystemUser);
$queue->Load($TicketObj->Queue);
my $owner = RT::User->new($RT::SystemUser);
$owner->Load($TicketObj->Owner);
my $user = RT::User->new($RT::SystemUser);
$user->Load($TicketObj->CurrentUser);
my $requestor = RT::User->new($RT::SystemUser);
my $people = $TicketObj->Requestors->UserMembersObj;
my @requestors;
while ( my $r = $people->Next ) {
push @requestors, $m->scomp('/Elements/ShowUserConcise', User => $r);
}
my $requestors = join ("\n ", @requestors);
my $Transactions = $TicketObj->Transactions;
my @attachments = @{$attachments->ItemsArrayRef()};
my @attachment_content = @{$attachment_content->ItemsArrayRef()};
my $mail;
my $mailsendt=0;
my $attach=0;
my $resolved = "";
my $created;
my $mail_head;
# Find the last time the ticket was resolved.
#### NEED FIX HERE ###
while ( my $Transaction = $Transactions->Next ) {
if ($Transaction->Description =~ /Status changed from \'.*\' to \'resolved\'/) {
$resolved = $Transaction->Description." - ".
$Transaction->CreatorObj->RealName." on ".
$Transaction->CreatedAsString()."\n";
$resolved =~ s/Status changed from \'.*\' to \'resolved\' /Resolved: /;
}
# Ignoring comments/statuschange/etc.
next if $Transaction->Description !~ /(Correspondence added|Ticket created )/;
# Get all the transactions:
my $Attach = $Transaction->Attachments->ItemsArrayRef();
next if ! defined $Attach->[0];
$mail .= $Transaction->BriefDescription." by ".$Transaction->CreatorObj->Name." - ".
$Transaction->CreatorObj->RealName."\n";
$mail .= "Date: ".$Transaction->CreatedAsString()." =================================================>\n\n";
$created = $Transaction->CreatedAsString() unless $created;
for (@$Attach) {
if ($_->ContentType =~ /text\/plain/ && not $_->Filename) {$mail .= $_->Content."\n\n"}
if ($_->ContentType =~ /text\/html/ && not $_->Filename) {
$mail .= $formater->format(parse_html("".$_->Content."
"))."\n\n"
}
# Finding the mail attachments that will be attached to the final email.
if ($_->Filename) {$mail .= "Attatchment: ".$_->Filename."\n\n";$attach=1;}
}
}
# Putting together the metadata. Some posibilities are commented.
# The ticket can have a created after the first transaction if an older ticket
# is merged into a newer ticket, so we use the creation-time of the first
# transaction.
$mail_head .= "============================================================================\n";
# $mail_head .= "Created Date:\t".$TicketObj->CreatedAsString."\n";
$mail_head .= "Created Date: $created\n";
$mail_head .= "Requestor(s): $requestors\n";
# $mail_head .= "Requestor(s): ".$TicketObj->Requestors->MemberEmailAddressesAsString."\n".$requestors;
# $mail_head .= "Cc: $TicketObj->Cc\n" if $TicketObj->Cc;
# $mail_head .= "X-AdminCc: $TicketObj->AdminCc\n" if $TicketObj->AdminCc;
$mail_head .= "X-Queue: ".$queue->Name."\n";
$mail_head .= "Owner: ".$owner->Name." - ".$owner->RealName."\n";
$mail_head .= "Subject: [rt #".$TicketObj->id."] ".$TicketObj->Subject."\n";
$mail_head .= $resolved;
$mail_head .= "============================================================================\n\n";
# Building an mime-email if we are to send an email.
if ($email){
my $mail = MIME::Entity->build(
Type => 'text/plain',
Charset => 'utf-8',
Data => encode("utf8", $mail_head."\n".$mail),
);
$mail->head->set(To=>RT::Interface::Email::EncodeToMIME(String => $email));
$mail->head->set(From=>RT::Interface::Email::EncodeToMIME(String => $user->EmailAddress));
$mail->head->set(Subject=>RT::Interface::Email::EncodeToMIME(String => "Fwd: [rt #".$TicketObj->id."] ".$TicketObj->Subject));
# We could have been smart and done this part when getting the other
# transactions, but we are not smart.
if ($attach){
while ( my $Transaction = $Transactions->Next ) {
my $Attach = $Transaction->Attachments->ItemsArrayRef();
next if ! defined $Attach->[0];
for (@$Attach) {
if ($_->Filename){
$mail->attach(
Type => $_->ContentType,
Disposition => 'attachment',
Filename => $_->Filename,
Description => $_->Filename,
Data => $_->Content,
);
}
}
}
}
RT::I18N::SetMIMEEntityToEncoding( $mail, RT->Config->Get('EmailOutputEncoding'), 'mime_words_ok', );
RT::Interface::Email::SendEmail( Entity => $mail );
# Marking the sending of an email with a transaction in the ticket.
# This should be something a little smarter, but that requires a change of
# lib/RT/Transactions_Overlay.pm
my $trans = RT::Transaction->new( $TicketObj->CurrentUser );
$trans->Create(
Ticket => $TicketObj->Id,
Type => 'Export',
Field => "Email",
OldValue => "Email",
NewValue => "$email",
ActivateScrips => 0
);
#Redirecting back to the ticket.
RT::Interface::Web::Redirect( RT->Config->Get('WebURL') . "Ticket/Display.html?id=" . $TicketObj->id );
$mailsendt=1;
}
my %link_rel;
%INIT>