ActionBar: Verwendung bei »Task-Server-Steuerung«
[kivitendo-erp.git] / SL / Controller / TaskServer.pm
1 package SL::Controller::TaskServer;
2
3 use strict;
4
5 use parent qw(SL::Controller::Base);
6
7 use SL::Helper::Flash;
8 use SL::Locale::String qw(t8);
9 use SL::System::TaskServer;
10
11 use Rose::Object::MakeMethods::Generic
12 (
13   'scalar --get_set_init' => [ qw(task_server) ],
14 );
15
16 __PACKAGE__->run_before('check_auth');
17
18 #
19 # actions
20 #
21
22 sub action_show {
23   my ($self) = @_;
24
25   $::request->{layout}->use_stylesheet('background_jobs.css');
26
27   flash('warning', $::locale->text('The task server does not appear to be running.')) if !$self->task_server->is_running;
28
29   $self->setup_show_action_bar;
30   $self->render('task_server/show',
31                 title               => $::locale->text('Task server status'),
32                 last_command_output => $::auth->get_session_value('TaskServer::last_command_output'));
33 }
34
35 sub action_start {
36   my ($self) = @_;
37
38   if ($self->task_server->is_running) {
39     flash_later('error', $::locale->text('The task server is already running.'));
40
41   } else {
42     if ($self->task_server->start) {
43       flash_later('info', $::locale->text('The task server was started successfully.'));
44     } else {
45       flash_later('error', $::locale->text('Starting the task server failed.'));
46     }
47
48     $::auth->set_session_value('TaskServer::last_command_output' => $self->task_server->last_command_output);
49   }
50
51   $self->redirect_to(action => 'show');
52 }
53
54 sub action_stop {
55   my ($self) = @_;
56
57   if (!$self->task_server->is_running) {
58     flash_later('error', $::locale->text('The task server is not running.'));
59
60   } else {
61     if ($self->task_server->stop) {
62       flash_later('info', $::locale->text('The task server was stopped successfully.'));
63     } else {
64       flash_later('error', $::locale->text('Stopping the task server failed. Output:'));
65     }
66
67     $::auth->set_session_value('TaskServer::last_command_output' => $self->task_server->last_command_output);
68   }
69
70   $self->redirect_to(action => 'show');
71 }
72
73 #
74 # filters
75 #
76
77 sub check_auth {
78   $::auth->assert('admin');
79 }
80
81 #
82 # helpers
83 #
84
85 sub init_task_server {
86   return SL::System::TaskServer->new;
87 }
88
89 sub setup_show_action_bar {
90   my ($self) = @_;
91
92   for my $bar ($::request->layout->get('actionbar')) {
93     $bar->add(
94       action => [
95         $self->task_server->is_running ? t8('Stop server') : t8('Start server'),
96         submit    => [ '#form' ],
97         accesskey => 'enter',
98       ],
99       link => [
100         t8('List of jobs'),
101         link => $self->url_for(controller => 'BackgroundJob', action => 'list'),
102       ],
103       link => [
104         t8('Job history'),
105         link => $self->url_for(controller => 'BackgroundJobHistory', action => 'list'),
106       ],
107     );
108   }
109 }
110
111 1;