ServiceForm.pm 5.29 KB
# --
# Kernel/System/ServiceForm.pm - core module - SeTIC - UFSC - http://setic.ufsc.br/
# Rodrigo Gonçalves - rodrigo.g@ufsc.br
# 
# Version 01/12/2015 - Support for OTRS 4.0.3
#
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --
package Kernel::System::ServiceForm;

use strict;
use warnings;
use utf8;

our @ObjectDependencies = (
"Kernel::System::DB",
"Kernel::System::Log");


sub new {
	my ( $Type, %Param ) = @_;

	# allocate new hash for object
	my $Self = {%Param};
	bless( $Self, $Type );

	return $Self;
}

=head

Returns a service form

=cut

sub GetServiceForm {

	my ( $Self, %Param ) = @_;

	my $DBObject = $Kernel::OM->Get("Kernel::System::DB");

	# get service form from db
	$DBObject->Prepare(
		SQL   => 'SELECT service_id, introduction, form, form_schema, fixed_values, custom_props FROM service_form WHERE service_id = ? and queue_id is null',
		Bind  => [ \$Param{ServiceID} ],
		Limit => 1,
	);

	# fetch the result
	my %ServiceData;
	while ( my @Row = $DBObject->FetchrowArray() ) {
		$ServiceData{ServiceID}    = $Row[0];
		$ServiceData{Introduction} = $Row[1];
		$ServiceData{Form}         = $Row[2];
		$ServiceData{Schema}       = $Row[3];
		$ServiceData{FixedValues}  = $Row[4];
		$ServiceData{CustomProps}  = $Row[5];
	}

	return %ServiceData;
}

=head

Returns a service form for a give queue

=cut

sub GetServiceFormForQueue {

	my ( $Self, %Param ) = @_;

	my $DBObject = $Kernel::OM->Get("Kernel::System::DB");

	# get service form from db
	$DBObject->Prepare(
		SQL   => 'SELECT service_id, introduction, form, form_schema, fixed_values, custom_props FROM service_form WHERE service_id = ? and queue_id = ? ',
		Bind  => [ \$Param{ServiceID}, \$Param{QueueID} ],
		Limit => 1,
	);

	# fetch the result
	my %ServiceData;
	while ( my @Row = $DBObject->FetchrowArray() ) {
		$ServiceData{ServiceID}    = $Row[0];
		$ServiceData{Introduction} = $Row[1];
		$ServiceData{Form}         = $Row[2];
		$ServiceData{Schema}       = $Row[3];
		$ServiceData{FixedValues}  = $Row[4];
		$ServiceData{CustomProps}  = $Row[5];
	}

	return %ServiceData;
}

=head

Saves a service form. 

=cut

sub SaveServiceForm {

	my ( $Self, %Param ) = @_;
	my $DBObject = $Kernel::OM->Get("Kernel::System::DB");
	
    for my $Argument (qw(ServiceID Introduction)) {
        if ( !$Param{$Argument} ) {
            $Kernel::OM->Get("Kernel::System::Log")->Log(
                Priority => 'error',
                Message  => "Need $Argument!",
            );
            return;
        }
    }
	
	my %serviceForm = $Self->GetServiceForm(ServiceID => $Param{ServiceID});
	my $update = ($serviceForm{ServiceID});
	
	if ($update) {		
		$DBObject->Do(SQL => 'UPDATE service_form SET introduction=?,form=?,form_schema=?,fixed_values=?,custom_props=? where service_id=? and queue_id is null',        
        Bind => [
            \$Param{Introduction}, \$Param{Form}, \$Param{Schema}, \$Param{FixedValues}, \$Param{CustomProps}, \$Param{ServiceID}
        ]);		
	} else {
		$DBObject->Do(SQL => 'INSERT INTO service_form(service_id,introduction,form,form_schema,fixed_values,custom_props) VALUES (?,?,?,?,?,?)',        
        Bind => [
            \$Param{ServiceID}, \$Param{Introduction}, \$Param{Form}, \$Param{Schema}, \$Param{FixedValues}, \$Param{CustomProps}
        ]);
	}	

    return 1;

}


=head

Saves a service form for a give queue. 

=cut

sub SaveServiceFormForQueue {

	my ( $Self, %Param ) = @_;
	my $DBObject = $Kernel::OM->Get("Kernel::System::DB");
	
    for my $Argument (qw(ServiceID Introduction)) {
        if ( !$Param{$Argument} ) {
            $Kernel::OM->Get("Kernel::System::Log")->Log(
                Priority => 'error',
                Message  => "Need $Argument!",
            );
            return;
        }
    }
	
	my %serviceForm = $Self->GetServiceFormForQueue(ServiceID => $Param{ServiceID}, QueueID => $Param{QueueID});
	my $update = ($serviceForm{ServiceID});
	
	if ($update) {		
		$DBObject->Do(SQL => 'UPDATE service_form SET introduction=?,form=?,form_schema=?,fixed_values=?,custom_props=? where service_id=? and queue_id=? ',        
        Bind => [
            \$Param{Introduction}, \$Param{Form}, \$Param{Schema}, \$Param{FixedValues}, \$Param{CustomProps}, \$Param{ServiceID}, \$Param{QueueID}
        ]);		
	} else {
		$DBObject->Do(SQL => 'INSERT INTO service_form(service_id,introduction,form,form_schema,queue_id,fixed_values,custom_props) VALUES (?,?,?,?,?,?,?)',        
        Bind => [
            \$Param{ServiceID}, \$Param{Introduction}, \$Param{Form}, \$Param{Schema}, \$Param{QueueID}, \$Param{FixedValues}, \$Param{CustomProps}
        ]);
	}	

    return 1;

}


=head

Removes a service form for a give queue. 

=cut

sub RemoveServiceFormForQueue {

	my ( $Self, %Param ) = @_;
	my $DBObject = $Kernel::OM->Get("Kernel::System::DB");
	
    for my $Argument (qw(ServiceID QueueID)) {
        if ( !$Param{$Argument} ) {
            $Kernel::OM->Get("Kernel::System::Log")->Log(
                Priority => 'error',
                Message  => "Need $Argument!",
            );
            return;
        }
    }
	
	$DBObject->Do(SQL => 'DELETE FROM service_form WHERE service_id=? AND queue_id=? ',
				  Bind => [\$Param{ServiceID}, \$Param{QueueID}]);

    return 1;
}

1;