apply the RT 3.8.17/4.0.13 database fix automatically, RT#13852, RT#17928
[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   my $dbh = dbh;
91
92   # selfservice and cron users
93   foreach my $username ('%%%SELFSERVICE_USER%%%', 'fs_daily') {
94     my $User = RT::User->new($CurrentUser);
95     $User->Load($username);
96     if (!defined($User->Id)) {
97       my ($val, $msg) = $User->Create(
98         'Name' => $username,
99         'Gecos' => $username,
100         'Privileged' => 1,
101         # any other fields needed?
102       );
103       die $msg if !$val;
104     }
105     my $Principal = $User->PrincipalObj; # can this ever fail?
106     my @rights = ( qw(ShowTicket SeeQueue ModifyTicket ReplyToTicket 
107                       CreateTicket SeeCustomField) );
108     foreach (@rights) {
109       next if $Principal->HasRight( 'Right' => $_, Object => $RT::System );
110       my ($val, $msg) = $Principal->GrantRight(
111         'Right' => $_,
112         'Object' => $RT::System,
113       );
114       die $msg if !$val;
115     }
116   } #foreach $username
117
118   # EscalateQueue custom field and friends
119   my $CF = RT::CustomField->new($CurrentUser);
120   $CF->Load('EscalateQueue');
121   if (!defined($CF->Id)) {
122     my ($val, $msg) = $CF->Create(
123       'Name' => 'EscalateQueue',
124       'Type' => 'Select',
125       'MaxValues' => 1,
126       'LookupType' => 'RT::Queue',
127       'Description' => 'Escalate to Queue',
128       'ValuesClass' => 'RT::CustomFieldValues::Queues', #magic!
129     );
130     die $msg if !$val;
131     my $OCF = RT::ObjectCustomField->new($CurrentUser);
132     ($val, $msg) = $OCF->Create(
133       'CustomField' => $CF->Id,
134       'ObjectId' => 0,
135     );
136     die $msg if !$val;
137   }
138
139   # Load from RT data file
140   our (@Groups, @Users, @ACL, @Queues, @ScripActions, @ScripConditions,
141        @Templates, @CustomFields, @Scrips, @Attributes, @Initial, @Final,
142        %Delete_Scrips);
143   my $datafile = '%%%RT_PATH%%%/etc/initialdata';
144   eval { require $datafile };
145   if ( $@ ) {
146     warn "Couldn't load RT data from '$datafile': $@\n(skipping)\n";
147     return;
148   }
149
150   # Cache existing ScripCondition, ScripAction, and Template IDs.
151   # Complicated because we don't want to just step on multiple IDs 
152   # with the same name.
153   my $cachify = sub {
154     my ($class, $hash) = @_;
155     my $search = $class->new($CurrentUser);
156     $search->UnLimit;
157     while ( my $item = $search->Next ) {
158       my $ids = $hash->{lc($item->Name)} ||= [];
159       if ( $item->Creator == 1 ) { # RT::SystemUser
160         unshift @$ids, $item->Id;
161       }
162       else {
163         push @$ids, $item->Id;
164       }
165     }
166   };
167
168   my (%condition, %action, %template);
169   &$cachify('RT::ScripConditions', \%condition);
170   &$cachify('RT::ScripActions', \%action);
171   &$cachify('RT::Templates', \%template);
172   # $condition{name} = [ ids... ]
173   # with the id of the system-created object first, if there is one
174
175   # ScripConditions
176   my $ScripCondition = RT::ScripCondition->new($CurrentUser);
177   foreach my $sc (@ScripConditions) {
178     # $sc: Name, Description, ApplicableTransTypes, ExecModule, Argument
179     next if exists( $condition{ lc($sc->{Name}) } );
180     my ($val, $msg) = $ScripCondition->Create( %$sc );
181     die $msg if !$val;
182     $condition{ lc($ScripCondition->Name) } = [ $ScripCondition->Id ];
183   }
184
185   # ScripActions
186   my $ScripAction = RT::ScripAction->new($CurrentUser);
187   foreach my $sa (@ScripActions) {
188     # $sa: Name, Description, ExecModule, Argument
189     next if exists( $action{ lc($sa->{Name}) } );
190     my ($val, $msg) = $ScripAction->Create( %$sa );
191     die $msg if !$val;
192     $action{ lc($ScripAction->Name) } = [ $ScripAction->Id ];
193   }
194
195   # Templates
196   my $Template = RT::Template->new($CurrentUser);
197   foreach my $t (@Templates) {
198     # $t: Queue, Name, Description, Content
199     next if exists( $template{ lc($t->{Name}) } );
200     my ($val, $msg) = $Template->Create( %$t );
201     die $msg if !$val;
202     $template{ lc($Template->Name) } = [ $Template->Id ];
203   }
204
205   # Scrips
206   my %scrip; # $scrips{condition}{action}{template} = id
207   my $search = RT::Scrips->new($CurrentUser);
208   $search->Limit(FIELD => 'Queue', VALUE => 0);
209   while (my $item = $search->Next) {
210     my ($c, $a, $t) = map {lc $item->$_->Name} 
211       ('ScripConditionObj', 'ScripActionObj', 'TemplateObj');
212     if ( exists $scrip{$c}{$a}{$t} and $item->Creator == 1 ) {
213       warn "Deleting duplicate scrip $c $a [$t]\n";
214       my ($val, $msg) = $item->Delete;
215       warn "error deleting scrip: $msg\n" if !$val;
216     }
217     elsif ( exists $Delete_Scrips{$c}{$a}{$t} and $item->Creator == 1 ) {
218       warn "Deleting obsolete scrip $c $a [$t]\n";
219       my ($val, $msg) = $item->Delete;
220       warn "error deleting scrip: $msg\n" if !$val;
221     }
222     else {
223       $scrip{$c}{$a}{$t} = $item->id;
224     }
225   }
226   my $Scrip = RT::Scrip->new($CurrentUser);
227   foreach my $s ( @Scrips ) {
228     my $desc = $s->{'Description'};
229     my ($c, $a, $t) = map lc,
230       @{ $s }{'ScripCondition', 'ScripAction', 'Template'};
231     # skip existing scrips
232     next if ( exists($scrip{$c}{$a}{$t}) );
233     if ( !exists($condition{$c}) ) {
234       warn "ScripCondition '$c' not found.\n";
235       next;
236     }
237     if ( !exists($action{$a}) ) {
238       warn "ScripAction '$a' not found.\n";
239       next;
240     }
241     if ( !exists($template{$t}) ) {
242       warn "Template '$t' not found.\n";
243       next;
244     }
245     my %new_param = (
246       ScripCondition => $condition{$c}->[0],
247       ScripAction => $action{$a}->[0],
248       Template => $template{$t}->[0],
249       Queue => 0,
250       Description => $desc,
251     );
252     warn "Creating scrip: $c $a [$t]\n";
253     my ($val, $msg) = $Scrip->Create(%new_param);
254     die $msg if !$val;
255   } #foreach (@Scrips)
256
257   # one-time fix: accumulator fields (support time, etc.) that had values 
258   # entered on ticket creation need OCFV records attached to their Create
259   # transactions
260   my $sql = 'SELECT first_ocfv.ObjectId, first_ocfv.Created, Content '.
261     'FROM ObjectCustomFieldValues as first_ocfv '.
262     'JOIN ('.
263       # subquery to get the first OCFV with a certain name for each ticket
264       'SELECT min(ObjectCustomFieldValues.Id) AS Id '.
265       'FROM ObjectCustomFieldValues '.
266       'JOIN CustomFields '.
267       'ON (ObjectCustomFieldValues.CustomField = CustomFields.Id) '.
268       'WHERE ObjectType = \'RT::Ticket\' '.
269       'AND CustomFields.Name = ? '.
270       'GROUP BY ObjectId'.
271     ') AS first_ocfv_id USING (Id) '.
272     'JOIN ('.
273       # subquery to get the first transaction date for each ticket
274       # other than the Create
275       'SELECT ObjectId, min(Created) AS Created FROM Transactions '.
276       'WHERE ObjectType = \'RT::Ticket\' '.
277       'AND Type != \'Create\' '.
278       'GROUP BY ObjectId'.
279     ') AS first_txn ON (first_ocfv.ObjectId = first_txn.ObjectId) '.
280     # where the ticket custom field acquired a value before any transactions
281     # on the ticket (i.e. it was set on ticket creation)
282     'WHERE first_ocfv.Created < first_txn.Created '.
283     # and we haven't already fixed the ticket
284     'AND NOT EXISTS('.
285       'SELECT 1 FROM Transactions JOIN ObjectCustomFieldValues '.
286       'ON (Transactions.Id = ObjectCustomFieldValues.ObjectId) '.
287       'JOIN CustomFields '.
288       'ON (ObjectCustomFieldValues.CustomField = CustomFields.Id) '.
289       'WHERE ObjectCustomFieldValues.ObjectType = \'RT::Transaction\' '.
290       'AND CustomFields.Name = ? '.
291       'AND Transactions.Type = \'Create\''.
292       'AND Transactions.ObjectType = \'RT::Ticket\''.
293       'AND Transactions.ObjectId = first_ocfv.ObjectId'.
294     ')';
295     #whew
296
297   # prior to this fix, the only name an accumulate field could possibly have 
298   # was "Support time".
299   my $sth = $dbh->prepare($sql);
300   $sth->execute('Support time', 'Support time');
301   my $rows = $sth->rows;
302   warn "Fixing support time on $rows rows...\n" if $rows > 0;
303   while ( my $row = $sth->fetchrow_arrayref ) {
304     my ($tid, $created, $content) = @$row;
305     my $Txns = RT::Transactions->new($CurrentUser);
306     $Txns->Limit(FIELD => 'ObjectId', VALUE => $tid);
307     $Txns->Limit(FIELD => 'ObjectType', VALUE => 'RT::Ticket');
308     $Txns->Limit(FIELD => 'Type', VALUE => 'Create');
309     my $CreateTxn = $Txns->First;
310     if ($CreateTxn) {
311       my ($val, $msg) = $CreateTxn->AddCustomFieldValue(
312         Field => 'Support time',
313         Value => $content,
314         RecordTransaction => 0,
315       );
316       warn "Error setting transaction support time: $msg\n" unless $val;
317     } else {
318       warn "Create transaction not found for ticket $tid.\n";
319     }
320   }
321
322   #Pg-specific 
323   my $cve_2013_3373_sql = q(
324     UPDATE Tickets SET Subject = REPLACE(Subject,E'\n','')
325   );
326   #need this for mysql
327   #UPDATE Tickets SET Subject = REPLACE(Subject,'\n','');
328
329   my $cve_2013_3373_sth = $dbh->prepare( $cve_2013_3373_sql)
330     or die $dbh->errstr;
331   $cve_2013_3373_sth->execute or die $cve_2013_3373_sth->errstr;
332
333   return;
334 }
335
336 1;