Commit 075bd737 authored by Timmer, Marius's avatar Timmer, Marius

Replaced "old" attachment export feature with modded code from REL-3_2_0

parent 040e98ed
......@@ -39,7 +39,7 @@ sub Configure {
);
$Self->AddOption(
Name => 'attachment',
Description => 'Includes the ticket attachment to the exported files',
Description => 'Includes the tickets attachments to the exported files',
Required => 0,
HasValue => 0
);
......@@ -52,7 +52,7 @@ sub Run {
# Read out the arguments from the command line
my $QueueName = $Self->GetOption('queue');
my $Filepath = $Self->GetOption('filepath');
my $Attachment = $Self->GetOption('attachment');
my $Attachments = defined($Self->GetOption('attachment')) ? 1 : 0;
my $TicketExportObject = $Kernel::OM->Get('Kernel::System::TicketExport');
if (!$TicketExportObject) {
......@@ -94,17 +94,17 @@ sub Run {
# Do the export
my $ExportedTickets = $TicketExportObject->Run(
QueueID => $QueueID,
Filepath => $Filepath,
Attachment => $Attachment,
UserID => 1,
Print => 1
QueueID => $QueueID,
Filepath => $Filepath,
Attachments => $Attachments,
UserID => 1,
Print => 1
);
if ($ExportedTickets) {
# There are some exported tickets. Tell the user how many tickets
# were exported to a own PDF file.
$Self->Print(sprintf("Wrote %d files.\n", $ExportedTickets));
$Self->Print(sprintf("Exported %d tickets.\n", $ExportedTickets));
} else {
# There is no written file. This could mean a error occured or
# maybe the queue just do not has any tickets in it to export.
......
......@@ -25,7 +25,8 @@ sub new {
LogObject => $Kernel::OM->Get('Kernel::System::Log'),
QueueObject => $Kernel::OM->Get('Kernel::System::Queue'),
TicketObject => $Kernel::OM->Get('Kernel::System::Ticket'),
UserObject => $Kernel::OM->Get('Kernel::System::User')
UserObject => $Kernel::OM->Get('Kernel::System::User'),
MainObject => $Kernel::OM->Get('Kernel::System::Main');
};
bless($Self, $Type);
return $Self;
......@@ -36,10 +37,10 @@ sub new {
Exports all tickets and writes them into files.
my $NumberOfWrittenTickets = $TicketExportObject->Run(
QueueID => 1,
Filepath => './',
Attachment => 0,
Print => 1
QueueID => 1,
Filepath => './',
Attachments => 0,
Print => 1
);
=cut
......@@ -54,7 +55,7 @@ sub Run {
}
my $QueueID = $Param{QueueID};
my $Filepath = $Param{Filepath};
$Self->{Attachment} = $Param{Attachment} || 0;
$Self->{Attachments} = $Param{Attachments};
$Self->{UserID} = $Param{UserID} || 1;
my $QueueName = $Self->{QueueObject}->QueueLookup(
......@@ -107,18 +108,19 @@ sub Run {
}
}
if ($Self->{Attachment}) {
# The user wants to store the attachments of the ticket articles
# as well. This will be performed in this code block.
if ($Param{Print} && $Param{Print} == 1) {
print STDOUT "[". $PrintStatus ."]\r\n";
}
# After writing the ticket itself (and the article bodys) into the pdf
# file we have to check if the attachments should be exported as well
# and do so if necessary.
if ($Self->{Attachments}) {
$Self->StoreAttachments(
TicketID => $TicketID,
TicketID => $TicketID,
FilePrefix => $Filepath . $QueueName .'_'. $TicketID .'_'. $TicketData{TicketNumber} .'_'
);
}
if ($Param{Print} && $Param{Print} == 1) {
print STDOUT "[". $PrintStatus ."]\r\n";
}
}
return $SucceedTickets;
}
......@@ -150,41 +152,67 @@ sub StoreAttachments {
TicketID => $Param{TicketID},
UserID => $Self->{UserID}
);
my %LatestVersion = pop(@TicketHistory);
my $ArticleID = $LatestVersion{ArticleID};
my %Attachments = $Self->{TicketObject}->ArticleAttachmentIndex(
ArticleID => $ArticleID,
UserID => $Self->{UserID}
);
for my $FileID (sort keys %Attachments) {
my %AttachmentData = $Self->{TicketObject}->ArticleAttachment(
ArticleID => $ArticleID,
FileID => $FileID,
UserID => $Self->{UserID}
);
# We iterate through the whole history of the ticket. Doing that we have to
# filter the articles because all events in the history without an article
# are not important to us. Keep in mind that multiple history entries for
# one ArticleID are possible. That is why we store the ArticleID as key in
# a hash rather than using an simple array. That would cause multiple
# attachment exports for a single file.
my %ArticleIDs;
for my $History (@TicketHistory) {
if ((defined($History->{ArticleID})) && ($History->{ArticleID} > 0)) {
$ArticleIDs{$History->{ArticleID}} = 1;
}
}
my $Filename = $Param{FilePrefix} . $AttachmentData{Filename};
my $WritingStatus = $Kernel::OM->Get('Kernel::System::Main')->FileWrite(
Location => $Filename,
Content => $AttachmentData{Content},
Mode => 'binmode',
Permission => '644'
);
if ($WritingStatus) {
# Write a message to stdout that the attachment
# was exported into its file with success.
print STDOUT sprintf("Wrote attachment to \"%s\"\n", $Filename);
} else {
# An unknown error occured while exporting the attachment
# into a file. Print out all data we have at this point.
print STDERR sprintf(
"Could not write file \"%s\" (%d bytes) of content type: %s\n",
$Filename,
$AttachmentData{FilesizeRaw},
$AttachmentData{ContentType}
);
}
}
# Here we iterate through all articles from the current ticket we still
# have. The Attachments for each article will be found.
for my $ArticleID (sort keys %ArticleIDs) {
my %Attachments = $Self->{TicketObject}->ArticleAttachmentIndex(
ArticleID => $ArticleID,
UserID => $Self->{UserID},
StripPlainBodyAsAttachment => 1
);
# Now we know all attachments of the current article and export them
# into separate files. Keep in mind that the article body itself is a
# attachment as well which needs to be ignored.
for my $FileID (sort keys %Attachments) {
if ($FileID == 1) {
# The first element in this list is always the article content
# itself (body of the email) which is already written to the
# pdf file. We only want to export the attachments separate.
# The body-files would have always the same name and overwrite
# each other. So we have to ignore it.
next;
}
my %AttachmentData = $Self->{TicketObject}->ArticleAttachment(
ArticleID => $ArticleID,
FileID => $FileID,
UserID => $Self->{UserID}
);
my $FileName = $Param{FilePrefix} . 'Article'. $ArticleID .'_Attachment'. $FileID .'_'. $AttachmentData{Filename};
my $WritingStatus = $Self->{MainObject}->FileWrite(
Location => $FileName,
Content => \$AttachmentData{Content},
Mode => 'binmode',
Permission => '644'
);
if ($WritingStatus) {
# Write a message to stdout that the attachment
# was exported into its file with success.
print STDOUT sprintf("\tWrote attachment to \"%s\"\n", $FileName);
} else {
# An unknown error occured while exporting the attachment
# into a file. Print out all data we have at this point.
print STDERR sprintf(
"\tCould not write file \"%s\" (%d bytes) of content type: %s\n",
$FileName,
$AttachmentData{FilesizeRaw},
$AttachmentData{ContentType}
);
}
}
}
}
sub CreatePDF {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment