190 lines
6.0 KiB
Perl
190 lines
6.0 KiB
Perl
# --
|
|
# 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->AddArgument(
|
|
Name => 'name',
|
|
Description => 'Name for the new field.',
|
|
Required => 1,
|
|
ValueRegex => qr/[A-Za-z0-9]+/smx,
|
|
);
|
|
$Self->AddArgument(
|
|
Name => 'label',
|
|
Description => 'Label which will be displayed alongside the field.',
|
|
Required => 1,
|
|
ValueRegex => qr/.*/smx,
|
|
);
|
|
$Self->AddArgument(
|
|
Name => 'field-type',
|
|
Description => 'Dynamic field type.',
|
|
Required => 1,
|
|
ValueRegex => qr/[A-Za-z0-9]/smx,
|
|
);
|
|
$Self->AddArgument(
|
|
Name => 'object-type',
|
|
Description => 'Object type for the new field.',
|
|
Required => 1,
|
|
ValueRegex => qr/[A-Za-z0-9]/smx,
|
|
);
|
|
$Self->AddArgument(
|
|
Name => 'config',
|
|
Description =>
|
|
'Config for dynamic field. Takes either an YAML or JSON string. See also Admin::DynamicField::ConfigDump and Admin::DynamicField::ConfigBuild.',
|
|
Required => 0,
|
|
ValueRegex => qr/.*/smx,
|
|
);
|
|
$Self->AddArgument(
|
|
Name => 'namespace',
|
|
Description => 'Namespace to place the new field into.',
|
|
Required => 0,
|
|
ValueRegex => qr/[A-Za-z0-9]+/smx,
|
|
);
|
|
$Self->AddArgument(
|
|
Name => 'field-order',
|
|
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,
|
|
ValueRegex => qr/\d+/smx,
|
|
);
|
|
$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/^[[0-1]{1}$/smx,
|
|
);
|
|
$Self->AddArgument(
|
|
Name => 'valid',
|
|
Description => 'Whether the field to add is valid or invalid. Defaults to 1.',
|
|
Required => 0,
|
|
ValueRegex => qr/^[0-1]{1}$/smx,
|
|
);
|
|
$Self->AddOption(
|
|
Name => 'stdin',
|
|
Description => 'Read config from STDIN.',
|
|
Required => 0,
|
|
HasValue => 0,
|
|
);
|
|
|
|
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');
|
|
|
|
# check field order
|
|
my $DynamicFieldList = $DynamicFieldObject->DynamicFieldListGet(
|
|
ObjectType => 'All',
|
|
Valid => 0,
|
|
);
|
|
my $FieldOrder = $Self->GetOption('field-order') || max map { $_->{FieldOrder} } $DynamicFieldList->@*;
|
|
|
|
# check config string
|
|
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();
|
|
}
|
|
|
|
# trying to parse as yaml and as json - if both fails, not valid
|
|
$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();
|
|
}
|
|
}
|
|
|
|
# check namespace
|
|
my $Namespace = $Self->GetOption('namespace');
|
|
my $Name = $Self->GetOption('name');
|
|
if ($Namespace) {
|
|
$Name = "$Namespace-$Name";
|
|
}
|
|
|
|
# add queue
|
|
my $Success = $Kernel::OM->Get('Kernel::System::DynamicField')->DynamicFieldAdd(
|
|
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,
|
|
);
|
|
|
|
# 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;
|