From: mark Date: Tue, 10 May 2011 23:22:58 +0000 (+0000) Subject: replace missing RT scrips on upgrade, #9569 X-Git-Tag: freeside_2_1_3~212 X-Git-Url: http://git.freeside.biz/gitweb/?a=commitdiff_plain;h=cf60dddb3ba70a9499ae77bafa8367064a3c5e1f;p=freeside.git replace missing RT scrips on upgrade, #9569 --- diff --git a/FS/FS/TicketSystem.pm b/FS/FS/TicketSystem.pm index d53d2f679..bde5777db 100644 --- a/FS/FS/TicketSystem.pm +++ b/FS/FS/TicketSystem.pm @@ -29,23 +29,80 @@ sub AUTOLOAD { sub _upgrade_data { return if $system ne 'RT_Internal'; - my ($class, %opts) = @_; - my ($t, $exec, @fields) = map { driver_name =~ /^mysql/i ? $_ : lc($_) } - (qw( ScripConditions ExecModule - Name Description ExecModule ApplicableTransTypes - Creator Created LastUpdatedBy LastUpdated)); - my $count_sql = "SELECT COUNT(*) FROM $t WHERE $exec = 'CustomFieldChange'"; - my $sth = dbh->prepare($count_sql) or die dbh->errstr; - $sth->execute or die $sth->errstr; - my $total = $sth->fetchrow_arrayref->[0]; - return if $total > 0; - - my $insert_sql = "INSERT INTO $t (".join(',',@fields).") VALUES (". - "'On Custom Field Change', 'When a custom field is changed to some value', - 'CustomFieldChange', 'Any', 1, CURRENT_DATE, 1, CURRENT_DATE )"; - $sth = dbh->prepare($insert_sql) or die dbh->errstr; - $sth->execute or die $sth->errstr; + + # go ahead and use the RT API for this + + FS::TicketSystem->init; + my $session = FS::TicketSystem->session(); + my $CurrentUser = $session->{'CurrentUser'} + or die 'freeside-upgrade must run as a valid RT user'; + + # CustomFieldChange scrip condition + my $ScripCondition = RT::ScripCondition->new($CurrentUser); + $ScripCondition->LoadByCols('ExecModule' => 'CustomFieldChange'); + if (!defined($ScripCondition->Id)) { + my ($val, $msg) = $ScripCondition->Create( + 'Name' => 'On Custom Field Change', + 'Description' => 'When a custom field is changed to some value', + 'ExecModule' => 'CustomFieldChange', + 'ApplicableTransTypes' => 'Any', + ); + die $msg if !$val; + } + + # Create any missing scrips + our (@Groups, @Users, @ACL, @Queues, @ScripActions, @ScripConditions, + @Templates, @CustomFields, @Scrips, @Attributes, @Initial, @Final); + my $datafile = '%%%RT_PATH%%%/etc/initialdata'; + eval { require $datafile }; + if ( $@ ) { + warn "Couldn't load RT data from '$datafile': $@\n(skipping)\n"; + return; + } + + my $search = RT::ScripConditions->new($CurrentUser); + $search->UnLimit; + my %condition = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef }; + + $search = RT::ScripActions->new($CurrentUser); + $search->UnLimit; + my %action = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef }; + + $search = RT::Templates->new($CurrentUser); + $search->UnLimit; + my %template = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef }; + + my $Scrip = RT::Scrip->new($CurrentUser); + foreach my $s ( @Scrips ) { + my $desc = $s->{'Description'}; + my ($c, $a, $t) = map lc, + @{ $s }{'ScripCondition', 'ScripAction', 'Template'}; + if ( !$condition{$c} ) { + warn "ScripCondition '$c' not found.\n"; + next; + } + if ( !$action{$a} ) { + warn "ScripAction '$a' not found.\n"; + next; + } + if ( !$template{$t} ) { + warn "Template '$t' not found.\n"; + next; + } + my %param = ( + ScripCondition => $condition{$c}, + ScripAction => $action{$a}, + Template => $template{$t}, + Queue => 0, + ); + $Scrip->LoadByCols(%param); + if (!defined($Scrip->Id)) { + my ($val, $msg) = $Scrip->Create(%param, Description => $desc); + die $msg if !$val; + } + } #foreach (@Scrips) + return; } diff --git a/Makefile b/Makefile index 660f64961..2036c681a 100644 --- a/Makefile +++ b/Makefile @@ -196,6 +196,7 @@ perl-modules: s|%%%FREESIDE_CACHE%%%|${FREESIDE_CACHE}|g;\ s'%%%FREESIDE_DOCUMENT_ROOT%%%'${FREESIDE_DOCUMENT_ROOT}'g; \ s'%%%RT_ENABLED%%%'${RT_ENABLED}'g; \ + s'%%%RT_PATH%%%'${RT_PATH}'g; \ s'%%%MASONDATA%%%'${MASONDATA}'g;\ " blib/lib/FS/*.pm;\ perl -p -i -e "\ diff --git a/rt/Makefile b/rt/Makefile index 6ba9063e1..8f9202080 100644 --- a/rt/Makefile +++ b/rt/Makefile @@ -327,6 +327,8 @@ fixperms: chmod 0440 $(DESTDIR)$(CONFIG_FILE) chmod 0640 $(DESTDIR)$(SITE_CONFIG_FILE) + # Make this externally readable + chmod 0440 $(DESTDIR)$(RT_ETC_PATH)/initialdata # Make the system binaries cd $(DESTDIR)$(RT_BIN_PATH) && ( chmod 0755 $(BINARIES) ; chown $(BIN_OWNER) $(BINARIES); chgrp $(RTGROUP) $(BINARIES)) diff --git a/rt/Makefile.in b/rt/Makefile.in index d41633fff..f963faa25 100644 --- a/rt/Makefile.in +++ b/rt/Makefile.in @@ -327,6 +327,8 @@ fixperms: chmod 0440 $(DESTDIR)$(CONFIG_FILE) chmod 0640 $(DESTDIR)$(SITE_CONFIG_FILE) + # Make this externally readable + chmod 0440 $(DESTDIR)$(RT_ETC_PATH)/initialdata # Make the system binaries cd $(DESTDIR)$(RT_BIN_PATH) && ( chmod 0755 $(BINARIES) ; chown $(BIN_OWNER) $(BINARIES); chgrp $(RTGROUP) $(BINARIES))