1 package FS::TicketSystem;
4 use vars qw( $conf $system $AUTOLOAD );
6 use FS::UID qw( dbh driver_name );
8 FS::UID->install_callback( sub {
10 $system = $conf->config('ticket_system');
19 my $conf = new FS::Conf;
20 die "FS::TicketSystem::$AUTOLOAD called, but no ticket system configured\n"
23 eval "use FS::TicketSystem::$system;";
31 return if !defined($system) || $system ne 'RT_Internal';
32 my ($class, %opts) = @_;
34 # go ahead and use the RT API for this
36 FS::TicketSystem->init;
37 my $session = FS::TicketSystem->session();
38 my $CurrentUser = $session->{'CurrentUser'}
39 or die 'freeside-upgrade must run as a valid RT user';
41 # Load from RT data file
42 our (@Groups, @Users, @ACL, @Queues, @ScripActions, @ScripConditions,
43 @Templates, @CustomFields, @Scrips, @Attributes, @Initial, @Final);
44 my $datafile = '%%%RT_PATH%%%/etc/initialdata';
45 eval { require $datafile };
47 warn "Couldn't load RT data from '$datafile': $@\n(skipping)\n";
51 # Cache existing ScripCondition, ScripAction, and Template IDs
52 my $search = RT::ScripConditions->new($CurrentUser);
54 my %condition = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
56 $search = RT::ScripActions->new($CurrentUser);
58 my %action = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
60 $search = RT::Templates->new($CurrentUser);
62 my %template = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
65 my $ScripCondition = RT::ScripCondition->new($CurrentUser);
66 foreach my $sc (@ScripConditions) {
67 # $sc: Name, Description, ApplicableTransTypes, ExecModule, Argument
68 next if exists( $condition{ lc($sc->{Name}) } );
69 my ($val, $msg) = $ScripCondition->Create( %$sc );
71 $condition{ lc($ScripCondition->Name) } = $ScripCondition->Id;
75 my $ScripAction = RT::ScripAction->new($CurrentUser);
76 foreach my $sa (@ScripActions) {
77 # $sa: Name, Description, ExecModule, Argument
78 next if exists( $action{ lc($sa->{Name}) } );
79 my ($val, $msg) = $ScripAction->Create( %$sa );
81 $action{ lc($ScripAction->Name) } = $ScripAction->Id;
85 my $Template = RT::Template->new($CurrentUser);
86 foreach my $t (@Templates) {
87 # $t: Queue, Name, Description, Content
88 next if exists( $template{ lc($t->{Name}) } );
89 my ($val, $msg) = $Template->Create( %$t );
91 $template{ lc($Template->Name) } = $Template->Id;
95 my $Scrip = RT::Scrip->new($CurrentUser);
96 foreach my $s ( @Scrips ) {
97 my $desc = $s->{'Description'};
98 my ($c, $a, $t) = map lc,
99 @{ $s }{'ScripCondition', 'ScripAction', 'Template'};
100 if ( !$condition{$c} ) {
101 warn "ScripCondition '$c' not found.\n";
104 if ( !$action{$a} ) {
105 warn "ScripAction '$a' not found.\n";
108 if ( !$template{$t} ) {
109 warn "Template '$t' not found.\n";
113 ScripCondition => $condition{$c},
114 ScripAction => $action{$a},
115 Template => $template{$t},
118 $Scrip->LoadByCols(%param);
119 if (!defined($Scrip->Id)) {
120 my ($val, $msg) = $Scrip->Create(%param, Description => $desc);