From 12d01b226c9254fa554fb31292cdc795b3877408 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Fri, 27 Oct 2023 09:29:15 -0400 Subject: [PATCH] Make Wake/Shutdown/Restart buttons buttons honor the location filter #336 The buttons in question affect all clients at all locations. It would make more sense if the buttons affected the clients based on the location filter. This will still allow all clients to be affected at once, but also to only affect those that are visible. --- .../Controller/Administration/API/Client.pm | 45 +++++++++++++------ .../dynamic/templates/administration/index.tt | 45 ++++++++++++------- 2 files changed, 62 insertions(+), 28 deletions(-) diff --git a/lib/Libki/Controller/Administration/API/Client.pm b/lib/Libki/Controller/Administration/API/Client.pm index 9c279d4c..8f1029e5 100644 --- a/lib/Libki/Controller/Administration/API/Client.pm +++ b/lib/Libki/Controller/Administration/API/Client.pm @@ -528,16 +528,26 @@ The status change will send a message to each client to initiate shutdown. =cut -sub shutdown_all : Local : Args(0) { - my ( $self, $c ) = @_; +sub shutdown_all : Local { + my ( $self, $c, $location ) = @_; - my $success = 0; - my $clients = $c->model('DB::Client')->search({ instance => $c->instance }); - my $status = $c->setting('ClientShutdownAction') || 'shutdown'; + my $success = 1; + + my $search; + $search->{instance} = $c->instance; + $search->{location} = $location if $location; + my $clients = $c->model('DB::Client')->search($search); + my $status = $c->setting('ClientShutdownAction') || 'shutdown'; + + my $count = 0; while ( my $client = $clients->next() ) { if ($client->status eq 'online') { - $success = 1 if $client->update( { status => $status } ); + if ( $client->update( { status => $status } ) ) { + $count++; + } else { + $success = 0; + } $c->model('DB::Statistic')->create( { @@ -554,7 +564,7 @@ sub shutdown_all : Local : Args(0) { } } - $c->stash( 'success' => $success ); + $c->stash( success => $success, count => $count ); $c->forward( $c->view('JSON') ); } @@ -565,15 +575,24 @@ The status change will send a message to each client to initiate reboot. =cut -sub restart_all : Local : Args(0) { - my ( $self, $c ) = @_; +sub restart_all : Local { + my ( $self, $c, $location ) = @_; - my $success = 0; - my $clients = $c->model('DB::Client')->search({ instance => $c->instance }); + my $success = 1; + my $search; + $search->{instance} = $c->instance; + $search->{location} = $location if $location; + my $clients = $c->model('DB::Client')->search($search); + + my $count = 0; while ( my $client = $clients->next() ) { if ($client->status eq 'online') { - $success = 1 if $client->update( { status => 'restart' } ); + if ( $client->update( { status => 'restart' } ) ) { + $count++; + } else { + $success = 0; + } $c->model('DB::Statistic')->create( { @@ -590,7 +609,7 @@ sub restart_all : Local : Args(0) { } } - $c->stash( 'success' => $success ); + $c->stash( success => $success, count => $count ); $c->forward( $c->view('JSON') ); } diff --git a/root/dynamic/templates/administration/index.tt b/root/dynamic/templates/administration/index.tt index 1b5c61a0..a6d1ca25 100644 --- a/root/dynamic/templates/administration/index.tt +++ b/root/dynamic/templates/administration/index.tt @@ -130,10 +130,10 @@ @@ -1288,39 +1288,54 @@ $(document).ready(function() { }); $(".turn-on-button").click(function () { - if ( confirm("[% c.loc("Are you sure you want to turn on all clients?") %]") ) { - $.getJSON( '[% c.uri_for('/administration/api/client/wakeup') %]', function(data) { + const location = window.location_filter || ""; + + let msg = "[% c.loc("Are you sure you want to turn on all clients?") %]"; + if ( location ) msg = "[% c.loc("Are you sure you want to turn on all clients at location ") %]" + location; + + if ( confirm( msg ) ) { + $.getJSON( '[% c.uri_for('/administration/api/client/wakeup/') %]' + location, function(data) { if ( data.success ) { - DisplayMessage( "success", "[% c.loc("Clients turned on.") %]" ); + DisplayMessage( "success", "[% c.loc("Clients turned on: ") %]" + data.count ); $("#client-table").dataTable().fnDraw(true); } else { - DisplayMessage( "error", "[% c.loc("Unable to turn on all clients.") %]" ); + DisplayMessage( "error", "[% c.loc("Unable to wake all clients. Clients turned on: ") %]" + data.count ); } }); } }); $(".shutdown-button").click(function () { - if ( confirm("[% c.loc("Are you sure you want to turn off all clients?") %]") ) { - $.getJSON( '[% c.uri_for('/administration/api/client/shutdown_all') %]', function(data) { + const location = window.location_filter || ""; + + let msg = "[% c.loc("Are you sure you want to shutdown all clients?") %]"; + if ( location ) msg = "[% c.loc("Are you sure you want to turn off all clients at location ") %]" + location; + + if ( confirm( msg ) ) { + $.getJSON( '[% c.uri_for('/administration/api/client/shutdown_all/') %]' + location, function(data) { if ( data.success ) { - DisplayMessage( "success", "[% c.loc("Clients turned off.") %]" ); + DisplayMessage( "success", "[% c.loc("Clients turned off: ") %]" + data.count ); $("#client-table").dataTable().fnDraw(true); } else { - DisplayMessage( "error", "[% c.loc("Unable to turn off all clients.") %]" ); + DisplayMessage( "error", "[% c.loc("Unable to shutdown all clients. Clients turned off: ") %]" + data.count ); } }); } }); $(".restart-button").click(function () { - if ( confirm("[% c.loc("Are you sure you want to restart all clients?") %]") ) { - $.getJSON( '[% c.uri_for('/administration/api/client/restart_all') %]', function(data) { + const location = window.location_filter || ""; + + let msg = "[% c.loc("Are you sure you want to restart all clients?") %]"; + if ( location ) msg = "[% c.loc("Are you sure you want to restart all clients at location ") %]" + location; + + if ( confirm( msg ) ) { + $.getJSON( '[% c.uri_for('/administration/api/client/restart_all/') %]' + location, function(data) { if ( data.success ) { - DisplayMessage( "success", "[% c.loc("Clients restarted.") %]" ); + DisplayMessage( "success", "[% c.loc("Clients restarted: ") %]" + data.count ); $("#client-table").dataTable().fnDraw(true); } else { - DisplayMessage( "error", "[% c.loc("Unable to restart all clients.") %]" ); + DisplayMessage( "error", "[% c.loc("Unable to restart all clients. Clients restarted: ") %]" + data.count ); } }); }