rt 4.2.15
[freeside.git] / rt / lib / RT / Interface / Web / Menu.pm
index e783382..26cf44e 100644 (file)
@@ -2,7 +2,7 @@
 #
 # COPYRIGHT:
 #
-# This software is Copyright (c) 1996-2014 Best Practical Solutions, LLC
+# This software is Copyright (c) 1996-2018 Best Practical Solutions, LLC
 #                                          <sales@bestpractical.com>
 #
 # (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<RT::Interface::Web::Menu> object.  Possible keys in the
 I<PARAMHASH> are L</parent>, L</title>, L</description>, L</path>,
-L</raw_html>, L<escape_title>, L</sort_order>, L</class>, L</target> and
-L</active>.  See the subroutines with the respective name below for
-each option's use.
+L</raw_html>, L<escape_title>, L</sort_order>, L</class>, L</target>,
+L<attributes>, and L</active>.  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</raw_html> 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</class> and L</target>.  This is only used if L</raw_html> isn't specified.
+
 =head2 path
 
 Gets or sets the URL that the menu's link goes to.  If the link
@@ -369,4 +375,52 @@ sub _insert_sibling {
     $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;