Commit 9392c1c8 authored by Timmer, Marius's avatar Timmer, Marius

Upgrade

Upgraded the sources to fit in OTRS 6.
parent b06e3bc4
This diff is collapsed.
......@@ -13,68 +13,68 @@ use base qw(Kernel::System::Console::BaseCommand);
use Kernel::System::VariableCheck qw(:all);
our @ObjectDependencies = (
'Kernel::Language',
'Kernel::System::Console::BaseCommand',
'Kernel::System::Queue',
'Kernel::System::TicketExport'
'Kernel::Language',
'Kernel::System::Console::BaseCommand',
'Kernel::System::Queue',
'Kernel::System::TicketExport'
);
sub Configure {
my ($Self, %Param) = @_;
my ($Self, %Param) = @_;
$Self->Description('Exports each ticket of a queue into PDF files.');
$Self->AddOption(
Name => 'queue',
Description => 'Gives the name of the queue which you want to export.',
Required => 1,
HasValue => 1,
ValueRegex => qr/.*/smx
);
$Self->AddOption(
Name => 'filepath',
Description => 'Location of the pdf files. The queue name and ticket ids will be appended to the file names.',
Required => 1,
HasValue => 1,
ValueRegex => qr/.*/smx
);
$Self->AddOption(
Name => 'attachment',
Description => 'Includes the tickets attachments to the exported files',
Required => 0,
HasValue => 0
);
return;
$Self->Description('Exports each ticket of a queue into PDF files.');
$Self->AddOption(
Name => 'queue',
Description => 'Gives the name of the queue which you want to export.',
Required => 1,
HasValue => 1,
ValueRegex => qr/.*/smx
);
$Self->AddOption(
Name => 'filepath',
Description => 'Location of the pdf files. The queue name and ticket ids will be appended to the file names.',
Required => 1,
HasValue => 1,
ValueRegex => qr/.*/smx
);
$Self->AddOption(
Name => 'attachment',
Description => 'Includes the tickets attachments to the exported files',
Required => 0,
HasValue => 0
);
return;
}
sub Run {
my ($Self, %Param) = @_;
my ($Self, %Param) = @_;
# Read out the arguments from the command line
my $QueueName = $Self->GetOption('queue');
my $Filepath = $Self->GetOption('filepath');
my $Attachments = defined($Self->GetOption('attachment')) ? 1 : 0;
# Read out the arguments from the command line
my $QueueName = $Self->GetOption('queue');
my $Filepath = $Self->GetOption('filepath');
my $Attachments = defined($Self->GetOption('attachment')) ? 1 : 0;
my $TicketExportObject = $Kernel::OM->Get('Kernel::System::TicketExport');
if (!$TicketExportObject) {
# Could not create a new instance of the TicketExport package. That is
# because the constructor returned undef which is a hint for a internal
# error. Inform the user and abort here.
$Self->PrintError("Could not create a new instance of TicketExport.\n");
return $Self->ExitCodeError();
}
my $TicketExportObject = $Kernel::OM->Get('Kernel::System::TicketExport');
if (!$TicketExportObject) {
# Could not create a new instance of the TicketExport package. That is
# because the constructor returned undef which is a hint for a internal
# error. Inform the user and abort here.
$Self->PrintError("Could not create a new instance of TicketExport.\n");
return $Self->ExitCodeError();
}
# In further execution the queue id is used instead of the name
my $QueueID = $Kernel::OM->Get('Kernel::System::Queue')->QueueLookup(
Queue => $QueueName
);
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. Keep in mind that the queue name is case-sensitive.\n");
return $Self->ExitCodeError();
}
# In further execution the queue id is used instead of the name
my $QueueID = $Kernel::OM->Get('Kernel::System::Queue')->QueueLookup(
Queue => $QueueName
);
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. Keep in mind that the queue name is case-sensitive.\n");
return $Self->ExitCodeError();
}
# Prepare the location we want to store the exported tickets to. We
# Prepare the location we want to store the exported tickets to. We
# have to check if the location ends with a slash and append it if
# needed. After that we have to make sure the directory exists.
if (substr($Filepath, -1) ne '/') {
......@@ -83,34 +83,34 @@ sub Run {
}
if (! -d $Filepath) {
# The directory does not exist. The exported pdf files have to be stored
# in the given directory which is not possible. Inform the user about it
# and abort the execution.
$Self->PrintError(sprintf(
'Directory %s not found!',
$Filepath
));
return $Self->ExitCodeError();
# in the given directory which is not possible. Inform the user about it
# and abort the execution.
$Self->PrintError(sprintf(
'Directory %s not found!',
$Filepath
));
return $Self->ExitCodeError();
}
# Do the export
my $ExportedTickets = $TicketExportObject->Run(
QueueID => $QueueID,
Filepath => $Filepath,
Attachments => $Attachments,
UserID => 1,
Print => 1
);
# Do the export
my $ExportedTickets = $TicketExportObject->Run(
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("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.
$Self->Print("No files has been written!\n");
}
return $Self->ExitCodeOk();
if ($ExportedTickets) {
# There are some exported tickets. Tell the user how many tickets
# were exported to a own PDF file.
$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.
$Self->Print("No files has been written!\n");
}
return $Self->ExitCodeOk();
}
1;
......@@ -113,9 +113,9 @@ sub Run {
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.
# 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,
......@@ -156,69 +156,70 @@ sub StoreAttachments {
TicketID => $Param{TicketID},
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;
}
}
# 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'
);
# 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;
}
}
# 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},
ExcludePlainText => 0,
ExcludeHTMLBody => 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 ($Print == 1) {
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}
);
}
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}
);
}
}
}
}
}
}
}
=item CreatePDF()
......@@ -272,21 +273,21 @@ Executes the sql query to get all the ids of the tickets in a specific queue.
=cut
sub GetTicketList {
my ($Self, %Param) = @_;
if (!$Param{QueueID}) {
return;
}
my $Query = 'SELECT id FROM ticket WHERE queue_id = ?;';
my @TicketIDs;
$Self->{DBObject}->Prepare(
SQL => $Query,
Bind => [ \$Param{QueueID} ]
);
while (my ($ID) = $Self->{DBObject}->FetchrowArray()) {
push(@TicketIDs, $ID);
}
return @TicketIDs;
my ($Self, %Param) = @_;
if (!$Param{QueueID}) {
return;
}
my $Query = 'SELECT id FROM ticket WHERE queue_id = ?;';
my @TicketIDs;
$Self->{DBObject}->Prepare(
SQL => $Query,
Bind => [ \$Param{QueueID} ]
);
while (my ($ID) = $Self->{DBObject}->FetchrowArray()) {
push(@TicketIDs, $ID);
}
return @TicketIDs;
}
1;
# ExportTicketsToPDF - A Plugin for OTRS
# ExportTicketsToPDF
## What this plugin does
OTRS by default is able to print single tickets including the attachments to a single pdf file using the web frontend. This plugin uses the already existing function to export all tickets within 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. This feature will be accessable in the command line for the admins.
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.
## Build
To build the OPM file of this plugin you just need to use the sources in this repository according to the official OTRS documentation: [Package Build](https://otrs.github.io/doc/manual/developer/5.0/en/html/package-building.html#package-build "Documentation").
## Installation
To install this plugin you just need to build the OPM file from the sources in this repository. [Package Build](https://otrs.github.io/doc/manual/developer/5.0/en/html/package-building.html#package-build "Documentation") After that you can simply install the OPM file using the package manager in your admin menu or the command line.
## Installation
After generating (*or downloading*) the OPM file of the plugin you can simply install it using the web frontend (Admin menu, packages) or you use the command line option in the otrs.Console.pl script.
Keep in mind that this plugin is not verified by the OTRS group which means you will see a annoying warning in your package administration from now on. We hope you can live with that.
## How it works
To export the tickets of a queue you need to use the command line. The otrs.Console will offer you the new option `Admin::Queue::TicketExport` which is the plugin. You can use the following arguments:
* `queue`: Name of the queue whose tickets should be printed
......
<?xml version="1.0" encoding="utf-8" ?>
<otrs_package version="1.0">
<Name>ExportTicketsToPDF</Name>
<Version>5.0.11</Version>
<Framework>5.0.x</Framework>
<Version>6.0.0</Version>
<Framework>6.0.x</Framework>
<Vendor>Westfälische Wilhelms-Universität Münster</Vendor>
<URL>https://www.uni-muenster.de/</URL>
<License>GNU General Public License, version 3</License>
<ChangeLog Version="6.0.0" Date="2018-01-23">Upgraded package for OTRS 6.</ChangeLog>
<ChangeLog Version="5.0.11" Date="2017-05-11">Attachments can be exported now.</ChangeLog>
<ChangeLog Version="1.0.0" Date="2016-07-27 10:42:00">Append file extension if not done by the user and print the ticket ids to the pdf document.</ChangeLog>
<ChangeLog Version="0.9.3" Date="2016-07-26 15:25:00">Renamed the plugin from "MultipleTicketExport" to "ExportTicketsToPDF".</ChangeLog>
<ChangeLog Version="0.9.0" Date="2016-07-25 17:37:00">First version of the ExportTicketToPDF plugin which allows to export all tickets of a queue into a pdf file.</ChangeLog>
<Description Lang="en">This plugin provides the opportunity to export all tickets in a queue.</Description>
<Description Lang="en">This plugin provides the ability to export all tickets of a queue.</Description>
<Description Lang="de">Dieses Plugin ermöglicht das Exportieren aller Tickets einer Queue.</Description>
<Filelist>
<File Permission="644" Location="Kernel/System/Console/Command/Admin/Queue/TicketExport.pm"></File>
......
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