Upgrade to 4.2.8
[usit-rt.git] / etc / upgrade / 4.1.21 / content
1 use strict;
2 use warnings;
3
4 sub dashboards_for_object {
5     my $object = shift;
6     my $user   = shift;
7     my %dashboards;
8     my $privacy = RT::Dashboard->_build_privacy($object);
9
10     while ( my $attr = $object->Attributes->Next ) {
11         if ( $attr->Name =~ /^Dashboard\b/ ) {
12             my $dashboard = RT::Dashboard->new($user);
13             my ( $ok, $msg ) = $dashboard->Load( $privacy, $attr->id );
14             next unless $ok;
15
16             if ( $object->isa('RT::System') ) {
17                 push @{ $dashboards{system} }, $dashboard;
18             }
19             elsif ( $object->isa('RT::User') ) {
20                 push @{ $dashboards{personal} }, $dashboard;
21             }
22             elsif ( $object->isa('RT::Group') ) {
23                 push @{ $dashboards{group}{ $object->Name } }, $dashboard;
24             }
25         }
26     }
27     return \%dashboards;
28 }
29
30 our @Final = (
31     sub {
32         my $users = RT::Users->new( RT->SystemUser );
33         $users->LimitToPrivileged();
34         while ( my $user = $users->Next ) {
35             my @objs = RT::Dashboard->new($user)->ObjectsForLoading( IncludeSuperuserGroups => 0 );
36
37             my %dashboard_map;
38
39             for my $object (@objs) {
40                 my $dashboards = dashboards_for_object( $object, $user );
41                 push @{ $dashboard_map{$_} }, @{ $dashboards->{$_} || [] } for qw/personal system/;
42
43                 push @{ $dashboard_map{group}{$_} }, @{ $dashboards->{group}{$_} }
44                     for keys %{ $dashboards->{group} || {} };
45             }
46
47             my @dashboards = (
48                 ( sort { $a->Id <=> $b->Id } @{ $dashboard_map{personal} || [] } ),
49                 ( sort { $a->Id <=> $b->Id } @{ $dashboard_map{system}   || [] } ),
50
51                 map {
52                     sort { $a->Id <=> $b->Id }
53                         @{ $dashboard_map{group}{$_} }
54                     }
55                     keys %{ $dashboard_map{group} || {} },
56             );
57
58             splice @dashboards, 7 if @dashboards > 7;
59             @dashboards = map { $_->id } @dashboards;
60             my ( $ret, $msg ) = $user->SetPreferences( 'DashboardsInMenu', { dashboards => \@dashboards } );
61             RT->Logger->error( $msg ) unless $ret;
62         }
63     },
64 );