notify on custom field change, #11274
[freeside.git] / FS / FS / TicketSystem.pm
1 package FS::TicketSystem;
2
3 use strict;
4 use vars qw( $conf $system $AUTOLOAD );
5 use FS::Conf;
6 use FS::UID qw( dbh driver_name );
7
8 FS::UID->install_callback( sub { 
9   $conf = new FS::Conf;
10   $system = $conf->config('ticket_system');
11 } );
12
13 sub AUTOLOAD {
14   my $self = shift;
15
16   my($sub)=$AUTOLOAD;
17   $sub =~ s/.*://;
18
19   my $conf = new FS::Conf;
20   die "FS::TicketSystem::$AUTOLOAD called, but no ticket system configured\n"
21     unless $system;
22
23   eval "use FS::TicketSystem::$system;";
24   die $@ if $@;
25
26   $self .= "::$system";
27   $self->$sub(@_);
28 }
29
30 sub _upgrade_data {
31   return if !defined($system) || $system ne 'RT_Internal';
32   my ($class, %opts) = @_;
33
34   # go ahead and use the RT API for this
35   
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';
40
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 };
46   if ( $@ ) {
47     warn "Couldn't load RT data from '$datafile': $@\n(skipping)\n";
48     return;
49   }
50
51   # Cache existing ScripCondition, ScripAction, and Template IDs
52   my $search = RT::ScripConditions->new($CurrentUser);
53   $search->UnLimit;
54   my %condition = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
55
56   $search = RT::ScripActions->new($CurrentUser);
57   $search->UnLimit;
58   my %action = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
59
60   $search = RT::Templates->new($CurrentUser);
61   $search->UnLimit;
62   my %template = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
63
64   # ScripConditions
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 );
70     die $msg if !$val;
71     $condition{ lc($ScripCondition->Name) } = $ScripCondition->Id;
72   }
73
74   # ScripActions
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 );
80     die $msg if !$val;
81     $action{ lc($ScripAction->Name) } = $ScripAction->Id;
82   }
83
84   # Templates
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 );
90     die $msg if !$val;
91     $template{ lc($Template->Name) } = $Template->Id;
92   }
93
94   # Scrips
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";
102       next;
103     }
104     if ( !$action{$a} ) {
105       warn "ScripAction '$a' not found.\n";
106       next;
107     }
108     if ( !$template{$t} ) {
109       warn "Template '$t' not found.\n";
110       next;
111     }
112     my %param = (
113       ScripCondition => $condition{$c},
114       ScripAction => $action{$a},
115       Template => $template{$t},
116       Queue => 0,
117     );
118     $Scrip->LoadByCols(%param);
119     if (!defined($Scrip->Id)) {
120       my ($val, $msg) = $Scrip->Create(%param, Description => $desc);
121       die $msg if !$val;
122     }
123   } #foreach (@Scrips)
124
125   return;
126 }
127
128 1;