App-OTRS-CreateTicket

 view release on metacpan or  search on metacpan

bin/otrs.CreateTicket.pl  view on Meta::CPAN

    'PendingTime=s',
    # options for article
    @ArticleOptions,
    # dynamic fields; can be multiple
    'DynamicField=s%',
    # attachments; can be multiple
    'Attachment=s@',
);

if ( $Param{help} || !$Param{Url} && !$Param{Server} ) {
    pod2usage( -exitstatus => 0 );
}

my $URL;

# if we do not have a URL, compose one
if ( !$Param{Url} ) {
    my $Server = $Param{Server} || 'localhost';
    my $HTTPType = $Param{Ssl} ? 'https://' : 'http://';
    $URL = $HTTPType . $Server . '/otrs/nph-genericinterface.pl/Webservice/GenericTicketConnector'
}
else {
    $URL = $Param{Url};
}

# handle attachments
my @Attachments;
if (defined $Param{Attachment}) {

    for my $File ( @{$Param{Attachment}} ) {
        die "Can't find attachment '$File'.\n" if !-f $File;

        # open attachment && read in
        open my $fh, "<:unix", $File or die "Couldn't open $File: $!\n";
        read $fh, my $Content, -s $fh or die "Couldn't read $File: $!";
        close $fh;

        # add to array with  basename of file and filename
        push @Attachments, \SOAP::Data->name(Attachment => SOAP::Data->value(
         SOAP::Data->name(Content     => encode_base64($Content)),
         SOAP::Data->name(ContentType => mimetype($File)),
         SOAP::Data->name(Filename    => basename($File)),
        ));
  }
}

# this name space should match the specified name space in the SOAP transport for the web service
my $NameSpace = $Param{Namespace} || 'http://www.otrs.org/TicketConnector/';

# this is operation to execute, it could be TicketCreate, TicketUpdate, TicketGet, TicketSearch
# or SessionCreate. and they must to be defined in the web service.
my $Operation = $Param{Operation} || 'TicketCreate';

# assign values for ticket and article data if undefined
$Param{Queue}    ||= 'Postmaster';
$Param{Priority} ||= '3 normal';
$Param{Type}     ||= 'default';
$Param{Title}    ||= 'No title';
$Param{State}    ||= 'new';

$Param{ContentType} ||= 'text/plain; charset=utf8';
$Param{Subject}     ||= $Param{Title};
$Param{SenderType}  ||= 'customer';
$Param{TimeUnit}    ||= 0;

if ( $Param{BodyFile} ) {
    open my $Filehandle, '<', $Param{BodyFile} or die "Can't open file $Param{BodyFile}: $!";
    # read in file at once as in PBP
    $Param{Body} = do { local $/; <$Filehandle> };
    close $Filehandle;
} elsif ( !$Param{Body} ) {
    binmode STDIN;
    while ( my $Line = <> ) {
        $Param{Body} .= $Line;
    }
}

# handle PendingTime, if it's a number, add it as minutes to current time.
# otherwise, parse it as a string in YYYY-MM-DDTHH:MM format
my $PendingTime;
if ( defined $Param{PendingTime} && $Param{PendingTime} =~ /^\d*$/) {
    $PendingTime = localtime;
    $PendingTime += (60 * $Param{PendingTime});
}
elsif (defined $Param{PendingTime} ) {

    $PendingTime = Time::Piece->strptime($Param{PendingTime}, "%Y-%m-%dT%R");
}

# Converting Ticket and Article data into SOAP data structure
my @TicketData;
for my $Element (@TicketFields) {
    if ( defined $Param{$Element} ) {
        my $Param = SOAP::Data->name( $Element => $Param{$Element} );
        $Param->type('string');
        push @TicketData, $Param;
    }
}

if ( $PendingTime ) {

    # create SOAP datastructure containing time elements
    my @PendingData;
    push @PendingData, SOAP::Data->name( Year => $PendingTime->year);
    push @PendingData, SOAP::Data->name( Month => $PendingTime->mon);
    push @PendingData, SOAP::Data->name( Day => $PendingTime->mday);
    push @PendingData, SOAP::Data->name( Hour => $PendingTime->hour);
    push @PendingData, SOAP::Data->name( Minute => $PendingTime->minute);

    # add datastructure to ticket tree
    push @TicketData, SOAP::Data->name( PendingTime => \SOAP::Data->value(@PendingData));
}

my @ArticleData;
for my $Element (@ArticleFields) {
    if ( defined $Param{$Element} ) {
        my $Param = SOAP::Data->name( $Element => $Param{$Element} );
        $Param->type('string');
        push @ArticleData, $Param;
    }
}



( run in 2.600 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )