2023-05-26 16:20:38 +02:00
# --
# OTOBO is a web-based ticketing system for service organisations.
# --
# Copyright (C) 2001-2020 OTRS AG, https://otrs.com/
# Copyright (C) 2019-2023 Rother OSS GmbH, https://otobo.de/
# --
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
# --
package Kernel::System::Console::Command::Admin::DynamicField::Add ;
use strict ;
use warnings ;
# core modules
use List::Util qw( max ) ;
# CPAN modules
# OTOBO modules
use parent qw( Kernel::System::Console::BaseCommand ) ;
our @ ObjectDependencies = (
'Kernel::System::DynamicField' ,
'Kernel::System::YAML' ,
'Kernel::System::JSON' ,
) ;
sub Configure {
my ( $ Self , % Param ) = @ _ ;
$ Self - > Description ( 'Create a new dynamic field.' ) ;
$ Self - > AddOption (
Name = > 'name' ,
Description = > 'Name for the new field.' ,
Required = > 1 ,
HasValue = > 1 ,
ValueRegex = > qr/[A-Za-z0-9]+/ smx ,
) ;
$ Self - > AddOption (
Name = > 'label' ,
Description = > 'Label which will be displayed alongside the field.' ,
Required = > 1 ,
HasValue = > 1 ,
ValueRegex = > qr/.*/ smx ,
) ;
$ Self - > AddOption (
Name = > 'field-type' ,
Description = > 'Dynamic field type.' ,
Required = > 1 ,
HasValue = > 1 ,
ValueRegex = > qr/[A-Za-z0-9]/ smx ,
) ;
$ Self - > AddOption (
Name = > 'object-type' ,
Description = > 'Object type for the new field.' ,
Required = > 1 ,
HasValue = > 1 ,
ValueRegex = > qr/[A-Za-z0-9]/ smx ,
) ;
$ Self - > AddOption (
Name = > 'config' ,
2023-05-28 18:18:26 +02:00
Description = >
'Config for dynamic field. Takes either an YAML or JSON string. See also Admin::DynamicField::ConfigDump and Admin::DynamicField::ConfigBuild.' ,
Required = > 0 ,
HasValue = > 1 ,
ValueRegex = > qr/.*/ smx ,
2023-05-26 16:20:38 +02:00
) ;
$ Self - > AddOption (
Name = > 'namespace' ,
Description = > 'Namespace to place the new field into.' ,
Required = > 0 ,
HasValue = > 1 ,
ValueRegex = > qr/[A-Za-z0-9]+/ smx ,
) ;
$ Self - > AddOption (
Name = > 'field-order' ,
2023-05-28 18:18:26 +02:00
Description = >
'Field order to place the field at. Defaults to hightest order plus 1. Per default, other fields are reordered if an occupied order number is chosen.' ,
Required = > 0 ,
HasValue = > 1 ,
ValueRegex = > qr/\d+/ smx ,
2023-05-26 16:20:38 +02:00
) ;
$ Self - > AddOption (
Name = > 'reorder' ,
Description = > 'Whether to reorder other fields if an occupied order number is chosen. Defaults to 1.' ,
Required = > 0 ,
HasValue = > 1 ,
ValueRegex = > qr/\d/ smx ,
) ;
$ Self - > AddOption (
Name = > 'valid' ,
Description = > 'Whether the field to add is valid or invalid. Defaults to 1.' ,
Required = > 0 ,
HasValue = > 1 ,
ValueRegex = > qr/\d/ smx ,
) ;
2023-05-28 18:18:26 +02:00
$ Self - > AddOption (
Name = > 'stdin' ,
Description = > 'Read config from STDIN.' ,
Required = > 0 ,
HasValue = > 0 ,
) ;
2023-05-26 16:20:38 +02:00
return ;
}
sub Run {
my ( $ Self , % Param ) = @ _ ;
$ Self - > Print ( "<yellow>Adding a new dynamic field...</yellow>\n" ) ;
# get necessary objects
my $ DynamicFieldObject = $ Kernel:: OM - > Get ( 'Kernel::System::DynamicField' ) ;
2023-05-28 18:18:26 +02:00
# check field order
2023-05-26 16:20:38 +02:00
my $ DynamicFieldList = $ DynamicFieldObject - > DynamicFieldListGet (
ObjectType = > 'All' ,
2023-05-28 18:18:26 +02:00
Valid = > 0 ,
2023-05-26 16:20:38 +02:00
) ;
2023-05-28 18:18:26 +02:00
my $ FieldOrder = $ Self - > GetOption ( 'field-order' ) || max map { $ _ - > { FieldOrder } } $ DynamicFieldList - > @ * ;
2023-05-26 16:20:38 +02:00
# check config string
2023-05-28 18:18:26 +02:00
my $ PlainConfig ;
# either fetch config from stdin or param
if ( $ Self - > GetOption ( 'stdin' ) ) {
$ Self - > Print ( "<green>Reading config from stdin...</green>\n" ) ;
while ( my $ Line = <STDIN> ) { ## no critic qw(ProhibitExplicitStdin)
$ PlainConfig . = $ Line ;
}
}
elsif ( $ Self - > GetOption ( 'config' ) ) {
$ PlainConfig = $ Self - > GetOption ( 'config' ) ;
}
else {
$ Self - > Print ( "<red>Either param config or param stdin has to be provided!</red>" ) ;
$ Self - > ExitCodeError ( ) ;
}
2023-05-26 16:20:38 +02:00
2023-05-28 18:18:26 +02:00
# trying to parse as yaml and as json - if both fails, not valid
2023-05-26 16:20:38 +02:00
$ Self - > Print ( "<yellow>Trying to parse config as YAML...</yellow>\n" ) ;
my $ ConfigHashRef = $ Kernel:: OM - > Get ( 'Kernel::System::YAML' ) - > Load (
Data = > $ PlainConfig ,
) ;
if ( ! defined $ ConfigHashRef ) {
$ Self - > Print ( "<yellow>Parsing config as YAML failed. Trying JSON now...</yellow>\n" ) ;
$ ConfigHashRef = $ Kernel:: OM - > Get ( 'Kernel::System::JSON' ) - > Decode (
Data = > $ PlainConfig ,
) ;
if ( ! defined $ ConfigHashRef ) {
$ Self - > Print ( "<red>Parsing config as JSON failed also. Aborting.</red>\n" ) ;
return $ Self - > ExitCodeError ( ) ;
}
2023-05-28 18:18:26 +02:00
}
2023-05-26 16:20:38 +02:00
# check namespace
my $ Namespace = $ Self - > GetOption ( 'namespace' ) ;
2023-05-28 18:18:26 +02:00
my $ Name = $ Self - > GetOption ( 'name' ) ;
if ( $ Namespace ) {
2023-05-26 16:20:38 +02:00
$ Name = "$Namespace-$Name" ;
2023-05-28 18:18:26 +02:00
}
2023-05-26 16:20:38 +02:00
# add queue
my $ Success = $ Kernel:: OM - > Get ( 'Kernel::System::DynamicField' ) - > DynamicFieldAdd (
2023-05-28 18:18:26 +02:00
Name = > $ Name ,
Label = > $ Self - > GetOption ( 'label' ) ,
FieldOrder = > $ FieldOrder ,
FieldType = > $ Self - > GetOption ( 'field-type' ) ,
ObjectType = > $ Self - > GetOption ( 'object-type' ) ,
Config = > $ ConfigHashRef ,
Reorder = > $ Self - > GetOption ( 'reorder' ) || 1 ,
ValidID = > $ Self - > GetOption ( 'valid' ) || 1 ,
UserID = > 1 ,
2023-05-26 16:20:38 +02:00
) ;
# error handling
if ( ! $ Success ) {
$ Self - > PrintError ( "Can't create dynamic field.\n" ) ;
return $ Self - > ExitCodeError ( ) ;
}
$ Self - > Print ( "<green>Done.</green>\n" ) ;
return $ Self - > ExitCodeOk ( ) ;
}
1 ;