8ea4b03a8af0e1ab78f29672e786bde95aeabf08
[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 $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   # CustomFieldChange scrip condition
42   my $ScripCondition = RT::ScripCondition->new($CurrentUser);
43   $ScripCondition->LoadByCols('ExecModule' => 'CustomFieldChange');
44   if (!defined($ScripCondition->Id)) {
45     my ($val, $msg) = $ScripCondition->Create(
46       'Name' => 'On Custom Field Change',
47       'Description' => 'When a custom field is changed to some value',
48       'ExecModule' => 'CustomFieldChange',
49       'ApplicableTransTypes' => 'Any',
50     );
51     die $msg if !$val;
52   }
53
54   # SetPriority scrip action
55   my $ScripAction = RT::ScripAction->new($CurrentUser);
56   $ScripAction->LoadByCols('ExecModule' => 'SetPriority');
57   if (!defined($ScripAction->Id)) {
58     my ($val, $msg) = $ScripAction->Create(
59       'Name' => 'Set Priority',
60       'Description' => 'Set ticket priority',
61       'ExecModule' => 'SetPriority',
62       'Argument' => '',
63     );
64     die $msg if !$val;
65   }
66
67   # EscalateQueue custom field and friends
68   my $CF = RT::CustomField->new($CurrentUser);
69   $CF->Load('EscalateQueue');
70   if (!defined($CF->Id)) {
71     my ($val, $msg) = $CF->Create(
72       'Name' => 'EscalateQueue',
73       'Type' => 'Select',
74       'MaxValues' => 1,
75       'LookupType' => 'RT::Queue',
76       'Description' => 'Escalate to Queue',
77       'ValuesClass' => 'RT::CustomFieldValues::Queues', #magic!
78     );
79     die $msg if !$val;
80     my $OCF = RT::ObjectCustomField->new($CurrentUser);
81     ($val, $msg) = $OCF->Create(
82       'CustomField' => $CF->Id,
83       'ObjectId' => 0,
84     );
85     die $msg if !$val;
86   }
87
88   # Create any missing scrips
89   our (@Groups, @Users, @ACL, @Queues, @ScripActions, @ScripConditions,
90        @Templates, @CustomFields, @Scrips, @Attributes, @Initial, @Final);
91   my $datafile = '%%%RT_PATH%%%/etc/initialdata';
92   eval { require $datafile };
93   if ( $@ ) {
94     warn "Couldn't load RT data from '$datafile': $@\n(skipping)\n";
95     return;
96   }
97
98   my $search = RT::ScripConditions->new($CurrentUser);
99   $search->UnLimit;
100   my %condition = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
101
102   $search = RT::ScripActions->new($CurrentUser);
103   $search->UnLimit;
104   my %action = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
105
106   $search = RT::Templates->new($CurrentUser);
107   $search->UnLimit;
108   my %template = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
109
110   my $Scrip = RT::Scrip->new($CurrentUser);
111   foreach my $s ( @Scrips ) {
112     my $desc = $s->{'Description'};
113     my ($c, $a, $t) = map lc,
114       @{ $s }{'ScripCondition', 'ScripAction', 'Template'};
115     if ( !$condition{$c} ) {
116       warn "ScripCondition '$c' not found.\n";
117       next;
118     }
119     if ( !$action{$a} ) {
120       warn "ScripAction '$a' not found.\n";
121       next;
122     }
123     if ( !$template{$t} ) {
124       warn "Template '$t' not found.\n";
125       next;
126     }
127     my %param = (
128       ScripCondition => $condition{$c},
129       ScripAction => $action{$a},
130       Template => $template{$t},
131       Queue => 0,
132     );
133     $Scrip->LoadByCols(%param);
134     if (!defined($Scrip->Id)) {
135       my ($val, $msg) = $Scrip->Create(%param, Description => $desc);
136       die $msg if !$val;
137     }
138   } #foreach (@Scrips)
139
140   return;
141 }
142
143 1;