RT future ticket resolve, #13853
[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 use FS::Record qw( dbdef );
8
9 FS::UID->install_callback( sub { 
10   $conf = new FS::Conf;
11   $system = $conf->config('ticket_system');
12 } );
13
14 sub AUTOLOAD {
15   my $self = shift;
16
17   my($sub)=$AUTOLOAD;
18   $sub =~ s/.*://;
19
20   my $conf = new FS::Conf;
21   die "FS::TicketSystem::$AUTOLOAD called, but no ticket system configured\n"
22     unless $system;
23
24   eval "use FS::TicketSystem::$system;";
25   die $@ if $@;
26
27   $self .= "::$system";
28   $self->$sub(@_);
29 }
30
31 # Our schema changes
32 my %columns = (
33   Tickets => {
34     WillResolve => { type => 'timestamp', null => 1, default => '', },
35   },
36   CustomFields => {
37     Required => { type => 'integer', default => 0, null => 0 },
38   },
39 );
40
41 sub _upgrade_schema {
42   my $system = FS::Conf->new->config('ticket_system');
43   return if !defined($system) || $system ne 'RT_Internal';
44   my ($class, %opts) = @_;
45
46   my $dbh = dbh;
47   my @sql;
48   my $case = driver_name eq 'mysql' ? sub {@_} : sub {map lc, @_};
49   foreach my $tablename (keys %columns) {
50     my $table = dbdef->table(&$case($tablename));
51     if ( !$table ) {
52       warn 
53       "$tablename table does not exist.  Your RT installation is incomplete.\n";
54       next;
55     }
56     foreach my $colname (keys %{ $columns{$tablename} }) {
57       if ( !$table->column(&$case($colname)) ) {
58         my $col = new DBIx::DBSchema::Column {
59             table_obj => $table,
60             name => &$case($colname),
61             %{ $columns{$tablename}->{$colname} }
62           };
63         $col->table_obj($table);
64         push @sql, $col->sql_add_column($dbh);
65       }
66     } #foreach $colname
67   } #foreach $tablename
68
69   return if !@sql;
70   warn "Upgrading RT schema:\n";
71   foreach my $statement (@sql) {
72     warn "$statement\n";
73     $dbh->do( $statement )
74       or die "Error: ". $dbh->errstr. "\n executing: $statement";
75   }
76   return;
77 }
78
79 sub _upgrade_data {
80   return if !defined($system) || $system ne 'RT_Internal';
81   my ($class, %opts) = @_;
82
83   # go ahead and use the RT API for this
84   
85   FS::TicketSystem->init;
86   my $session = FS::TicketSystem->session();
87   # bypass RT ACLs--we're going to do lots of things
88   my $CurrentUser = $RT::SystemUser;
89
90   # selfservice user
91   my $User = RT::User->new($CurrentUser);
92   $User->Load('%%%SELFSERVICE_USER%%%');
93   if (!defined($User->Id)) {
94     my ($val, $msg) = $User->Create(
95       'Name' => '%%%SELFSERVICE_USER%%%',
96       'Gecos' => '%%%SELFSERVICE_USER%%%',
97       'Privileged' => 1,
98       # any other fields needed?
99     );
100     die $msg if !$val;
101   }
102   my $Principal = $User->PrincipalObj; # can this ever fail?
103   my @rights = ( qw(ShowTicket SeeQueue ModifyTicket ReplyToTicket 
104                     CreateTicket SeeCustomField) );
105   foreach (@rights) {
106     next if $Principal->HasRight( 'Right' => $_, Object => $RT::System );
107     my ($val, $msg) = $Principal->GrantRight(
108       'Right' => $_,
109       'Object' => $RT::System,
110     );
111     die $msg if !$val;
112   }
113
114   # EscalateQueue custom field and friends
115   my $CF = RT::CustomField->new($CurrentUser);
116   $CF->Load('EscalateQueue');
117   if (!defined($CF->Id)) {
118     my ($val, $msg) = $CF->Create(
119       'Name' => 'EscalateQueue',
120       'Type' => 'Select',
121       'MaxValues' => 1,
122       'LookupType' => 'RT::Queue',
123       'Description' => 'Escalate to Queue',
124       'ValuesClass' => 'RT::CustomFieldValues::Queues', #magic!
125     );
126     die $msg if !$val;
127     my $OCF = RT::ObjectCustomField->new($CurrentUser);
128     ($val, $msg) = $OCF->Create(
129       'CustomField' => $CF->Id,
130       'ObjectId' => 0,
131     );
132     die $msg if !$val;
133   }
134
135   # Load from RT data file
136   our (@Groups, @Users, @ACL, @Queues, @ScripActions, @ScripConditions,
137        @Templates, @CustomFields, @Scrips, @Attributes, @Initial, @Final);
138   my $datafile = '%%%RT_PATH%%%/etc/initialdata';
139   eval { require $datafile };
140   if ( $@ ) {
141     warn "Couldn't load RT data from '$datafile': $@\n(skipping)\n";
142     return;
143   }
144
145   # Cache existing ScripCondition, ScripAction, and Template IDs
146   my $search = RT::ScripConditions->new($CurrentUser);
147   $search->UnLimit;
148   my %condition = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
149
150   $search = RT::ScripActions->new($CurrentUser);
151   $search->UnLimit;
152   my %action = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
153
154   $search = RT::Templates->new($CurrentUser);
155   $search->UnLimit;
156   my %template = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
157
158   # ScripConditions
159   my $ScripCondition = RT::ScripCondition->new($CurrentUser);
160   foreach my $sc (@ScripConditions) {
161     # $sc: Name, Description, ApplicableTransTypes, ExecModule, Argument
162     next if exists( $condition{ lc($sc->{Name}) } );
163     my ($val, $msg) = $ScripCondition->Create( %$sc );
164     die $msg if !$val;
165     $condition{ lc($ScripCondition->Name) } = $ScripCondition->Id;
166   }
167
168   # ScripActions
169   my $ScripAction = RT::ScripAction->new($CurrentUser);
170   foreach my $sa (@ScripActions) {
171     # $sa: Name, Description, ExecModule, Argument
172     next if exists( $action{ lc($sa->{Name}) } );
173     my ($val, $msg) = $ScripAction->Create( %$sa );
174     die $msg if !$val;
175     $action{ lc($ScripAction->Name) } = $ScripAction->Id;
176   }
177
178   # Templates
179   my $Template = RT::Template->new($CurrentUser);
180   foreach my $t (@Templates) {
181     # $t: Queue, Name, Description, Content
182     next if exists( $template{ lc($t->{Name}) } );
183     my ($val, $msg) = $Template->Create( %$t );
184     die $msg if !$val;
185     $template{ lc($Template->Name) } = $Template->Id;
186   }
187
188   # Scrips
189   my $Scrip = RT::Scrip->new($CurrentUser);
190   foreach my $s ( @Scrips ) {
191     my $desc = $s->{'Description'};
192     my ($c, $a, $t) = map lc,
193       @{ $s }{'ScripCondition', 'ScripAction', 'Template'};
194     if ( !$condition{$c} ) {
195       warn "ScripCondition '$c' not found.\n";
196       next;
197     }
198     if ( !$action{$a} ) {
199       warn "ScripAction '$a' not found.\n";
200       next;
201     }
202     if ( !$template{$t} ) {
203       warn "Template '$t' not found.\n";
204       next;
205     }
206     my %param = (
207       ScripCondition => $condition{$c},
208       ScripAction => $action{$a},
209       Template => $template{$t},
210       Queue => 0,
211     );
212     $Scrip->LoadByCols(%param);
213     if (!defined($Scrip->Id)) {
214       my ($val, $msg) = $Scrip->Create(%param, Description => $desc);
215       die $msg if !$val;
216     }
217   } #foreach (@Scrips)
218
219   return;
220 }
221
222 1;