X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=rt%2Flib%2FRT%2FInterface%2FWeb%2FMenu.pm;h=26cf44e31922b93da6a6d2f1f4e4af3a773434a6;hp=6b351e94b1c93aed022c750bc386f8aa61003116;hb=187086c479a09629b7d180eec513fb7657f4e291;hpb=43a06151e47d2c59b833cbd8c26d97865ee850b6 diff --git a/rt/lib/RT/Interface/Web/Menu.pm b/rt/lib/RT/Interface/Web/Menu.pm index 6b351e94b..26cf44e31 100644 --- a/rt/lib/RT/Interface/Web/Menu.pm +++ b/rt/lib/RT/Interface/Web/Menu.pm @@ -2,7 +2,7 @@ # # COPYRIGHT: # -# This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC +# This software is Copyright (c) 1996-2018 Best Practical Solutions, LLC # # # (Except where explicitly superseded by other copyright notices) @@ -57,7 +57,7 @@ use URI; use Scalar::Util qw(weaken); __PACKAGE__->mk_accessors(qw( - key title description raw_html escape_title sort_order target class + key title description raw_html escape_title sort_order target class attributes )); =head1 NAME @@ -70,9 +70,9 @@ RT::Interface::Web::Menu - Handle the API for menu navigation Creates a new L object. Possible keys in the I are L, L, L, L, -L, L, L, L, L and -L. See the subroutines with the respective name below for -each option's use. +L, L, L, L, L, +L, and L. See the subroutines with the respective name +below for each option's use. =cut @@ -139,6 +139,12 @@ Get or set the frame or pseudo-target for this link. something like L<_blank> Gets or sets the CSS class the menu item should have in addition to the default classes. This is only used if L isn't specified. +=head2 attributes [HASHREF] + +Gets or sets a hashref of HTML attribute name-value pairs that the menu item +should have in addition to the attributes which have their own accessor, like +L and L. This is only used if L isn't specified. + =head2 path Gets or sets the URL that the menu's link goes to. If the link @@ -150,10 +156,12 @@ treated as relative to it's parent's path, and made absolute. sub path { my $self = shift; if (@_) { - $self->{path} = shift; - $self->{path} = URI->new_abs($self->{path}, $self->parent->path . "/")->as_string - if defined $self->{path} and $self->parent and $self->parent->path; - $self->{path} =~ s!///!/! if $self->{path}; + if (defined($self->{path} = shift)) { + my $base = ($self->parent and $self->parent->path) ? $self->parent->path : ""; + $base .= "/" unless $base =~ m{/$}; + my $uri = URI->new_abs($self->{path}, $base); + $self->{path} = $uri->as_string; + } } return $self->{path}; } @@ -230,6 +238,7 @@ sub child { if ( defined $path and length $path ) { my $base_path = $HTML::Mason::Commands::r->path_info; my $query = $HTML::Mason::Commands::m->cgi_object->query_string; + $base_path =~ s!/+!/!g; $base_path .= "?$query" if defined $query and length $query; $base_path =~ s/index\.html$//; @@ -311,4 +320,107 @@ sub children { return wantarray ? @kids : \@kids; } +=head2 add_after + +Called on a child, inserts a new menu item after it and shifts any other +menu items at this level to the right. + +L by default would insert at the end of the list of children, unless you +did manual sort_order calculations. + +Takes all the regular arguments to L. + +=cut + +sub add_after { shift->_insert_sibling("after", @_) } + +=head2 add_before + +Called on a child, inserts a new menu item at the child's location and shifts +the child and the other menu items at this level to the right. + +L by default would insert at the end of the list of children, unless you +did manual sort_order calculations. + +Takes all the regular arguments to L. + +=cut + +sub add_before { shift->_insert_sibling("before", @_) } + +sub _insert_sibling { + my $self = shift; + my $where = shift; + my $parent = $self->parent; + my $sort_order; + for my $contemporary ($parent->children) { + if ( $contemporary->key eq $self->key ) { + if ($where eq "before") { + # Bump the current child and the following + $sort_order = $contemporary->sort_order; + } + elsif ($where eq "after") { + # Leave the current child along, bump the rest + $sort_order = $contemporary->sort_order + 1; + next; + } + else { + # never set $sort_order, act no differently than ->child() + } + } + if ( $sort_order ) { + $contemporary->sort_order( $contemporary->sort_order + 1 ); + } + } + $parent->child( @_, sort_order => $sort_order ); +} + +=head2 RemoveDashboardMenuItems + +Remove dashboards from individual user and system dash menus. + +Requires a hash with DashboardId and CurrentUser object. + + $menu->RemoveDashboardMenuItem( DashboardId => $id, CurrentUser => $session{CurrentUser}->UserObj ); + +=cut + +sub RemoveDashboardMenuItem { + my $self = shift; + my %args = @_; + + return unless $args{'DashboardId'} and $args{'CurrentUser'}; + my $dashboard_id = $args{'DashboardId'}; + my $current_user = $args{'CurrentUser'}; + + # First clear from user's dashboards + my $dashboards_in_menu = $current_user->Preferences('DashboardsInMenu', {} ); + + my @dashboards = grep { $_ != $dashboard_id } @{$dashboards_in_menu->{'dashboards'}}; + $dashboards_in_menu->{'dashboards'} = \@dashboards || []; + + my ($ret, $msg) = $current_user->SetPreferences('DashboardsInMenu', $dashboards_in_menu); + RT::Logger->warn("Unable to update dashboard for user " . $current_user->Name . ": $msg") + unless $ret; + + # Now update the system dashboard + my $system = RT::System->new( $current_user ); + my ($default_dashboards) = $system->Attributes->Named('DashboardsInMenu'); + + if ($default_dashboards) { + $dashboards_in_menu = $default_dashboards->Content; + my @dashboards = grep { $_ != $dashboard_id } @{$dashboards_in_menu->{'dashboards'}}; + + # Update only if we removed one + if ( @{$dashboards_in_menu->{'dashboards'}} > @dashboards ){ + $dashboards_in_menu->{'dashboards'} = \@dashboards || []; + + ($ret, $msg) = $default_dashboards->SetContent($dashboards_in_menu); + RT::Logger->warn("Unable to update system dashboard menu: $msg") + unless $ret; + } + } + return; +} + 1;