
Hi Martin, please see attached otrs.addGroup and otrs.addQueue These are working versions for me. They should work, without any patch. I am submitting these for the developers to have a look at so they can see what changes will be neccessary in future to make otrs more front-end independant. I have added a method GroupAdd in Kernel::System::User package copied from UserAdd but not in the User.pm file. It should eventually go there, In otrs.addQueue I have also added a function to Kernel::System::Queue package called QueueAdd Please also see the comments in this method. Most notably: No Log or Config object in Kernel::System::Queue Let me know what you think. If you feel these changes are ok, I'll add them to their proper packages. best regards Martin Edenhofer wrote:
It's 100% the OpenTRS style.
-- Atif Ghaffar ---------------------------. +41 78 845 31 64 ¦ tel aghaffar@developer.ch ¦ email http://atifghaffar.com ¦ www 8206786 ¦ icq #!/usr/bin/perl -w # -- # otrs.addGroup - Add Group from CLI package main; BEGIN{ use FindBin; unshift @INC, ($FindBin::Bin , "$FindBin::Bin/../"); use vars qw (%opts); use Getopt::Std; getopt('c', \%opts); unless ($ARGV[0]){ print "$FindBin::Script [-c comment] groupname"; print "\n"; exit; } } package Kernel::System::User; # -- this function should evenutally go in # Kernel/System/User.pm # or # Kernel/System/Group.pm sub GroupAdd { my $Self = shift; my %Param = @_; # -- # qoute params # -- my @Params = ('Name', 'Comment', 'ValidID'); foreach (@Params) { $Param{$_} = $Self->{DBObject}->Quote($Param{$_}) || ''; } my $SQL = "INSERT INTO groups (name, comment, valid_id, ". " create_time, create_by, change_time, change_by)" . " VALUES " . " ('$Param{Name}', '$Param{Comment}', " . " $Param{ValidID}, current_timestamp, $Param{UserID}, ". " current_timestamp, $Param{UserID})"; if ($Self->{DBObject}->Do(SQL => $SQL)) { # -- # get new group id # -- $SQL = "SELECT id ". " FROM " . " groups " . " WHERE " . " name = '$Param{Name}'"; my $GroupID = ''; $Self->{DBObject}->Prepare(SQL => $SQL); while (my @RowTmp = $Self->{DBObject}->FetchrowArray()) { $GroupID = $RowTmp[0]; } # -- # log notice # -- $Self->{LogObject}->Log( Priority => 'notice', Message => "Group: '$Param{Name}' ID: '$GroupID' created successfully ($Param{UserID})!", ); return $GroupID; } else { return; } } package main; use lib '/opt/OpenTRS/'; use strict; use Kernel::Config; use Kernel::System::Syslog; use Kernel::System::DB; use Kernel::System::User; # -- # create common objects # -- my %CommonObject = (); $CommonObject{LogObject} = Kernel::System::Syslog->new(); $CommonObject{ConfigObject} = Kernel::Config->new(%CommonObject); $CommonObject{DBObject} = Kernel::System::DB->new(%CommonObject); $CommonObject{UserObject} = Kernel::System::User->new(%CommonObject); my %Param; undef %Param; #user id of the person adding the record? $Param{UserID}='1'; #Validrecord $Param{ValidID}='1'; $Param{Comment}=$opts{'c'}; $Param{Name}=$ARGV[0]; if ( my $gid=$CommonObject{UserObject}->GroupAdd(%Param) ){ print "Group added. group id is $gid\n"; } # -- exit (0); #!/usr/bin/perl -w # -- # otrs.addQueue - Add Queue from CLI package main; BEGIN{ use FindBin; unshift @INC, ($FindBin::Bin , "$FindBin::Bin/../"); use vars qw (%opts); use Getopt::Std; getopt('g', \%opts); unless ($ARGV[0] && $opts{'g'}){ print "$FindBin::Script -g group_id queue_name"; print "\n"; exit; } } package Kernel::System::Queue; # Adding a method to this package. # Eventually it should be kept in its # package file sub QueueAdd { my $Self=shift; my %Param = @_; # NOTE:, please see below, I will redefine @Param #my @Params = ( # 'Name', # 'GroupID', # 'UnlockTimeout', # 'SystemAddressID', # 'SalutationID', # 'SignatureID', # 'FollowUpID', # 'FollowUpLock', # 'EscalationTime', # 'Comment', # 'ValidID', #); # ' and , are for modems. Line noise # A less noisy way to defining @Params is my @Params = qw( Name GroupID UnlockTimeout SystemAddressID SalutationID SignatureID FollowUpID FollowUpLock EscalationTime Comment ValidID ); foreach (@Params) { $Param{$_} = $Self->{DBObject}->Quote($Param{$_}) || ''; #Ooooh what does this button do? }; # why is this module not loading Config and Log? # almost anything should have acess to Config and Log object. require Kernel::Config; import Kernel::Config; require Kernel::System::Syslog; import Kernel::System::Syslog; # bring in the missing LogObject $Self->{LogObject}=Kernel::System::Syslog->new(); #bring in the missing Config Object $Self->{ConfigObject}=Kernel::Config->new('LogObject', $Self->{LogObject}); for (qw(UnlockTimeout EscalationTime FollowUpLock SystemAddressID SalutationID SignatureID FollowUpID FollowUpLock)) { # these are coming from Config.pm # I added default values in the Load Routine $Param{$_} = $Self->{ConfigObject}{QueueDefaults}{$_} || 0 unless ($Param{$_}); }; my $SQL = "INSERT INTO queue " . "(name, " . " group_id, " . " unlock_timeout, " . " system_address_id, " . " salutation_id, " . " signature_id, " . " escalation_time, " . " follow_up_id, " . " follow_up_lock, " . " valid_id, " . " comment, " . " create_time, " . " create_by, " . " change_time, " . " change_by)" . " VALUES " . " ('$Param{Name}', " . " $Param{GroupID}, " . " $Param{UnlockTimeout}, " . " $Param{SystemAddressID}, " . " $Param{SalutationID}, " . " $Param{SignatureID}, " . " $Param{EscalationTime}, " . " $Param{FollowUpID}, " . " $Param{FollowUpLock}, " . " $Param{ValidID}, " . " '$Param{Comment}', " . " current_timestamp, " . " $Param{UserID}, " . " current_timestamp, " . " $Param{UserID})"; if ($Self->{DBObject}->Do(SQL => $SQL)) { # -- # get new queue id # -- $SQL = "SELECT id ". " FROM " . " queue " . " WHERE " . " name = '$Param{Name}'"; my $QueueID = ''; $Self->{DBObject}->Prepare(SQL => $SQL); while (my @RowTmp = $Self->{DBObject}->FetchrowArray()) { $QueueID = $RowTmp[0]; } return $QueueID; } else { return; } } package main; use lib '/opt/OpenTRS/'; use strict; use Kernel::Config; use Kernel::System::Syslog; use Kernel::System::DB; use Kernel::System::Queue; # -- # create common objects # -- my %CommonObject = (); $CommonObject{LogObject} = Kernel::System::Syslog->new(); $CommonObject{ConfigObject} = Kernel::Config->new(%CommonObject); $CommonObject{DBObject} = Kernel::System::DB->new(%CommonObject); $CommonObject{QueueObject} = Kernel::System::Queue->new(%CommonObject); my %Param; undef %Param; #user id of the person adding the queue? $Param{UserID}='1'; #Validrecord $Param{ValidID}='1'; $Param{GroupID}=$opts{'g'}; $Param{Name}=$ARGV[0]; $CommonObject{QueueObject}->QueueAdd(%Param); exit (0); __END__ Add the following in Load sub in Config.pm # ----------------------------------------------------# # queue defaults # # ----------------------------------------------------# $Self->{QueueDefaults} = { UnlockTimeout => 0, EscalationTime => 0, FollowUpLock => 0, SystemAddressID => 1, SalutationID => 1, SignatureID => 1, FollowUpID => 1, FollowUpLock => 0, };