Commit 6392bfc3 authored by Timmer, Marius's avatar Timmer, Marius

Added the functionality to export the attachments as well (not tested yet)

parent 5b6ddeb6
......@@ -37,6 +37,12 @@ sub Configure {
HasValue => 1,
ValueRegex => qr/.*/smx
);
$Self->AddOption(
Name => 'attachment',
Description => 'Includes the ticket attachment to the exported files',
Required => 0,
HasValue => 0
);
return;
}
......@@ -46,6 +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 $TicketExportObject = $Kernel::OM->Get('Kernel::System::TicketExport');
if (!$TicketExportObject) {
......@@ -63,7 +70,7 @@ sub Run {
if (!$QueueID) {
# There is no queue id for the given queue name which means the user
# gave us a wrong name. Report this fatal error and stop execution.
$Self->PrintError("This queue is not known.\n");
$Self->PrintError("This queue is not known. Keep in mind that the queue name is case-sensitive.\n");
return $Self->ExitCodeError();
}
......@@ -89,6 +96,7 @@ sub Run {
my $ExportedTickets = $TicketExportObject->Run(
QueueID => $QueueID,
Filepath => $Filepath,
Attachment => $Attachment,
UserID => 1,
Print => 1
);
......
......@@ -19,13 +19,14 @@ our @ObjectDependencies = (
sub new {
my ($Type, %Param) = @_;
my $Self = {
DBObject => $Kernel::OM->Get('Kernel::System::DB'),
EncodeObject => $Kernel::OM->Get('Kernel::System::Encode'),
GroupObject => $Kernel::OM->Get('Kernel::System::Group'),
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')
DBObject => $Kernel::OM->Get('Kernel::System::DB'),
EncodeObject => $Kernel::OM->Get('Kernel::System::Encode'),
GroupObject => $Kernel::OM->Get('Kernel::System::Group'),
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'),
ArticleObject => $Kernel::OM->Get('Kernel::System::Ticket::Article')
};
bless($Self, $Type);
return $Self;
......@@ -38,10 +39,11 @@ Exports all tickets and writes them into files.
my $NumberOfWrittenTickets = $TicketExportObject->Run(
QueueID => 1,
Filepath => './',
Attachment => 0,
Print => 1
);
=cut;
=cut
sub Run {
my ($Self, %Param) = @_;
......@@ -53,6 +55,7 @@ sub Run {
}
my $QueueID = $Param{QueueID};
my $Filepath = $Param{Filepath};
$Self->{Attachment} = $Param{Attachment} || 0;
$Self->{UserID} = $Param{UserID} || 1;
my $QueueName = $Self->{QueueObject}->QueueLookup(
......@@ -104,6 +107,16 @@ sub Run {
$SucceedTickets++;
}
}
if ($Self->{Attachment}) {
# The user wants to store the attachments of the ticket articles
# as well. This will be performed in this code block.
$Self->StoreAttachments(
TicketID => $TicketID,
FilePrefix => $Filepath . $QueueName .'_'. $TicketID .'_'. $TicketData{TicketNumber} .'_'
);
}
if ($Param{Print} && $Param{Print} == 1) {
print STDOUT "[". $PrintStatus ."]\r\n";
}
......@@ -111,6 +124,70 @@ sub Run {
return $SucceedTickets;
}
=item StoreAttachments()
Reads out all attachments of the latest article for the given TicketID and
writes it into a file. The contents should be written into a file which is
called just like the exported ticket itself having the original file name
of the attachment at the end.
$Self->StoreAttachments(
TicketID => $TicketID,
FilePrefix => '/path/to/exports/filenameprefix'
);
=cut
sub StoreAttachments {
my ($Self, %Param) = @_;
for my $Key (qw(TicketID FilePrefix)) {
if (!$Param{$Key}) {
print STDERR sprintf('"%s" is required!', $Key);
return;
}
}
my @TicketHistory = $Self->{TicketObject}->HistoryGet(
TicketID => $Param{TicketID},
UserID => $Self->{UserID}
);
my %LatestVersion = pop(@TicketHistory);
my $ArticleID = $LatestVersion{ArticleID};
my %Attachments = $Self->{ArticleObject}->ArticleAttachmentIndex(
ArticleID => $ArticleID,
UserID => $Self->{UserID}
);
for my $FileID (sort keys %Attachments) {
my %AttachmentData = $Self->{TicketObject}->ArticleAttachment(
ArticleID => $ArticleID,
FileID => $FileID,
UserID => $Self->{UserID}
);
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}
);
}
}
}
sub CreatePDF {
my ($Self, %Param) = @_;
......
......@@ -2,7 +2,7 @@
## What this plugin does
OTRS by itself is able to print single tickets to single pdf files. This plugin uses this function to export all tickets of a queue into separated files for each ticket.
OTRS by itself is able to print single tickets to single pdf files. This plugin uses this function to export all tickets of a queue into separated files for each ticket. Additionally the attachments will be stored in separate files having the same name prefix like the exported ticket itself and the original file name of the attachment.
## Installation
......
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