NewTicketWizardServiceForm.pm 7.58 KB
# --
# 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.
#
# Frontend module for customizing service forms - SeTIC - UFSC - http://setic.ufsc.br/
# Rodrigo Gonçalves - rodrigo.g@ufsc.br
#
# Version 01/08/2015 - Support for OTRS 4.0.3
# Version 2017-12-18 - Support for OTRS 6 and module publishing
#
# --
package Kernel::Modules::NewTicketWizardServiceForm;

use strict;
use warnings;

use Kernel::System::VariableCheck qw(:all);

our @ObjectDependencies = (
	"Kernel::System::Web::Request", "Kernel::System::DB",
	"Kernel::Output::HTML::Layout", "KerneL::System::Log",
	"Kernel::Config",               "Kernel::System::DynamicField::Backend",
	"Kernel::System::Service",      "Kernel::System::ServiceForm",
	"Kernel::System::SystemAddress"
);

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

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

	return $Self;
}

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

	my $ParamObject       = $Kernel::OM->Get("Kernel::System::Web::Request");
	my $subaction = $ParamObject->GetParam( Param => "Subaction" );
	
	if ( $subaction ) {
		if ( $subaction eq 'QueueFormRemoval' ) {
			my $serviceID = $ParamObject->GetParam( Param => "ServiceID" );
			my $queueID   = $ParamObject->GetParam( Param => "QueueID" );

			if ( $serviceID && $queueID ) {
				return $Self->ServiceQueueFormRemoval();
			} else {
				return $Self->ServiceEdit();
			}
		}
		elsif ( $subaction eq 'ServiceEdit' ) {
			return $Self->ServiceEdit();
		}
		elsif ( $subaction eq 'ServiceSave' ) {
			return $Self->ServiceSave();
		}
	}
	else {
		return $Self->Overview();
	}
}

sub ServiceQueueFormRemoval {
	my ( $Self, %Param ) = @_;
	my $ParamObject       = $Kernel::OM->Get("Kernel::System::Web::Request");
	my $ServiceFormObject = $Kernel::OM->Get("Kernel::System::ServiceForm");
	my $LayoutObject      = $Kernel::OM->Get("Kernel::Output::HTML::Layout");
	my $QueueObject       = $Kernel::OM->Get("Kernel::System::Queue");

	my $serviceID = $ParamObject->GetParam( Param => "ServiceID" );
	my $queueID   = $ParamObject->GetParam( Param => "QueueID" );
	
	$ServiceFormObject->RemoveServiceFormForQueue(ServiceID => $serviceID, QueueID => $queueID);
	
	return $Self->Overview();	
}

sub ServiceEdit {
	my ( $Self, %Param ) = @_;
	my $ParamObject       = $Kernel::OM->Get("Kernel::System::Web::Request");
	my $ServiceFormObject = $Kernel::OM->Get("Kernel::System::ServiceForm");
	my $ServiceObject 	  = $Kernel::OM->Get("Kernel::System::Service");
	my $LayoutObject      = $Kernel::OM->Get("Kernel::Output::HTML::Layout");
	my $QueueObject       = $Kernel::OM->Get("Kernel::System::Queue");
	my %Data = ();

	# header
	my $Output = $LayoutObject->Header();
	$Output .= $LayoutObject->NavigationBar();

	my %serviceForm;

	# if form specific for queue
	if ( $ParamObject->GetParam( Param => "QueueID" ) ) {
		%serviceForm = $ServiceFormObject->GetServiceFormForQueue(
			ServiceID => $ParamObject->GetParam( Param => "ServiceID" ),
			QueueID   => $ParamObject->GetParam( Param => "QueueID" )
		);
	}
	else {
		%serviceForm = $ServiceFormObject->GetServiceForm(
			ServiceID => $ParamObject->GetParam( Param => "ServiceID" ) );
	}

	$Data{Introduction} = $serviceForm{Introduction};
	$Data{Form}         = $serviceForm{Form};
	$Data{Schema}       = $serviceForm{Schema};
	$Data{FixedValues}  = $serviceForm{FixedValues};
	$Data{CustomProps}  = $serviceForm{CustomProps};
	$Data{ServiceID}    = $ParamObject->GetParam( Param => "ServiceID" );
	$Data{QueueID}      = $ParamObject->GetParam( Param => "QueueID" );
	
	my %serviceInfo = $ServiceObject->ServiceGet(ServiceID => $Data{ServiceID}, UserID => $Self->{UserID});
	$Data{ServiceName}    = $serviceInfo{Name};

	my %queueList = $QueueObject->QueueList( Valid => 1 );

	if ( $Data{QueueID} ) {
		$Self->Debug( "Specific queue selected: " . $Data{QueueID} );
		my %queueInfo = $QueueObject->QueueGet(ID => $Data{QueueID});
		$Data{QueueName}    = $queueInfo{Name};
	}
	else {
		$Self->Debug("No queue selected");
	}

	$Data{QueuesStrg} = $LayoutObject->AgentQueueListOption(
		Data           => { %queueList, '' => '-' },
		Multiple       => 0,
		Size           => 0,
		Class          => '',
		Name           => 'QueueID',
		SelectedID     => $Data{QueueID},
		TreeView       => 1,
		OnChangeSubmit => 1,
	);

	if ( $Data{QueueID} ) {
		$LayoutObject->Block(
			Name => 'CustomQueueRemoval',
			Data => \%Data,
		);
	}

	$Output .= $LayoutObject->Output(
		Data         => \%Data,
		TemplateFile => 'NewTicketWizardServiceFormEdit',
	);

	# html output
	$Output .= $LayoutObject->Footer();

	return $Output;
}

sub ServiceSave {

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

	my $ParamObject       = $Kernel::OM->Get("Kernel::System::Web::Request");
	my $ServiceFormObject = $Kernel::OM->Get("Kernel::System::ServiceForm");
	my $LayoutObject      = $Kernel::OM->Get("Kernel::Output::HTML::Layout");
	my $QueueObject       = $Kernel::OM->Get("Kernel::System::Queue");

	# header
	my $Output = $LayoutObject->Header();
	$Output .= $LayoutObject->NavigationBar();

	# If form is specific for a queue
	if ( $ParamObject->GetParam( Param => "QueueID" ) ) {
		$ServiceFormObject->SaveServiceFormForQueue(
			ServiceID    => $ParamObject->GetParam( Param => "ServiceID" ),
			QueueID      => $ParamObject->GetParam( Param => "QueueID" ),
			Introduction => $ParamObject->GetParam( Param => "Introduction" ),
			Form         => $ParamObject->GetParam( Param => "Form" ),
			Schema       => $ParamObject->GetParam( Param => "Schema" ),
			CustomProps  => $ParamObject->GetParam( Param => "CustomProps" ),
			FixedValues  => $ParamObject->GetParam( Param => "FixedValues" ),
		);
	}
	else {
		$ServiceFormObject->SaveServiceForm(
			ServiceID    => $ParamObject->GetParam( Param => "ServiceID" ),
			Introduction => $ParamObject->GetParam( Param => "Introduction" ),
			Form         => $ParamObject->GetParam( Param => "Form" ),
			Schema       => $ParamObject->GetParam( Param => "Schema" ),
			CustomProps  => $ParamObject->GetParam( Param => "CustomProps" ),
			FixedValues  => $ParamObject->GetParam( Param => "FixedValues" ),
		);
	}

	return $Self->Overview();

}

sub Overview {

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

	my %Data = ();

	my $LayoutObject  = $Kernel::OM->Get("Kernel::Output::HTML::Layout");
	my $ServiceObject = $Kernel::OM->Get("Kernel::System::Service");
	my $LanguageObject = $Kernel::OM->Get("Kernel::Language");

	# build output
	$LayoutObject->Block(
		Name => 'Overview',
		Data => { %Param, },
	);

	$LayoutObject->Block(
		Name => 'OverviewList',
		Data => { %Param, },
	);

	# get service list
	my $ServiceList = $ServiceObject->ServiceListGet(
		Valid  => 1,
		UserID => $Self->{UserID},
	);

	# if there are any services defined, they are shown
	if ( @{$ServiceList} ) {

		# sort the service list by long service name
		@{$ServiceList} =
		  sort { $a->{Name} . '::' cmp $b->{Name} . '::' } @{$ServiceList};

		for my $ServiceData ( @{$ServiceList} ) {
			if ( !grep { index( $_->{Name}, $ServiceData->{Name} . "::" ) >= 0 }
				@{$ServiceList} )
			{

				# output row
				$LayoutObject->Block(
					Name => 'OverviewListRow',
					Data => { %{$ServiceData} },
				);
			}
		}
	}

	my $Output = $LayoutObject->Header(
		Title => $LanguageObject->Translate("Service Form") );

	$Output .= $LayoutObject->NavigationBar();
	$Output .= $LayoutObject->Output(
		Data         => \%Data,
		TemplateFile => 'NewTicketWizardServiceForm',
	);
	$Output .= $LayoutObject->Footer();
	return $Output;
}

sub Debug {
	my $Self = shift;
	my $msg  = shift;
	$Kernel::OM->Get("Kernel::System::Log")
	  ->Log( Priority => 'debug', Message => $msg );
}

1;